From 0f22680206b15d5c6786aa6b504dbe8aed50714d Mon Sep 17 00:00:00 2001 From: CXu0630 Date: Fri, 20 Nov 2026 03:17:20 -0500 Subject: [PATCH] Escape keys in compiled getter via JSON.stringify --- dist/index.js | 12 ++++++++++-- dist/test/test.test.js | 22 ++++++++++++++++++++++ index.ts | 13 +++++++++++-- test/test.test.ts | 27 +++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 27da4e9..e531eb9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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); } @@ -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; diff --git a/dist/test/test.test.js b/dist/test/test.test.js index 2b6ce6a..626220b 100644 --- a/dist/test/test.test.js +++ b/dist/test/test.test.js @@ -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)); + }); + }); }); diff --git a/index.ts b/index.ts index 82b3785..2c1b82c 100644 --- a/index.ts +++ b/index.ts @@ -373,13 +373,13 @@ 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) as (obj: any) => any; @@ -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; diff --git a/test/test.test.ts b/test/test.test.ts index 683d705..d34a226 100644 --- a/test/test.test.ts +++ b/test/test.test.ts @@ -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)); + }); + }); });