Skip to content

Commit 25d2eba

Browse files
StiensWoutclaude
andcommitted
fix(web,mobile): answers to a question named __proto__ reach the provider
Web and mobile built the answer map by assigning into `{}`, so a question id of `__proto__` hit the prototype setter and was dropped from the response. The shared draft seeding did the same and also read `Object.prototype` as an existing draft, so it never seeded that question. Build both with `Object.fromEntries` and check `Object.hasOwn`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8827df5 commit 25d2eba

6 files changed

Lines changed: 73 additions & 18 deletions

File tree

‎apps/mobile/src/lib/threadActivity.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,6 +2242,24 @@ describe("pending user input answers", () => {
22422242
).toEqual({ customAnswer: "" });
22432243
});
22442244

2245+
it.each([
2246+
["single-select", singleSelectQuestion, ["Go"], '"Go"'],
2247+
["multi-select", multiSelectQuestion, ["Orders", "Listings"], '["Orders","Listings"]'],
2248+
])("sends a %s answer to a question with the id __proto__", (_kind, question, selected, json) => {
2249+
const answers = buildPendingUserInputAnswers(
2250+
[
2251+
{ ...question, id: "__proto__" },
2252+
{ ...singleSelectQuestion, id: "name" },
2253+
],
2254+
{
2255+
["__proto__"]: { selectedOptionValues: selected },
2256+
name: { selectedOptionValues: ["Go"] },
2257+
},
2258+
);
2259+
2260+
expect(JSON.stringify(answers)).toBe(`{"__proto__":${json},"name":"Go"}`);
2261+
});
2262+
22452263
it("builds array answers for multi-select questions", () => {
22462264
expect(
22472265
buildPendingUserInputAnswers([singleSelectQuestion, multiSelectQuestion], {

‎apps/mobile/src/lib/threadActivity.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,21 +1730,25 @@ export function togglePendingUserInputOptionSelection(
17301730
};
17311731
}
17321732

1733+
/**
1734+
* Answers keyed by question id, or null while any question is unanswered.
1735+
* Entries rather than assignment, so an id such as `__proto__` stays an own key.
1736+
*/
17331737
export function buildPendingUserInputAnswers(
17341738
questions: ReadonlyArray<ThreadUserInputQuestion>,
17351739
draftAnswers: Record<string, PendingUserInputDraftAnswer>,
17361740
): Record<string, string | ReadonlyArray<string>> | null {
1737-
const answers: Record<string, string | ReadonlyArray<string>> = {};
1741+
const entries: Array<[string, string | ReadonlyArray<string>]> = [];
17381742

17391743
for (const question of questions) {
17401744
const answer = resolvePendingUserInputAnswer(question, draftAnswers[question.id]);
17411745
if (answer === null) {
17421746
return null;
17431747
}
1744-
answers[question.id] = answer;
1748+
entries.push([question.id, answer]);
17451749
}
17461750

1747-
return answers;
1751+
return Object.fromEntries(entries);
17481752
}
17491753

17501754
/**

‎apps/web/src/pendingUserInput.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,24 @@ describe("buildPendingUserInputAnswers", () => {
262262
expect(buildPendingUserInputAnswers([singleSelectQuestion], {})).toBeNull();
263263
});
264264

265+
it.each([
266+
["single-select", singleSelectQuestion, ["Orchestration-first"], '"Orchestration-first"'],
267+
["multi-select", multiSelectQuestion, ["Server", "Web"], '["Server","Web"]'],
268+
])("sends a %s answer to a question with the id __proto__", (_kind, question, selected, json) => {
269+
const answers = buildPendingUserInputAnswers(
270+
[
271+
{ ...question, id: "__proto__" },
272+
{ ...singleSelectQuestion, id: "name" },
273+
],
274+
{
275+
["__proto__"]: { selectedOptionValues: selected },
276+
name: { selectedOptionValues: ["Orchestration-first"] },
277+
},
278+
);
279+
280+
expect(JSON.stringify(answers)).toBe(`{"__proto__":${json},"name":"Orchestration-first"}`);
281+
});
282+
265283
it.each([" first\t", ""])("preserves the exact selected option value %j", (value) => {
266284
const question = {
267285
...nativeChoiceQuestion,

‎apps/web/src/pendingUserInput.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,25 @@ export function togglePendingUserInputOptionSelection(
149149
};
150150
}
151151

152+
/**
153+
* Answers keyed by question id, or null while any question is unanswered.
154+
* Entries rather than assignment, so an id such as `__proto__` stays an own key.
155+
*/
152156
export function buildPendingUserInputAnswers(
153157
questions: ReadonlyArray<UserInputQuestion>,
154158
draftAnswers: Record<string, PendingUserInputDraftAnswer>,
155159
): Record<string, string | string[]> | null {
156-
const answers: Record<string, string | string[]> = {};
160+
const entries: Array<[string, string | string[]]> = [];
157161

158162
for (const question of questions) {
159163
const answer = resolvePendingUserInputAnswer(question, draftAnswers[question.id]);
160164
if (answer === null) {
161165
return null;
162166
}
163-
answers[question.id] = answer;
167+
entries.push([question.id, answer]);
164168
}
165169

166-
return answers;
170+
return Object.fromEntries(entries);
167171
}
168172

169173
export function countAnsweredPendingUserInputQuestions(

‎packages/client-runtime/src/state/threadRequests.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ describe("initial user input drafts", () => {
4343
customAnswer: " Proposed commit\n\n",
4444
});
4545
});
46+
47+
it("seeds a question with the id __proto__ as its own draft", () => {
48+
const seeded = seedUserInputDraftAnswers(
49+
[
50+
{ id: "__proto__", initialAnswer: "Proposed" },
51+
{ id: "name", initialAnswer: "Control" },
52+
],
53+
{},
54+
);
55+
56+
expect(JSON.stringify(seeded)).toBe(
57+
'{"__proto__":{"customAnswer":"Proposed"},"name":{"customAnswer":"Control"}}',
58+
);
59+
});
4660
});
4761
const projection: OrchestrationV2ThreadProjection = {
4862
...v2Projection,

‎packages/client-runtime/src/state/threadRequests.ts‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,15 @@ export function seedUserInputDraftAnswers<Draft extends { readonly customAnswer?
4848
>,
4949
drafts: Record<string, Draft>,
5050
): Record<string, Draft | { customAnswer: string }> {
51-
let seeded: Record<string, Draft | { customAnswer: string }> = drafts;
52-
for (const question of questions) {
53-
if (
54-
question.initialAnswer === undefined ||
55-
question.allowCustomAnswer === false ||
56-
seeded[question.id] !== undefined
57-
)
58-
continue;
59-
if (seeded === drafts) seeded = { ...drafts };
60-
seeded[question.id] = { customAnswer: question.initialAnswer };
61-
}
62-
return seeded;
51+
// Entries, not assignment, so an id such as `__proto__` becomes an own draft.
52+
const seeds = questions.flatMap((question) =>
53+
question.initialAnswer === undefined ||
54+
question.allowCustomAnswer === false ||
55+
Object.hasOwn(drafts, question.id)
56+
? []
57+
: [[question.id, { customAnswer: question.initialAnswer }] as const],
58+
);
59+
return seeds.length === 0 ? drafts : { ...drafts, ...Object.fromEntries(seeds) };
6360
}
6461

6562
/** Joins pending request entities to the request items that carry display data. */

0 commit comments

Comments
 (0)