mirror of
https://github.com/morluto/rea.git
synced 2026-11-30 10:27:42 +00:00
fix(tar): recognize gzip archives with long extra fields (#14861)
Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
1 parent
af5db9d4c8
commit
f1ca91efd3
6 files changed
+66
-10
No files matched your search
@@ -83,6 +83,7 @@ import {
|
||||
} from "../domain/apple/appleArchiveFormat.js";
|
||||
import { hasDiskImageSignature } from "../domain/apple/diskImageFormat.js";
|
||||
import { recognizeArtifactFormat } from "../domain/artifactFormatRecognition.js";
|
||||
import { gzipTarExtraFieldProbeBytes } from "../domain/tarFormat.js";
|
||||
import {
|
||||
ISO9660_DESCRIPTOR_OFFSET,
|
||||
ISO9660_SIGNATURE_BYTES,
|
||||
@@ -780,6 +781,17 @@ const detectArtifactFormat = async (
|
||||
initial.size,
|
||||
() => throwIfTargetResolutionCancelled(signal),
|
||||
);
|
||||
const gzipProbeBytes = Math.min(
|
||||
initial.size,
|
||||
gzipTarExtraFieldProbeBytes(magic),
|
||||
);
|
||||
const recognitionPrefix =
|
||||
gzipProbeBytes > magic.length
|
||||
? Buffer.concat([
|
||||
magic,
|
||||
await readCachedRange(magic.length, gzipProbeBytes - magic.length),
|
||||
])
|
||||
: magic;
|
||||
// The shared recognizer table decides; this route reads the trailer or
|
||||
// locates an ESP flash layout only when a recognizer asks for it, and
|
||||
// refuses a file whose header names a format it cannot open.
|
||||
@@ -787,7 +799,7 @@ const detectArtifactFormat = async (
|
||||
let espFlashLayout: boolean | undefined;
|
||||
let iso9660Descriptor: Uint8Array | undefined;
|
||||
const recognitionInput = () => ({
|
||||
prefix: magic,
|
||||
prefix: recognitionPrefix,
|
||||
fileSize: initial.size,
|
||||
suffix,
|
||||
espFlashLayout,
|
||||
|
||||
@@ -26,6 +26,7 @@ import {
|
||||
ISO9660_DESCRIPTOR_OFFSET,
|
||||
ISO9660_SIGNATURE_BYTES,
|
||||
} from "../domain/iso9660/iso9660Structures.js";
|
||||
import { gzipTarExtraFieldProbeBytes } from "../domain/tarFormat.js";
|
||||
import {
|
||||
createAsarHeaderSignatureProbe,
|
||||
type FinalAsarHeaderProbeObservation,
|
||||
@@ -292,6 +293,12 @@ export const createArtifactClassificationProbe = (
|
||||
}
|
||||
return prefix;
|
||||
};
|
||||
const signaturePrefixLimit = (): number =>
|
||||
Math.max(
|
||||
ARTIFACT_CLASSIFICATION_PREFIX_BYTES,
|
||||
retainedBytes < 12 ? 0 : gzipTarExtraFieldProbeBytes(blocks[0]!),
|
||||
);
|
||||
|
||||
return {
|
||||
get length(): number {
|
||||
return observedBytes;
|
||||
@@ -300,7 +307,7 @@ export const createArtifactClassificationProbe = (
|
||||
if (observedBytes < ARTIFACT_CLASSIFICATION_PREFIX_BYTES)
|
||||
return ARTIFACT_CLASSIFICATION_PREFIX_BYTES;
|
||||
return Math.max(
|
||||
ARTIFACT_CLASSIFICATION_PREFIX_BYTES,
|
||||
signaturePrefixLimit(),
|
||||
xml.state === "pending" ? MAX_XML_PLIST_PROBE_BYTES : 0,
|
||||
asar.observation.state === "pending" ? asar.limit : 0,
|
||||
);
|
||||
@@ -310,12 +317,13 @@ export const createArtifactClassificationProbe = (
|
||||
asar.write(bytes);
|
||||
let offset = 0;
|
||||
while (offset < bytes.length) {
|
||||
const limit =
|
||||
retainedBytes < ARTIFACT_CLASSIFICATION_PREFIX_BYTES
|
||||
? ARTIFACT_CLASSIFICATION_PREFIX_BYTES
|
||||
: xml.state === "pending"
|
||||
? MAX_XML_PLIST_PROBE_BYTES
|
||||
: retainedBytes;
|
||||
const limit = Math.max(
|
||||
signaturePrefixLimit(),
|
||||
xml.state === "pending" &&
|
||||
retainedBytes >= ARTIFACT_CLASSIFICATION_PREFIX_BYTES
|
||||
? MAX_XML_PLIST_PROBE_BYTES
|
||||
: 0,
|
||||
);
|
||||
if (retainedBytes >= limit) break;
|
||||
const selected = bytes.subarray(
|
||||
offset,
|
||||
|
||||
@@ -89,6 +89,17 @@ export const tarFixture = (members: readonly TarFixtureMember[]): Buffer => {
|
||||
export const gzipTarFixture = (members: readonly TarFixtureMember[]): Buffer =>
|
||||
gzipSync(tarFixture(members));
|
||||
|
||||
/** Add one RFC 1952 extra subfield to a gzipSync member. */
|
||||
export const withGzipExtraField = (gzip: Buffer, size: number): Buffer => {
|
||||
const header = Buffer.from(gzip.subarray(0, 10));
|
||||
header[3] = 4;
|
||||
const extra = Buffer.alloc(6 + size);
|
||||
extra.writeUInt16LE(4 + size);
|
||||
extra.write("RA", 2);
|
||||
extra.writeUInt16LE(size, 4);
|
||||
return Buffer.concat([header, extra, gzip.subarray(10)]);
|
||||
};
|
||||
|
||||
/** A small release-like tree: a `./` root, a directory, an executable and a symlink. */
|
||||
export const RELEASE_TAR_MEMBERS: readonly TarFixtureMember[] = [
|
||||
{ name: "./", type: "5", mode: 0o755 },
|
||||
|
||||
@@ -207,6 +207,16 @@ export const hasTarHeader = (prefix: Uint8Array): boolean =>
|
||||
const GZIP_TAR_PROBE_INPUT_BYTES = 2048;
|
||||
const GZIP_TAR_PROBE_OUTPUT_BYTES = 4 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* Prefix needed for a gzip member with an RFC 1952 FEXTRA field. XLEN is a
|
||||
* 16-bit byte count, so readers can extend acquisition only for this framing
|
||||
* without raising their ordinary format probe limit.
|
||||
*/
|
||||
export const gzipTarExtraFieldProbeBytes = (prefix: Uint8Array): number =>
|
||||
prefix.length >= 12 && hasGzipMagic(prefix) && (prefix[3]! & 0x4) !== 0
|
||||
? 12 + (prefix[10]! | (prefix[11]! << 8)) + GZIP_TAR_PROBE_INPUT_BYTES + 8
|
||||
: 0;
|
||||
|
||||
/**
|
||||
* Bytes of an RFC 1952 member header, including FEXTRA, FNAME, FCOMMENT and
|
||||
* FHCRC, or `undefined` when the prefix ends inside it.
|
||||
|
||||
@@ -6,6 +6,7 @@ import { cliTest } from "../support/cli/cliFixture.js";
|
||||
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
||||
import {
|
||||
gzipTarFixture,
|
||||
withGzipExtraField,
|
||||
RELEASE_TAR_MEMBERS,
|
||||
tarFixture,
|
||||
} from "../../src/artifacts/tar/tar.fixture.js";
|
||||
@@ -27,6 +28,11 @@ cliTest.for([
|
||||
bytes: gzipTarFixture(RELEASE_TAR_MEMBERS),
|
||||
compression: "gzip",
|
||||
},
|
||||
{
|
||||
name: "release-with-extra",
|
||||
bytes: withGzipExtraField(gzipTarFixture(RELEASE_TAR_MEMBERS), 9000),
|
||||
compression: "gzip",
|
||||
},
|
||||
] as const)(
|
||||
"inventories and extracts $name through the compiled CLI",
|
||||
async ({ name, bytes, compression }, { cli }) => {
|
||||
|
||||
@@ -70,6 +70,7 @@ import {
|
||||
import { devicetreeFixture } from "../../../src/domain/firmware/devicetree.fixture.js";
|
||||
import {
|
||||
gzipTarFixture,
|
||||
withGzipExtraField,
|
||||
RELEASE_TAR_MEMBERS,
|
||||
tarFixture,
|
||||
} from "../../../src/artifacts/tar/tar.fixture.js";
|
||||
@@ -961,13 +962,21 @@ it("classifies stored and gzip-compressed tar archives as tar whatever their nam
|
||||
const directory = await createTestTempDirectory("rea-tar-target-");
|
||||
const stored = join(directory, "release.tar");
|
||||
const gzip = join(directory, "package.unitypackage");
|
||||
const extra = join(directory, "package-with-extra");
|
||||
const plainGzip = join(directory, "notes.gz");
|
||||
await Promise.all([
|
||||
writeFile(stored, tarFixture(RELEASE_TAR_MEMBERS)),
|
||||
writeFile(gzip, gzipTarFixture(RELEASE_TAR_MEMBERS)),
|
||||
writeFile(plainGzip, gzipSync(Buffer.alloc(4096, 0x41))),
|
||||
writeFile(
|
||||
extra,
|
||||
withGzipExtraField(gzipTarFixture(RELEASE_TAR_MEMBERS), 65531),
|
||||
),
|
||||
writeFile(
|
||||
plainGzip,
|
||||
withGzipExtraField(gzipSync(Buffer.alloc(4096, 0x41)), 9000),
|
||||
),
|
||||
]);
|
||||
for (const path of [stored, gzip])
|
||||
for (const path of [stored, gzip, extra])
|
||||
expect(await parseBinaryTarget(path)).toMatchObject({
|
||||
ok: true,
|
||||
value: { kind: "archive", format: "tar" },
|
||||
|
||||
Reference in new issue
Block a user