mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
refactor(apk): project signing block pair limits from the parser (#12670)
* refactor(apk): project the signing-block pair limit from the parser * test(apk): compare the pair refusal with the parser-owned reason
This commit is contained in:
1 parent
976edd68e7
commit
bd887a96be
6 files changed
+89
-22
No files matched your search
@@ -6,7 +6,7 @@
|
||||
"src/android/JadxFailure.ts": 2,
|
||||
"src/android/JadxSession.ts": 2,
|
||||
"src/apkSignature/ApkSignatureAdmission.ts": 1,
|
||||
"src/apkSignature/apkSignatureFailures.ts": 4,
|
||||
"src/apkSignature/apkSignatureFailures.ts": 3,
|
||||
"src/apktool/ApktoolConfiguration.ts": 1,
|
||||
"src/application/JsonAuthenticationHeadroom.ts": 2,
|
||||
"src/application/JsonInputFile.ts": 3,
|
||||
|
||||
@@ -13,7 +13,10 @@ it.each([
|
||||
[
|
||||
"block pairs",
|
||||
mapApkBlockError(
|
||||
{ kind: "budget-exceeded", resource: "signing-block-pairs", limit: 4096 },
|
||||
decodeLimit("apk-signing-block", "Fixture limit reached.", {
|
||||
name: "max_signing_block_pairs",
|
||||
maximum: 4096,
|
||||
}),
|
||||
"/selected.apk",
|
||||
),
|
||||
"max_signing_block_pairs",
|
||||
|
||||
@@ -72,17 +72,16 @@ export const mapApkBlockError = (
|
||||
return inputError(
|
||||
`Malformed APK Signing Block at byte ${String(error.offset)}: ${error.detail}.`,
|
||||
);
|
||||
case "budget-exceeded":
|
||||
return new AnalysisResourceConstraintError(
|
||||
OPERATION,
|
||||
"capacity",
|
||||
`APK Signing Block holds more ${error.resource} than the ${String(error.limit)} limit.`,
|
||||
{ max_signing_block_pairs: error.limit },
|
||||
fixedAllowanceOptions(
|
||||
`The APK Signing Block allowance of ${String(error.limit)} pairs (this release has no runtime override)`,
|
||||
case "limit": {
|
||||
const { name, maximum } = error.failure.limit;
|
||||
return parseLimitConstraint(OPERATION, "capacity", error, {
|
||||
legacyMaximumKey: name,
|
||||
...fixedAllowanceOptions(
|
||||
`The APK Signing Block allowance of ${String(maximum)} pairs (this release has no runtime override)`,
|
||||
"count",
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { isParseLimit } from "../parseFailureAnalysisError.js";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { zip64Tail } from "../../../tests/fixtures/apkSignature/zip64Tail.js";
|
||||
|
||||
@@ -112,6 +113,22 @@ describe("APK signing block locator", () => {
|
||||
expect([...block.value.pairs[0]!.value]).toEqual([0xaa, 0xbb, 0xcc, 0xdd]);
|
||||
});
|
||||
|
||||
it("names the enforced limit for a physically framed extra pair", () => {
|
||||
const bytes = apkWithBlock(
|
||||
Array.from({ length: 4097 }, () => ({ id: 0xffffffff, value: [] })),
|
||||
);
|
||||
const tail = parseZipTail(bytes);
|
||||
if (!tail.ok) throw new Error("Expected a framed ZIP tail");
|
||||
const result = locateApkSigningBlock(bytes, tail.value);
|
||||
if (result.ok) throw new Error("Expected a signing-block pair refusal");
|
||||
expect(isParseLimit(result.error, "apk-signing-block")).toBe(true);
|
||||
expect(result.error).toMatchObject({
|
||||
failure: {
|
||||
limit: { name: "max_signing_block_pairs", maximum: 4096 },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("returns null when no block magic precedes the central directory", () => {
|
||||
const bytes = apkWithBlock(
|
||||
[{ id: APK_BLOCK_ID.signatureV2, value: [1, 2, 3, 4] }],
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
* throw. Absence of the block is `ok(null)` (the APK is v1-only or unsigned),
|
||||
* not an error. All integers are little-endian (ZIP).
|
||||
*/
|
||||
import { decodeLimit, type ParseFailureError } from "../parseFailure.js";
|
||||
import { type Result, err, ok } from "../result.js";
|
||||
|
||||
/** "APK Sig Block 42" — the 16-byte block magic (JAR/APK signing block spec). */
|
||||
@@ -68,11 +69,7 @@ export type ApkSigningBlockError =
|
||||
readonly offset: number;
|
||||
readonly detail: string;
|
||||
}
|
||||
| {
|
||||
readonly kind: "budget-exceeded";
|
||||
readonly resource: "signing-block-pairs";
|
||||
readonly limit: number;
|
||||
};
|
||||
| ParseFailureError<"limit">;
|
||||
|
||||
export interface ZipTail {
|
||||
readonly eocdOffset: number;
|
||||
@@ -397,11 +394,17 @@ export const locateApkSigningBlock = (
|
||||
detail: "pair length out of range",
|
||||
});
|
||||
if (pairs.length >= APK_SIGNING_BLOCK_MAX_PAIRS)
|
||||
return err({
|
||||
kind: "budget-exceeded",
|
||||
resource: "signing-block-pairs",
|
||||
limit: APK_SIGNING_BLOCK_MAX_PAIRS,
|
||||
});
|
||||
return err(
|
||||
decodeLimit(
|
||||
"apk-signing-block",
|
||||
`APK Signing Block holds more signing-block-pairs than the ${String(APK_SIGNING_BLOCK_MAX_PAIRS)} limit.`,
|
||||
{
|
||||
name: "max_signing_block_pairs",
|
||||
maximum: APK_SIGNING_BLOCK_MAX_PAIRS,
|
||||
},
|
||||
{ offset: cursor },
|
||||
),
|
||||
);
|
||||
const id = reader.u32(cursor + 8);
|
||||
const valueStart = cursor + 12;
|
||||
pairs.push({
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { expect } from "vitest";
|
||||
|
||||
import {
|
||||
locateApkSigningBlock,
|
||||
parseZipTail,
|
||||
} from "../../src/domain/apkSignature/apkSigningBlock.js";
|
||||
import { parseEvidence } from "../../src/domain/evidence.js";
|
||||
import { analysisErrorProjectionSchema } from "../../src/domain/analysisErrorProjectionSchema.js";
|
||||
import { apkSignatureSummaryViewResultSchema } from "../../src/domain/analysisView/analysisViewResult.js";
|
||||
@@ -29,6 +33,7 @@ cliTest(
|
||||
]);
|
||||
const paths = new Map<number, string>();
|
||||
const digests = new Map<number, string>();
|
||||
const malformedPath = join(root, "pair-overrun.apk");
|
||||
for (const count of [1, 4096, 4097]) {
|
||||
const path = join(root, `pairs-${count}.apk`);
|
||||
const bytes = withSigningBlock(
|
||||
@@ -41,7 +46,23 @@ cliTest(
|
||||
await writeFile(path, bytes);
|
||||
digests.set(count, createHash("sha256").update(bytes).digest("hex"));
|
||||
paths.set(count, path);
|
||||
if (count === 4097) {
|
||||
const malformed = Buffer.from(bytes);
|
||||
const directoryOffset = malformed.readUInt32LE(
|
||||
malformed.length - 22 + 16,
|
||||
);
|
||||
// The last pair has room for its ID only. Its declared value would
|
||||
// cross into the 24-byte block footer.
|
||||
malformed.writeBigUInt64LE(5n, directoryOffset - 24 - 12);
|
||||
await writeFile(malformedPath, malformed);
|
||||
}
|
||||
}
|
||||
const bytes = await readFile(paths.get(4097)!);
|
||||
const tail = parseZipTail(bytes);
|
||||
if (!tail.ok) throw new Error("Expected a framed ZIP tail");
|
||||
const parsed = locateApkSigningBlock(bytes, tail.value);
|
||||
if (parsed.ok || parsed.error.kind !== "limit")
|
||||
throw new Error("Expected a signing-block pair limit");
|
||||
const expected = {
|
||||
code: "resource_constraint",
|
||||
category: "resource_constraint",
|
||||
@@ -49,6 +70,7 @@ cliTest(
|
||||
details: {
|
||||
operation: "inspect_apk_signature",
|
||||
resource: "capacity",
|
||||
reason: parsed.error.failure.detail,
|
||||
reported_limits: { max_signing_block_pairs: 4096 },
|
||||
},
|
||||
};
|
||||
@@ -65,6 +87,25 @@ cliTest(
|
||||
"Waiting for an active analysis to finish does not raise it",
|
||||
);
|
||||
}
|
||||
const malformedExpected = {
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [
|
||||
{
|
||||
path: ["path"],
|
||||
reason: "invalid_value",
|
||||
message: expect.stringContaining("pair length out of range"),
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const malformedReply = await cli.run({
|
||||
arguments: ["inspect-apk-signature", malformedPath, "--json"],
|
||||
});
|
||||
expect(malformedReply.exitCode, malformedReply.stderr).toBe(1);
|
||||
expect(
|
||||
analysisErrorProjectionSchema.parse(malformedReply.json),
|
||||
).toMatchObject(malformedExpected);
|
||||
const { call } = await connectLocalToolsMcp({
|
||||
REA_MCP_OUTPUT_SCHEMA_PROFILE: "off",
|
||||
});
|
||||
@@ -72,6 +113,10 @@ cliTest(
|
||||
path: paths.get(4097),
|
||||
});
|
||||
expect(parseMcpToolError(rejected).error).toMatchObject(expected);
|
||||
const malformed = await call("inspect_apk_signature", {
|
||||
path: malformedPath,
|
||||
});
|
||||
expect(parseMcpToolError(malformed).error).toMatchObject(malformedExpected);
|
||||
for (const count of [4096, 1]) {
|
||||
const path = paths.get(count)!;
|
||||
const reply = await call("inspect_apk_signature", { path });
|
||||
|
||||
Reference in new issue
Block a user