Skip to content

Commit d3b7e8b

Browse files
fix(desktop): resolve annotation tool shortcuts with fixed key cases
The preload shares the inspected page's JavaScript realm, so a plain object lookup keyed by event.key also reads properties the page plants on Object.prototype. A page setting Object.prototype.x = "send" turned a genuine x key outside the overlay into a submission. Compare the four tool keys directly again and pin the inherited-property case in the existing outside-overlay test. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent ba21d8d commit d3b7e8b

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎apps/desktop/src/preview/AnnotationKeyboard.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,16 @@ describe("handleAnnotationKeyEvent", () => {
118118
expect(dispatchKey({ inOverlay: false }, { key: "r" }).action).toBe("marquee");
119119
expect(dispatchKey({ inOverlay: false }, { key: "Escape" }).action).toBe("cancel");
120120
expect(dispatchKey({ inOverlay: false }, { key: "r" }, "keyup").action).toBeNull();
121+
// The preload shares the page's realm, so the page owns Object.prototype.
122+
/* eslint-disable no-extend-native -- the inspected page can do this */
123+
const inherited = Object.getOwnPropertyDescriptor(Object.prototype, "x");
124+
Object.defineProperty(Object.prototype, "x", { value: "send", configurable: true });
125+
try {
126+
expect(dispatchKey({ inOverlay: false }, { key: "x" }).action).toBeNull();
127+
} finally {
128+
if (inherited) Object.defineProperty(Object.prototype, "x", inherited);
129+
else Reflect.deleteProperty(Object.prototype, "x");
130+
}
131+
/* eslint-enable no-extend-native */
121132
});
122133
});

‎apps/desktop/src/preview/AnnotationKeyboard.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ interface AnnotationKeyEvent extends AnnotationKeyboardEvent {
2121

2222
export type AnnotationKeyAction = "cancel" | AnnotationTool | PreviewAnnotationSubmission;
2323

24-
const TOOL_SHORTCUTS: Readonly<Record<string, AnnotationTool>> = {
25-
v: "select",
26-
r: "marquee",
27-
d: "draw",
28-
e: "erase",
29-
};
30-
3124
// Runs from the preload's window capture listeners, ahead of any listener the
3225
// inspected page installs. Overlay events are hidden from the page but never
3326
// cancelled, so the editor still receives Space, caret keys and composition.
@@ -55,7 +48,20 @@ export function handleAnnotationKeyEvent(
5548
if (submission) event.preventDefault();
5649
return submission;
5750
}
58-
return TOOL_SHORTCUTS[event.key] ?? null;
51+
// Fixed cases, not a table: the preload shares the page's realm, so an
52+
// object lookup would also read keys the page planted on Object.prototype.
53+
switch (event.key) {
54+
case "v":
55+
return "select";
56+
case "r":
57+
return "marquee";
58+
case "d":
59+
return "draw";
60+
case "e":
61+
return "erase";
62+
default:
63+
return null;
64+
}
5965
}
6066

6167
export function resolveAnnotationSubmission(

0 commit comments

Comments
 (0)