Skip to content

Commit 06b1a54

Browse files
matt2eclaude
andcommitted
fix(composer): refuse control characters committed as text
Corrects the range 4bfb6ff8 inferred with the character that was observed. That commit refused the private-use function-key character AppKit puts in a caret key's NSEvent (Right Arrow is U+F703) and said the box the user saw was, by every sign, that character. The persisted draft shows otherwise: read from a copy of the desktop build's WebKit local storage, its last paragraph is a single text node of fourteen U+001D characters, one per Right Arrow press, and the draft's text field ends in the same fourteen. U+001D is the C0 control ASCII calls the Group Separator, nowhere near the guarded block, so both seams let it through and the box still appeared. The character is a keyboard-layout translation surviving into text. An in-process AppKit and Carbon probe gives U+001D for Right Arrow's keycode from CGEvent.keyboardGetUnicodeString and UCKeyTranslate, while a real NSEvent built from the press carries U+F703: AppKit remaps the arrows (U+001C through U+001F), Home and End (U+0001 and U+0004), Page Up and Page Down (U+000B and U+000C) and the function keys to the private-use block. WebKit's KeyEventMac.mm blanks a key event's text only when its single character lies in U+F700 through U+F7FF, so a hardware Right Arrow inserts nothing at the end of the text, while a raw layout translation, which only a directly constructed key event carries in its characters, matches no key binding and is committed through insertText as ordinary text. The boxes are therefore inferred to come from a synthetic key event rather than a physical press; whatever the source, a lone control character is never prose and the composer refuses it. CONTROL_CHARACTERS replaces FUNCTION_KEY_CHARACTERS and controlCharacterText replaces functionKeyText. The class covers the whole Unicode Control category as \p{Cc} (the C0 controls, DEL and the C1 controls U+0080 through U+009F, none of which a layout types or a font draws) plus the private-use block; Biome's noControlCharactersInRegex rule refuses explicit C0 escapes, and the category is the clearer statement anyway. Tab, newline and carriage return, which typed text legitimately holds, are excluded by a second regex. The all-or-nothing semantics hold: text mixing a real character with a control character passes untouched. Both seams stay: beforeinput cancels a cancelable insertText whose data is control text so the DOM never changes, and handleTextInput returns true for it without a transaction so an insertion that arrived anyway is redrawn away. The paste, drop and programmatic insert() paths and the two-transaction undo shape of the typed conversions are untouched, and no typed-conversion rule runs for a refused insertion. The comment now states the observed U+001D and its origin first and keeps the private-use rationale as the secondary case. docs/channels.md does not name the guarded characters and is unchanged. Tests: the function-key table in EditableInput.test.tsx becomes a control-character table adding U+001C through U+001F, U+0001, U+0004, U+000B, U+000C, DEL and the C1 bounds U+0080 and U+009F, each refused at both seams with the draft, DOM and caret unchanged and typing continuing; the four caret-key contexts and the typed-conversion case commit Right Arrow as U+001D beside U+F703; a new case asserts that a native tab, newline and carriage return (read as a newline by ProseMirror's parser) and the mixed string a\u001D are still inserted whole. MessageComposer dispatches U+001D beside U+F703 and asserts nothing was sent; the composer-formats journey commits U+001D beside U+F703 through keyboard.insertText on Chromium and WebKit. The seventeen Vitest cases and both browser journeys fail at 97b429fa, where U+001D lands in the document, and pass with the fix. Both composer suites (264 tests), the whole browser spec on both engines, tsc --noEmit and biome check --error-on-warnings pass, the last reporting only its pre-existing note that biome.json names schema 2.5.12 while 2.5.14 is installed. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent 5b93463 commit 06b1a54

4 files changed

Lines changed: 139 additions & 78 deletions

File tree

‎src/features/messages/EditableInput.test.tsx‎

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,20 +1209,37 @@ it("retains the native caret after replacing text with an unchanged suffix", asy
12091209
expect(h.input.selectionEnd).toBe(10);
12101210
});
12111211

1212-
/** The private-use character AppKit puts in a caret or function key's NSEvent
1213-
* (NSUpArrowFunctionKey U+F700 onwards), which the host's native text-input
1214-
* path can commit as typed text. */
1215-
const functionKeys = [
1216-
["ArrowUp", "\uF700"],
1217-
["ArrowDown", "\uF701"],
1218-
["ArrowLeft", "\uF702"],
1219-
["ArrowRight", "\uF703"],
1220-
["F1", "\uF704"],
1221-
["Home", "\uF729"],
1222-
["End", "\uF72B"],
1223-
["PageUp", "\uF72C"],
1224-
["PageDown", "\uF72D"],
1212+
/** A character the host's native text-input path can commit as typed text for
1213+
* a caret or function key. The desktop build committed U+001D on every Right
1214+
* Arrow press: a key's raw keyboard-layout translation is a C0 control (the
1215+
* arrows are U+001C through U+001F), which WebKit does not strip from a key
1216+
* event's text. AppKit's own NSEvent carries the private-use function-key
1217+
* character instead (NSUpArrowFunctionKey U+F700 onwards), which WebKit strips
1218+
* and the guard refuses as well. DEL and the C1 controls are as glyphless. */
1219+
const controlKeys = [
1220+
["Left Arrow's layout translation U+001C", "\u001C"],
1221+
["Right Arrow's layout translation U+001D", "\u001D"],
1222+
["Up Arrow's layout translation U+001E", "\u001E"],
1223+
["Down Arrow's layout translation U+001F", "\u001F"],
1224+
["Home's layout translation U+0001", "\u0001"],
1225+
["End's layout translation U+0004", "\u0004"],
1226+
["Page Up's layout translation U+000B", "\u000B"],
1227+
["Page Down's layout translation U+000C", "\u000C"],
1228+
["DEL U+007F", "\u007F"],
1229+
["the first C1 control U+0080", "\u0080"],
1230+
["the last C1 control U+009F", "\u009F"],
1231+
["Up Arrow's function-key character U+F700", "\uF700"],
1232+
["Down Arrow's function-key character U+F701", "\uF701"],
1233+
["Left Arrow's function-key character U+F702", "\uF702"],
1234+
["Right Arrow's function-key character U+F703", "\uF703"],
1235+
["F1's function-key character U+F704", "\uF704"],
1236+
["Home's function-key character U+F729", "\uF729"],
1237+
["End's function-key character U+F72B", "\uF72B"],
1238+
["Page Up's function-key character U+F72C", "\uF72C"],
1239+
["Page Down's function-key character U+F72D", "\uF72D"],
12251240
] as const;
1241+
/** Right Arrow committed as text in both forms the host can produce. */
1242+
const rightArrowCharacters = ["\u001D", "\uF703"] as const;
12261243
const caretKeys = [
12271244
"ArrowRight",
12281245
"ArrowLeft",
@@ -1296,9 +1313,9 @@ function nativeInsert(
12961313
return inserted;
12971314
}
12981315

1299-
it.each(functionKeys)(
1300-
"refuses the %s function-key character at both native seams",
1301-
async (_key, character) => {
1316+
it.each(controlKeys)(
1317+
"refuses %s committed as text at both native seams",
1318+
async (_label, character) => {
13021319
const h = mount("abc");
13031320
const before = snapshot(h);
13041321
// The cancelable seam: the DOM never changes.
@@ -1316,6 +1333,23 @@ it.each(functionKeys)(
13161333
},
13171334
);
13181335

1336+
it.each([
1337+
["a tab", "\t", "abc\t"],
1338+
["a newline", "\n", "abc\n"],
1339+
// ProseMirror's DOM parser reads a carriage return as a newline.
1340+
["a carriage return", "\r", "abc\n"],
1341+
// No key yields a control character beside real text; such text is left
1342+
// alone rather than reshaped under the native selection that describes it.
1343+
["a control character beside real text, whole", "a\u001D", "abca\u001D"],
1344+
])("still inserts %s committed as text", async (_label, text, value) => {
1345+
const h = mount("abc");
1346+
expect(nativeInsert(h.input, text)).toBe(true);
1347+
await waitFor(() => expect(h.input).toHaveValue(value));
1348+
expect(h.draft().text).toBe(value);
1349+
expect(h.input.selectionStart).toBe(value.length);
1350+
expect(h.input.selectionEnd).toBe(value.length);
1351+
});
1352+
13191353
it.each([
13201354
["after text", async (h: ReturnType<typeof mount>) => h.input.value],
13211355
[
@@ -1346,7 +1380,7 @@ it.each([
13461380
},
13471381
],
13481382
])(
1349-
"keeps the document unchanged by caret keys %s, and by a Right Arrow committed as text",
1383+
"keeps the document unchanged by caret keys %s, and by a Right Arrow committed as text in either form",
13501384
async (_context, setup) => {
13511385
const h = mount("abc");
13521386
const value = await setup(h);
@@ -1357,27 +1391,32 @@ it.each([
13571391
expect(snapshot(h), key).toEqual(before);
13581392
expect(h.input.selectionStart, key).toBe(h.input.selectionEnd);
13591393
}
1360-
act(() => h.input.setSelectionRange(start, start));
1361-
expect(nativeInsert(h.input, "\uF703")).toBe(false);
1362-
expect(snapshot(h)).toEqual(before);
1363-
expect(nativeInsert(h.input, "\uF703", false)).toBe(true);
1364-
await waitFor(() => expect(h.input.textContent).not.toContain("\uF703"));
1365-
expect(snapshot(h)).toEqual(before);
1394+
for (const character of rightArrowCharacters) {
1395+
act(() => h.input.setSelectionRange(start, start));
1396+
expect(nativeInsert(h.input, character), character).toBe(false);
1397+
expect(snapshot(h), character).toEqual(before);
1398+
expect(nativeInsert(h.input, character, false), character).toBe(true);
1399+
await waitFor(() => expect(h.input.textContent).not.toContain(character));
1400+
expect(snapshot(h), character).toEqual(before);
1401+
}
13661402
expect(h.input).toHaveValue(value);
13671403
expect(h.draft().recipients.map((item) => item.name)).toEqual(
13681404
value.includes("@Honey") ? ["Honey"] : [],
13691405
);
13701406
},
13711407
);
13721408

1373-
it("never runs a typed conversion for a function-key character", async () => {
1374-
const h = mount();
1375-
await h.user.keyboard("**a*");
1376-
const before = snapshot(h);
1377-
expect(nativeInsert(h.input, "\uF703", false)).toBe(true);
1378-
await waitFor(() => expect(h.input.textContent).toBe("**a*"));
1379-
expect(snapshot(h)).toEqual(before);
1380-
await h.user.keyboard("*");
1381-
expect(h.input.querySelector("strong")).toHaveTextContent("a");
1382-
expect(h.markdown()).toBe("**a**");
1383-
});
1409+
it.each(rightArrowCharacters)(
1410+
"never runs a typed conversion for the control character %s",
1411+
async (character) => {
1412+
const h = mount();
1413+
await h.user.keyboard("**a*");
1414+
const before = snapshot(h);
1415+
expect(nativeInsert(h.input, character, false)).toBe(true);
1416+
await waitFor(() => expect(h.input.textContent).toBe("**a*"));
1417+
expect(snapshot(h)).toEqual(before);
1418+
await h.user.keyboard("*");
1419+
expect(h.input.querySelector("strong")).toHaveTextContent("a");
1420+
expect(h.markdown()).toBe("**a**");
1421+
},
1422+
);

‎src/features/messages/EditableInput.tsx‎

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,30 @@ export type EditorDecoration = {
8686
content: ReactNode;
8787
editAsText?: boolean;
8888
};
89-
/** AppKit's NSEvent carries a private-use "function key" character for every
90-
* caret, editing and function key (NSUpArrowFunctionKey U+F700 through
91-
* NSModeSwitchFunctionKey U+F747; Right Arrow is U+F703). No font has a glyph
92-
* for them and no keyboard layout types them, so text made only of these is a
93-
* key the host's native text-input path committed as if typed, not prose. */
94-
const FUNCTION_KEY_CHARACTERS = /[\uF700-\uF747]/gu;
95-
/** True when a native text insertion holds only function-key characters. */
96-
function functionKeyText(text: string | null | undefined) {
97-
return !!text && !text.replace(FUNCTION_KEY_CHARACTERS, "");
89+
/** Characters a caret or function key can leave in the document when the
90+
* host's native text-input path commits the key as if typed. In the desktop
91+
* build every Right Arrow press at the end of a message committed U+001D, the
92+
* key's raw keyboard-layout translation: the arrows translate to U+001C
93+
* through U+001F, Home and End to U+0001 and U+0004, Page Up and Page Down to
94+
* U+000B and U+000C. WebKit blanks a key event's text only for the private-use
95+
* block U+F700 through U+F7FF, so these C0 controls survive into insertText.
96+
* AppKit's own NSEvent carries that private-use "function key" character
97+
* instead (NSUpArrowFunctionKey U+F700 through NSModeSwitchFunctionKey U+F747;
98+
* Right Arrow is U+F703), so the block is refused as well. No font has a glyph
99+
* for any of these and no layout types them as prose; the whole Unicode
100+
* Control category (C0, DEL and C1) is covered, except the tab, newline and
101+
* carriage return that typed text legitimately holds. */
102+
const CONTROL_CHARACTERS = /[\p{Cc}\uF700-\uF747]/gu;
103+
/** The control characters typed text holds: tab, newline and carriage return. */
104+
const TYPED_CONTROLS = /[\t\n\r]/u;
105+
/** True when a native text insertion holds only control characters. Text that
106+
* mixes them with real characters is not control text and passes untouched. */
107+
function controlCharacterText(text: string | null | undefined) {
108+
return (
109+
!!text &&
110+
!TYPED_CONTROLS.test(text) &&
111+
!text.replace(CONTROL_CHARACTERS, "")
112+
);
98113
}
99114
export type EditableInputProps = Omit<
100115
HTMLAttributes<ComposerInputElement>,
@@ -962,15 +977,14 @@ export function EditableInput({
962977
},
963978
},
964979
handleTextInput(_view, from, to, text, defaultTransaction) {
965-
// A caret key committed natively as its function-key character is
966-
// refused: with no transaction the document stays as it was, and
967-
// ProseMirror redraws the changed DOM from that state. Only the
968-
// beforeinput seam can stop the DOM change itself; this seam covers
969-
// an insertion that arrived without one, or one that was not
970-
// cancelable. No key yields such a character beside real text, so
971-
// mixed text is left alone rather than reshaped under the native
972-
// selection that describes it.
973-
if (functionKeyText(text)) return true;
980+
// A caret key committed natively as a control character is refused:
981+
// with no transaction the document stays as it was, and ProseMirror
982+
// redraws the changed DOM from that state. Only the beforeinput seam
983+
// can stop the DOM change itself; this seam covers an insertion that
984+
// arrived without one, or one that was not cancelable. No key yields
985+
// such a character beside real text, so mixed text is left alone
986+
// rather than reshaped under the native selection that describes it.
987+
if (controlCharacterText(text)) return true;
974988
// Provenance is not an inheritable formatting mark. Replacing text,
975989
// including an identical string, revokes the identity it touches.
976990
const source = projection();
@@ -1151,14 +1165,14 @@ export function EditableInput({
11511165
},
11521166
beforeinput(_view, event) {
11531167
if (!event.isComposing) syncNativeSelection();
1154-
// The host's native text-input path can commit a caret key's
1155-
// function-key character (Right Arrow is U+F703) as typed text.
1156-
// Cancel it before the DOM changes; handleTextInput refuses what
1157-
// arrives anyway.
1168+
// The host's native text-input path can commit a caret key as a
1169+
// control character (Right Arrow arrives as U+001D, or as AppKit's
1170+
// U+F703) typed as text. Cancel it before the DOM changes;
1171+
// handleTextInput refuses what arrives anyway.
11581172
if (
11591173
event.inputType === "insertText" &&
11601174
event.cancelable &&
1161-
functionKeyText(event.data)
1175+
controlCharacterText(event.data)
11621176
) {
11631177
event.preventDefault();
11641178
return true;

‎src/features/messages/MessageComposer.test.tsx‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ it("sends a pasted fenced block verbatim on Enter instead of opening a block fro
772772
expect(h.input()).toHaveValue("");
773773
});
774774

775-
it("keeps a composed message unchanged through caret keys at its end and refuses a Right Arrow committed as text", async () => {
775+
it("keeps a composed message unchanged through caret keys at its end and refuses a Right Arrow committed as text in either form", async () => {
776776
const h = mount();
777777
await h.user.type(h.input(), "```");
778778
expect(h.input().querySelector("pre")).not.toBeNull();
@@ -803,22 +803,26 @@ it("keeps a composed message unchanged through caret keys at its end and refuses
803803
expect(h.input()).toHaveValue("Hello!\nacascac\naa a\nacacs\n");
804804
expect(h.input().innerHTML).toBe(html);
805805
expect(h.messages.send).not.toHaveBeenCalled();
806-
let prevented = false;
807-
act(() => {
808-
h.input().focus();
809-
prevented = !h.input().dispatchEvent(
810-
new InputEvent("beforeinput", {
811-
bubbles: true,
812-
cancelable: true,
813-
inputType: "insertText",
814-
data: "\uF703",
815-
}),
816-
);
817-
});
818-
expect(prevented).toBe(true);
819-
expect(h.input()).toHaveValue("Hello!\nacascac\naa a\nacacs\n");
820-
expect(h.input().innerHTML).toBe(html);
821-
expect(h.messages.send).not.toHaveBeenCalled();
806+
// The desktop build committed Right Arrow's raw keyboard-layout translation
807+
// U+001D; AppKit's function-key character for the key is U+F703.
808+
for (const character of ["\u001D", "\uF703"]) {
809+
let prevented = false;
810+
act(() => {
811+
h.input().focus();
812+
prevented = !h.input().dispatchEvent(
813+
new InputEvent("beforeinput", {
814+
bubbles: true,
815+
cancelable: true,
816+
inputType: "insertText",
817+
data: character,
818+
}),
819+
);
820+
});
821+
expect(prevented, character).toBe(true);
822+
expect(h.input()).toHaveValue("Hello!\nacascac\naa a\nacacs\n");
823+
expect(h.input().innerHTML).toBe(html);
824+
expect(h.messages.send).not.toHaveBeenCalled();
825+
}
822826
});
823827

824828
it("prefixes thread replies with the selected media time and clears it after send", async () => {

‎tests/browser/composer-formats.spec.mjs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,17 @@ test("caret keys at the end of a composed message never insert a character", asy
160160
await page.keyboard.press("Meta");
161161
expect(await state()).toEqual(before);
162162
// The platform's own text-insertion path (WebKit's insertTextAsync, Chromium's
163-
// IME commit) handing the composer Right Arrow's function-key character U+F703
164-
// is refused before the DOM changes, and typing continues as before.
163+
// IME commit) handing the composer Right Arrow's raw keyboard-layout
164+
// translation U+001D, the character the desktop build committed on every
165+
// press, or AppKit's function-key character for the key U+F703, is refused
166+
// before the DOM changes, and typing continues as before.
165167
await input.evaluate((el) =>
166168
el.setSelectionRange(el.value.length, el.value.length),
167169
);
168-
await page.keyboard.insertText("\uF703");
169-
expect(await state()).toEqual(before);
170+
for (const character of ["\u001D", "\uF703"]) {
171+
await page.keyboard.insertText(character);
172+
expect(await state(), JSON.stringify(character)).toEqual(before);
173+
}
170174
await page.keyboard.type("!");
171175
await expect
172176
.poll(() => input.evaluate((el) => el.value))

0 commit comments

Comments
 (0)