diff --git a/docs/guides/native/native-investigation.md b/docs/guides/native/native-investigation.md index 0b774a6d43..cef7e49f63 100644 --- a/docs/guides/native/native-investigation.md +++ b/docs/guides/native/native-investigation.md @@ -723,7 +723,11 @@ directly when their number facts need no further classification; explicitly identified JSON sources can have scalar roots. Unidentified primitive text keeps its existing conversion route, so OpenStep `123` remains a string. OpenStep, unsupported XML encodings such as UTF-32 and declared legacy encodings, and JSON -numbers requiring XML type facts retain the `plutil` route. Invalid UTF-8 and +numbers requiring XML type facts retain the `plutil` route. So does XML that +REA's XML reader rejects as not well-formed, which CoreFoundation can still +accept: a limitation names REA's reason, and a document `plutil` also rejects +stays refused with its diagnostic. Plist structure errors in well-formed XML +remain refusals without conversion. Invalid UTF-8 and encoding-declaration mismatches remain explicit failures. Direct-source provenance records `file` without claiming conversion; conversion limitations and invocations appear only when conversion actually ran. diff --git a/src/native/NativeMacOSProvider.ts b/src/native/NativeMacOSProvider.ts index d0a85c69d6..8a76229865 100644 --- a/src/native/NativeMacOSProvider.ts +++ b/src/native/NativeMacOSProvider.ts @@ -145,6 +145,7 @@ import { type ParsedPlist, } from "./parsers/plist.js"; import { XmlPlistTextDecodeError } from "../domain/propertyListXmlText.js"; +import { ParseFailureError } from "../domain/parseFailure.js"; import { architectureLocations, inspectNativeMacho, @@ -1289,6 +1290,14 @@ class NativeMacOSClient implements AnalysisClient { parsed.error.reason === "unsupported-encoding" ) return undefined; + const refusal = parsed.ok ? undefined : xmlSyntaxRefusal(parsed.error); + if (refusal !== undefined) + return await this.#inspectConvertedPlist( + fileInput, + classified, + format, + refusal, + ); } else { const json = readPlistJsonSource(source.value); if ( @@ -1325,6 +1334,7 @@ class NativeMacOSClient implements AnalysisClient { fileInput: NativePlistReadContext, classified: NativeCommandCapture, format: z.infer["format"], + sourceRefusal?: string, ): Promise> { const { path } = fileInput.selection; const { signal, timeoutMs } = fileInput; @@ -1374,7 +1384,16 @@ class NativeMacOSClient implements AnalysisClient { ].map((item) => invocation(item, path, "$PLIST")); return ok( nativePlistObservation({ - parsed: parsed.value, + parsed: + sourceRefusal === undefined + ? parsed.value + : { + ...parsed.value, + limitations: [ + `REA's XML reader refused the source (${sourceRefusal}), but plutil read it, so the value comes from plutil's conversion rather than the original text.`, + ...parsed.value.limitations, + ], + }, format, path, resourceRoot: this.bundleResourceRoot, @@ -2244,6 +2263,20 @@ const nativePlistDecodeFailure = ( ); }; +/** + * The reason REA's XML reader refused a plist's syntax. CoreFoundation accepts + * some documents that strict XML rejects (#12234), so plutil decides those. + * Plist structure errors and refused document types are not syntax refusals. + */ +const xmlSyntaxRefusal = (error: unknown): string | undefined => { + const cause = error instanceof AnalysisOutputError ? error.cause : undefined; + return cause instanceof ParseFailureError && + cause.failure.format === "xml" && + cause.kind === "malformed" + ? cause.message + : undefined; +}; + const plutilDiagnostic = ( capture: Pick, path: string, diff --git a/tests/boundary/providers/native/nativePlistTextSource.test.ts b/tests/boundary/providers/native/nativePlistTextSource.test.ts index 2a67cb636e..4be3ccd9fb 100644 --- a/tests/boundary/providers/native/nativePlistTextSource.test.ts +++ b/tests/boundary/providers/native/nativePlistTextSource.test.ts @@ -141,6 +141,32 @@ it.for([ value: { name: "UTF32 control" }, conversions: [{ format: "json", output: '{"name":"UTF32 control"}' }], }, + { + name: "malformed XML comment that plutil reads", + bytes: Buffer.from( + 'namex', + ), + description: "XML 1.0 document text", + format: "xml", + value: { name: "x" }, + limitations: [ + "REA's XML reader refused the source (XML is not well-formed: 1:31: malformed comment.), but plutil read it, so the value comes from plutil's conversion rather than the original text.", + ], + conversions: [{ format: "json", output: '{"name":"x"}' }], + }, + { + name: "XML control character that plutil reads", + bytes: Buffer.from( + "name", + ), + description: "XML 1.0 document text", + format: "xml", + value: { name: "" }, + limitations: [ + "REA's XML reader refused the source (XML is not well-formed: 1:37: disallowed character.), but plutil read it, so the value comes from plutil's conversion rather than the original text.", + ], + conversions: [{ format: "json", output: '{"name":"\\u0001"}' }], + }, ])("owns $name interpretation before optional conversion", async (fixture) => { const root = await createTestTempDirectory("rea-plist-text-source-"); const path = join(root, "selected.plist"); @@ -214,3 +240,68 @@ it.for([ ); expect(Buffer.compare(await readFile(path), fixture.bytes)).toBe(0); }); + +const inspectRefusal = async ( + bytes: string, + conversions: readonly string[], +): Promise<{ readonly error: unknown; readonly tools: readonly string[] }> => { + const root = await createTestTempDirectory("rea-plist-text-refusal-"); + const path = join(root, "selected.plist"); + await writeFile(path, bytes); + const replay = new NativeFixtureRunner({ file: "XML 1.0 document text" }); + const tools: string[] = []; + const client = new NativeMacOSProvider( + {}, + { + async run(tool, args) { + tools.push(tool === "plutil" ? `plutil ${args[0]} ${args[1]}` : tool); + const result = await replay.run(tool, args); + if ( + tool !== "plutil" || + !result.ok || + !conversions.includes(args[1] ?? "") + ) + return result; + return ok({ + ...result.value, + stdout: "", + stdoutBytes: 0, + stderr: `${path}: Property List error`, + exitCode: 1, + }); + }, + }, + "darwin", + ).createClient(nativeMachoTarget(join(root, "target"))); + onTestFinished(async () => { + await client.close(); + }); + const inspected = await client.execute("inspect_plist", { path }); + return { error: inspected.ok ? undefined : inspected.error, tools }; +}; + +it("refuses malformed XML that plutil also rejects (#12234)", async () => { + const { error, tools } = await inspectRefusal( + '', + ["json", "xml1"], + ); + expect(error).toMatchObject({ + issues: [{ message: expect.stringMatching(/plutil could not decode/u) }], + }); + expect(tools).toEqual([ + "file", + "plutil -convert json", + "plutil -convert xml1", + ]); +}); + +it("keeps refusing a well-formed XML plist with an invalid structure", async () => { + const { error, tools } = await inspectRefusal( + "", + [], + ); + expect(error).toMatchObject({ + issues: [{ message: expect.stringMatching(/Invalid PLIST tag unknown/u) }], + }); + expect(tools).toEqual(["file"]); +}); diff --git a/tests/boundary/providers/native/nativeProvider.test.ts b/tests/boundary/providers/native/nativeProvider.test.ts index 41fcf32e24..e1108a9899 100644 --- a/tests/boundary/providers/native/nativeProvider.test.ts +++ b/tests/boundary/providers/native/nativeProvider.test.ts @@ -1538,7 +1538,7 @@ describe("native plist defaults for iOS-style bundles", () => { }); }); -it("rejects malformed selected plist bytes before mocked conversion can invent facts", async () => { +it("rejects malformed selected plist bytes that plutil also rejects instead of inventing facts", async () => { directory = await createTestTempDirectory("rea-native-malformed-plist-"); const app = join(directory, "Malformed.app"); const path = join(app, "Contents/Info.plist"); @@ -1547,9 +1547,19 @@ it("rejects malformed selected plist bytes before mocked conversion can invent f const tools: string[] = []; const replay = new FixtureRunner(); const runner: NativeCommandRunner = { - run(tool, arguments_, options) { + async run(tool, arguments_, options) { tools.push(tool); - return replay.run(tool, arguments_, options); + const result = await replay.run(tool, arguments_, options); + // REA's XML reader refuses these bytes, so plutil decides (#12234); + // like the real tool, it rejects them rather than printing a plist. + if (tool !== "plutil" || !result.ok) return result; + return ok({ + ...result.value, + stdout: "", + stdoutBytes: 0, + stderr: `${path}: Property List error`, + exitCode: 1, + }); }, }; const client = new NativeMacOSProvider({}, runner, "darwin").createClient( @@ -1565,7 +1575,14 @@ it("rejects malformed selected plist bytes before mocked conversion can invent f error: { _tag: tag, operation: "inspect_plist" }, }); } - expect(tools).toEqual(["file", "file"]); + expect(tools).toEqual([ + "file", + "plutil", + "plutil", + "file", + "plutil", + "plutil", + ]); expect(await readFile(path, "utf8")).toBe("fixture"); });