mirror of
https://github.com/morluto/rea.git
synced 2026-11-29 18:06:46 +00:00
feat(metadata)!: give NTFS, Maven report and NuGet budgets typed per-call controls (#14629)
* feat(metadata)!: give NTFS, Maven report and NuGet budgets typed per-call controls inspect_ntfs_journal refused acquisitions above a fixed 64 MiB allowance; it now takes max_file_bytes (default 64 MiB) and re-admits a retained acquisition under the call's budget, so lowering it cannot reuse a larger cached read. inspect_maven_gradle_metadata refused at six fixed report allowances; they are now report_limits fields (CLI --report-limits JSON) with today's values as defaults. Maven max_file_bytes and NuGet max_file_bytes, max_entries and max_nuspec_bytes refusals now report details.limit from the same bounds objects the schemas use. The Ghidra installation-properties read allowance is registered as a protocol bound: it reads the provider's own short installation descriptor, not analysis input. BREAKING CHANGE: NTFS and Maven report refusals report details.limit instead of a fixed_ceiling refusal, and parseMavenGradleMetadata and the inspect_ntfs_journal input contract gain the new controls. * fix(metadata): keep omitted NTFS budgets omitted and type host and maximum recovery inspect_ntfs_journal recorded the default max_file_bytes as if the caller had chosen it; the field is now optional and resolved for execution only, so Evidence keeps omission. A host allocation failure while holding an admitted acquisition is reported as measured memory exhaustion instead of generic I/O. A Maven report refusal whose need exceeds the control's maximum no longer suggests raising it. A public CLI/MCP test covers report_limits refusal and recovery for a POM.
This commit is contained in:
1 parent
49e2eca4f9
commit
4f66985978
22 files changed
+544
-230
No files matched your search
@@ -1,19 +1,11 @@
|
||||
import { decodeLimit } from "../domain/parseFailure.js";
|
||||
import type { MavenGradleReportLimitField } from "../domain/java/mavenGradleMetadata.js";
|
||||
/** The ParseFailure format of a Maven POM or Gradle module metadata file. */
|
||||
export const MAVEN_GRADLE_METADATA_FORMAT = "maven-gradle-metadata";
|
||||
|
||||
/** Fixed report allowances; each names the representation it bounds. */
|
||||
export type MavenGradleReportAllowance =
|
||||
| "repeated_variant_name_chars"
|
||||
| "dependency_rows"
|
||||
| "variants"
|
||||
| "exclusions"
|
||||
| "unresolved_records"
|
||||
| "xml_nodes";
|
||||
|
||||
const REPORT_REFUSALS: Readonly<
|
||||
Record<
|
||||
MavenGradleReportAllowance,
|
||||
MavenGradleReportLimitField,
|
||||
(required: number, limit: number) => string
|
||||
>
|
||||
> = {
|
||||
@@ -37,7 +29,7 @@ const REPORT_REFUSALS: Readonly<
|
||||
* repeated variant name expands a few input bytes into a much larger report.
|
||||
*/
|
||||
export const metadataReportLimit = (
|
||||
allowance: MavenGradleReportAllowance,
|
||||
allowance: MavenGradleReportLimitField,
|
||||
maximum: number,
|
||||
required: number,
|
||||
) =>
|
||||
|
||||
@@ -771,18 +771,16 @@ it.each([
|
||||
source: repeatedVariantModule(),
|
||||
maximum: 4_194_304,
|
||||
required: 5_000_000,
|
||||
recovery: "character allowance for variant names",
|
||||
},
|
||||
{
|
||||
boundary: "variants",
|
||||
source: `{"formatVersion":"1.1","variants":[${Array(4_097).fill("{}").join(",")}]}`,
|
||||
maximum: 4_096,
|
||||
required: 4_097,
|
||||
recovery: "4096-variant allowance",
|
||||
},
|
||||
])(
|
||||
"reports the measured $boundary fixed allowance",
|
||||
async ({ boundary, source, maximum, required, recovery }) => {
|
||||
] as const)(
|
||||
"reports the measured $boundary allowance as its report_limits control",
|
||||
async ({ boundary, source, maximum, required }) => {
|
||||
const root = await createTestTempDirectory("rea-gradle-report-");
|
||||
const path = join(root, "demo.module");
|
||||
await writeFile(path, source);
|
||||
@@ -792,17 +790,31 @@ it.each([
|
||||
path,
|
||||
max_file_bytes: 1024 * 1024,
|
||||
});
|
||||
if (result.ok)
|
||||
throw new Error(`Expected a fixed allowance for ${boundary}`);
|
||||
if (result.ok) throw new Error(`Expected a report limit for ${boundary}`);
|
||||
expect(projectAnalysisError(result.error)).toMatchObject({
|
||||
code: "resource_constraint",
|
||||
retryable: false,
|
||||
details: {
|
||||
reported_limits: { boundary, maximum, required },
|
||||
refusal: { kind: "fixed_ceiling", allowance_kind: "report" },
|
||||
limit: {
|
||||
control_kind: "parameter",
|
||||
parameter: `report_limits.${boundary}`,
|
||||
effective_value: maximum,
|
||||
default_value: maximum,
|
||||
applies_at: "next_call",
|
||||
},
|
||||
},
|
||||
remediation: {
|
||||
action: expect.stringContaining(`report_limits.${boundary}`),
|
||||
},
|
||||
remediation: { action: expect.stringContaining(recovery) },
|
||||
});
|
||||
// The same document is reported once the caller raises the control.
|
||||
const raised = await provider.inspect({
|
||||
path,
|
||||
max_file_bytes: 1024 * 1024,
|
||||
report_limits: { [boundary]: required },
|
||||
});
|
||||
if (!raised.ok) throw raised.error;
|
||||
} finally {
|
||||
await provider.close();
|
||||
}
|
||||
|
||||
@@ -15,11 +15,9 @@ import {
|
||||
xmlFirstChildNamed,
|
||||
} from "../domain/xmlDocument.js";
|
||||
import {
|
||||
MAVEN_GRADLE_METADATA_DEPENDENCY_ROWS,
|
||||
MAVEN_GRADLE_METADATA_EXCLUSIONS,
|
||||
MAVEN_GRADLE_METADATA_REPEATED_VARIANT_NAME_CHARS,
|
||||
MAVEN_GRADLE_METADATA_VARIANTS,
|
||||
MAVEN_GRADLE_METADATA_XML_NODES,
|
||||
mavenGradleReportLimits,
|
||||
type MavenGradleReportLimitField,
|
||||
type MavenGradleReportLimits,
|
||||
requestedVersionKind,
|
||||
type MavenGradleDependency,
|
||||
type MavenGradleMetadata,
|
||||
@@ -28,7 +26,6 @@ import { redactRecordedTransportUrl } from "../domain/recordedTransportUrl.js";
|
||||
import {
|
||||
MAVEN_GRADLE_METADATA_FORMAT,
|
||||
metadataReportLimit,
|
||||
type MavenGradleReportAllowance,
|
||||
} from "./MavenGradleMetadata.errors.js";
|
||||
|
||||
const BASE_LIMITATIONS = [
|
||||
@@ -81,7 +78,7 @@ const isMavenProjectRoot = (root: Node | null): root is Element => {
|
||||
|
||||
/** Refuse a report whose expanded collection would outgrow its fixed allowance. */
|
||||
const admit = (
|
||||
allowance: MavenGradleReportAllowance,
|
||||
allowance: MavenGradleReportLimitField,
|
||||
limit: number,
|
||||
required: number,
|
||||
): void => {
|
||||
@@ -288,12 +285,13 @@ const parseMavenDependency = <
|
||||
const parsePom = (
|
||||
xml: string,
|
||||
artifact: { path: string; sha256: string; bytes: number },
|
||||
limits: MavenGradleReportLimits,
|
||||
): MavenGradleMetadata => {
|
||||
const limitations: string[] = [...BASE_LIMITATIONS];
|
||||
// Maven applies XML 1.0 end-of-line handling to property and coordinate text.
|
||||
const parsed = parseXmlDocumentWithinNodeLimit(xml, {
|
||||
lineEndings: "normalize",
|
||||
maximumNodes: MAVEN_GRADLE_METADATA_XML_NODES,
|
||||
maximumNodes: limits.xml_nodes,
|
||||
});
|
||||
if (!parsed.ok && parsed.error.kind === "node-limit")
|
||||
throw metadataReportLimit(
|
||||
@@ -317,7 +315,9 @@ const parsePom = (
|
||||
MAVEN_GRADLE_METADATA_FORMAT,
|
||||
"Selected XML document is not a Maven project POM.",
|
||||
);
|
||||
const unresolved = new MavenGradleUnresolvedCollector();
|
||||
const unresolved = new MavenGradleUnresolvedCollector(
|
||||
limits.unresolved_records,
|
||||
);
|
||||
const parentElement = soleNamed(root, "parent");
|
||||
const parent =
|
||||
parentElement === undefined
|
||||
@@ -381,12 +381,12 @@ const parsePom = (
|
||||
);
|
||||
admit(
|
||||
"dependency_rows",
|
||||
MAVEN_GRADLE_METADATA_DEPENDENCY_ROWS,
|
||||
limits.dependency_rows,
|
||||
managedElements.length + dependencyElements.length,
|
||||
);
|
||||
admit(
|
||||
"exclusions",
|
||||
MAVEN_GRADLE_METADATA_EXCLUSIONS,
|
||||
limits.exclusions,
|
||||
[...managedElements, ...dependencyElements].reduce(
|
||||
(count, element) => count + mavenExclusionElements(element).length,
|
||||
0,
|
||||
@@ -485,6 +485,7 @@ const parseGradleDependency = <
|
||||
const parseGradle = (
|
||||
source: string,
|
||||
artifact: { path: string; sha256: string; bytes: number },
|
||||
limits: MavenGradleReportLimits,
|
||||
): MavenGradleMetadata => {
|
||||
const json = parseJsonWithDuplicatePolicy(source, "reject");
|
||||
if (!json.ok) {
|
||||
@@ -534,7 +535,9 @@ const parseGradle = (
|
||||
limitations.push(
|
||||
`Recognized fields in this unfamiliar format version were interpreted using Gradle module metadata ${GRADLE_MODULE_METADATA_VERSION}. Version-specific declarations may be unreported.`,
|
||||
);
|
||||
const unresolved = new MavenGradleUnresolvedCollector();
|
||||
const unresolved = new MavenGradleUnresolvedCollector(
|
||||
limits.unresolved_records,
|
||||
);
|
||||
const coordinates = {
|
||||
group_id: component === null ? null : gradleText(component.group),
|
||||
artifact_id: component === null ? null : gradleText(component.module),
|
||||
@@ -547,7 +550,7 @@ const parseGradle = (
|
||||
// Count each expanded collection before any report record is built.
|
||||
admit(
|
||||
"variants",
|
||||
MAVEN_GRADLE_METADATA_VARIANTS,
|
||||
limits.variants,
|
||||
gradleArray(variantsValue, "variants").length,
|
||||
);
|
||||
const variants = gradleObjects(variantsValue, "variants");
|
||||
@@ -557,7 +560,7 @@ const parseGradle = (
|
||||
]);
|
||||
admit(
|
||||
"dependency_rows",
|
||||
MAVEN_GRADLE_METADATA_DEPENDENCY_ROWS,
|
||||
limits.dependency_rows,
|
||||
rowLists.reduce((count, rows) => count + rows.length, 0),
|
||||
);
|
||||
let exclusions = 0;
|
||||
@@ -565,16 +568,14 @@ const parseGradle = (
|
||||
for (const row of rows)
|
||||
if (isRecord(row))
|
||||
exclusions += gradleArray(row.excludes, "excludes").length;
|
||||
admit("exclusions", MAVEN_GRADLE_METADATA_EXCLUSIONS, exclusions);
|
||||
admit("exclusions", limits.exclusions, exclusions);
|
||||
const dependencies: MavenGradleDependency<"variant-dependencies">[] = [];
|
||||
const dependency_management: MavenGradleDependency<"variant-constraints">[] =
|
||||
[];
|
||||
const gradleVariants: NonNullable<MavenGradleMetadata["gradle"]>["variants"] =
|
||||
[];
|
||||
let nextIndex = 0;
|
||||
const repeatedVariantNames = new Budget(
|
||||
MAVEN_GRADLE_METADATA_REPEATED_VARIANT_NAME_CHARS,
|
||||
);
|
||||
const repeatedVariantNames = new Budget(limits.repeated_variant_name_chars);
|
||||
const variantNameIndices = new Map<string, number>();
|
||||
for (const [index, variant] of variants.entries()) {
|
||||
const declaredName = variant.name;
|
||||
@@ -686,6 +687,7 @@ const parseGradle = (
|
||||
export const parseMavenGradleMetadata = (
|
||||
bytes: Uint8Array,
|
||||
artifact: { path: string; sha256: string; bytes: number },
|
||||
limits: MavenGradleReportLimits = mavenGradleReportLimits(),
|
||||
): MavenGradleMetadata => {
|
||||
if (bytes.length !== artifact.bytes)
|
||||
throw malformedInput(
|
||||
@@ -708,9 +710,9 @@ export const parseMavenGradleMetadata = (
|
||||
}
|
||||
const trimmed = source.trimStart();
|
||||
if (trimmed.startsWith("{") || trimmed.startsWith("["))
|
||||
return parseGradle(source, artifact);
|
||||
return parseGradle(source, artifact, limits);
|
||||
if (opensLikeXml(trimmed) || trimmed.startsWith("<"))
|
||||
return parsePom(source, artifact);
|
||||
return parsePom(source, artifact, limits);
|
||||
throw unsupportedInput(
|
||||
MAVEN_GRADLE_METADATA_FORMAT,
|
||||
"Selected file is not a Maven POM or Gradle module-metadata document.",
|
||||
|
||||
@@ -16,15 +16,16 @@ import { decodeLimit } from "../domain/parseFailure.js";
|
||||
import { errorMessage } from "../kernel/errorMessage.js";
|
||||
import { ArtifactOperationError } from "../domain/artifactOperationError.js";
|
||||
import {
|
||||
MAX_INSPECT_MAVEN_GRADLE_METADATA_FILE_BYTES,
|
||||
MAVEN_GRADLE_FILE_BYTES,
|
||||
mavenGradleFileBytesLimit,
|
||||
mavenGradleReportLimit,
|
||||
mavenGradleReportLimits,
|
||||
MAVEN_GRADLE_REPORT_LIMIT_BOUNDS,
|
||||
type MavenGradleReportLimitField,
|
||||
type InspectMavenGradleMetadataInput,
|
||||
} from "../domain/java/mavenGradleMetadata.js";
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import {
|
||||
MAVEN_GRADLE_METADATA_FORMAT,
|
||||
type MavenGradleReportAllowance,
|
||||
} from "./MavenGradleMetadata.errors.js";
|
||||
import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js";
|
||||
import { MAVEN_GRADLE_METADATA_FORMAT } from "./MavenGradleMetadata.errors.js";
|
||||
import { MAVEN_GRADLE_METADATA_PROVIDER_IDENTITY } from "./MavenGradleMetadataProviderIdentity.js";
|
||||
import { parseMavenGradleMetadata } from "./MavenGradleMetadataParser.js";
|
||||
import { classifyProviderFailure } from "../providers/classifyProviderFailure.js";
|
||||
@@ -65,11 +66,15 @@ export class MavenGradleMetadataProvider {
|
||||
this.#resources,
|
||||
);
|
||||
return ok(
|
||||
parseMavenGradleMetadata(snapshot.bytes, {
|
||||
path: input.path,
|
||||
sha256: snapshot.sha256,
|
||||
bytes: snapshot.bytes.length,
|
||||
}),
|
||||
parseMavenGradleMetadata(
|
||||
snapshot.bytes,
|
||||
{
|
||||
path: input.path,
|
||||
sha256: snapshot.sha256,
|
||||
bytes: snapshot.bytes.length,
|
||||
},
|
||||
mavenGradleReportLimits(input.report_limits),
|
||||
),
|
||||
);
|
||||
});
|
||||
} catch (cause) {
|
||||
@@ -81,27 +86,13 @@ export class MavenGradleMetadataProvider {
|
||||
const inputBudgetRemediation = (
|
||||
input: InspectMavenGradleMetadataInput,
|
||||
): string =>
|
||||
input.max_file_bytes >= MAX_INSPECT_MAVEN_GRADLE_METADATA_FILE_BYTES
|
||||
? `The file exceeds the ${String(MAX_INSPECT_MAVEN_GRADLE_METADATA_FILE_BYTES)}-byte input ceiling this operation accepts; select a smaller artifact.`
|
||||
: `Raise max_file_bytes above ${String(input.max_file_bytes)} bytes, up to ${String(MAX_INSPECT_MAVEN_GRADLE_METADATA_FILE_BYTES)}, or select a smaller artifact; this is the caller-selected input read budget.`;
|
||||
input.max_file_bytes >= MAVEN_GRADLE_FILE_BYTES.maximum
|
||||
? `The file exceeds the ${String(MAVEN_GRADLE_FILE_BYTES.maximum)}-byte input ceiling this operation accepts; select a smaller artifact.`
|
||||
: `Raise max_file_bytes above ${String(input.max_file_bytes)} bytes, up to ${String(MAVEN_GRADLE_FILE_BYTES.maximum)}, or select a smaller artifact; this is the caller-selected input read budget.`;
|
||||
|
||||
/** The subject a fixed-allowance remediation names for each report allowance. */
|
||||
const REPORT_ALLOWANCE_SUBJECTS: Readonly<
|
||||
Record<MavenGradleReportAllowance, (limit: number) => string>
|
||||
> = {
|
||||
dependency_rows: (limit) =>
|
||||
`The ${String(limit)}-row allowance for dependency and constraint rows`,
|
||||
variants: (limit) =>
|
||||
`The ${String(limit)}-variant allowance for Gradle module variants`,
|
||||
exclusions: (limit) =>
|
||||
`The ${String(limit)}-exclusion allowance for dependency exclusions`,
|
||||
unresolved_records: (limit) =>
|
||||
`The ${String(limit)}-record allowance for unresolved references and coordinates`,
|
||||
xml_nodes: (limit) =>
|
||||
`The ${String(limit)}-node allowance for a Maven POM document tree`,
|
||||
repeated_variant_name_chars: (limit) =>
|
||||
`The ${String(limit)}-character allowance for variant names repeated across Gradle dependency rows`,
|
||||
};
|
||||
const REPORT_LIMIT_FIELDS = Object.keys(
|
||||
MAVEN_GRADLE_REPORT_LIMIT_BOUNDS,
|
||||
) as MavenGradleReportLimitField[];
|
||||
|
||||
const mavenGradleMetadataFailure = (
|
||||
cause: unknown,
|
||||
@@ -121,28 +112,25 @@ const mavenGradleMetadataFailure = (
|
||||
recognise: (failure) => {
|
||||
if (!isParseLimit(failure, MAVEN_GRADLE_METADATA_FORMAT))
|
||||
return undefined;
|
||||
const { name, maximum } = failure.failure.limit;
|
||||
const { name, maximum, required } = failure.failure.limit;
|
||||
const field = REPORT_LIMIT_FIELDS.find((field) => field === name);
|
||||
const limit =
|
||||
field === undefined
|
||||
? undefined
|
||||
: mavenGradleReportLimit(field, maximum);
|
||||
return parseLimitConstraint(OPERATION, "memory", failure, {
|
||||
path: input.path,
|
||||
...fixedAllowanceOptions(
|
||||
Object.entries(REPORT_ALLOWANCE_SUBJECTS).find(
|
||||
([key]) => key === name,
|
||||
)?.[1](maximum) ?? name,
|
||||
name === "xml_nodes" ? "parser" : "report",
|
||||
{
|
||||
limit: maximum,
|
||||
unit:
|
||||
name === "repeated_variant_name_chars"
|
||||
? "UTF-16 code units"
|
||||
: name === "unresolved_records"
|
||||
? "records"
|
||||
: name === "dependency_rows"
|
||||
? "rows"
|
||||
: name === "xml_nodes"
|
||||
? "nodes"
|
||||
: name,
|
||||
},
|
||||
),
|
||||
retryable: false,
|
||||
...(limit === undefined
|
||||
? {}
|
||||
: {
|
||||
limit,
|
||||
remediationAction:
|
||||
maximum < limit.maximum &&
|
||||
(required === undefined || required <= limit.maximum)
|
||||
? `Pass a larger ${limit.parameter}${required === undefined ? "" : ` (at least ${String(required)})`} on the next call for the same document; no restart is required.`
|
||||
: `${limit.parameter} cannot exceed ${String(limit.maximum)}, which does not admit this document; select a document within that bound.`,
|
||||
}),
|
||||
});
|
||||
},
|
||||
fileSize: (failure) =>
|
||||
@@ -157,6 +145,7 @@ const mavenGradleMetadataFailure = (
|
||||
),
|
||||
{
|
||||
retryable: false,
|
||||
limit: mavenGradleFileBytesLimit(input.max_file_bytes),
|
||||
remediationAction: inputBudgetRemediation(input),
|
||||
},
|
||||
),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Budget } from "../domain/budget.js";
|
||||
import {
|
||||
MAVEN_GRADLE_METADATA_UNRESOLVED_RECORDS,
|
||||
MAVEN_GRADLE_REPORT_LIMIT_BOUNDS,
|
||||
mavenPropertyOccurrences,
|
||||
type MavenGradleUnresolved,
|
||||
} from "../domain/java/mavenGradleMetadata.js";
|
||||
@@ -13,7 +13,10 @@ export class MavenGradleUnresolvedCollector {
|
||||
readonly #records: MavenGradleUnresolved[] = [];
|
||||
readonly #budget: Budget;
|
||||
|
||||
constructor(maximum = MAVEN_GRADLE_METADATA_UNRESOLVED_RECORDS) {
|
||||
constructor(
|
||||
maximum: number = MAVEN_GRADLE_REPORT_LIMIT_BOUNDS.unresolved_records
|
||||
.default_value,
|
||||
) {
|
||||
this.#budget = new Budget(maximum);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user