fix(managed): classify truncated signatures as malformed (#598)
* fix(managed): classify truncated signatures as malformed Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> * fix(managed): use typed signature parsing failures Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> * chore(managed): refresh signature schema commitments Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> --------- Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
@@ -292,6 +292,7 @@ const managedParseIssueSchema = z.object({
|
||||
"invalid-heap-index",
|
||||
"invalid-string",
|
||||
"invalid-blob",
|
||||
"unsupported-signature",
|
||||
"invalid-guid",
|
||||
"invalid-resource",
|
||||
]),
|
||||
|
||||
@@ -110,3 +110,39 @@ describe("managed member inspection", () => {
|
||||
expect(result.coverage).toMatchObject({ state: "complete", issues: [] });
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
Buffer.alloc(0),
|
||||
Buffer.from([0]),
|
||||
Buffer.from([0, 0, 0x12]),
|
||||
Buffer.from([0x10, 0x80]),
|
||||
Buffer.from([0, 0xe0]),
|
||||
])(
|
||||
"marks incomplete or reserved method signatures malformed: %j",
|
||||
(methodSignature) => {
|
||||
const bytes = buildManagedPeFixture({ methodSignature });
|
||||
const result = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
expect(result.methods[0]?.signature).toMatchObject({
|
||||
parse_status: "malformed",
|
||||
kind: "unknown",
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps an admitted but unimplemented element type unsupported", () => {
|
||||
// ECMA-335 ELEMENT_TYPE_ARRAY is valid, but multidimensional arrays are not decoded here.
|
||||
const bytes = buildManagedPeFixture({
|
||||
fieldSignature: Buffer.from([6, 0x14, 8, 1, 0, 0]),
|
||||
});
|
||||
const result = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
managedPeFixtureTarget(bytes),
|
||||
);
|
||||
expect(result.fields[0]?.signature).toMatchObject({
|
||||
parse_status: "unsupported",
|
||||
issue: "unsupported element type 0x14",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
managedFailure,
|
||||
ManagedReaderFailure,
|
||||
} from "./ManagedReaderFailure.js";
|
||||
import type { ManagedMemberInspection } from "../domain/managedArtifact.js";
|
||||
import {
|
||||
type ManagedMetadataLayout,
|
||||
@@ -208,7 +212,13 @@ const readCompressed = (
|
||||
offset: number,
|
||||
): { readonly value: number; readonly next: number } => {
|
||||
const first = bytes[offset];
|
||||
if (first === undefined) throw new RangeError("truncated compressed integer");
|
||||
if (first === undefined)
|
||||
throw managedFailure(
|
||||
"invalid-blob",
|
||||
"signature",
|
||||
"truncated compressed integer",
|
||||
offset,
|
||||
);
|
||||
if ((first & 0x80) === 0) return { value: first, next: offset + 1 };
|
||||
const second = bytes[offset + 1];
|
||||
if ((first & 0xc0) === 0x80 && second !== undefined)
|
||||
@@ -227,7 +237,12 @@ const readCompressed = (
|
||||
(first & 0x1f) * 0x01_00_00_00 + b1 * 0x01_00_00 + b2 * 0x0100 + b3,
|
||||
next: offset + 4,
|
||||
};
|
||||
throw new RangeError("reserved compressed integer");
|
||||
throw managedFailure(
|
||||
"invalid-blob",
|
||||
"signature",
|
||||
"reserved compressed integer",
|
||||
offset,
|
||||
);
|
||||
};
|
||||
|
||||
const ELEMENT_TYPES = new Map<number, string>([
|
||||
@@ -260,7 +275,13 @@ const readTypeSignature = (
|
||||
offset: number,
|
||||
): { readonly value: string; readonly next: number } => {
|
||||
const kind = blob[offset];
|
||||
if (kind === undefined) throw new RangeError("truncated type signature");
|
||||
if (kind === undefined)
|
||||
throw managedFailure(
|
||||
"invalid-blob",
|
||||
"signature",
|
||||
"truncated type signature",
|
||||
offset,
|
||||
);
|
||||
const named = ELEMENT_TYPES.get(kind);
|
||||
if (named !== undefined) return { value: named, next: offset + 1 };
|
||||
const suffix = ELEMENT_TYPE_SUFFIXES.get(kind);
|
||||
@@ -287,7 +308,12 @@ const readTypeSignature = (
|
||||
const variable = readCompressed(blob, offset + 1);
|
||||
return { value: `var:${String(variable.value)}`, next: variable.next };
|
||||
}
|
||||
throw new RangeError(`unsupported element type 0x${kind.toString(16)}`);
|
||||
throw managedFailure(
|
||||
"unsupported-signature",
|
||||
"signature",
|
||||
`unsupported element type 0x${kind.toString(16)}`,
|
||||
offset,
|
||||
);
|
||||
};
|
||||
|
||||
const callingConvention = (value: number): string => {
|
||||
@@ -314,7 +340,8 @@ export const signature = (blob: Buffer): ManagedSignature => {
|
||||
raw_sha256: sha256Bytes(blob),
|
||||
};
|
||||
try {
|
||||
if (blob.length === 0) throw new RangeError("empty signature");
|
||||
if (blob.length === 0)
|
||||
throw managedFailure("invalid-blob", "signature", "empty signature", 0);
|
||||
const first = blob[0] ?? 0;
|
||||
if ((first & 0x0f) === 6) {
|
||||
const fieldType = readTypeSignature(blob, 1);
|
||||
@@ -362,17 +389,26 @@ export const signature = (blob: Buffer): ManagedSignature => {
|
||||
issue: offset === blob.length ? null : "Trailing signature data",
|
||||
};
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ManagedReaderFailure)) throw cause;
|
||||
if (
|
||||
cause.issue.code !== "invalid-blob" &&
|
||||
cause.issue.code !== "unsupported-signature"
|
||||
)
|
||||
throw cause;
|
||||
return {
|
||||
...raw,
|
||||
kind: "unknown",
|
||||
parse_status: cause instanceof RangeError ? "unsupported" : "malformed",
|
||||
parse_status:
|
||||
cause.issue.code === "unsupported-signature"
|
||||
? "unsupported"
|
||||
: "malformed",
|
||||
calling_convention: null,
|
||||
generic_parameter_count: null,
|
||||
parameter_count: null,
|
||||
return_type: null,
|
||||
parameter_types: [],
|
||||
field_type: null,
|
||||
issue: cause instanceof Error ? cause.message : "Signature parse failed",
|
||||
issue: cause.issue.detail,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user