fix(input): validate escaped text against the actual command budget
The editor could clear 2048 quote characters although escaping exceeded the Native 4096-byte command budget. Measure the complete command before sending and retain oversized text. Core, Native and Browser use the 8192-byte UTF-8 carrier bound; independent queue budgets keep their own names. Static Browser values are compared with compiled Core, with no new WASM hot-path dependency. Real WebCrypto boundary probes, Native compilation and all 2521 Web tests pass.
This commit is contained in:
1 parent
ec34b77aec
commit
aa4e6ee45c
14 files changed
+65
-17
No files matched your search
@@ -2,6 +2,8 @@ export type InputIdentity = { endpointId: string; secret: string };
|
||||
export type InputPacket = { id?: string; epoch?: string; data?: string; closed?: boolean; ready?: boolean };
|
||||
export type InputTransport = Pick<RTCDataChannel, "readyState" | "bufferedAmount" | "send" | "close" | "addEventListener" | "removeEventListener">;
|
||||
|
||||
const MAX_QUEUED_BASE64_CHARS = 8192;
|
||||
|
||||
// Keys arrive over authenticated room signaling. An intermediate Viewer/SFU
|
||||
// transports ciphertext and cannot impersonate the approved controller.
|
||||
export class EncryptedInput extends EventTarget implements InputTransport {
|
||||
@@ -12,7 +14,7 @@ export class EncryptedInput extends EventTarget implements InputTransport {
|
||||
private readonly key: Promise<CryptoKey>;
|
||||
private outbound = Promise.resolve();
|
||||
private inbound = Promise.resolve();
|
||||
private incomingBytes = 0;
|
||||
private incomingBase64Chars = 0;
|
||||
constructor(private readonly identity: InputIdentity, private readonly counter: { sent: bigint }, private readonly transmit: (packet: InputPacket) => void,
|
||||
epoch = crypto.randomUUID().replaceAll("-", "")) {
|
||||
super();
|
||||
@@ -37,8 +39,8 @@ export class EncryptedInput extends EventTarget implements InputTransport {
|
||||
}).catch(() => this.close()).finally(() => { this.bufferedAmount -= plain.length; });
|
||||
}
|
||||
receive(data: string): void {
|
||||
if (this.incomingBytes + data.length > 8192) { this.close(); return; }
|
||||
this.incomingBytes += data.length;
|
||||
if (this.incomingBase64Chars + data.length > MAX_QUEUED_BASE64_CHARS) { this.close(); return; }
|
||||
this.incomingBase64Chars += data.length;
|
||||
this.inbound = this.inbound.then(async () => {
|
||||
if (this.readyState !== "open") return;
|
||||
const bytes = Uint8Array.from(atob(data), c => c.charCodeAt(0));
|
||||
@@ -51,7 +53,7 @@ export class EncryptedInput extends EventTarget implements InputTransport {
|
||||
this.received = sequence;
|
||||
this.dispatchEvent(new MessageEvent("message", { data: new TextDecoder().decode(plain) }));
|
||||
}).catch(() => { /* Unauthenticated relay data has no input meaning. */ })
|
||||
.finally(() => { this.incomingBytes -= data.length; });
|
||||
.finally(() => { this.incomingBase64Chars -= data.length; });
|
||||
}
|
||||
close(): void {
|
||||
if (this.readyState === "closed") return;
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import type { InputTransport } from "./input-route";
|
||||
import { debugError, debugEvent } from "../lib/debug";
|
||||
import { remoteInputStateSchema, remoteTextAvailable, remoteGamepadAvailable, type RemoteInputState, type RemoteGamepadReport } from "./protocol";
|
||||
import { remoteInputStateSchema, remoteTextFits, remoteTextAvailable, remoteGamepadAvailable, type RemoteInputState, type RemoteGamepadReport } from "./protocol";
|
||||
export type { RemoteInputState } from "./protocol";
|
||||
const MAX_QUEUED_PLAINTEXT_BYTES = 8192;
|
||||
|
||||
// Each virtual contact has its own source. Physical keys use the default source.
|
||||
type KeyInput = { type: "key"; code: string; down: boolean; source?: symbol; repeat?: boolean };
|
||||
|
||||
@@ -110,7 +112,7 @@ export class RemoteInputSession {
|
||||
if (this.disposed || this.channel.readyState !== "open") return false;
|
||||
try {
|
||||
// A stalled input queue must not replay seconds of old actions later.
|
||||
if (this.channel.bufferedAmount > 8192) throw new Error("input queue stalled");
|
||||
if (this.channel.bufferedAmount > MAX_QUEUED_PLAINTEXT_BYTES) throw new Error("input queue stalled");
|
||||
this.channel.send(JSON.stringify(event));
|
||||
return true;
|
||||
} catch (error) {
|
||||
@@ -183,6 +185,7 @@ export class RemoteInputSession {
|
||||
const state = this.state;
|
||||
if (!this.intent || !state?.grant || state.intent !== this.intent ||
|
||||
(state.state !== "active" && !((event.type === "reset" || event.type === "gamepad-reset" || event.type === "gamepad-stop") && state.state === "suspended"))) return;
|
||||
if (event.type === "text" && !remoteTextFits(event.text, state.grant)) return;
|
||||
if (event.type === "key") {
|
||||
const { type, code, down } = event;
|
||||
if (this.keyTransition(event)) this.send({ type, code, down, grant: state.grant });
|
||||
|
||||
@@ -1,8 +1,22 @@
|
||||
import { z } from "zod";
|
||||
import { isComposingOrAltGraph } from "../ui/shortcut-key";
|
||||
|
||||
// A text submission still fits one encrypted input packet and the existing queue.
|
||||
export const INPUT_FRAME_BYTES = 8192;
|
||||
export const REMOTE_INPUT_COMMAND_BYTES = 4096;
|
||||
export const REMOTE_TEXT_BYTES = 2048;
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
export function inputFrameFits(frame: string): boolean {
|
||||
return frame.length <= INPUT_FRAME_BYTES && encoder.encode(frame).byteLength <= INPUT_FRAME_BYTES;
|
||||
}
|
||||
|
||||
// The editor can measure the command before sending. Every grant has the same hex length.
|
||||
const exampleGrant = "0".repeat(64);
|
||||
export function remoteTextFits(text: string, grant = exampleGrant): boolean {
|
||||
return text.length <= REMOTE_TEXT_BYTES && encoder.encode(text).byteLength <= REMOTE_TEXT_BYTES
|
||||
&& encoder.encode(JSON.stringify({ type: "text", text, grant })).byteLength <= REMOTE_INPUT_COMMAND_BYTES;
|
||||
}
|
||||
|
||||
// F12 is reserved by Windows debuggers, including for global modified chords.
|
||||
export const remoteShortcutKeySchema = z.number().int().min(1).max(11);
|
||||
export const REMOTE_SHORTCUT_KEYS = Array.from({ length: 11 }, (_, index) => index + 1);
|
||||
|
||||
Reference in new issue
Block a user