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:
gensokyo-devandgensokyo-dev authored and Ysopwnable committed 2026-12-02 09:04:06 +08:00
1 parent 70fc6b745b
commit e2fce6bb6c
3 files changed
+102 -11

No files matched your search

+29 -10
View File
@@ -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);