mirror of
https://github.com/morluto/rea.git
synced 2026-11-10 17:30:32 -10:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
78d4e2a1ba |
No files matched your search
@@ -20,11 +20,6 @@ it("reads a Python 3.9 reference-flagged PYZ table", () => {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("reads marshal one-byte string tags as Latin-1", () => {
|
|
||||||
for (const hex of ["6101000000e9", "4101000000e9", "7a01e9", "5a01e9"])
|
|
||||||
expect(readMarshalSubset(bytes(hex))).toEqual({ ok: true, value: "é" });
|
|
||||||
});
|
|
||||||
|
|
||||||
it("decodes CPython marshal Unicode with surrogatepass", () => {
|
it("decodes CPython marshal Unicode with surrogatepass", () => {
|
||||||
expect(
|
expect(
|
||||||
readMarshalSubset(
|
readMarshalSubset(
|
||||||
|
|||||||
@@ -168,8 +168,14 @@ const readAsciiBytes = (
|
|||||||
): Result<string, PyInstallerFailure> => {
|
): Result<string, PyInstallerFailure> => {
|
||||||
const bytes = readBytes(cursor, length);
|
const bytes = readBytes(cursor, length);
|
||||||
if (!bytes.ok) return bytes;
|
if (!bytes.ok) return bytes;
|
||||||
// These tags use CPython's one-byte Unicode constructor, not an ASCII check.
|
if (bytes.value.some((byte) => byte > 0x7f))
|
||||||
return ok(bytes.value.toString("latin1"));
|
return err(
|
||||||
|
pyInstallerFailure(
|
||||||
|
"format",
|
||||||
|
"PYZ marshal ASCII string contains a non-ASCII byte",
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return ok(bytes.value.toString("ascii"));
|
||||||
};
|
};
|
||||||
|
|
||||||
const readUtf8 = (
|
const readUtf8 = (
|
||||||
|
|||||||
@@ -369,7 +369,10 @@ export class TsharkCaptureProvider implements PacketCapturePort {
|
|||||||
resourcePath = "path" in input ? input.path : command;
|
resourcePath = "path" in input ? input.path : command;
|
||||||
// TShark resolves `-e` aliases before emitting canonical field names;
|
// TShark resolves `-e` aliases before emitting canonical field names;
|
||||||
// the command arguments, not output names, own field selection.
|
// the command arguments, not output names, own field selection.
|
||||||
const matchingPackets = parsePacketOutput(output.stdout.text);
|
const matchingPackets = parsePacketOutput(
|
||||||
|
output.stdout.text,
|
||||||
|
input.packet_limit,
|
||||||
|
);
|
||||||
const start = Math.min(input.match_offset, matchingPackets.length);
|
const start = Math.min(input.match_offset, matchingPackets.length);
|
||||||
const end =
|
const end =
|
||||||
input.match_limit === null
|
input.match_limit === null
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { packetFieldsSchema } from "../domain/captures/packetCapture.js";
|
|||||||
const producerSchema = z.array(
|
const producerSchema = z.array(
|
||||||
z.object({ _source: z.object({ layers: packetFieldsSchema }) }),
|
z.object({ _source: z.object({ layers: packetFieldsSchema }) }),
|
||||||
);
|
);
|
||||||
export const parsePacketOutput = (text: string) => {
|
export const parsePacketOutput = (text: string, packetLimit: number | null) => {
|
||||||
const decoded: unknown = JSON.parse(text);
|
const decoded: unknown = JSON.parse(text);
|
||||||
const packets = producerSchema
|
const packets = producerSchema
|
||||||
.parse(decoded)
|
.parse(decoded)
|
||||||
@@ -19,6 +19,11 @@ export const parsePacketOutput = (text: string) => {
|
|||||||
"inspect_packet_capture",
|
"inspect_packet_capture",
|
||||||
"TShark returned unordered frames.",
|
"TShark returned unordered frames.",
|
||||||
);
|
);
|
||||||
|
if (packetLimit !== null && frame > BigInt(packetLimit))
|
||||||
|
throw new AnalysisOutputError(
|
||||||
|
"inspect_packet_capture",
|
||||||
|
"TShark returned a frame beyond the selected input packet limit.",
|
||||||
|
);
|
||||||
previous = frame;
|
previous = frame;
|
||||||
}
|
}
|
||||||
return packets;
|
return packets;
|
||||||
|
|||||||
@@ -407,30 +407,6 @@ it("charges long digits to the string budget and keeps large previews bounded",
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("reads marshal one-byte string tags as Latin-1 with Unicode identity", () => {
|
|
||||||
for (const tag of [0x61, 0x41, 0x7a, 0x5a]) {
|
|
||||||
const encoded = Buffer.concat([
|
|
||||||
Buffer.from([tag]),
|
|
||||||
tag === 0x61 || tag === 0x41 ? u32(1) : Buffer.from([1]),
|
|
||||||
Buffer.from([0xe9]),
|
|
||||||
]);
|
|
||||||
const sameUnicode = marshalUnicode(Buffer.from("é", "utf8"));
|
|
||||||
const set = Buffer.concat([
|
|
||||||
Buffer.from([0x3c]),
|
|
||||||
u32(2),
|
|
||||||
encoded,
|
|
||||||
sameUnicode,
|
|
||||||
]);
|
|
||||||
const report = root(parse(withConsts([encoded, set])));
|
|
||||||
expect(report.consts[0]).toMatchObject({
|
|
||||||
kind: "str",
|
|
||||||
value: "é",
|
|
||||||
bytes: 1,
|
|
||||||
});
|
|
||||||
expect(report.consts[1]).toEqual({ kind: "set", length: 1 });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("decodes marshal strings with CPython surrogatepass rules", () => {
|
it("decodes marshal strings with CPython surrogatepass rules", () => {
|
||||||
const report = parse(
|
const report = parse(
|
||||||
withConsts([marshalUnicode(Buffer.from("efbbbf73eda080efbbbf", "hex"))]),
|
withConsts([marshalUnicode(Buffer.from("efbbbf73eda080efbbbf", "hex"))]),
|
||||||
@@ -1558,36 +1534,6 @@ it("hashes a long TYPE_REF tuple chain without recursing", () => {
|
|||||||
expect(pythonPycSchema.safeParse(report).success).toBe(true);
|
expect(pythonPycSchema.safeParse(report).success).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("hashes shallow TYPE_REF code chains without using the JavaScript stack", () => {
|
|
||||||
const links = 3000;
|
|
||||||
const codes = Array.from({ length: links }, (_, index) =>
|
|
||||||
marshalCode("py311", {
|
|
||||||
flagged: true,
|
|
||||||
name: "f",
|
|
||||||
consts: index === 0 ? [Buffer.from([0x4e])] : [marshalRef(index - 1)],
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
const bytes = Buffer.concat([
|
|
||||||
pythonPycHeader(3531),
|
|
||||||
marshalCode("py311", {
|
|
||||||
constsObject: Buffer.concat([
|
|
||||||
Buffer.from([0x28]),
|
|
||||||
u32(links + 1),
|
|
||||||
...codes,
|
|
||||||
Buffer.from([0x3c]),
|
|
||||||
u32(1),
|
|
||||||
marshalRef(links - 1),
|
|
||||||
]),
|
|
||||||
}),
|
|
||||||
]);
|
|
||||||
const report = parseWith(bytes, { max_objects: 65_536 });
|
|
||||||
expect(root(report).consts.at(-1)).toEqual({ kind: "set", length: 1 });
|
|
||||||
expect(report.coverage).toMatchObject({
|
|
||||||
status: "complete",
|
|
||||||
code_objects: links + 1,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("rejects code CPython treats as corrupt instead of complete coverage", () => {
|
it("rejects code CPython treats as corrupt instead of complete coverage", () => {
|
||||||
const file = (
|
const file = (
|
||||||
layout: "py37" | "py38" | "py311",
|
layout: "py37" | "py38" | "py311",
|
||||||
|
|||||||
@@ -332,7 +332,7 @@ class MarshalReader {
|
|||||||
readonly #codeKeys = new WeakMap<CodeNode, string>();
|
readonly #codeKeys = new WeakMap<CodeNode, string>();
|
||||||
readonly #codeIdentities = new WeakMap<CodeNode, number>();
|
readonly #codeIdentities = new WeakMap<CodeNode, number>();
|
||||||
readonly #codeEquality = new WeakMap<CodeNode, CodeEquality>();
|
readonly #codeEquality = new WeakMap<CodeNode, CodeEquality>();
|
||||||
/** Completed Python hashability checks per code object. */
|
/** Python hashability per code object. False while that check is in progress. */
|
||||||
readonly #codeHashable = new WeakMap<CodeNode, boolean>();
|
readonly #codeHashable = new WeakMap<CodeNode, boolean>();
|
||||||
/** Tuples whose items are still being read; CPython cannot hash them yet. */
|
/** Tuples whose items are still being read; CPython cannot hash them yet. */
|
||||||
readonly #readingTuples = new WeakSet<MarshalValue>();
|
readonly #readingTuples = new WeakSet<MarshalValue>();
|
||||||
@@ -436,19 +436,13 @@ class MarshalReader {
|
|||||||
};
|
};
|
||||||
case TYPE_INTERNED:
|
case TYPE_INTERNED:
|
||||||
case TYPE_UNICODE:
|
case TYPE_UNICODE:
|
||||||
return this.string(
|
return this.utf8(this.nonNegativeInt32("Marshal string length"), false);
|
||||||
this.nonNegativeInt32("Marshal string length"),
|
|
||||||
"utf8",
|
|
||||||
);
|
|
||||||
case TYPE_ASCII:
|
case TYPE_ASCII:
|
||||||
case TYPE_ASCII_INTERNED:
|
case TYPE_ASCII_INTERNED:
|
||||||
return this.string(
|
return this.utf8(this.nonNegativeInt32("Marshal string length"), true);
|
||||||
this.nonNegativeInt32("Marshal string length"),
|
|
||||||
"latin1",
|
|
||||||
);
|
|
||||||
case TYPE_SHORT_ASCII:
|
case TYPE_SHORT_ASCII:
|
||||||
case TYPE_SHORT_ASCII_INTERNED:
|
case TYPE_SHORT_ASCII_INTERNED:
|
||||||
return this.string(this.u8(), "latin1");
|
return this.utf8(this.u8(), true);
|
||||||
case TYPE_TUPLE:
|
case TYPE_TUPLE:
|
||||||
return this.sequenceValue(
|
return this.sequenceValue(
|
||||||
"tuple",
|
"tuple",
|
||||||
@@ -1315,70 +1309,13 @@ class MarshalReader {
|
|||||||
#isCodeHashable(node: CodeNode): boolean {
|
#isCodeHashable(node: CodeNode): boolean {
|
||||||
const known = this.#codeHashable.get(node);
|
const known = this.#codeHashable.get(node);
|
||||||
if (known !== undefined) return known;
|
if (known !== undefined) return known;
|
||||||
const frames: { value: MarshalValue; finish: boolean }[] = [
|
this.#codeHashable.set(node, false);
|
||||||
{ value: { kind: "code", value: node }, finish: false },
|
const material =
|
||||||
];
|
this.#codeEquality.get(node) ??
|
||||||
const gray = new Set<MarshalValue | CodeNode>();
|
malformed("Code object is missing equality material.");
|
||||||
const complete = new Set<MarshalValue>();
|
const hashable = this.#equalityKey(material.constsTuple, "hash") !== null;
|
||||||
const reject = (): false => {
|
this.#codeHashable.set(node, hashable);
|
||||||
this.#codeHashable.set(node, false);
|
return hashable;
|
||||||
return false;
|
|
||||||
};
|
|
||||||
while (frames.length > 0) {
|
|
||||||
this.#takeKeyStep();
|
|
||||||
const frame = frames.pop()!;
|
|
||||||
const value = frame.value;
|
|
||||||
const identity = value.kind === "code" ? value.value : value;
|
|
||||||
if (frame.finish) {
|
|
||||||
gray.delete(identity);
|
|
||||||
if (value.kind === "code") {
|
|
||||||
const material =
|
|
||||||
this.#codeEquality.get(value.value) ??
|
|
||||||
malformed("Code object is missing equality material.");
|
|
||||||
// Child code checks are now complete, so the existing tuple key
|
|
||||||
// walker preserves its equality and expansion budgets without recursion.
|
|
||||||
const hashable =
|
|
||||||
this.#equalityKey(material.constsTuple, "hash") !== null;
|
|
||||||
this.#codeHashable.set(value.value, hashable);
|
|
||||||
if (!hashable) return reject();
|
|
||||||
} else complete.add(value);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (value.kind === "code") {
|
|
||||||
const cached = this.#codeHashable.get(value.value);
|
|
||||||
if (cached === false) return reject();
|
|
||||||
if (cached === true) continue;
|
|
||||||
} else {
|
|
||||||
const cached = this.#keyCache.get(value);
|
|
||||||
if (cached === null) return reject();
|
|
||||||
if (cached !== undefined || complete.has(value)) continue;
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
gray.has(identity) ||
|
|
||||||
this.#readingTuples.has(value) ||
|
|
||||||
value.kind === "list" ||
|
|
||||||
value.kind === "set" ||
|
|
||||||
value.kind === "dict" ||
|
|
||||||
value.kind === "null"
|
|
||||||
)
|
|
||||||
return reject();
|
|
||||||
const children =
|
|
||||||
value.kind === "code"
|
|
||||||
? [
|
|
||||||
(
|
|
||||||
this.#codeEquality.get(value.value) ??
|
|
||||||
malformed("Code object is missing equality material.")
|
|
||||||
).constsTuple,
|
|
||||||
]
|
|
||||||
: value.kind === "tuple" || value.kind === "frozenset"
|
|
||||||
? value.items
|
|
||||||
: [];
|
|
||||||
gray.add(identity);
|
|
||||||
frames.push({ value, finish: true });
|
|
||||||
for (let index = children.length - 1; index >= 0; index -= 1)
|
|
||||||
frames.push({ value: children[index]!, finish: false });
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#codeIdentity(node: CodeNode): number {
|
#codeIdentity(node: CodeNode): number {
|
||||||
@@ -1555,20 +1492,17 @@ class MarshalReader {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
string(length: number, encoding: "latin1" | "utf8"): MarshalValue {
|
utf8(length: number, ascii: boolean): MarshalValue {
|
||||||
const bytes = this.blob(length);
|
const bytes = this.blob(length);
|
||||||
if (encoding === "latin1") {
|
if (ascii) {
|
||||||
// CPython reads the ASCII-tagged forms as one-byte Unicode, even when
|
if (bytes.some((byte) => byte > 0x7f))
|
||||||
// high bytes appear. Identity must agree with the UTF-8 string form.
|
malformed("Marshal ASCII string contains a non-ASCII byte.");
|
||||||
const value = bytes.toString("latin1");
|
|
||||||
return {
|
return {
|
||||||
kind: "str",
|
kind: "str",
|
||||||
value,
|
value: bytes.toString("latin1"),
|
||||||
previewEnd: Math.min(length, PYTHON_PYC_CONST_PREVIEW_LIMIT),
|
previewEnd: Math.min(length, PYTHON_PYC_CONST_PREVIEW_LIMIT),
|
||||||
bytes: length,
|
bytes: length,
|
||||||
raw: bytes.some((byte) => byte > 0x7f)
|
raw: bytes,
|
||||||
? Buffer.from(value, "utf8")
|
|
||||||
: bytes,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
const decoded = decodeMarshalUtf8(bytes, PYTHON_PYC_CONST_PREVIEW_LIMIT);
|
const decoded = decodeMarshalUtf8(bytes, PYTHON_PYC_CONST_PREVIEW_LIMIT);
|
||||||
|
|||||||
@@ -247,6 +247,26 @@ it("preserves TShark canonical output for a caller-selected field alias", async
|
|||||||
expect(report.tool.arguments).toContain("bootp.type");
|
expect(report.tool.arguments).toContain("bootp.type");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("refuses producer frames outside the selected input read scope before paging", async () => {
|
||||||
|
const f = await fixture("many");
|
||||||
|
const request = { path: f.path, fields: ["ip.addr"], match_limit: 1 };
|
||||||
|
expect(
|
||||||
|
await f.service.inspect({ ...request, packet_limit: 10 }),
|
||||||
|
).toMatchObject({
|
||||||
|
ok: false,
|
||||||
|
error: { _tag: "AnalysisOutputError", operation: "inspect_packet_capture" },
|
||||||
|
});
|
||||||
|
const admitted = await f.service.inspect({ ...request, packet_limit: 90 });
|
||||||
|
if (!admitted.ok) throw admitted.error;
|
||||||
|
const report = packetCaptureSchema.parse(admitted.value.normalized_result);
|
||||||
|
expect(report.output_window).toEqual({
|
||||||
|
matching_packets: 90,
|
||||||
|
returned_packets: 1,
|
||||||
|
has_more: true,
|
||||||
|
});
|
||||||
|
expect(report.packets[0]?.["frame.number"]).toEqual(["1"]);
|
||||||
|
});
|
||||||
|
|
||||||
it("returns canonical alias output through the public packet-capture CLI", async () => {
|
it("returns canonical alias output through the public packet-capture CLI", async () => {
|
||||||
const f = await fixture("alias-canonical");
|
const f = await fixture("alias-canonical");
|
||||||
const cli = Cli.create("capture-alias-test", { sync: false });
|
const cli = Cli.create("capture-alias-test", { sync: false });
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ it("preserves repeated field values and missing fields while discarding export i
|
|||||||
},
|
},
|
||||||
{ _source: { layers: { "frame.number": ["7"] } } },
|
{ _source: { layers: { "frame.number": ["7"] } } },
|
||||||
]);
|
]);
|
||||||
expect(parsePacketOutput(output)).toEqual([
|
expect(parsePacketOutput(output, 7)).toEqual([
|
||||||
{ "frame.number": ["2"], "ip.addr": ["192.0.2.1", "192.0.2.2"] },
|
{ "frame.number": ["2"], "ip.addr": ["192.0.2.1", "192.0.2.2"] },
|
||||||
{ "frame.number": ["7"] },
|
{ "frame.number": ["7"] },
|
||||||
]);
|
]);
|
||||||
@@ -49,7 +49,7 @@ it.each([
|
|||||||
])("rejects malformed producer fields: $layers", ({ layers, issue }) => {
|
])("rejects malformed producer fields: $layers", ({ layers, issue }) => {
|
||||||
let failure: unknown;
|
let failure: unknown;
|
||||||
try {
|
try {
|
||||||
parsePacketOutput(JSON.stringify([{ _source: { layers } }]));
|
parsePacketOutput(JSON.stringify([{ _source: { layers } }]), null);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
failure = error;
|
failure = error;
|
||||||
}
|
}
|
||||||
@@ -72,6 +72,7 @@ it.each([
|
|||||||
_source: { layers: { "frame.number": frame } },
|
_source: { layers: { "frame.number": frame } },
|
||||||
})),
|
})),
|
||||||
),
|
),
|
||||||
|
null,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
failure = error;
|
failure = error;
|
||||||
@@ -88,7 +89,18 @@ it("preserves canonical field names emitted for a selected alias", () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
expect(parsePacketOutput(output)).toEqual([
|
expect(parsePacketOutput(output, null)).toEqual([
|
||||||
{ "frame.number": ["1"], "dhcp.type": ["1"] },
|
{ "frame.number": ["1"], "dhcp.type": ["1"] },
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("validates every frame against the input read limit while allowing filtered gaps", () => {
|
||||||
|
const output = JSON.stringify(
|
||||||
|
[2, 7].map((frame) => ({
|
||||||
|
_source: { layers: { "frame.number": [String(frame)] } },
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
expect(parsePacketOutput(output, 7)).toHaveLength(2);
|
||||||
|
expect(() => parsePacketOutput(output, 6)).toThrow(AnalysisOutputError);
|
||||||
|
expect(parsePacketOutput(output, null)).toHaveLength(2);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user