diff --git a/docs/erlang-beam.md b/docs/erlang-beam.md index 8422550469..78d3423232 100644 --- a/docs/erlang-beam.md +++ b/docs/erlang-beam.md @@ -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 diff --git a/src/erlang/BeamParser.ts b/src/erlang/BeamParser.ts index 444d19aff1..df3a7af8f0 100644 --- a/src/erlang/BeamParser.ts +++ b/src/erlang/BeamParser.ts @@ -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 = []; + 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 = []; - 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 }; }; diff --git a/tests/boundary/mcp/erlangBeamMcp.test.ts b/tests/boundary/mcp/erlangBeamMcp.test.ts index d7adc2852b..0e1f1a05cc 100644 --- a/tests/boundary/mcp/erlangBeamMcp.test.ts +++ b/tests/boundary/mcp/erlangBeamMcp.test.ts @@ -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 }) => {