Skip to content

Commit

Permalink
Ignore '$' attribute (used by xml2js parser)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo Zanivan committed Dec 17, 2019
1 parent 49a7ec6 commit 3d7c135
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dist/Shield.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Shield {
static traverse(obj, opts) {
let error;
for (const k in obj) {
if (opts.mongo && Utils_1.default.isString(k) && k.indexOf('$') === 0) {
if (opts.mongo && Utils_1.default.isString(k) && k.indexOf('$') === 0 && k.length > 1) {
error = new ShieldError_1.default('Mongo $ injection found', 'mongo_error', obj);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Shield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Shield {
private static traverse(obj: any, opts: ShieldOptions): ShieldError | undefined {
let error;
for (const k in obj) {
if (opts.mongo && Utils.isString(k) && k.indexOf('$') === 0) {
if (opts.mongo && Utils.isString(k) && k.indexOf('$') === 0 && k.length > 1) {
error = new ShieldError('Mongo $ injection found', 'mongo_error', obj);
break;
}
Expand Down
11 changes: 11 additions & 0 deletions test/Shield.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ describe('Shield', () => {
});
});

it('should not block "$" attribute', (done) => {
const payload = {
$: {
id: 1,
},
};
shield.evaluate(payload, {
mongo: true,
}, done);
});

it('should not block __proto__ object', (done) => {
const payload = JSON.parse('{ "__proto__": { "admin": true } }');
shield.evaluate(payload, {
Expand Down

0 comments on commit 3d7c135

Please sign in to comment.