Skip to content

Commit

Permalink
Fix array coercion with empty string, protect from prototype poisoning
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Mar 24, 2021
1 parent c483caf commit 7435065
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Load modules

const Hoek = require('@hapi/hoek');
const Bourne = require('@hapi/bourne');
const Schema = require('./schema');

// Declare internals
Expand Down Expand Up @@ -231,7 +232,7 @@ internals.coerce = function (value, type, options) {
break;
case 'array':
if (typeof value === 'string') {
result = value.split(options.splitToken);
result = value ? value.split(options.splitToken) : [];
}
else {
result = undefined;
Expand All @@ -258,7 +259,7 @@ internals.coerce = function (value, type, options) {
break;
case 'object':
try {
result = JSON.parse(value);
result = Bourne.parse(value);
}
catch (e) {
result = undefined;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"api"
],
"dependencies": {
"@hapi/bourne": "2.x.x",
"@hapi/hoek": "9.x.x",
"alce": "1.x.x",
"joi": "17.x.x",
Expand Down
5 changes: 3 additions & 2 deletions test/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internals.replaceEnv = (obj) => {

const replaced = {};
for (const key in obj) {
if (obj[key]) {
if (key in obj && obj[key] !== null) {
replaced[key] = process.env[key] ? process.env[key] : null;
process.env[key] = obj[key];
}
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('get()', () => {

get('/coerceArray1', ['a'], {}, [], {});
get('/coerceArray1', ['a', 'b'], {}, [], { ARRAY: 'a,b' });
get('/coerceArray1', ['a'], {}, [], { ARRAY: '' });
get('/coerceArray1', [], {}, [], { ARRAY: '' });
get('/coerceArray2', ['a', 'b'], {}, [], { ARRAY: 'a/b' });
get('/coerceArray3', ['a', 'b'], {}, [], { ARRAY: 'a-b' });

Expand All @@ -274,6 +274,7 @@ describe('get()', () => {
get('/coerceObject1', { a: 'b' }, {}, [], {});
get('/coerceObject1', { b: 'a' }, {}, [], { 'OBJECT': '{"b":"a"}' });
get('/coerceObject1', { a: 'b' }, {}, [], { 'OBJECT': 'BROKEN JSON' });
get('/coerceObject1', { a: 'b' }, { obj: '{"b":"a","__proto__":"x"}' }, []);

it('fails on invalid key', () => {

Expand Down

0 comments on commit 7435065

Please sign in to comment.