Skip to content
Open
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
21 changes: 21 additions & 0 deletions packages/agent-script/src/interp/globals/object-array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ describe("createObjectArrayGlobals", () => {
await expect(getClosure(getProperty(globals.Array, "from")).call(["10", globals.Number])).resolves.toEqual([1, 0]);
});


it("blocks assign and freeze on host prototype objects", async () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add coverage for the sandbox object-literal/null-prototype path here too. The current test covers Object.prototype, but not {} from the interpreter, which is the regression path for Object.assign({}, source) and Object.freeze({}).

const globals = createObjectArrayGlobals({
budget: new Budget()
});

expect(() =>
getClosure(getProperty(globals.Object, "assign")).call([
Object.prototype as unknown as SandboxObject,
{
polluted: true
}
])
).toThrowError("Object.assign(target, ...sources) requires an object or array target.");

expect(await getClosure(getProperty(globals.Object, "freeze")).call([Object.prototype as unknown as SandboxObject])).toBe(
Object.prototype
);
expect(Object.isFrozen(Object.prototype)).toBe(false);
});

it("treats sandbox coercion helpers as opaque Object sources", async () => {
const globals = createObjectArrayGlobals({
budget: new Budget()
Expand Down
27 changes: 19 additions & 8 deletions packages/agent-script/src/interp/globals/object-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,7 @@ export function createObjectArrayGlobals(options: { budget: Budget }): ObjectArr
name: "fromEntries"
}),
freeze: createSandboxClosure({
call: ([value]) => {
if (typeof value === "object" && value !== null) {
Object.freeze(value);
}

return value;
},
call: ([value]) => freezeSandboxValue(value),
name: "freeze"
}),
assign: createSandboxClosure({
Expand Down Expand Up @@ -80,6 +74,15 @@ export function createObjectArrayGlobals(options: { budget: Budget }): ObjectArr
};
}

function freezeSandboxValue(value: SandboxValue): SandboxValue {
if (!isAssignableSandboxTarget(value)) {
return value;
}

Object.freeze(value);
return value;
}

function assignSandboxValues(target: SandboxValue, sources: readonly SandboxValue[]): SandboxValue {
if (target === null || target === undefined) {
throw new TypeError("Object.assign(target, ...sources) requires a non-null target.");
Expand All @@ -105,7 +108,15 @@ function assignSandboxValues(target: SandboxValue, sources: readonly SandboxValu
function isAssignableSandboxTarget(
value: SandboxValue
): value is SandboxObject & Record<string, SandboxValue> | SandboxValue[] & Record<string, SandboxValue> {
return typeof value === "object" && value !== null && !isSandboxClosure(value) && !isSandboxPromise(value);
if (typeof value !== "object" || value === null || isSandboxClosure(value) || isSandboxPromise(value)) {
return false;
}

if (Array.isArray(value)) {
return true;
}

return Object.getPrototypeOf(value) === Object.prototype;
Comment on lines +115 to +119
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object literals are created with Object.create(null) in interpreter.ts, so this now rejects ordinary script code like Object.assign({}, { ok: true }) and Object.freeze({}). I verified run('return Object.assign({}, { ok: true }).ok') now throws this TypeError. Please allow sandbox null-prototype objects while still blocking the host prototypes.

Suggested change
if (Array.isArray(value)) {
return true;
}
return Object.getPrototypeOf(value) === Object.prototype;
if (Array.isArray(value)) {
return value !== Array.prototype;
}
const prototype = Object.getPrototypeOf(value);
return prototype === Object.prototype || (prototype === null && value !== Object.prototype);

}

async function arrayFromSandboxValues(args: readonly SandboxValue[], budget: Budget): Promise<SandboxValue> {
Expand Down
Loading