fix(app): import worker hardenings from the security gate; restore_copy size cap in the core
Page messages validated by field (size and chunk length as safe byte counts, length within the buffer); the task post wrapped so a refused clone leaves no orphan worker; a second ready ignored; the 16 MiB copy cap enforced in facade::restore_copy as well as in the worker (the uncommitted part of R4.3a). Gate verdict PASS, 7 LOW.
This commit is contained in:
1 parent
e0127b0726
commit
8ddf185aa6
4 files changed
+46
-13
No files matched your search
@@ -112,14 +112,22 @@ class ImportClient {
|
|||||||
this.fail(this.ready ? 'engine' : 'worker_unavailable', 'messageerror')
|
this.fail(this.ready ? 'engine' : 'worker_unavailable', 'messageerror')
|
||||||
this.arm(this.options.startTimeoutMs, 'worker_unavailable')
|
this.arm(this.options.startTimeoutMs, 'worker_unavailable')
|
||||||
const { task, file, sheet, module } = this.input
|
const { task, file, sheet, module } = this.input
|
||||||
this.worker.postMessage({
|
try {
|
||||||
v: PROTOCOL_VERSION,
|
this.worker.postMessage({
|
||||||
type: 'task',
|
v: PROTOCOL_VERSION,
|
||||||
task,
|
type: 'task',
|
||||||
size: file.size,
|
task,
|
||||||
sheet,
|
size: file.size,
|
||||||
module,
|
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 {
|
cancel(): void {
|
||||||
@@ -134,6 +142,7 @@ class ImportClient {
|
|||||||
private dispatch(message: WorkerMessage): void {
|
private dispatch(message: WorkerMessage): void {
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case 'ready':
|
case 'ready':
|
||||||
|
if (this.ready) return
|
||||||
this.ready = true
|
this.ready = true
|
||||||
this.arm(this.options.taskTimeoutMs, 'timeout')
|
this.arm(this.options.taskTimeoutMs, 'timeout')
|
||||||
void this.feed()
|
void this.feed()
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export type EngineLoader = (
|
|||||||
module: WebAssembly.Module | null,
|
module: WebAssembly.Module | null,
|
||||||
) => Promise<WorkerEngine>
|
) => Promise<WorkerEngine>
|
||||||
|
|
||||||
/** 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_PDF_BYTES = 50 * 1024 * 1024
|
||||||
export const MAX_COPY_BYTES = 16 * 1024 * 1024
|
export const MAX_COPY_BYTES = 16 * 1024 * 1024
|
||||||
|
|
||||||
|
|||||||
@@ -111,9 +111,28 @@ export function isWorkerMessage(data: unknown): data is WorkerMessage {
|
|||||||
return ofProtocol(data, WORKER_TYPES)
|
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 {
|
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. */
|
/** Error name and first message line, bounded, for a `failed` message. */
|
||||||
|
|||||||
@@ -85,8 +85,13 @@ pub fn import_pdf(sheet: &LoadedSheet, bytes: &[u8]) -> String {
|
|||||||
pub fn restore_copy(sheet: &LoadedSheet, bytes: Vec<u8>) -> String {
|
pub fn restore_copy(sheet: &LoadedSheet, bytes: Vec<u8>) -> String {
|
||||||
use crate::domain::session::restore_draft;
|
use crate::domain::session::restore_draft;
|
||||||
use crate::ops::{over_value_budget, restore_failure};
|
use crate::ops::{over_value_budget, restore_failure};
|
||||||
let outcome = String::from_utf8(bytes)
|
let outcome = Ok(bytes)
|
||||||
.map_err(|_| Failure::new("invalid", "the copy is not valid UTF-8"))
|
.and_then(|bytes: Vec<u8>| {
|
||||||
|
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| {
|
.and_then(|text| {
|
||||||
let text = text.strip_prefix('\u{FEFF}').map_or(text.as_str(), |t| t);
|
let text = text.strip_prefix('\u{FEFF}').map_or(text.as_str(), |t| t);
|
||||||
if over_value_budget(text) {
|
if over_value_budget(text) {
|
||||||
|
|||||||
Reference in new issue
Block a user