Skip to content

Commit

Permalink
Merge 5796865 into 6d571cb
Browse files Browse the repository at this point in the history
  • Loading branch information
djsg committed Jul 31, 2018
2 parents 6d571cb + 5796865 commit 110196d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
19 changes: 18 additions & 1 deletion lib/validators/RequestValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @author Ben Stahl <bhstahl@gmail.com>
*/

const debug = require('debug');
const log = debug('tus-node-server');

const CONSTANTS = require('../constants');

Expand All @@ -32,10 +34,25 @@ class RequestValidator {
// MUST NOT be empty. The key SHOULD be ASCII encoded and the value MUST
// be Base64 encoded. All keys MUST be unique.
static _invalidUploadMetadataHeader(value) {
if (value === '') {
return true;
}

const keypairs = value.split(',')
.map((keypair) => keypair.trim());
// Original code: check key/value pair. If value is empty, return true.
// return keypairs.some((keypair) => keypair.split(' ').length !== 2);
for (let i = 0; i < keypairs.length; i++) {
if (keypairs[i].replace(/\s/g, '').length === 0) {
log('Metadata: Empty');
return true;
}
else if (keypairs[i].split(' ').length !== 2) {
log('Metadata: Empty value for key: ${keypairs[i]}');
}
}

return keypairs.some((keypair) => keypair.split(' ').length !== 2);
return false;
}

static _invalidXRequestedWithHeader() {
Expand Down
5 changes: 3 additions & 2 deletions test/Test-RequestValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ describe('RequestValidator', () => {
});

it('should fail on non comma separated list', (done) => {
assert.equal(RequestValidator._invalidUploadMetadataHeader('hello'), true);
assert.equal(RequestValidator._invalidUploadMetadataHeader('hello world, tusrules'), true);
assert.equal(RequestValidator._invalidUploadMetadataHeader('hello'), false);
assert.equal(RequestValidator._invalidUploadMetadataHeader('hello world, tusrules'), false);
assert.equal(RequestValidator._invalidUploadMetadataHeader(''), true);
assert.equal(RequestValidator._invalidUploadMetadataHeader(' \t\n'), true);
done();
});
});
Expand Down

0 comments on commit 110196d

Please sign in to comment.