mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
fix(erlang): validate admitted BEAM atom framing before capacity (#12484)
This commit is contained in:
1 parent
a836c89d75
commit
18abd3221a
3 files changed
+67
-16
No files matched your search
@@ -137,6 +137,9 @@ Malformed `FOR1`/`BEAM` headers, truncated
|
||||
chunks, and `AtU8` names that are not valid UTF-8 fail without partial success.
|
||||
The reader checks each encountered chunk's payload and alignment padding against
|
||||
the declared form before applying the chunk count limit.
|
||||
An atom count above `max_atoms` still checks the length fields of at most
|
||||
`max_atoms` entries. A length that leaves too few bytes for the remaining
|
||||
entries is a format failure.
|
||||
|
||||
This workflow does not execute bytecode, inflate `LitT` literals, or decode
|
||||
`Attr`/`CInf` external-term payloads. Complete coverage describes this static
|
||||
|
||||
+18
-16
@@ -127,27 +127,29 @@ const parseAtoms = (
|
||||
// Every entry needs at least one length byte, regardless of the layout.
|
||||
// Reject an impossible table before treating its count as a budget refusal.
|
||||
if (count > payload.length - 4) fail("BEAM atom table is truncated.");
|
||||
if (count > maxAtoms)
|
||||
const overBudget = count > maxAtoms;
|
||||
const atoms: NonNullable<ErlangBeam["atoms"]> = [];
|
||||
const examined = Math.min(count, maxAtoms);
|
||||
let at = 4;
|
||||
for (let index = 1; index <= examined; index += 1) {
|
||||
const [length, next] = atomLength(payload, at, compact);
|
||||
// Reserve at least one length byte per remaining atom. A length inside
|
||||
// the admitted prefix can disprove framing without inspecting those rows.
|
||||
if (length > payload.length - next - (count - index))
|
||||
fail("BEAM atom table is truncated.");
|
||||
at = next + length;
|
||||
if (overBudget) continue;
|
||||
const nameBytes = payload.subarray(next, at);
|
||||
if (encoding === "utf8" && !isUtf8(nameBytes))
|
||||
fail("BEAM atom name is not valid UTF-8.");
|
||||
atoms.push({ index, name: nameBytes.toString(encoding) });
|
||||
}
|
||||
if (overBudget)
|
||||
throw decodeLimit(
|
||||
"beam",
|
||||
`BEAM atom table exceeds the ${String(maxAtoms)}-atom budget.`,
|
||||
{ name: "max_atoms", maximum: maxAtoms, required: count },
|
||||
);
|
||||
const atoms: NonNullable<ErlangBeam["atoms"]> = [];
|
||||
let at = 4;
|
||||
for (let index = 1; index <= count; index += 1) {
|
||||
const [length, next] = atomLength(payload, at, compact);
|
||||
at = next;
|
||||
requireRange(payload, at, length);
|
||||
const nameBytes = payload.subarray(at, at + length);
|
||||
if (encoding === "utf8" && !isUtf8(nameBytes))
|
||||
fail("BEAM atom name is not valid UTF-8.");
|
||||
atoms.push({
|
||||
index,
|
||||
name: nameBytes.toString(encoding),
|
||||
});
|
||||
at += length;
|
||||
}
|
||||
return { atoms, trailingBytes: payload.length - at };
|
||||
};
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import { createServer } from "../../../src/server/createServer.js";
|
||||
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
||||
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
|
||||
import {
|
||||
erlangBeamChunksFixture,
|
||||
erlangBeamDeclaredRowsFixture,
|
||||
erlangBeamRowsFixture,
|
||||
erlangBeamFixture,
|
||||
@@ -138,6 +139,51 @@ mcpTest(
|
||||
},
|
||||
);
|
||||
|
||||
mcpTest(
|
||||
"checks admitted atom lengths before reporting capacity",
|
||||
async ({ mcp }) => {
|
||||
const { client, session } = await connect(mcp);
|
||||
const root = await createTestTempDirectory("rea-beam-atom-framing-");
|
||||
try {
|
||||
for (const [chunk, count, validLength, invalidLength] of [
|
||||
["Atom", [0, 0, 0, 2], 1, 2],
|
||||
["AtU8", [0, 0, 0, 2], 1, 2],
|
||||
["AtU8", [0xff, 0xff, 0xff, 0xfe], 0x10, 0x20],
|
||||
] as const) {
|
||||
for (const [length, finalByte, malformedPrefix] of [
|
||||
[validLength, 0, false],
|
||||
[validLength, validLength, false], // The truncated second name is outside the scan.
|
||||
[invalidLength, 0x62, true],
|
||||
] as const) {
|
||||
const path = join(root, "atoms.beam");
|
||||
const payload = Buffer.from([...count, length, 0x61, finalByte]);
|
||||
await writeFile(path, erlangBeamChunksFixture([[chunk, payload]]));
|
||||
const response = await client.callTool({
|
||||
name: "inspect_erlang_beam",
|
||||
arguments: { path, max_atoms: 1 },
|
||||
});
|
||||
expect(response.isError).toBe(true);
|
||||
expect(parseMcpToolError(response).error).toMatchObject(
|
||||
malformedPrefix
|
||||
? {
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [{ path: ["path"], reason: "invalid_format" }],
|
||||
},
|
||||
}
|
||||
: {
|
||||
code: "resource_constraint",
|
||||
details: { reported_limits: { max_atoms: 1 } },
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await session.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
mcpTest(
|
||||
"refuses BEAM summary before file access without retention and still delivers complete output",
|
||||
async ({ mcp }) => {
|
||||
|
||||
Reference in new issue
Block a user