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
14 changes: 7 additions & 7 deletions typescript/src/ts/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function isValidFunctionName(name: unknown): name is string {
*/
export function createModuleTextFromProgram(jsonObject: object): Result<string> {
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;
Expand All @@ -119,15 +119,15 @@ export function createModuleTextFromProgram(jsonObject: object): Result<string>
}

function objectToString(obj: Record<string, unknown>) {
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)})`;
Expand Down Expand Up @@ -171,16 +171,16 @@ export async function evaluateJsonProgram(program: Program, onCall: (func: strin
}

async function evaluateObject(obj: Record<string, unknown>) {
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)}`);
Expand Down
21 changes: 21 additions & 0 deletions typescript/test/program.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
Loading