mirror of
https://github.com/morluto/rea.git
synced 2026-11-10 17:30:32 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2eb48add14 |
No files matched your search
@@ -66,6 +66,28 @@ const report: PythonPyc = {
|
||||
],
|
||||
};
|
||||
|
||||
it.each([
|
||||
{
|
||||
flags: 1,
|
||||
hash_based: true,
|
||||
source_hash_hex: "",
|
||||
source_mtime: null,
|
||||
source_size: null,
|
||||
},
|
||||
{ flags: 1 },
|
||||
])("refuses inconsistent provider header facts %j", async (header) => {
|
||||
const result = await new PythonPycService({
|
||||
close: async () => {},
|
||||
identity,
|
||||
inspect: async () =>
|
||||
ok({ ...report, header: { ...report.header, ...header } }),
|
||||
}).inspect({ path: report.artifact.path });
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: { _tag: "AnalysisOutputError" },
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects a relative path and extra arguments before reading", async () => {
|
||||
let calls = 0;
|
||||
const service = new PythonPycService({
|
||||
|
||||
@@ -3,9 +3,49 @@ import {
|
||||
inspectPythonPycInputSchema,
|
||||
pythonPycMarshalProfile,
|
||||
PYTHON_PYC_MARSHAL_PROFILES,
|
||||
pythonPycHeaderSchema,
|
||||
} from "./pythonPyc.js";
|
||||
import { pythonBytecodeIdentity } from "./pythonBytecodeIdentity.js";
|
||||
|
||||
const hashHeader = {
|
||||
magic: 3531,
|
||||
python_version: "3.12",
|
||||
code_layout: "py311",
|
||||
flags: 3,
|
||||
hash_based: true,
|
||||
check_source: true,
|
||||
source_hash_hex: "0123456789abcdef",
|
||||
source_mtime: null,
|
||||
source_size: null,
|
||||
};
|
||||
|
||||
it.each(["", "ab", "g".repeat(16)])(
|
||||
"rejects an invalid source hash %j",
|
||||
(source_hash_hex) => {
|
||||
expect(
|
||||
pythonPycHeaderSchema.safeParse({ ...hashHeader, source_hash_hex })
|
||||
.success,
|
||||
).toBe(false);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
{ hash_based: false },
|
||||
{ check_source: false },
|
||||
{ source_hash_hex: null },
|
||||
{ source_mtime: 0 },
|
||||
{ source_size: 0 },
|
||||
])("keeps header facts consistent with the recorded flags: %j", (changed) => {
|
||||
expect(
|
||||
pythonPycHeaderSchema.safeParse({ ...hashHeader, ...changed }).success,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("preserves unknown raw header flags and valid hash bytes", () => {
|
||||
const header = { ...hashHeader, flags: 0x80000003 };
|
||||
expect(pythonPycHeaderSchema.parse(header)).toEqual(header);
|
||||
});
|
||||
|
||||
it("keeps pinned CPython identity separate from standalone marshal coverage", () => {
|
||||
expect(pythonPycMarshalProfile(3394)).toEqual({
|
||||
code_layout: "py37",
|
||||
|
||||
@@ -205,17 +205,53 @@ const pythonCodeObjectSchema = z.strictObject({
|
||||
});
|
||||
export type PythonPycCodeObject = z.infer<typeof pythonCodeObjectSchema>;
|
||||
|
||||
export const pythonPycHeaderSchema = z.strictObject({
|
||||
magic: z.number().int().min(0).max(0xffff),
|
||||
python_version: z.string().min(1),
|
||||
code_layout: z.enum(PYTHON_PYC_CODE_LAYOUTS),
|
||||
flags: uint,
|
||||
hash_based: z.boolean(),
|
||||
check_source: z.boolean(),
|
||||
source_hash_hex: z.string().nullable(),
|
||||
source_mtime: uint.nullable(),
|
||||
source_size: uint.nullable(),
|
||||
});
|
||||
export const pythonPycHeaderSchema = z
|
||||
.strictObject({
|
||||
magic: z.number().int().min(0).max(0xffff),
|
||||
python_version: z.string().min(1),
|
||||
code_layout: z.enum(PYTHON_PYC_CODE_LAYOUTS),
|
||||
flags: uint,
|
||||
hash_based: z.boolean(),
|
||||
check_source: z.boolean(),
|
||||
source_hash_hex: z
|
||||
.string()
|
||||
.regex(/^[0-9a-f]{16}$/u)
|
||||
.nullable(),
|
||||
source_mtime: uint.nullable(),
|
||||
source_size: uint.nullable(),
|
||||
})
|
||||
.superRefine((header, context) => {
|
||||
for (const [field, flag] of [
|
||||
["hash_based", PYTHON_PYC_HASH_BASED],
|
||||
["check_source", PYTHON_PYC_CHECK_SOURCE],
|
||||
] as const)
|
||||
if (header[field] !== ((header.flags & flag) !== 0))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: [field],
|
||||
message: `${field} disagrees with its recorded header flag.`,
|
||||
});
|
||||
if (header.hash_based === (header.source_hash_hex === null))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["source_hash_hex"],
|
||||
message:
|
||||
"Hash-based pycs require an 8-byte source hash; others must omit it.",
|
||||
});
|
||||
if (header.hash_based === (header.source_mtime !== null))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["source_mtime"],
|
||||
message: "Timestamp pycs require mtime; hash-based pycs must omit it.",
|
||||
});
|
||||
if (header.hash_based === (header.source_size !== null))
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["source_size"],
|
||||
message:
|
||||
"Timestamp pycs require source_size; hash-based pycs must omit it.",
|
||||
});
|
||||
});
|
||||
|
||||
/** Bounded CPython 3.7–3.13 pyc observation; marshal is walked, bytecode is not decompiled. */
|
||||
export const pythonPycSchema = z
|
||||
@@ -253,29 +289,6 @@ export const pythonPycSchema = z
|
||||
message: "Marshal byte count disagrees with the header-stripped file.",
|
||||
});
|
||||
}
|
||||
if (report.header.hash_based === (report.header.source_hash_hex === null)) {
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["header", "source_hash_hex"],
|
||||
message:
|
||||
"Hash-based pycs require an 8-byte source hash; others must omit it.",
|
||||
});
|
||||
}
|
||||
if (report.header.hash_based === (report.header.source_mtime !== null)) {
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["header", "source_mtime"],
|
||||
message: "Timestamp pycs require mtime; hash-based pycs must omit it.",
|
||||
});
|
||||
}
|
||||
if (report.header.hash_based === (report.header.source_size !== null)) {
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
path: ["header", "source_size"],
|
||||
message:
|
||||
"Timestamp pycs require source_size; hash-based pycs must omit it.",
|
||||
});
|
||||
}
|
||||
if (report.coverage.code_objects !== report.code_objects.length) {
|
||||
context.addIssue({
|
||||
code: "custom",
|
||||
|
||||
@@ -127,17 +127,10 @@ describe("MFT header facts", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it.each([
|
||||
[0, 1],
|
||||
[49, 1],
|
||||
[64, 1],
|
||||
[72, 1],
|
||||
[48, 1],
|
||||
[48, 0],
|
||||
])(
|
||||
"reports invalid first attribute offset %i with USA sequence %i",
|
||||
(offset, sequenceNumber) => {
|
||||
const record = recordOf({ sequenceNumber });
|
||||
it.each([0, 49, 64, 72])(
|
||||
"reports an invalid first attribute offset %i",
|
||||
(offset) => {
|
||||
const record = recordOf();
|
||||
new DataView(record.buffer).setUint16(20, offset, true);
|
||||
const parsed = parseMftImage(record);
|
||||
if (!parsed.ok) throw new Error(parsed.error.message);
|
||||
@@ -146,29 +139,20 @@ describe("MFT header facts", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it.each([1, 0, 0xffff])(
|
||||
"reports a contradictory declared record number with USA sequence %i",
|
||||
(sequenceNumber) => {
|
||||
const bytes = streamOf(recordOf(), recordOf({ sequenceNumber }));
|
||||
new DataView(bytes.buffer).setUint32(1024 + 44, 0, true);
|
||||
const parsed = parseMftImage(bytes);
|
||||
if (!parsed.ok) throw new Error(parsed.error.message);
|
||||
expect(parsed.value.records[1]).toMatchObject({
|
||||
index: 1,
|
||||
declaredRecordNumber: 0,
|
||||
recordNumberMatchesSlot: false,
|
||||
fixups: {
|
||||
malformed: sequenceNumber === 0 || sequenceNumber === 0xffff,
|
||||
},
|
||||
});
|
||||
expect(parsed.value.metadata.fixup_unverified_record_count).toBe(
|
||||
sequenceNumber === 1 ? 0 : 1,
|
||||
);
|
||||
expect(parsed.value.limitations.join(" ")).toContain(
|
||||
"disagrees with their stream slot",
|
||||
);
|
||||
},
|
||||
);
|
||||
it("reports a declared record number that contradicts its stream position", () => {
|
||||
const bytes = streamOf(recordOf(), recordOf());
|
||||
new DataView(bytes.buffer).setUint32(1024 + 44, 0, true);
|
||||
const parsed = parseMftImage(bytes);
|
||||
if (!parsed.ok) throw new Error(parsed.error.message);
|
||||
expect(parsed.value.records[1]).toMatchObject({
|
||||
index: 1,
|
||||
declaredRecordNumber: 0,
|
||||
recordNumberMatchesSlot: false,
|
||||
});
|
||||
expect(parsed.value.limitations.join(" ")).toContain(
|
||||
"disagrees with their stream slot",
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps the inline header window separate from the parsed record count", () => {
|
||||
const parsed = parseMftImage(
|
||||
|
||||
@@ -373,13 +373,8 @@ const readRecord = (
|
||||
true,
|
||||
);
|
||||
|
||||
const usaLayoutValid = updateSequenceArrayFits(
|
||||
usaOffset,
|
||||
fixups.entries,
|
||||
recordBytes / FIXUP_SECTOR_BYTES,
|
||||
);
|
||||
const declaredRecordNumber =
|
||||
usaLayoutValid && usaOffset >= RECORD_HEADER_WITH_NUMBER_BYTES
|
||||
!fixups.malformed && usaOffset >= RECORD_HEADER_WITH_NUMBER_BYTES
|
||||
? view.getUint32(RECORD_HEADER.recordNumber, true)
|
||||
: null;
|
||||
|
||||
@@ -401,7 +396,7 @@ const readRecord = (
|
||||
firstAttributeOffset % 8 === 0 &&
|
||||
firstAttributeOffset >= NTFS_MFT_MIN_RECORD_BYTES &&
|
||||
firstAttributeOffset + 4 <= usedBytes &&
|
||||
(!usaLayoutValid ||
|
||||
(fixups.malformed ||
|
||||
firstAttributeOffset >= usaOffset + fixups.entries * 2),
|
||||
baseRecordReference:
|
||||
baseRecordReference === 0n
|
||||
|
||||
Reference in new issue
Block a user