From 9787cfbf22c07ac4f7e427cd86c8746b1fcadad3 Mon Sep 17 00:00:00 2001 From: bercedev <75228263+bercedev@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:38:22 +0300 Subject: [PATCH] fix(ts): handle shadowed hasOwnProperty in programs Check own properties without invoking methods on parsed program values. Cover malformed steps and valid object arguments. --- typescript/src/ts/program.ts | 14 +++++++------- typescript/test/program.test.ts | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/typescript/src/ts/program.ts b/typescript/src/ts/program.ts index c19d01f8..17d1c237 100644 --- a/typescript/src/ts/program.ts +++ b/typescript/src/ts/program.ts @@ -98,7 +98,7 @@ function isValidFunctionName(name: unknown): name is string { */ export function createModuleTextFromProgram(jsonObject: object): Result { const steps = (jsonObject as Program)["@steps"]; - if (!(Array.isArray(steps) && steps.every(step => typeof step === "object" && step !== null && step.hasOwnProperty("@func")))) { + if (!(Array.isArray(steps) && steps.every(step => typeof step === "object" && step !== null && Object.prototype.hasOwnProperty.call(step, "@func")))) { return error("JSON object is not a valid program", { data: jsonObject }); } let hasError = false; @@ -119,15 +119,15 @@ export function createModuleTextFromProgram(jsonObject: object): Result } function objectToString(obj: Record) { - if (obj.hasOwnProperty("@ref")) { + if (Object.prototype.hasOwnProperty.call(obj, "@ref")) { const index = obj["@ref"]; if (typeof index === "number" && Number.isInteger(index) && index >= 0 && index < currentStep && Object.keys(obj).length === 1) { return `step${index + 1}`; } } - else if (obj.hasOwnProperty("@func")) { + else if (Object.prototype.hasOwnProperty.call(obj, "@func")) { const func = obj["@func"]; - const hasArgs = obj.hasOwnProperty("@args"); + const hasArgs = Object.prototype.hasOwnProperty.call(obj, "@args"); const args = hasArgs ? obj["@args"] : []; if (isValidFunctionName(func) && (Array.isArray(args)) && Object.keys(obj).length === (hasArgs ? 2 : 1)) { return `api.${func}(${arrayToString(args)})`; @@ -171,16 +171,16 @@ export async function evaluateJsonProgram(program: Program, onCall: (func: strin } async function evaluateObject(obj: Record) { - if (obj.hasOwnProperty("@ref")) { + if (Object.prototype.hasOwnProperty.call(obj, "@ref")) { const index = obj["@ref"]; if (Object.keys(obj).length === 1 && typeof index === "number" && Number.isInteger(index) && index >= 0 && index < results.length) { return results[index]; } throw new Error(`Invalid result reference: ${String(index)}`); } - else if (obj.hasOwnProperty("@func")) { + else if (Object.prototype.hasOwnProperty.call(obj, "@func")) { const func = obj["@func"]; - const hasArgs = obj.hasOwnProperty("@args"); + const hasArgs = Object.prototype.hasOwnProperty.call(obj, "@args"); const args = hasArgs ? obj["@args"] : []; if (!isValidFunctionName(func)) { throw new Error(`Invalid function name: ${JSON.stringify(func)}`); diff --git a/typescript/test/program.test.ts b/typescript/test/program.test.ts index b95515bb..54091fac 100644 --- a/typescript/test/program.test.ts +++ b/typescript/test/program.test.ts @@ -109,6 +109,27 @@ describe("createModuleTextFromProgram result references", () => { // @func name validation (source injection / unchecked dispatch) // --------------------------------------------------------------------------- +describe("hasOwnProperty shadowing in parsed programs", () => { + it("returns an error result for a step with no @func", () => { + const input = JSON.parse('{"@steps":[{"hasOwnProperty":1}]}'); + const result = createModuleTextFromProgram(input); + assert.equal(result.success, false); + }); + + it("generates code for an object argument named hasOwnProperty", () => { + const program = JSON.parse('{"@steps":[{"@func":"first","@args":[{"hasOwnProperty":1}]}]}'); + const result = createModuleTextFromProgram(program); + assert.equal(result.success, true); + assert.match((result as { data: string }).data, /"hasOwnProperty": 1/); + }); + + it("evaluates an object argument named hasOwnProperty", async () => { + const program = JSON.parse('{"@steps":[{"@func":"first","@args":[{"hasOwnProperty":1}]}]}'); + const result = await evaluateJsonProgram(program, async (_func, args) => args[0]); + assert.deepEqual(result, { hasOwnProperty: 1 }); + }); +}); + describe("@func name validation", () => { const maliciousProgram: Program = { "@steps": [