Skip to content

An open-source, faster Devin alternative

TypeScript

SDK: application data comes back with every key as it came, and no prototype is set

Igor Zalutski2be7caa63d7c
diff --git a/sdks/typescript/src/agents/client.test.ts b/sdks/typescript/src/agents/client.test.ts
index 7e22d2a1..1ed9eac7 100644
--- a/sdks/typescript/src/agents/client.test.ts
+++ b/sdks/typescript/src/agents/client.test.ts
@@ -291,6 +291,40 @@ describe("OpenComputer client", () => {
expect(await client.sessions.get("extra")).toEqual({ ...session, nextThing: { added: true } });
});
+ // Application data is the application's. A body whose JSON has a key named
+ // `__proto__` is valid JSON, and parsing keeps it as an own key; a
+ // validator that rebuilt the object by assignment set the copy's prototype
+ // instead, so the key vanished from the returned value and the copy
+ // inherited whatever the key held. Labels are rebuilt the same way.
+ it("returns application data and labels with every key as it came, a key named __proto__ included, and never sets a prototype", async () => {
+ const body =
+ '{"id":"ses_1","agentId":"worker","deploymentId":"dep_1","status":"idle","source":"api",' +
+ '"labels":{"__proto__":"kept","title":"Fix login"},' +
+ '"turns":[{"id":"turn_1","input":"go","mode":"queue","status":"completed",' +
+ '"payload":{"__proto__":{"isAdmin":true},"safe":"ok"},"createdAt":"t","updatedAt":"t"}],' +
+ '"result":{"turnId":"turn_1","callId":"call_1","reportedAt":"t",' +
+ '"data":{"__proto__":{"isAdmin":true},"nested":[{"__proto__":1}]}},' +
+ '"createdAt":"t","updatedAt":"t"}';
+ const api = fakeApi({
+ "GET /api/managed-agents/sessions/ses_1": () =>
+ new Response(body, { status: 200, headers: { "content-type": "application/json" } }),
+ });
+ const session = await oc(api).sessions.get("ses_1");
+ const payload = session.turns[0]!.payload as Record<string, unknown>;
+ expect(JSON.stringify(payload)).toBe('{"__proto__":{"isAdmin":true},"safe":"ok"}');
+ expect(Object.hasOwn(payload, "__proto__")).toBe(true);
+ expect(Object.getPrototypeOf(payload)).toBe(Object.prototype);
+ expect(payload.isAdmin).toBeUndefined();
+ const data = session.result!.data as Record<string, unknown>;
+ expect(JSON.stringify(data)).toBe('{"__proto__":{"isAdmin":true},"nested":[{"__proto__":1}]}');
+ expect(Object.getPrototypeOf(data)).toBe(Object.prototype);
+ expect(data.isAdmin).toBeUndefined();
+ expect(JSON.stringify(session.labels)).toBe('{"__proto__":"kept","title":"Fix login"}');
+ expect(Object.getPrototypeOf(session.labels)).toBe(Object.prototype);
+ // Nothing else moved either: the session serializes back to the body it came from.
+ expect(JSON.stringify(session)).toBe(body);
+ });
+
// The API answers a repeated key with the turn's persisted status. A
// retry of a turn that has since completed, failed or been cancelled must
// say so; a receipt that read "queued" for a completed turn was the
diff --git a/sdks/typescript/src/agents/shapes.ts b/sdks/typescript/src/agents/shapes.ts
index e60b3ee1..c4fff482 100644
--- a/sdks/typescript/src/agents/shapes.ts
+++ b/sdks/typescript/src/agents/shapes.ts
@@ -121,13 +121,15 @@ export const anyRecord: Shape<Record<string, unknown>> = (value, path) => {
return value;
};
-/** An object whose values all match `item`, keyed by strings. */
+/**
+ * An object whose values all match `item`, keyed by strings. Built from its
+ * entries, never by assignment: a key named `__proto__` is a key of the body,
+ * and assigning it would set the copy's prototype and drop the key instead.
+ */
export function record<T>(item: Shape<T>): Shape<Record<string, T>> {
return (value, path) => {
if (!isRecord(value)) throw new ShapeError(path, "an object");
- const result: Record<string, T> = {};
- for (const [key, entry] of Object.entries(value)) result[key] = item(entry, at(path, key));
- return result;
+ return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, item(entry, at(path, key))]));
};
}
@@ -151,21 +153,33 @@ export function object<F extends Fields>(fields: F): Shape<Inferred<F>> {
};
}
-/** A JSON value: strings, numbers, booleans, null, arrays and objects of the same. Application data is checked as this and nothing more. */
+/**
+ * A JSON value: strings, numbers, booleans, null, arrays and objects of the
+ * same. Application data is checked as this and nothing more, and returned
+ * as it came: nothing is rebuilt, so every key stays a key (one named
+ * `__proto__` included) and an object keeps the prototype parsing gave it.
+ */
export const jsonValue: Shape<DataValue> = (value, path) => {
- if (value === null || typeof value === "string" || typeof value === "boolean") return value;
+ checkJsonValue(value, path);
+ return value as DataValue;
+};
+
+function checkJsonValue(value: unknown, path: string): void {
+ if (value === null || typeof value === "string" || typeof value === "boolean") return;
if (typeof value === "number") {
if (!Number.isFinite(value)) throw new ShapeError(path, "a JSON value");
- return value;
+ return;
+ }
+ if (Array.isArray(value)) {
+ value.forEach((entry, index) => checkJsonValue(entry, at(path, index)));
+ return;
}
- if (Array.isArray(value)) return value.map((entry, index) => jsonValue(entry, at(path, index)));
if (isRecord(value)) {
- const result: Record<string, DataValue> = {};
- for (const [key, entry] of Object.entries(value)) result[key] = jsonValue(entry, at(path, key));
- return result;
+ for (const [key, entry] of Object.entries(value)) checkJsonValue(entry, at(path, key));
+ return;
}
throw new ShapeError(path, "a JSON value");
-};
+}
/** A `204` or an empty body. */
export const none: Shape<void> = (value, path) => {