Skip to content

Commit 0877cc8

Browse files
committed
Test case covering the use of __proto__ as a section name
Closes #40
1 parent a250c95 commit 0877cc8

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/properties-reader.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const {readFileSync, statSync} = require('fs');
22
const propertyAppender = require('./property-appender').propertyAppender;
33
const propertyWriter = require('./property-writer').propertyWriter;
44

5+
const has = Object.prototype.hasOwnProperty.call.bind(Object.prototype.hasOwnProperty);
6+
57
const SECTION = Symbol('SECTION');
68

79
function PropertiesReader (sourceFile, encoding, options = {}) {
@@ -213,7 +215,12 @@ PropertiesReader.prototype.set = function (key, value) {
213215
if (expanded.length >= 1 && typeof source[step] === 'string') {
214216
source[step] = {'': source[step]};
215217
}
216-
source = (source[step] = source[step] || {});
218+
219+
if (!has(source, step)) {
220+
Object.defineProperty(source, step, { value: Object.create(null) });
221+
}
222+
223+
source = source[step]
217224
}
218225

219226
if (typeof parsedValue === 'string' && typeof source[expanded[0]] === 'object') {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const {createTestContext} = require('./__fixtues__/create-test-context');
2+
3+
const propertiesReader = require('../');
4+
5+
describe('prototype-pollution', () => {
6+
let context;
7+
8+
beforeEach(async () => {
9+
context = await createTestContext();
10+
});
11+
12+
it('does not pollute global Object.prototype', async () => {
13+
const file = `
14+
[__proto__]
15+
polluted = polluted
16+
parsed = true
17+
`;
18+
const props = propertiesReader(await context.file('props.ini', file));
19+
20+
expect(({}).polluted).toBeUndefined();
21+
expect(props.path().__proto__.polluted).toBe('polluted');
22+
expect(props.getRaw('__proto__.polluted')).toBe('polluted');
23+
expect(props.get('__proto__.polluted')).toBe('polluted');
24+
expect(props.getRaw('__proto__.parsed')).toBe('true');
25+
expect(props.get('__proto__.parsed')).toBe(true);
26+
});
27+
28+
});

0 commit comments

Comments
 (0)