Skip to content

Commit

Permalink
Merge pull request #41 from steveukx/feature/tests-update
Browse files Browse the repository at this point in the history
Snyk prototype pollution fix
  • Loading branch information
steveukx committed Dec 30, 2020
2 parents 79637cb + 4e4bc39 commit 45cd522
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/properties-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const {readFileSync, statSync} = require('fs');
const propertyAppender = require('./property-appender').propertyAppender;
const propertyWriter = require('./property-writer').propertyWriter;

const has = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);

const SECTION = Symbol('SECTION');

function PropertiesReader (sourceFile, encoding, options = {}) {
Expand Down Expand Up @@ -213,7 +215,12 @@ PropertiesReader.prototype.set = function (key, value) {
if (expanded.length >= 1 && typeof source[step] === 'string') {
source[step] = {'': source[step]};
}
source = (source[step] = source[step] || {});

if (!has(source, step)) {
Object.defineProperty(source, step, { value: {} });
}

source = source[step]
}

if (typeof parsedValue === 'string' && typeof source[expanded[0]] === 'object') {
Expand Down
28 changes: 28 additions & 0 deletions test/fix-prototype-pollution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const {createTestContext} = require('./__fixtues__/create-test-context');

const propertiesReader = require('../');

describe('prototype-pollution', () => {
let context;

beforeEach(async () => {
context = await createTestContext();
});

it('does not pollute global Object.prototype', async () => {
const file = `
[__proto__]
polluted = polluted
parsed = true
`;
const props = propertiesReader(await context.file('props.ini', file));

expect(({}).polluted).toBeUndefined();
expect(props.path().__proto__.polluted).toBe('polluted');
expect(props.getRaw('__proto__.polluted')).toBe('polluted');
expect(props.get('__proto__.polluted')).toBe('polluted');
expect(props.getRaw('__proto__.parsed')).toBe('true');
expect(props.get('__proto__.parsed')).toBe(true);
});

});

0 comments on commit 45cd522

Please sign in to comment.