Skip to content

Commit

Permalink
if metadata is empty, return true; if only contains spaces, \t, \n, r…
Browse files Browse the repository at this point in the history
…eturn true
  • Loading branch information
djsg committed Jul 31, 2018
1 parent a819acb commit 2f2fc0d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/validators/RequestValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ 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].split(' ').length !== 2) {
if (keypairs[i].replace(/\s/g, '').length === 0) {
return true;
} else if (keypairs[i].split(' ').length !== 2) {
log('Empty Metadata: ${keypairs[i]}');
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/Test-RequestValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ describe('RequestValidator', () => {
it('should fail on non comma separated list', (done) => {
assert.equal(RequestValidator._invalidUploadMetadataHeader('hello'), false);
assert.equal(RequestValidator._invalidUploadMetadataHeader('hello world, tusrules'), false);
assert.equal(RequestValidator._invalidUploadMetadataHeader(''), false);
assert.equal(RequestValidator._invalidUploadMetadataHeader(''), true);
assert.equal(RequestValidator._invalidUploadMetadataHeader(' \t\n'), true);
done();
});
});
Expand Down

0 comments on commit 2f2fc0d

Please sign in to comment.