mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 17:23:37 +08:00
fix(maven): read POMs in their declared XML encoding
The Maven reader decoded every file as strict UTF-8, so an ISO-8859-1 POM such as Maven's own pom-encoding fixture, or a BOM-marked UTF-16 POM, failed as "not valid UTF-8". Decode with the shared XML text decoder and admit ISO-8859-1. A POM that declares an encoding REA does not decode is now refused as unsupported and names that declaration. Fixes #15664 Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
This commit is contained in:
committed by
Ysopwnable
co-authored by
gensokyo-dev
parent
70fc6b745b
commit
e2fce6bb6c
@@ -28,7 +28,10 @@ in another `http://maven.apache.org/POM/` model namespace is
|
||||
`unsupported_target`, and so is a project in a Maven POM namespace whose
|
||||
`modelVersion` names another model. A legacy unqualified project that declares
|
||||
another `modelVersion` is read with the 4.0.0 model rules. REA reads structural
|
||||
declarations in the project's namespace. A Gradle metadata object starts with
|
||||
declarations in the project's namespace. A POM is read as UTF-8, as UTF-16
|
||||
with a byte order mark, or as ISO-8859-1 when its XML declaration says so; a
|
||||
POM that declares another encoding is `unsupported_target`. A Gradle metadata
|
||||
object starts with
|
||||
`formatVersion`.
|
||||
Version `1.1` can omit `component` and `variants`. For other revisions, REA
|
||||
also requires a `component` object or `variants` array as format evidence.
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { MavenGradleUnresolvedCollector } from "./MavenGradleUnresolvedCollector.js";
|
||||
import { Budget } from "../domain/budget.js";
|
||||
import { decodeStrictUtf8 } from "../domain/strictUtf8.js";
|
||||
import {
|
||||
decodeXmlPlistText,
|
||||
XmlPlistTextDecodeError,
|
||||
} from "../domain/propertyListXmlText.js";
|
||||
import { parseJsonWithDuplicatePolicy } from "../domain/jsonDuplicateMember.js";
|
||||
import { isRecord } from "../kernel/isRecord.js";
|
||||
import { malformedInput, unsupportedInput } from "../domain/parseFailure.js";
|
||||
@@ -719,6 +722,30 @@ const parseGradle = (
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Read text by XML encoding rules, which also cover UTF-8 JSON. Maven reads a
|
||||
* POM in its declared encoding, and ISO-8859-1 POMs are common.
|
||||
*/
|
||||
const decodeMetadataText = (bytes: Uint8Array): string => {
|
||||
try {
|
||||
return decodeXmlPlistText(bytes, ["iso-8859-1"]);
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof XmlPlistTextDecodeError)) throw cause;
|
||||
if (cause.reason === "unsupported-encoding")
|
||||
throw unsupportedInput(MAVEN_GRADLE_METADATA_FORMAT, cause.message, {
|
||||
cause,
|
||||
undecodedContent: true,
|
||||
});
|
||||
throw malformedInput(
|
||||
MAVEN_GRADLE_METADATA_FORMAT,
|
||||
cause.reason === "encoding-mismatch"
|
||||
? "Selected file's XML encoding declaration disagrees with its bytes."
|
||||
: "Selected file is not valid text in its detected encoding.",
|
||||
{ cause },
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/** Decode one bounded Maven POM or Gradle module-metadata document without resolution. */
|
||||
export const parseMavenGradleMetadata = (
|
||||
bytes: Uint8Array,
|
||||
@@ -735,15 +762,7 @@ export const parseMavenGradleMetadata = (
|
||||
MAVEN_GRADLE_METADATA_FORMAT,
|
||||
"Selected file is empty.",
|
||||
);
|
||||
let source: string;
|
||||
try {
|
||||
source = decodeStrictUtf8(bytes, "strip-signature");
|
||||
} catch {
|
||||
throw malformedInput(
|
||||
MAVEN_GRADLE_METADATA_FORMAT,
|
||||
"Selected file is not valid UTF-8.",
|
||||
);
|
||||
}
|
||||
const source = decodeMetadataText(bytes);
|
||||
const trimmed = source.trimStart();
|
||||
if (trimmed.startsWith("{") || trimmed.startsWith("["))
|
||||
return parseGradle(source, artifact, limits);
|
||||
|
||||
@@ -598,6 +598,75 @@ cliTest("reads a Maven 4.1.0 model POM", async ({ cli }) => {
|
||||
);
|
||||
});
|
||||
|
||||
const encodedPom = (
|
||||
encoding: string,
|
||||
) => `<?xml version="1.0" encoding="${encoding}"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.apache.maven.its.mng2254</groupId>
|
||||
<artifactId>latin-1</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<properties><greeting>caf\u00e9 \u00c5</greeting></properties>
|
||||
</project>
|
||||
`;
|
||||
|
||||
cliTest.for([
|
||||
{
|
||||
label: "declared ISO-8859-1",
|
||||
bytes: Buffer.from(encodedPom("ISO-8859-1"), "latin1"),
|
||||
},
|
||||
{
|
||||
label: "BOM-marked UTF-16",
|
||||
bytes: Buffer.concat([
|
||||
Buffer.from([0xff, 0xfe]),
|
||||
Buffer.from(encodedPom("UTF-16"), "utf16le"),
|
||||
]),
|
||||
},
|
||||
])("reads a POM in $label", async ({ bytes }, { cli }) => {
|
||||
const root = await createTestTempDirectory("rea-maven-cli-encoding-");
|
||||
const path = join(root, "pom.xml");
|
||||
await writeFile(path, bytes);
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-maven-gradle-metadata", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode, result.stdout + result.stderr).toBe(0);
|
||||
const report = outputSchema.parse(result.json).normalized_result;
|
||||
expect(report.coordinates).toMatchObject({
|
||||
group_id: "org.apache.maven.its.mng2254",
|
||||
artifact_id: "latin-1",
|
||||
version: "0.1-SNAPSHOT",
|
||||
});
|
||||
expect(report.properties).toEqual([
|
||||
{
|
||||
name: "greeting",
|
||||
value: "caf\u00e9 \u00c5",
|
||||
value_credentials_redacted: false,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
cliTest(
|
||||
"refuses a POM in an XML encoding this reader does not decode",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-maven-cli-cp1252-");
|
||||
const path = join(root, "pom.xml");
|
||||
await writeFile(path, Buffer.from(encodedPom("windows-1252"), "latin1"));
|
||||
const result = await cli.run({
|
||||
arguments: ["inspect-maven-gradle-metadata", path, "--json"],
|
||||
});
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.json).toMatchObject({
|
||||
code: "unsupported_target",
|
||||
details: {
|
||||
failure_kind: "unsupported",
|
||||
reason: expect.stringContaining(
|
||||
"Unsupported XML encoding declaration: windows-1252.",
|
||||
),
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"fails closed on a file that is not Maven POM or Gradle module metadata",
|
||||
async ({ cli }) => {
|
||||
|
||||
Reference in New Issue
Block a user