mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 10:34:41 +00:00
fix(javascript): authenticate transfer bytes before decoding (#10078)
fix(javascript): authenticate raw analysis transfer bytes
This commit is contained in:
1 parent
d1895841a4
commit
a14a0c20ed
3 files changed
+242
-16
No files matched your search
@@ -4,6 +4,7 @@ import { open, stat } from "node:fs/promises";
|
||||
import { z } from "zod";
|
||||
import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js";
|
||||
import type { JsonValue } from "../../domain/jsonValue.js";
|
||||
import { safeParseUtf8Json } from "../../domain/safeJson.js";
|
||||
|
||||
import type { JavaScriptArtifactFile } from "../../domain/javascript/javascriptArtifactFiles.js";
|
||||
import type { JavaScriptModuleSemanticIr } from "../../domain/javascript/javascriptModuleSemanticIr.js";
|
||||
@@ -367,28 +368,38 @@ export async function readJavaScriptAnalysisTransfer(
|
||||
...(signal === undefined ? {} : { signal }),
|
||||
});
|
||||
let bytes = 0;
|
||||
let pending = "";
|
||||
stream.setEncoding("utf8");
|
||||
let pending: Buffer[] = [];
|
||||
let pendingBytes = 0;
|
||||
try {
|
||||
for await (const chunk of stream) {
|
||||
signal?.throwIfAborted();
|
||||
if (typeof chunk !== "string")
|
||||
throw new TypeError("JavaScript transfer stream is not UTF-8 text");
|
||||
bytes += Buffer.byteLength(chunk);
|
||||
if (!Buffer.isBuffer(chunk))
|
||||
throw new TypeError("JavaScript transfer stream is not bytes");
|
||||
bytes += chunk.length;
|
||||
if (bytes > expected.bytes)
|
||||
throw new RangeError("JavaScript transfer grew while being read");
|
||||
digest.update(chunk);
|
||||
pending += chunk;
|
||||
let start = 0;
|
||||
let newline: number;
|
||||
while ((newline = pending.indexOf("\n")) >= 0) {
|
||||
const line = pending.slice(0, newline);
|
||||
pending = pending.slice(newline + 1);
|
||||
if (Buffer.byteLength(line) + 1 > FRAME_BYTES)
|
||||
while ((newline = chunk.indexOf(0x0a, start)) >= 0) {
|
||||
const fragment = chunk.subarray(start, newline);
|
||||
const lineBytes = pendingBytes + fragment.length;
|
||||
if (lineBytes + 1 > FRAME_BYTES)
|
||||
throw new RangeError(
|
||||
"JavaScript transfer record exceeds its byte budget",
|
||||
);
|
||||
const input: unknown = JSON.parse(line);
|
||||
const record = recordSchema.parse(input);
|
||||
// Authenticate physical bytes and frame before decoding. Borrow read
|
||||
// windows until one bounded record is complete, then copy it once.
|
||||
const line =
|
||||
pending.length === 0
|
||||
? fragment
|
||||
: Buffer.concat([...pending, fragment], lineBytes);
|
||||
pending = [];
|
||||
pendingBytes = 0;
|
||||
start = newline + 1;
|
||||
const decoded = safeParseUtf8Json(line);
|
||||
if (!decoded.ok) throw decoded.cause;
|
||||
const record = recordSchema.parse(decoded.value);
|
||||
if (
|
||||
[
|
||||
"module-header",
|
||||
@@ -398,10 +409,10 @@ export async function readJavaScriptAnalysisTransfer(
|
||||
"module-link",
|
||||
].includes(record.kind)
|
||||
) {
|
||||
moduleBytes += Buffer.byteLength(line) + 1;
|
||||
moduleBytes += lineBytes + 1;
|
||||
moduleRecords += 1;
|
||||
} else {
|
||||
graphBytes += Buffer.byteLength(line) + 1;
|
||||
graphBytes += lineBytes + 1;
|
||||
graphRecords += 1;
|
||||
}
|
||||
if (record.kind === "projection-header") {
|
||||
@@ -496,16 +507,19 @@ export async function readJavaScriptAnalysisTransfer(
|
||||
}
|
||||
records += 1;
|
||||
}
|
||||
if (Buffer.byteLength(pending) + 1 > FRAME_BYTES)
|
||||
const tail = chunk.subarray(start);
|
||||
pendingBytes += tail.length;
|
||||
if (pendingBytes + 1 > FRAME_BYTES)
|
||||
throw new RangeError(
|
||||
"Unterminated JavaScript transfer record exceeds its byte budget",
|
||||
);
|
||||
if (tail.length > 0) pending.push(tail);
|
||||
}
|
||||
} finally {
|
||||
stream.destroy();
|
||||
}
|
||||
if (
|
||||
pending !== "" ||
|
||||
pendingBytes !== 0 ||
|
||||
records !== expected.records ||
|
||||
bytes !== expected.bytes ||
|
||||
moduleBytes !== expected.module_bytes ||
|
||||
|
||||
Reference in new issue
Block a user