diff --git a/apps/app/src/core/worker/client.ts b/apps/app/src/core/worker/client.ts index cd09c60c..af35a2c0 100644 --- a/apps/app/src/core/worker/client.ts +++ b/apps/app/src/core/worker/client.ts @@ -112,14 +112,22 @@ class ImportClient { this.fail(this.ready ? 'engine' : 'worker_unavailable', 'messageerror') this.arm(this.options.startTimeoutMs, 'worker_unavailable') const { task, file, sheet, module } = this.input - this.worker.postMessage({ - v: PROTOCOL_VERSION, - type: 'task', - task, - size: file.size, - sheet, - module, - }) + try { + this.worker.postMessage({ + v: PROTOCOL_VERSION, + type: 'task', + task, + size: file.size, + sheet, + module, + }) + } catch (error) { + // A clone the engine refuses (the Module, say) must not leave an orphan worker. + this.fail( + 'worker_unavailable', + error instanceof Error ? error.name : 'post failed', + ) + } } cancel(): void { @@ -134,6 +142,7 @@ class ImportClient { private dispatch(message: WorkerMessage): void { switch (message.type) { case 'ready': + if (this.ready) return this.ready = true this.arm(this.options.taskTimeoutMs, 'timeout') void this.feed() diff --git a/apps/app/src/core/worker/handler.ts b/apps/app/src/core/worker/handler.ts index 22c5f003..4f4c26e2 100644 --- a/apps/app/src/core/worker/handler.ts +++ b/apps/app/src/core/worker/handler.ts @@ -31,7 +31,7 @@ export type EngineLoader = ( module: WebAssembly.Module | null, ) => Promise -/** The caps the worker checks before reserving a byte (the core enforces the same). */ +/** The caps the worker checks before reserving a byte (the core and the sink enforce them too). */ export const MAX_PDF_BYTES = 50 * 1024 * 1024 export const MAX_COPY_BYTES = 16 * 1024 * 1024 diff --git a/apps/app/src/core/worker/protocol.ts b/apps/app/src/core/worker/protocol.ts index 9426a95d..b21fd1e9 100644 --- a/apps/app/src/core/worker/protocol.ts +++ b/apps/app/src/core/worker/protocol.ts @@ -111,9 +111,28 @@ export function isWorkerMessage(data: unknown): data is WorkerMessage { return ofProtocol(data, WORKER_TYPES) } -/** True for a message of this protocol, from the page. */ +/** True for a well-formed message of this protocol, from the page. */ export function isPageMessage(data: unknown): data is PageMessage { - return ofProtocol(data, PAGE_TYPES) + if (!ofProtocol(data, PAGE_TYPES)) return false + const record = data as { + type: string + size?: unknown + bytes?: unknown + length?: unknown + } + if (record.type === 'task') return isByteCount(record.size) + if (record.type === 'chunk') { + return ( + record.bytes instanceof ArrayBuffer && + isByteCount(record.length) && + (record.length as number) <= record.bytes.byteLength + ) + } + return true +} + +function isByteCount(value: unknown): boolean { + return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0 } /** Error name and first message line, bounded, for a `failed` message. */ diff --git a/crates/core/src/facade.rs b/crates/core/src/facade.rs index f0af39aa..f33d1670 100644 --- a/crates/core/src/facade.rs +++ b/crates/core/src/facade.rs @@ -85,8 +85,13 @@ pub fn import_pdf(sheet: &LoadedSheet, bytes: &[u8]) -> String { pub fn restore_copy(sheet: &LoadedSheet, bytes: Vec) -> String { use crate::domain::session::restore_draft; use crate::ops::{over_value_budget, restore_failure}; - let outcome = String::from_utf8(bytes) - .map_err(|_| Failure::new("invalid", "the copy is not valid UTF-8")) + let outcome = Ok(bytes) + .and_then(|bytes: Vec| { + if bytes.len() > crate::pdf::read::MAX_ATTACHMENT_BYTES { + return Err(Failure::new("invalid", "the copy is over the size cap")); + } + String::from_utf8(bytes).map_err(|_| Failure::new("invalid", "not valid UTF-8")) + }) .and_then(|text| { let text = text.strip_prefix('\u{FEFF}').map_or(text.as_str(), |t| t); if over_value_budget(text) {