Skip to content

feat: checksum extension #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"corejs": false,
"forceAllTransforms": true
} ]
]
],
"plugins": ["@babel/transform-runtime"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why this plugin is necessary?

}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSaveMode": "modifications"
}
1 change: 1 addition & 0 deletions demos/browser/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ function startUpload () {
endpoint,
chunkSize,
retryDelays: [0, 1000, 3000, 5000],
checksumAlgo: "sha-256",
parallelUploads,
metadata : {
filename: file.name,
Expand Down
39 changes: 39 additions & 0 deletions lib/browser/extensions/checksum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Checksum {
static supportedAlogrithms = ['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512'];

constructor (algo = 'SHA-256') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need a default value here. If no algorithm or an unsupported one is supplied, we can just error out.

this.algo = this._resolveAlgo(algo)
}

/**
* Resolves algorithm to one of the inbuilt types
* @param {String} algo contains user provided checksumAlgo option
*/
_resolveAlgo = (algo) => {
const resolvedAlgo = Checksum.supportedAlgorithms.find(supportedAlgo => supportedAlgo === algo.toUpperCase())
if (!resolvedAlgo) {
throw new Error(
`tus: unsupported checksumAlgo provided. Supported values are : ${Checksum.supportedAlgorithms.join(',')}`,
)
}
return resolvedAlgo
}
/**
* Gets Hexadecimal digest using the algorithm set in this.algo
* @param {ArrayBuffer} data contains the chunk of data to be hashed
*/

getHexDigest = async (data) => {
try {
const hashBuffer = await crypto.subtle.digest(this.algo, data)
const hashArray = Array.from(new Uint8Array(hashBuffer)) // convert buffer to byte array
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, '0'))
.join('') // convert bytes to hex string
return hashHex
} catch (err) {
throw new Error('tus: could not compute checksum for integrity check')
}
};
}
export default Checksum
38 changes: 38 additions & 0 deletions lib/node/extensions/checksum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import crypto from 'crypto'

class Checksum {
static supportedAlgorithms = ['sha1', 'sha256', 'sha384', 'sha512', 'md5'];

constructor (algo = 'sha256') {
this.algo = this.resolveAlgo(algo)
}

/**
* Resolves algorithm to one of the inbuilt types
* @param {String} algo contains user provided checksumAlgo option
*/
resolveAlgo = (algo) => {
const resolvedAlgo = Checksum.supportedAlgorithms.find(supportedAlgo => supportedAlgo === algo.toLowerCase())
if (!resolvedAlgo) throw new Error(`Checksum: unsupported checksumAlgo provided. Supported values are ${Checksum.supportedAlgorithms.join(',')}`)
return resolvedAlgo
}

/**
* Gets Hexadecimal digest using the algorithm set in this.algo
* @param {ArrayBuffer} data contains the chunk of data to be hashed
*/
getHexDigest = async (data) => {
try {
const hashHex = await crypto
.createHash(this.algo)
.update(data)
.digest('hex')

return hashHex
} catch (err) {
throw new Error('tus: could not compute checksum for integrity check')
}
};
}

export default Checksum
57 changes: 54 additions & 3 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class BaseUpload {
// An array of upload URLs which are used for uploading the different
// parts, if the parallelUploads option is used.
this._parallelUploadUrls = null

// A platform dependent checksum generator, this is instantiated if options.enableChecksum is provided
this._checksum = null
}

/**
Expand Down Expand Up @@ -531,6 +534,40 @@ class BaseUpload {
return
}

if (this.options.enableChecksum) {
const optionsReq = this._openRequest('OPTIONS', this.options.endpoint)
this._sendRequest(optionsReq)
.then((res) => {
const isChecksumSupported = res
.getHeader('Tus-Extension')
.split(',')
.includes('checksum')
if (!isChecksumSupported) {
log('Checksum is not supported by this tus server')
return
}
const supportedAlgorithms = res.getHeader('Tus-Checksum-Algorithm')
if (!supportedAlgorithms) {
throw new Error(
'tus: checksum is enabled, but Tus-Checksum-Algorithm is not defined by tus server',
)
}
let Checksum
if (window) {
// eslint-disable-next-line global-require
Checksum = require('./browser/extensions/checksum').default
} else {
// eslint-disable-next-line global-require
Checksum = require('./browser/extensions/checksum').default
}
this._checksum = new Checksum(
Checksum.supportedAlogrithms.find((supportedAlgo) => supportedAlgorithms.includes(supportedAlgo)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this check do? The combination of find and includes looks suspicious.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to fetch the supported algorithms from the server and see if one of them matches one of the supported algorithms on the client. If we're saying client and server have agreed upon an algorithm from the get go, then we can skip this OPTIONS call and let client just configure the algorithm in options.

)
})
.catch((err) => {
log('Failed to fetch options response', err)
})
}
const req = this._openRequest('POST', this.options.endpoint)

if (this.options.uploadLengthDeferred) {
Expand Down Expand Up @@ -749,11 +786,25 @@ class BaseUpload {
req.setHeader('Upload-Length', this._size)
}

if (value === null) {
if (this.options.enableChecksum) {
if (this._checksum) {
value.arrayBuffer().then((chunkBuffer) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this entire if block be merged with the last else block? They seem very similar and would avoid some duplication.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how this might reduce the duplication as one is async. I've removed one outer if condition. Is that okay?

this._checksum
.getHexDigest(chunkBuffer)
.then((hash) => {
req.setHeader('Upload-Checksum', `${this._checksum.algo} ${hash}`)
this._emitProgress(this._offset, this._size)
return this._sendRequest(req, value)
})
.catch((err) => log(err))
})
}
} else if (value === null) {
return this._sendRequest(req)
} else {
this._emitProgress(this._offset, this._size)
return this._sendRequest(req, value)
}
this._emitProgress(this._offset, this._size)
return this._sendRequest(req, value)
})
}

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"@babel/helper-get-function-arity": "^7.16.7",
"@babel/plugin-syntax-jsx": "^7.12.13",
"@babel/plugin-transform-modules-commonjs": "^7.9.6",
"@babel/plugin-transform-runtime": "^7.17.0",
"@babel/preset-env": "^7.0.0",
"@babel/runtime": "^7.17.9",
"axios": "^0.27.2",
"babelify": "^10.0.0",
"browserify": "^17.0.0",
Expand Down
21 changes: 21 additions & 0 deletions test/spec/test-browser-specific.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const assertUrlStorage = require('./helpers/assertUrlStorage')
const { TestHttpStack, waitableFunction, wait } = require('./helpers/utils')
const tus = require('../..')
const { default: Checksum } = require('../../lib.es5/browser/extensions/checksum')

describe('tus', () => {
beforeEach(() => {
Expand Down Expand Up @@ -778,4 +779,24 @@ describe('tus', () => {
await assertUrlStorage(tus.defaultOptions.urlStorage)
})
})

describe('#Checksum', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would also need a full test to ensure the checksum header is added to the actual requests. Such a test should be placed in test-common, which includes tests shared between browsers and Node.js.

Let me know if you need help with getting this running :)

it('should generate hex digest for a given chunk of file', async () => {
const checksum = new Checksum()
const hexDigest = await checksum.getHexDigest(
new TextEncoder().encode('hello'),
)
expect(hexDigest).toBe(
'2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824',
)
})

it('should throw an error when an unsupported algo is provided', () => {
try {
const checksum = new Checksum('md5')
} catch (err) {
expect(err.message).toContain('unsupported checksumAlgo')
}
})
})
})
17 changes: 17 additions & 0 deletions test/spec/test-node-specific.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const intoStream = require('into-stream')
const tus = require('../..')
const assertUrlStorage = require('./helpers/assertUrlStorage')
const { TestHttpStack, waitableFunction } = require('./helpers/utils')
const {default: Checksum} = require('../../lib.es5/node/extensions/checksum')

describe('tus', () => {
describe('#canStoreURLs', () => {
Expand Down Expand Up @@ -315,6 +316,22 @@ describe('tus', () => {
})
})

describe('#Checksum', () => {
it('should generate hex digest for a given chunk of file', async () => {
const checksum = new Checksum();
const hexDigest = await checksum.getHexDigest(Buffer.from('hello', 'utf8'))
expect(hexDigest).toBe('2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824')
})

it('should throw an error when an unsupported algo is provided', () => {
try {
const checksum = new Checksum('new-algo')
} catch (err) {
expect(err).toContain('unsupported checksumAlgo');
}
})
})

describe('#StreamSource', () => {
it('should slice at different ranges', async () => {
const input = stream.Readable.from(Buffer.from('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), {
Expand Down