Skip to content

Commit e16083b

Browse files
authored
Reject partial upload if concatenation extension is not supported (#262)
1 parent 3a3052c commit e16083b

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

lib/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const HEADERS = [
1515
'Tus-Max-Size',
1616
'Tus-Resumable',
1717
'Tus-Version',
18+
'Upload-Concat',
1819
'Upload-Defer-Length',
1920
'Upload-Length',
2021
'Upload-Metadata',

lib/handlers/PostHandler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class PostHandler extends BaseHandler {
1515
* @return {function}
1616
*/
1717
send(req, res) {
18+
if ('upload-concat' in req.headers && !this.store.hasExtension('concatentation')) {
19+
return Promise.resolve(super.send(res, 501, {}, 'Concatenation extension is not (yet) supported. Disable parallel uploads in the tus client. '));
20+
}
21+
1822
return this.store.create(req)
1923
.then(async(File) => {
2024
const url = this.store.relativeLocation ? `${req.baseUrl || ''}${this.store.path}/${File.id}` : `//${req.headers.host}${req.baseUrl || ''}${this.store.path}/${File.id}`;

lib/stores/DataStore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class DataStore extends EventEmitter {
4343
}
4444

4545
hasExtension(extension) {
46-
return this._extensions.indexOf(extension) !== -1;
46+
return this._extensions && this._extensions.indexOf(extension) !== -1;
4747
}
4848

4949
/**

lib/validators/RequestValidator.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ class RequestValidator {
7272
return false;
7373
}
7474

75+
static _invalidUploadConcatHeader(value) {
76+
const valid_partial = value === 'partial';
77+
const valid_final = value.startsWith('final;');
78+
79+
return !valid_partial && !valid_final;
80+
}
81+
7582
static capitalizeHeader(header_name) {
7683
return header_name.replace(/\b[a-z]/g, function() {
7784
return arguments[0].toUpperCase();

test/Test-PostHandler.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ describe('PostHandler', () => {
3535
.catch(done);
3636
});
3737

38+
it('must 501 if the \'concatenation\' extension is not supported', (done) => {
39+
req.headers = { 'upload-concat': 'partial' };
40+
handler.send(req, res).then(() => {
41+
assert.equal(res.statusCode, 501);
42+
return done();
43+
})
44+
.catch(done);
45+
});
46+
3847
it('must acknowledge successful POST requests with the 201', (done) => {
3948
req.headers = { 'upload-length': 1000, host: 'localhost:3000' };
4049

test/Test-RequestValidator.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,21 @@ describe('RequestValidator', () => {
9393
});
9494
});
9595

96+
describe('_invalidUploadConcatHeader', () => {
97+
it('should validate partial and final', (done) => {
98+
assert.equal(RequestValidator._invalidUploadConcatHeader('partial'), false);
99+
assert.equal(RequestValidator._invalidUploadConcatHeader('final;/files/a /files/b'), false);
100+
done();
101+
});
102+
103+
it('should invalidate everything else', (done) => {
104+
assert.equal(RequestValidator._invalidUploadConcatHeader(''), true);
105+
assert.equal(RequestValidator._invalidUploadConcatHeader('PARTIAL'), true);
106+
assert.equal(RequestValidator._invalidUploadConcatHeader('invalid-value'), true);
107+
done();
108+
});
109+
});
110+
96111
describe('_invalidXRequestedWithHeader', () => {
97112
it('always validate ', (done) => {
98113
assert.equal(RequestValidator._invalidXRequestedWithHeader(), false);

0 commit comments

Comments
 (0)