The best way to control Claude Code, Codex and more from any computer or phone
fix(web): show TAR hard links in archive previews (#34329)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Lakshmi Tanmay committed
Nov 15, 2026 at 01:02 UTC
85e009fbc99f17ec61a1b401497710655291c68b
2 files changed
+83
-7
apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx
+61
-1
@@ -85,13 +85,20 @@ import type { OfficeWorkerResult } from "./officePreviewProtocol";
85
describe("bounded binary previews", () => {
86
it("preserves length-delimited local and global PAX attributes", async () => {
87
const encode = (value: string) => new TextEncoder().encode(value);
88
- const block = (name: string, content: Uint8Array, type = "0", size = content.length) => {
88
+ const block = (
89
+ name: string,
90
+ content: Uint8Array,
91
+ type = "0",
92
+ size = content.length,
93
+ linkname = "",
94
+ ) => {
95
const bytes = new Uint8Array(512 + Math.ceil(content.length / 512) * 512);
96
const write = (offset: number, value: string) => bytes.set(encode(value), offset);
97
write(0, name);
98
write(124, size.toString(8).padStart(11, "0") + "\0");
99
bytes.fill(32, 148, 156);
100
write(156, type);
101
+ write(157, linkname);
102
write(
103
148,
104
bytes
@@ -123,6 +130,15 @@ describe("bounded binary previews", () => {
130
block("Local", attributes({ size: "3", mtime: "-172800" }), "x"),
131
block("short.txt", encode("bye"), "0", 0),
132
block("again.txt", encode("again"), "0", 0),
133
+ block("Global", attributes({ size: "", mtime: "" }), "g"),
134
+ block("original.txt", encode("hello world")),
135
+ block("alias.txt", encode(""), "1", 0, "original.txt"),
136
+ block("chain.txt", encode(""), "1", 0, "alias.txt"),
137
+ block("Local", attributes({ linkpath: "original.txt" }), "x"),
138
+ block("pax-link.txt", encode(""), "1", 0, "wrong-target"),
139
+ block("LongLink", encode("original.txt\0"), "K"),
140
+ block("gnu-link.txt", encode(""), "1", 0, "wrong-target"),
141
+ block("original.txt", encode("replacement")),
142
new Uint8Array(1024),
143
];
144
const bytes = new Uint8Array(chunks.reduce((size, chunk) => size + chunk.length, 0));
@@ -154,6 +170,14 @@ describe("bounded binary previews", () => {
170
["world.txt", 5, "1969-12-31T00:00:00.000Z"],
171
["short.txt", 3, "1969-12-30T00:00:00.000Z"],
172
["again.txt", 5, "1969-12-31T00:00:00.000Z"],
173
+ ...[
174
+ "original.txt",
175
+ "alias.txt",
176
+ "chain.txt",
177
+ "pax-link.txt",
178
+ "gnu-link.txt",
179
+ "original.txt",
180
+ ].map((path) => [path, 11, "1970-01-01T00:00:00.000Z"]),
181
]);
182
const entry = await request({ type: "entry", path: "café\n9 size=1\nname.txt" });
183
if (entry.type !== "entry") throw new Error("Expected entry");
@@ -164,6 +188,42 @@ describe("bounded binary previews", () => {
188
if (reopened.type !== "entry") throw new Error("Expected entry");
189
expect(new TextDecoder().decode(reopened.bytes)).toBe("hello");
190
expect(reopened.bytes.buffer.byteLength).toBe(5);
191
+ for (const path of ["alias.txt", "chain.txt", "pax-link.txt", "gnu-link.txt"]) {
192
+ const linked = await request({ type: "entry", path });
193
+ if (linked.type !== "entry") throw new Error("Expected hard link entry");
194
+ expect(new TextDecoder().decode(linked.bytes)).toBe("hello world");
195
+ }
196
+ const unresolved = new Uint8Array(
197
+ await new Blob([
198
+ block("missing.txt", encode(""), "1", 0, "nowhere.txt"),
199
+ block("early.txt", encode(""), "1", 0, "later.txt"),
200
+ block("later.txt", encode("hello")),
201
+ block("folder/", encode(""), "5"),
202
+ block("to-folder.txt", encode(""), "1", 0, "folder/"),
203
+ block("replaced.txt", encode("stale")),
204
+ block("replaced.txt", encode(""), "2", 0, "elsewhere.txt"),
205
+ block("to-replaced.txt", encode(""), "1", 0, "replaced.txt"),
206
+ block("Local", attributes({ linkpath: "" }), "x"),
207
+ block("emptied.txt", encode(""), "1", 0, "later.txt"),
208
+ new Uint8Array(1024),
209
+ ]).arrayBuffer(),
210
+ );
211
+ const skipped = await request({
212
+ type: "open",
213
+ kind: "archive",
214
+ name: "links.tar",
215
+ bytes: unresolved.buffer,
216
+ });
217
+ if (skipped.type !== "archive") throw new Error("Expected archive");
218
+ expect(skipped.entries.map((entry) => entry.path)).toEqual([
219
+ "later.txt",
220
+ "folder/",
221
+ "replaced.txt",
222
+ ]);
223
+ expect(await request({ type: "entry", path: "replaced.txt" })).toEqual({
224
+ type: "error",
225
+ message: "This archive entry is unavailable.",
226
+ });
227
} finally {
228
worker.terminate();
229
}
apps/web/src/components/files/binaryPreview.worker.ts
+22
-6
@@ -52,6 +52,7 @@ function tarEntries(bytes: Uint8Array<ArrayBuffer>) {
52
return Number.parseInt(value || "0", 8);
53
};
54
let longPath: string | null = null;
55
+ let longLink: string | null = null;
56
const globalPax: Record<string, string> = {};
57
let localPax: Record<string, string> = {};
58
let offset = 0;
@@ -64,7 +65,7 @@ function tarEntries(bytes: Uint8Array<ArrayBuffer>) {
65
checksum += index >= 148 && index < 156 ? 32 : bytes[offset + index]!;
66
if (checksum !== number(offset + 148, 8)) throw new Error("The TAR header is damaged.");
67
const type = text(offset + 156, 1);
67
- const metadata = type === "L" || type === "x" || type === "g";
68
+ const metadata = type === "L" || type === "K" || type === "x" || type === "g";
69
const attributes: Record<string, string> = metadata ? {} : { ...globalPax, ...localPax };
70
const size = attributes.size ? Number(attributes.size) : number(offset + 124, 12);
71
if (attributes.size && !/^\d+$/.test(attributes.size))
@@ -75,7 +76,9 @@ function tarEntries(bytes: Uint8Array<ArrayBuffer>) {
76
: "";
77
const path =
78
attributes.path || longPath || [prefix, text(offset, 100)].filter(Boolean).join("/");
79
+ const linkpath = attributes.linkpath ?? longLink ?? text(offset + 157, 100);
80
if (!metadata) {
81
+ longLink = null;
82
longPath = null;
83
localPax = {};
84
}
@@ -89,6 +92,7 @@ function tarEntries(bytes: Uint8Array<ArrayBuffer>) {
92
throw new Error("An archive entry is too large or damaged.");
93
const content = bytes.subarray(offset + 512, offset + 512 + size);
94
if (type === "L") longPath = new TextDecoder().decode(content).replace(/\0.*$/s, "");
95
+ else if (type === "K") longLink = new TextDecoder().decode(content).replace(/\0.*$/s, "");
96
else if (type === "x" || type === "g") {
97
if (size > 65536) throw new Error("Archive metadata is too large.");
98
for (let cursor = 0; cursor < content.length;) {
@@ -111,26 +115,38 @@ function tarEntries(bytes: Uint8Array<ArrayBuffer>) {
115
if (equals < 1) throw new Error("The TAR metadata is damaged.");
116
const key = record.slice(0, equals);
117
const value = record.slice(equals + 1);
114
- if (["path", "size", "mtime"].includes(key)) {
118
+ if (["path", "linkpath", "size", "mtime"].includes(key)) {
119
if (type === "x") localPax[key] = value;
116
- else if (value) globalPax[key] = value;
120
+ else if (value || key === "linkpath") globalPax[key] = value;
121
else delete globalPax[key];
122
}
123
cursor = end;
124
}
121
- } else if (type === "" || type === "0" || type === "5") {
125
+ } else if (
126
+ type === "" ||
127
+ type === "0" ||
128
+ type === "5" ||
129
+ // A hard link shares an earlier member's bytes; one without that member is skipped.
130
+ (type === "1" && linkpath && files.has(linkpath))
131
+ ) {
132
if (attributes.mtime && !/^[+-]?\d+(?:\.\d+)?$/.test(attributes.mtime))
133
throw new Error("The TAR metadata is damaged.");
134
const modified =
135
(attributes.mtime ? Number(attributes.mtime) : number(offset + 136, 12)) * 1000;
136
const date = new Date(modified);
137
+ const linked = type === "1" ? files.get(linkpath) : undefined;
138
entries.push({
139
path,
129
- size,
140
+ size: linked?.length ?? size,
141
directory: type === "5",
142
modified: Number.isNaN(date.getTime()) ? null : date.toISOString(),
143
});
133
- if (type !== "5") files.set(path, content.slice());
144
+ if (type === "5") {
145
+ files.delete(path);
146
+ files.delete(path.replace(/\/+$/, ""));
147
+ } else files.set(path, linked ?? content.slice());
148
+ } else {
149
+ files.delete(path);
150
}
151
offset += 512 + Math.ceil(size / 512) * 512;
152
}