mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
refactor(apk): project the v1 directory entry limit from the parser (#12665)
* refactor(apk): project the v1 directory entry limit from the parser * test(apk): compare v1 refusal with the parser-owned reason
This commit is contained in:
1 parent
e21efd7664
commit
a3df11ebf5
6 files changed
+128
-35
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": 5,
|
||||
"src/apkSignature/apkSignatureFailures.ts": 4,
|
||||
"src/apktool/ApktoolConfiguration.ts": 1,
|
||||
"src/application/JsonAuthenticationHeadroom.ts": 2,
|
||||
"src/application/JsonInputFile.ts": 3,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { decodeLimit } from "../domain/parseFailure.js";
|
||||
import {
|
||||
apkOutputBudgetError,
|
||||
mapApkBlockError,
|
||||
@@ -20,11 +21,12 @@ it.each([
|
||||
],
|
||||
[
|
||||
"v1 entries",
|
||||
mapCentralDirectoryError({
|
||||
kind: "budget-exceeded",
|
||||
resource: "v1-signature-entries",
|
||||
limit: 4096,
|
||||
}),
|
||||
mapCentralDirectoryError(
|
||||
decodeLimit("apk-v1-central-directory", "Fixture limit reached.", {
|
||||
name: "max_v1_signature_entries",
|
||||
maximum: 4096,
|
||||
}),
|
||||
),
|
||||
"max_v1_signature_entries",
|
||||
4096,
|
||||
],
|
||||
|
||||
@@ -108,21 +108,20 @@ export const mapApkSchemeError = (
|
||||
/** A central directory that cannot be walked leaves v1 presence unknown: refuse. */
|
||||
export const mapCentralDirectoryError = (
|
||||
error: ApkCentralDirectoryError,
|
||||
): AnalysisError =>
|
||||
error.kind === "budget-exceeded"
|
||||
? new AnalysisResourceConstraintError(
|
||||
OPERATION,
|
||||
"capacity",
|
||||
`The central directory holds more than ${String(error.limit)} META-INF v1 signing entries.`,
|
||||
{ max_v1_signature_entries: error.limit },
|
||||
fixedAllowanceOptions(
|
||||
`The APK v1 allowance of ${String(error.limit)} META-INF signing entries (this release has no runtime override)`,
|
||||
"count",
|
||||
),
|
||||
)
|
||||
: inputError(
|
||||
`Malformed ZIP central directory at byte ${String(error.offset)}: ${error.detail}.`,
|
||||
);
|
||||
): AnalysisError => {
|
||||
if (error.kind !== "limit")
|
||||
return inputError(
|
||||
`Malformed ZIP central directory at byte ${String(error.offset)}: ${error.detail}.`,
|
||||
);
|
||||
const { name, maximum } = error.failure.limit;
|
||||
return parseLimitConstraint(OPERATION, "capacity", error, {
|
||||
legacyMaximumKey: name,
|
||||
...fixedAllowanceOptions(
|
||||
`The APK v1 allowance of ${String(maximum)} META-INF signing entries (this release has no runtime override)`,
|
||||
"count",
|
||||
),
|
||||
});
|
||||
};
|
||||
|
||||
/** More v1 signers than a signed APK carries: refused rather than decoded. */
|
||||
export const v1SignerCapacityError = (limit: number): AnalysisError =>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { isParseLimit } from "../parseFailureAnalysisError.js";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
@@ -140,10 +141,14 @@ describe("APK v1 (JAR) signature detection", () => {
|
||||
);
|
||||
expect(result.ok).toBe(false);
|
||||
if (result.ok) return;
|
||||
expect(result.error).toEqual({
|
||||
kind: "budget-exceeded",
|
||||
resource: "v1-signature-entries",
|
||||
limit: MAX_V1_SIGNATURE_ENTRIES,
|
||||
expect(isParseLimit(result.error, "apk-v1-central-directory")).toBe(true);
|
||||
expect(result.error).toMatchObject({
|
||||
failure: {
|
||||
limit: {
|
||||
name: "max_v1_signature_entries",
|
||||
maximum: MAX_V1_SIGNATURE_ENTRIES,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
import { boundedList } from "../boundedList.js";
|
||||
import { BinaryReader } from "../binaryContainer.js";
|
||||
import { decodeLimit, type ParseFailureError } from "../parseFailure.js";
|
||||
import { type Result, err, ok } from "../result.js";
|
||||
import {
|
||||
ZIP_CENTRAL_HEADER,
|
||||
@@ -51,11 +52,7 @@ export type ApkCentralDirectoryError =
|
||||
readonly offset: number;
|
||||
readonly detail: string;
|
||||
}
|
||||
| {
|
||||
readonly kind: "budget-exceeded";
|
||||
readonly resource: "v1-signature-entries";
|
||||
readonly limit: number;
|
||||
};
|
||||
| ParseFailureError<"limit">;
|
||||
|
||||
/**
|
||||
* Signing-relevant `META-INF/` entries retained. A signed APK holds a manifest
|
||||
@@ -325,11 +322,17 @@ export const centralDirectoryV1SignatureEntries = (
|
||||
const name = decodeZipName(rawName);
|
||||
if (isV1SignatureMetadataName(name)) {
|
||||
if (entries.length >= MAX_V1_SIGNATURE_ENTRIES)
|
||||
return err({
|
||||
kind: "budget-exceeded",
|
||||
resource: "v1-signature-entries",
|
||||
limit: MAX_V1_SIGNATURE_ENTRIES,
|
||||
});
|
||||
return err(
|
||||
decodeLimit(
|
||||
"apk-v1-central-directory",
|
||||
`The central directory holds more than ${String(MAX_V1_SIGNATURE_ENTRIES)} META-INF v1 signing entries.`,
|
||||
{
|
||||
name: "max_v1_signature_entries",
|
||||
maximum: MAX_V1_SIGNATURE_ENTRIES,
|
||||
},
|
||||
{ offset: at },
|
||||
),
|
||||
);
|
||||
entries.push({
|
||||
name,
|
||||
rawName,
|
||||
|
||||
@@ -2,6 +2,8 @@ import {
|
||||
canonicalOutputSchema,
|
||||
withCanonicalOutputSchema,
|
||||
} from "../fixtures/canonicalOutputSchema.js";
|
||||
import { parseZipTail } from "../../src/domain/apkSignature/apkSigningBlock.js";
|
||||
import { centralDirectoryV1SignatureEntries } from "../../src/domain/apkSignature/apkV1Signature.js";
|
||||
import { apkSignatureResultSchema } from "../../src/domain/apkSignature/apkSignatureReport.js";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
@@ -12,6 +14,7 @@ import { closeVerifierResources } from "../../scripts/lib/verifier-cleanup.mjs";
|
||||
import { AnalysisProviderRegistry } from "../../src/application/binary/AnalysisProviderRegistry.js";
|
||||
import { composeBinarySession } from "../../src/application/binary/BinarySessionComposition.js";
|
||||
import { toolContract } from "../../src/contracts/toolContracts.js";
|
||||
import { analysisErrorProjectionSchema } from "../../src/domain/analysisErrorProjectionSchema.js";
|
||||
import { parseEvidence } from "../../src/domain/evidence.js";
|
||||
import { createServer } from "../../src/server/createServer.js";
|
||||
import {
|
||||
@@ -37,6 +40,7 @@ import {
|
||||
zipWith,
|
||||
} from "../fixtures/apkSignature/v1SignatureBlock.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
|
||||
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
|
||||
import { cliTest } from "../support/cli/cliFixture.js";
|
||||
import { createTestMcp } from "../support/mcp/mcpFixture.js";
|
||||
@@ -763,3 +767,83 @@ cliTest(
|
||||
]);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"preserves framing precedence at the APK v1 entry limit across CLI and MCP",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-apk-v1-entry-limit-");
|
||||
const entries = Array.from({ length: 4097 }, (_, index) => ({
|
||||
name: `META-INF/F${String(index)}.SF`,
|
||||
data: "",
|
||||
stored: true,
|
||||
}));
|
||||
const bytes = await zipWith(entries, { dataDescriptor: false });
|
||||
const tail = parseZipTail(bytes);
|
||||
if (!tail.ok) throw new Error("Expected a framed ZIP tail");
|
||||
const { centralDirectoryOffset, centralDirectorySize, totalEntries } =
|
||||
tail.value;
|
||||
const parsed = centralDirectoryV1SignatureEntries(
|
||||
bytes,
|
||||
centralDirectoryOffset,
|
||||
centralDirectorySize,
|
||||
totalEntries,
|
||||
);
|
||||
if (parsed.ok || parsed.error.kind !== "limit")
|
||||
throw new Error("Expected a v1 central-directory limit");
|
||||
const parserReason = parsed.error.failure.detail;
|
||||
const malformed = Buffer.from(bytes);
|
||||
const { central } = zipEntryOffsets(bytes, "META-INF/F4096.SF");
|
||||
malformed.writeUInt16LE(0xffff, central + 28);
|
||||
|
||||
const { call } = await connectLocalToolsMcp();
|
||||
for (const [name, selected, expected] of [
|
||||
[
|
||||
"framed",
|
||||
bytes,
|
||||
{
|
||||
code: "resource_constraint",
|
||||
retryable: false,
|
||||
details: {
|
||||
resource: "capacity",
|
||||
reason: parserReason,
|
||||
reported_limits: { max_v1_signature_entries: 4096 },
|
||||
},
|
||||
remediation: {
|
||||
action: expect.stringContaining("no runtime override"),
|
||||
},
|
||||
},
|
||||
],
|
||||
[
|
||||
"malformed",
|
||||
malformed,
|
||||
{
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [
|
||||
{
|
||||
path: ["path"],
|
||||
reason: "invalid_value",
|
||||
message: expect.stringContaining(
|
||||
"central-directory file name runs past the directory",
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
] as const) {
|
||||
const path = join(root, `${name}.apk`);
|
||||
await writeFile(path, selected);
|
||||
const reply = await cli.run({
|
||||
arguments: ["inspect-apk-signature", path, "--json"],
|
||||
});
|
||||
expect(reply.exitCode, reply.stderr).toBe(1);
|
||||
expect(analysisErrorProjectionSchema.parse(reply.json)).toMatchObject(
|
||||
expected,
|
||||
);
|
||||
expect(
|
||||
parseMcpToolError(await call("inspect_apk_signature", { path })).error,
|
||||
).toMatchObject(expected);
|
||||
}
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user