mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
fix(artifacts): preserve ASAR unpacked companion path identity (#11749)
* fix(artifacts): preserve ASAR unpacked path identity * test(artifacts): use the current ASAR resource entry * test(artifacts): assert ASAR unavailable content facts --------- Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
1 parent
de9da86333
commit
29a3f3c8dd
2 files changed
+105
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { appendCallerPath } from "../filesystem/callerPath.js";
|
||||
import { localPathStringSchema } from "../domain/localPath.js";
|
||||
import type { AsarHeaderAdmission } from "../config/asarHeaderAdmission.js";
|
||||
import { artifactIntegrityDetails } from "../domain/artifactIntegrity.js";
|
||||
import { constants, createWriteStream, type Stats } from "node:fs";
|
||||
@@ -497,6 +498,14 @@ export class AsarArtifactReader implements ArtifactReader {
|
||||
entry: ArtifactEntry,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Readable> {
|
||||
// Header names retain UTF-16 identity; filesystem encoding may replace
|
||||
// unpaired surrogates and select a different companion. Packed members
|
||||
// need no filesystem name and remain readable under their original names.
|
||||
if (!localPathStringSchema.safeParse(entry.adapterKey).success)
|
||||
throw new ArtifactEntryContentUnavailable(
|
||||
`ASAR unpacked member path cannot be represented without replacement: ${JSON.stringify(entry.path)}`,
|
||||
"ASAR unpacked member path cannot be represented without replacement on the local filesystem; companion bytes were not read and no content hash or child artifact was produced.",
|
||||
);
|
||||
const unpackedRoot = `${this.options.selectedPath ?? this.path}.unpacked`;
|
||||
let handle: SharedAsarFileHandle | undefined;
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { expect, it } from "vitest";
|
||||
import { AsarArtifactReader } from "../../../src/artifacts/AsarArtifactReader.js";
|
||||
import {
|
||||
ArtifactEntryContentUnavailable,
|
||||
type ArtifactEntry,
|
||||
} from "../../../src/resources/index.js";
|
||||
import { inventoryArtifact } from "../../fixtures/artifactInventory.js";
|
||||
import { asarHeaderBytes } from "../../fixtures/asarHeader.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
it.each([
|
||||
"\ud800.js",
|
||||
"\udc00.js",
|
||||
"dir\ud800/member.js",
|
||||
"dir\udc00/member.js",
|
||||
])("does not read a replacement-character companion for %j", async (name) => {
|
||||
const root = await createTestTempDirectory("rea-asar-unpacked-identity-");
|
||||
const archive = join(root, "app.asar");
|
||||
const packed = Buffer.from("exact packed member");
|
||||
const companion = Buffer.from("different filesystem identity");
|
||||
const replacement = name.replace(/[\ud800-\udfff]/gu, "\ufffd");
|
||||
let member: object = { size: companion.length, unpacked: true };
|
||||
for (const part of name.split("/").reverse())
|
||||
member = { files: { [part]: member } };
|
||||
const files = (member as { files: Record<string, object> }).files;
|
||||
let replacementMember: object = { size: companion.length, unpacked: true };
|
||||
for (const part of replacement.split("/").reverse())
|
||||
replacementMember = { files: { [part]: replacementMember } };
|
||||
Object.assign(files, (replacementMember as { files: object }).files);
|
||||
files.packed = {
|
||||
files: { [name.split("/").at(-1)!]: { size: packed.length, offset: "0" } },
|
||||
};
|
||||
files["🐍.js"] = { size: companion.length, unpacked: true };
|
||||
await writeFile(
|
||||
archive,
|
||||
Buffer.concat([asarHeaderBytes(JSON.stringify({ files })), packed]),
|
||||
);
|
||||
await mkdir(
|
||||
join(`${archive}.unpacked`, ...replacement.split("/").slice(0, -1)),
|
||||
{ recursive: true },
|
||||
);
|
||||
await writeFile(join(`${archive}.unpacked`, replacement), companion);
|
||||
await writeFile(join(`${archive}.unpacked`, "🐍.js"), companion);
|
||||
const reader = new AsarArtifactReader(archive);
|
||||
try {
|
||||
const entries: ArtifactEntry[] = [];
|
||||
for await (const entry of reader.entries()) entries.push(entry);
|
||||
const malformed = entries.find((entry) => entry.path === name)!;
|
||||
expect(malformed.unpacked).toBe(true);
|
||||
await expect(reader.open(malformed)).rejects.toBeInstanceOf(
|
||||
ArtifactEntryContentUnavailable,
|
||||
);
|
||||
const packedEntry = entries.find(
|
||||
(entry) => entry.path.startsWith("packed/") && entry.kind === "file",
|
||||
)!;
|
||||
const read = async (entry: typeof packedEntry) => {
|
||||
const chunks: Buffer[] = [];
|
||||
for await (const chunk of await reader.open(entry))
|
||||
chunks.push(Buffer.from(chunk as Uint8Array));
|
||||
return Buffer.concat(chunks);
|
||||
};
|
||||
expect((await read(packedEntry)).equals(packed)).toBe(true);
|
||||
expect(
|
||||
(await read(entries.find((entry) => entry.path === replacement)!)).equals(
|
||||
companion,
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
(await read(entries.find((entry) => entry.path === "🐍.js")!)).equals(
|
||||
companion,
|
||||
),
|
||||
).toBe(true);
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
const inventory = await inventoryArtifact(archive);
|
||||
const occurrence = inventory.occurrences.find(
|
||||
(entry) => entry.logical_path === name,
|
||||
)!;
|
||||
expect(occurrence).toMatchObject({ hash_status: "unavailable" });
|
||||
expect(occurrence.artifact_id).toBeNull();
|
||||
expect(occurrence.limitations.length).toBeGreaterThan(0);
|
||||
const packedOccurrence = inventory.occurrences.find(
|
||||
(entry) =>
|
||||
entry.logical_path.startsWith("packed/") &&
|
||||
entry.hash_status === "verified",
|
||||
)!;
|
||||
expect(
|
||||
inventory.nodes.find(
|
||||
(node) => node.artifact_id === packedOccurrence.artifact_id,
|
||||
)?.sha256,
|
||||
).toBe(createHash("sha256").update(packed).digest("hex"));
|
||||
});
|
||||
Reference in new issue
Block a user