From b5ca5c2ac541ff2aa013713d9747e6b462549094 Mon Sep 17 00:00:00 2001 From: Acconut Date: Mon, 6 Aug 2018 14:51:10 +0200 Subject: [PATCH] Allow empty values in Upload-Metadata header Fixes https://github.com/tus/tus-node-server/issues/117 Squashed commit of the following: commit 1ddbd46abd80d1c39a4e2425b5a9bb002e9609ac Author: Acconut Date: Mon Aug 6 14:50:44 2018 +0200 Cleanup code commit b0a8d2bbbfaff39d6267b458228e5edf54096388 Author: hedj Date: Tue Jul 31 18:47:26 2018 +0800 remove trailing space commit 5796865218c6065df4f607cd780b61e1aac30d38 Author: hedj Date: Tue Jul 31 18:41:40 2018 +0800 brace style error commit 58fd9c8ef2ed72b5384141647a1b156497bb66f1 Author: hedj Date: Tue Jul 31 18:32:54 2018 +0800 if metadata is empty, or only contains spaces, \t, \n, invalid commit a9abcc2d461c0c502ffdcbf821934981d1453abc Author: hedj Date: Tue Jul 31 18:29:01 2018 +0800 if metadata is empty, or only contains spaces, \t, \n, invalid commit 2f2fc0d7ab0fe7644e92ba3b9170a02b4ee39498 Author: hedj Date: Tue Jul 31 18:21:39 2018 +0800 if metadata is empty, return true; if only contains spaces, \t, \n, return true commit a819acbd05fd9a4b2b1e99a256163b356fa7f05a Author: hedj Date: Tue Jul 31 17:46:49 2018 +0800 get back my changes commit 12cb2f29d9e810344b3a8ebccba5fb1b755542b3 Author: hedj Date: Tue Jul 31 17:23:28 2018 +0800 try original code to see if my changes led to the travis CI error commit fcb895f3cf4c866d09ad875cce88156586c17452 Author: hedj Date: Tue Jul 31 17:14:52 2018 +0800 try original code to see if my changes led to the travis CI error commit 45f30a4d0fed6ad86f92f6ade32a69aedb80b5f1 Author: hedj Date: Tue Jul 31 17:03:26 2018 +0800 change test case 3 in line 85 to false since we allow empty metadata commit f4be7638d6a48e463da7d82c4eea429632fe729d Author: hedj Date: Tue Jul 31 16:59:15 2018 +0800 change test case 1 in line 83 to false commit 2749283da85d910a23fe8e10fe9f67129e31a20e Author: hedj Date: Tue Jul 31 16:55:36 2018 +0800 change test case 2 in line 84 to false commit bbefa754d65b8efc53469afd0ec8cf901b14f96a Author: hedj Date: Tue Jul 31 16:41:10 2018 +0800 bug in if statemetn commit a4289191358de23f271f446b17d2fedd47c82516 Author: hedj Date: Tue Jul 31 16:39:45 2018 +0800 syntax error in if statemetn commit db394d94832c710a8fc1e1f591f93b2c4840d179 Author: hedj Date: Tue Jul 31 16:35:05 2018 +0800 bug: RequestValidator _invalidUploadMetadataHeader should fail on non comma separated list commit 07068d2d27551c09d19a59fffd1707e38d3f3c7a Author: hedj Date: Tue Jul 31 16:14:26 2018 +0800 fix compiling error: should be 'let i' instead of 'const i' commit 95c516b39fc32d327017b97fad74cf91547099b2 Author: hedj Date: Tue Jul 31 16:02:37 2018 +0800 fix lint errors commit 324def544197235d14c5b60a038044800362ab14 Author: hedj Date: Tue Jul 31 12:46:34 2018 +0800 allowing empty metadata values in RequestValidator --- lib/validators/RequestValidator.js | 7 ++++--- test/Test-RequestValidator.js | 10 ++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/validators/RequestValidator.js b/lib/validators/RequestValidator.js index 487ff570..d8b1a27a 100644 --- a/lib/validators/RequestValidator.js +++ b/lib/validators/RequestValidator.js @@ -6,7 +6,6 @@ * @author Ben Stahl */ - const CONSTANTS = require('../constants'); class RequestValidator { @@ -33,9 +32,11 @@ class RequestValidator { // be Base64 encoded. All keys MUST be unique. static _invalidUploadMetadataHeader(value) { const keypairs = value.split(',') - .map((keypair) => keypair.trim()); + .map((keypair) => keypair.trim().split(' ')); - return keypairs.some((keypair) => keypair.split(' ').length !== 2); + return keypairs.some( + (keypair) => keypair[0] === '' || (keypair.length !== 2 && keypair.length !== 1) + ); } static _invalidXRequestedWithHeader() { diff --git a/test/Test-RequestValidator.js b/test/Test-RequestValidator.js index d0a3fc89..9351879e 100644 --- a/test/Test-RequestValidator.js +++ b/test/Test-RequestValidator.js @@ -79,10 +79,16 @@ describe('RequestValidator', () => { done(); }); + it('should validate keys without a value', (done) => { + assert.equal(RequestValidator._invalidUploadMetadataHeader('hello'), false); + assert.equal(RequestValidator._invalidUploadMetadataHeader('hello world, tusrules'), false); + done(); + }); + 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('too-many spaces'), true); assert.equal(RequestValidator._invalidUploadMetadataHeader(''), true); + assert.equal(RequestValidator._invalidUploadMetadataHeader(' \t\n'), true); done(); }); });