Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,12 @@ class Path extends Array {
var key;
for (; i < (this.length - 1); i++) {
key = this[i];
pathString += isIdent(key) ? '.' + key : formatAccessor(key);
pathString += isIdent(key) ? '.' + key : formatCompiledAccessor(key);
str += ' &&\n ' + pathString + ' != null';
}
str += ')\n';
key = this[i];
pathString += isIdent(key) ? '.' + key : formatAccessor(key);
pathString += isIdent(key) ? '.' + key : formatCompiledAccessor(key);
str += ' return ' + pathString + ';\nelse\n return undefined;';
return new Function('obj', str);
}
Expand Down Expand Up @@ -350,4 +350,12 @@ function formatAccessor(key) {
return '["' + key.replace(/"/g, '\\"') + '"]';
}
}
// Used only when generating source for compiledGetValueFromFn. JSON.stringify
// produces a valid JS string literal for any key (backslashes, quotes, line
// terminators); U+2028/U+2029 are escaped for pre-ES2019 engines.
function formatCompiledAccessor(key) {
return '[' + JSON.stringify(key)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029') + ']';
}
module.exports = Path;
22 changes: 22 additions & 0 deletions dist/test/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,26 @@ function assertInvalidPath(pathString) {
kp.setValueFrom(obj, 'something else');
vitest_1.assert.equal(obj.identifier[1]['string with space'], 'something else');
});
(0, vitest_1.it)('compiled getter does not execute code in keys', function () {
var g = globalThis;
delete g.__kpInjected;
// string path: backslash before the quote used to close the generated string literal
var p1 = index_1.default.get("['\\\"+(globalThis.__kpInjected=1)];//']");
vitest_1.assert.isTrue(p1.valid);
vitest_1.assert.isUndefined(p1.getValueFrom({ a: 1 }));
vitest_1.assert.isUndefined(g.__kpInjected);
// array path: bypasses the parser
var p2 = index_1.default.get(['\\"]+(globalThis.__kpInjected=2)//']);
vitest_1.assert.isUndefined(p2.getValueFrom({ a: 1 }));
vitest_1.assert.isUndefined(g.__kpInjected);
});
(0, vitest_1.it)('compiled getter matches property access for special-character keys', function () {
var keys = ['a\\b', 'a"b', "a'b", 'a\nb', String.fromCharCode(0x2028), '0x10', '0', '16'];
var obj = { 16: 'sixteen' };
keys.forEach(function (k, i) { obj[k] = i; });
keys.forEach(function (k) {
vitest_1.assert.strictEqual(index_1.default.get([k]).getValueFrom(obj), obj[k], JSON.stringify(k));
vitest_1.assert.strictEqual(index_1.default.get(['x', k]).getValueFrom({ x: obj }), obj[k], 'nested ' + JSON.stringify(k));
});
});
});
13 changes: 11 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,13 @@ class Path extends Array<string> {
var key;
for (; i < (this.length - 1); i++) {
key = this[i];
pathString += isIdent(key) ? '.' + key : formatAccessor(key);
pathString += isIdent(key) ? '.' + key : formatCompiledAccessor(key);
str += ' &&\n ' + pathString + ' != null';
}
str += ')\n';

key = this[i];
pathString += isIdent(key) ? '.' + key : formatAccessor(key);
pathString += isIdent(key) ? '.' + key : formatCompiledAccessor(key);

str += ' return ' + pathString + ';\nelse\n return undefined;';
return new Function('obj', str) as (obj: any) => any;
Expand Down Expand Up @@ -421,4 +421,13 @@ function formatAccessor(key: string): string {
}
}

// Used only when generating source for compiledGetValueFromFn. JSON.stringify
// produces a valid JS string literal for any key (backslashes, quotes, line
// terminators); U+2028/U+2029 are escaped for pre-ES2019 engines.
function formatCompiledAccessor(key: string): string {
return '[' + JSON.stringify(key)
.replace(/\u2028/g, '\\u2028')
.replace(/\u2029/g, '\\u2029') + ']';
}

export = Path;
27 changes: 27 additions & 0 deletions test/test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,31 @@ describe('key-path node module', function () {
kp.setValueFrom(obj, 'something else');
assert.equal(obj.identifier[1]['string with space'], 'something else');
});

it('compiled getter does not execute code in keys', function () {
var g: any = globalThis;
delete g.__kpInjected;

// string path: backslash before the quote used to close the generated string literal
var p1 = KeyPath.get("['\\\"+(globalThis.__kpInjected=1)];//']");
assert.isTrue(p1.valid);
assert.isUndefined(p1.getValueFrom({ a: 1 }));
assert.isUndefined(g.__kpInjected);

// array path: bypasses the parser
var p2 = KeyPath.get(['\\"]+(globalThis.__kpInjected=2)//']);
assert.isUndefined(p2.getValueFrom({ a: 1 }));
assert.isUndefined(g.__kpInjected);
});

it('compiled getter matches property access for special-character keys', function () {
var keys = ['a\\b', 'a"b', "a'b", 'a\nb', String.fromCharCode(0x2028), '0x10', '0', '16'];
var obj: any = { 16: 'sixteen' };
keys.forEach(function (k, i) { obj[k] = i; });

keys.forEach(function (k) {
assert.strictEqual(KeyPath.get([k]).getValueFrom(obj), obj[k], JSON.stringify(k));
assert.strictEqual(KeyPath.get(['x', k]).getValueFrom({ x: obj }), obj[k], 'nested ' + JSON.stringify(k));
});
});
});