Skip to content

Commit 355ca74

Browse files
author
Steve Hobbs
authored
Merge pull request from GHSA-prfq-f66g-43mp
* Applied #1085 from auth0.js (fixes broken tests) * Added an object helper for setting properties at a deep level * Masking password in original error object
1 parent 55b6ac8 commit 355ca74

File tree

4 files changed

+172
-1
lines changed

4 files changed

+172
-1
lines changed

src/helper/object.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,28 @@ function trimUserDetails(options) {
168168
return trimMultiple(options, ['username', 'email', 'phoneNumber']);
169169
}
170170

171+
/**
172+
* Updates the value of a property on the given object, using a deep path selector.
173+
* @param {object} obj The object to set the property value on
174+
* @param {string|array} path The path to the property that should have its value updated. e.g. 'prop1.prop2.prop3' or ['prop1', 'prop2', 'prop3']
175+
* @param {any} value The value to set
176+
*/
177+
function updatePropertyOn(obj, path, value) {
178+
if (typeof path === 'string') {
179+
path = path.split('.');
180+
}
181+
182+
var next = path[0];
183+
184+
if (obj.hasOwnProperty(next)) {
185+
if (path.length === 1) {
186+
obj[next] = value;
187+
} else {
188+
updatePropertyOn(obj[next], path.slice(1), value);
189+
}
190+
}
191+
}
192+
171193
export default {
172194
toSnakeCase: toSnakeCase,
173195
toCamelCase: toCamelCase,
@@ -178,5 +200,6 @@ export default {
178200
extend: extend,
179201
getOriginFromUrl: getOriginFromUrl,
180202
getLocationFromUrl: getLocationFromUrl,
181-
trimUserDetails: trimUserDetails
203+
trimUserDetails: trimUserDetails,
204+
updatePropertyOn: updatePropertyOn
182205
};

src/helper/response-handler.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ function wrapCallback(cb, options) {
2727
original: err
2828
};
2929

30+
objectHelper.updatePropertyOn(
31+
errObj,
32+
'original.response.req._data.password',
33+
'*****'
34+
);
35+
3036
if (err.response && err.response.statusCode) {
3137
errObj.statusCode = err.response.statusCode;
3238
}

test/helper/object.test.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,4 +707,62 @@ describe('helpers', function() {
707707
});
708708
});
709709
});
710+
711+
describe('setPropertyValue', function() {
712+
it('can set a property at the first level of the object', function() {
713+
var obj = {
714+
one: 1,
715+
two: 2,
716+
three: 3
717+
};
718+
719+
objectHelper.updatePropertyOn(obj, 'one', 'one');
720+
721+
expect(obj).to.eql({
722+
one: 'one',
723+
two: 2,
724+
three: 3
725+
});
726+
});
727+
728+
it('can set a nested property', function() {
729+
var obj = {
730+
one: {
731+
two: {
732+
three: 3
733+
}
734+
}
735+
};
736+
737+
objectHelper.updatePropertyOn(obj, 'one.two.three', 'three');
738+
739+
expect(obj).to.eql({
740+
one: {
741+
two: {
742+
three: 'three'
743+
}
744+
}
745+
});
746+
});
747+
748+
it("does not add new values if the key doesn't already exist", function() {
749+
var obj = {
750+
one: {
751+
two: {
752+
three: 3
753+
}
754+
}
755+
};
756+
757+
objectHelper.updatePropertyOn(obj, 'one.two.four', 4);
758+
759+
expect(obj).to.eql({
760+
one: {
761+
two: {
762+
three: 3
763+
}
764+
}
765+
});
766+
});
767+
});
710768
});

test/helper/response-handler.test.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,88 @@ describe('helpers responseHandler', function() {
241241
{ keepOriginalCasing: true }
242242
)(null, assert_data);
243243
});
244+
245+
it('should mask the password object in the original response object', function(done) {
246+
var assert_err = {
247+
code: 'the_error_code',
248+
error: 'The error description.',
249+
response: {
250+
req: {
251+
_data: {
252+
realm: 'realm',
253+
client_id: 'client_id',
254+
username: 'username',
255+
password: 'this is a password'
256+
}
257+
}
258+
}
259+
};
260+
261+
responseHandler(function(err, data) {
262+
expect(data).to.be(undefined);
263+
264+
expect(err).to.eql({
265+
original: {
266+
code: 'the_error_code',
267+
error: 'The error description.',
268+
response: {
269+
req: {
270+
_data: {
271+
realm: 'realm',
272+
client_id: 'client_id',
273+
username: 'username',
274+
password: '*****'
275+
}
276+
}
277+
}
278+
},
279+
code: 'the_error_code',
280+
description: 'The error description.'
281+
});
282+
283+
done();
284+
})(assert_err, null);
285+
});
286+
287+
it('should mask the password object in the data object', function(done) {
288+
var assert_err = {
289+
code: 'the_error_code',
290+
error: 'The error description.',
291+
response: {
292+
req: {
293+
_data: {
294+
realm: 'realm',
295+
client_id: 'client_id',
296+
username: 'username',
297+
password: 'this is a password'
298+
}
299+
}
300+
}
301+
};
302+
303+
responseHandler(function(err, data) {
304+
expect(data).to.be(undefined);
305+
306+
expect(err).to.eql({
307+
original: {
308+
code: 'the_error_code',
309+
error: 'The error description.',
310+
response: {
311+
req: {
312+
_data: {
313+
realm: 'realm',
314+
client_id: 'client_id',
315+
username: 'username',
316+
password: '*****'
317+
}
318+
}
319+
}
320+
},
321+
code: 'the_error_code',
322+
description: 'The error description.'
323+
});
324+
325+
done();
326+
})(assert_err, null);
327+
});
244328
});

0 commit comments

Comments
 (0)