fix(tools)!: simplify direct invocation and harden boundary contracts (#555)

Remove REA permission grants, scope ceilings, approval confirmations, and inert settings. Preserve selected local evidence, accept caller-chosen commands and paths, and report actual runtime failures.

Harden provider results, schema serialization, snapshot evidence binding, archive paths, source maps, native screenshots, managed EH parsing, and cancellation cleanup. Regenerate tool metadata and document direct invocation.

BREAKING CHANGE: Removed permission configuration and policy commands, approval fields, filesystem scope inputs, and fixed confirmation options. Process capture inherits the host environment and accepts command names; callers must refresh tool schemas and migrate removed fields.
This commit is contained in:
morluto
2026-10-05 20:16:44 +08:00
committed by GitHub
parent 4f099a10f8
commit b5e98915c6
350 changed files with 5347 additions and 10599 deletions
+79
View File
@@ -8,6 +8,23 @@ import {
managedPeFixtureTarget,
} from "./ManagedPe.fixture.js";
const managedBodyWithSections = (...sections: Buffer[]) => {
const header = Buffer.alloc(12);
header.writeUInt16LE(0x300b, 0);
header.writeUInt16LE(8, 2);
header.writeUInt32LE(1, 4);
const bytes = buildManagedPeFixture({
ilBody: Buffer.concat([
header,
Buffer.from([0x2a]),
Buffer.alloc(3),
...sections,
]),
});
return inspectManagedMembersBytes(bytes, managedPeFixtureTarget(bytes))
.methods[0]?.body;
};
describe("managed CIL decoding", () => {
it("reads fat method header size from the full flags-and-size word", () => {
const il = Buffer.from([
@@ -107,3 +124,65 @@ describe("managed CIL decoding", () => {
});
});
});
describe("managed exception section decoding", () => {
it("marks an exception section with a partial clause as malformed", () => {
const section = Buffer.alloc(17);
section[0] = 0x01;
section[1] = section.length;
expect(managedBodyWithSections(section)).toMatchObject({
status: "malformed",
exception_regions: [],
issue: "Exception section size does not contain whole clauses",
});
});
it("reads every chained exception section", () => {
const first = Buffer.alloc(16);
first[0] = 0x81;
first[1] = first.length;
const second = Buffer.alloc(16);
second[0] = 0x01;
second[1] = second.length;
expect(managedBodyWithSections(first, second)).toMatchObject({
status: "present",
issue: null,
exception_regions: [
{ flags: 0, try_offset: 0, try_length: 0, handler_offset: 0 },
{ flags: 0, try_offset: 0, try_length: 0, handler_offset: 0 },
],
});
});
it("reports malformed data in a chained section", () => {
const first = Buffer.alloc(16);
first[0] = 0x81;
first[1] = first.length;
const second = Buffer.from([0x02, 0x04, 0x00, 0x00]);
expect(managedBodyWithSections(first, second)).toMatchObject({
status: "malformed",
exception_regions: [],
issue: "Unsupported method data section kind 2",
});
});
it.each([
{
name: "unsupported section kind",
section: Buffer.from([0x02, 0x04, 0x00, 0x00]),
issue: "Unsupported method data section kind 2",
},
{
name: "section size outside artifact",
section: Buffer.from([0x41, 0xff, 0xff, 0xff]),
issue: "Exception section size leaves artifact",
},
])("rejects $name", ({ section, issue }) => {
expect(managedBodyWithSections(section)).toMatchObject({
status: "malformed",
exception_regions: [],
issue,
});
});
});
+102 -39
View File
@@ -422,48 +422,111 @@ export const parseExceptionRegions = (
bytes: Buffer,
offset: number,
methodEnd: number,
): ManagedExceptionRegion[] => {
if (offset >= methodEnd) return [];
const kind = bytes.readUInt8(offset);
if ((kind & 0x3f) !== 1) return [];
const fat = (kind & 0x40) !== 0;
const size = fat
? bytes.readUInt32LE(offset) >>> 8
: bytes.readUInt8(offset + 1);
const start = fat ? offset + 4 : offset + 4;
const clauseSize = fat ? 24 : 12;
if (size < 4 || offset > methodEnd - size) return [];
const count = Math.floor((size - 4) / clauseSize);
): ExceptionRegionParseResult => {
const regions: ManagedExceptionRegion[] = [];
let sectionOffset = offset;
let hasMoreSections = true;
while (hasMoreSections) {
if (
!Number.isSafeInteger(sectionOffset) ||
sectionOffset < 0 ||
sectionOffset > methodEnd - 4 ||
sectionOffset > bytes.length - 4
)
return malformedExceptionRegions(
"Exception section header leaves artifact",
);
const kind = bytes.readUInt8(sectionOffset);
if ((kind & 0x3f) !== 1)
return malformedExceptionRegions(
`Unsupported method data section kind ${String(kind & 0x3f)}`,
);
const fat = (kind & 0x40) !== 0;
const size = fat
? bytes.readUInt8(sectionOffset + 1) |
(bytes.readUInt8(sectionOffset + 2) << 8) |
(bytes.readUInt8(sectionOffset + 3) << 16)
: bytes.readUInt8(sectionOffset + 1);
const clauseSize = fat ? 24 : 12;
if (
size < 4 ||
sectionOffset > methodEnd - size ||
sectionOffset > bytes.length - size
)
return malformedExceptionRegions(
"Exception section size leaves artifact",
);
if ((size - 4) % clauseSize !== 0)
return malformedExceptionRegions(
"Exception section size does not contain whole clauses",
);
if (
!fat &&
(bytes[sectionOffset + 2] !== 0 || bytes[sectionOffset + 3] !== 0)
)
return malformedExceptionRegions(
"Small exception section reserved bytes are nonzero",
);
const count = (size - 4) / clauseSize;
regions.push(...readExceptionClauses(bytes, sectionOffset + 4, count, fat));
hasMoreSections = (kind & 0x80) !== 0;
sectionOffset += size;
}
return { status: "complete", regions };
};
interface ExceptionRegionParseSuccess {
readonly status: "complete";
readonly regions: ManagedExceptionRegion[];
}
interface ExceptionRegionParseFailure {
readonly status: "malformed";
readonly regions: [];
readonly issue: string;
}
type ExceptionRegionParseResult =
| ExceptionRegionParseSuccess
| ExceptionRegionParseFailure;
const malformedExceptionRegions = (
issue: string,
): ExceptionRegionParseFailure => ({ status: "malformed", regions: [], issue });
const readExceptionClauses = (
bytes: Buffer,
start: number,
count: number,
fat: boolean,
): ManagedExceptionRegion[] => {
const regions: ManagedExceptionRegion[] = [];
const clauseSize = fat ? 24 : 12;
for (let index = 0; index < count; index += 1) {
const clause = start + index * clauseSize;
if (fat) {
const flags = bytes.readUInt32LE(clause);
const extra = bytes.readUInt32LE(clause + 20);
regions.push({
flags,
try_offset: bytes.readUInt32LE(clause + 4),
try_length: bytes.readUInt32LE(clause + 8),
handler_offset: bytes.readUInt32LE(clause + 12),
handler_length: bytes.readUInt32LE(clause + 16),
class_token:
flags === 0 ? `0x${extra.toString(16).padStart(8, "0")}` : null,
filter_offset: flags === 1 ? extra : null,
});
} else {
const flags = bytes.readUInt16LE(clause);
const extra = bytes.readUInt32LE(clause + 8);
regions.push({
flags,
try_offset: bytes.readUInt16LE(clause + 2),
try_length: bytes.readUInt8(clause + 4),
handler_offset: bytes.readUInt16LE(clause + 5),
handler_length: bytes.readUInt8(clause + 7),
class_token:
flags === 0 ? `0x${extra.toString(16).padStart(8, "0")}` : null,
filter_offset: flags === 1 ? extra : null,
});
}
const flags = fat ? bytes.readUInt32LE(clause) : bytes.readUInt16LE(clause);
const extra = bytes.readUInt32LE(clause + (fat ? 20 : 8));
regions.push({
flags,
try_offset: fat
? bytes.readUInt32LE(clause + 4)
: bytes.readUInt16LE(clause + 2),
try_length: fat
? bytes.readUInt32LE(clause + 8)
: bytes.readUInt8(clause + 4),
handler_offset: fat
? bytes.readUInt32LE(clause + 12)
: bytes.readUInt16LE(clause + 5),
handler_length: fat
? bytes.readUInt32LE(clause + 16)
: bytes.readUInt8(clause + 7),
class_token:
flags === 0 ? `0x${extra.toString(16).padStart(8, "0")}` : null,
filter_offset: flags === 1 ? extra : null,
});
}
return regions;
};
+24 -10
View File
@@ -6,6 +6,17 @@ import {
} from "./ManagedMemberInstructionDecoder.js";
import { sha256Bytes } from "./ManagedMetadataHeaps.js";
const bodyStatus = (
decodedIssue: string | null,
truncatedInstructions: number,
exceptionRegions: ReturnType<typeof parseExceptionRegions> | null,
): "malformed" | "partial" | "present" => {
if (decodedIssue !== null || exceptionRegions?.status === "malformed")
return "malformed";
if (truncatedInstructions > 0) return "partial";
return "present";
};
interface MethodBodyHeader {
readonly format: "tiny" | "fat";
readonly size: number;
@@ -87,12 +98,15 @@ export const methodBody = (
);
const methodEnd = ilOffset + header.ilSize;
const sectionOffset = (methodEnd + 3) & ~3;
const status =
decoded.issue !== null
? "malformed"
: decoded.truncated > 0
? "partial"
: "present";
const exceptionRegions =
header.format === "fat" && (header.flags & 8) !== 0
? parseExceptionRegions(bytes, sectionOffset, bytes.length)
: null;
const status = bodyStatus(
decoded.issue,
decoded.truncated,
exceptionRegions,
);
return {
status,
header_format: header.format,
@@ -113,12 +127,12 @@ export const methodBody = (
truncated_instructions: decoded.truncated,
opcode_counts: opcodeCounts,
anchors,
exception_regions:
header.format === "fat" && (header.flags & 8) !== 0
? parseExceptionRegions(bytes, sectionOffset, bytes.length)
: [],
exception_regions: exceptionRegions?.regions ?? [],
issue:
decoded.issue ??
(exceptionRegions?.status === "malformed"
? exceptionRegions.issue
: null) ??
(decoded.truncated > 0
? "Instruction decoding stopped before the end of the method body"
: null),