GeezerVison is showing github.com — fix(web): skip unresolvable TAR hard links instead of failing the arc… · pingdotgg/t3code@2075331
File treeExpand file treeCollapse file tree Open diff view settings apps/web/src/components/files
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx +29-34Lines changed: 29 additions & 34 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -193,42 +193,37 @@ describe("bounded binary previews", () => { | 193 | 193 | if (linked.type !== "entry") throw new Error("Expected hard link entry"); | 194 | 194 | expect(new TextDecoder().decode(linked.bytes)).toBe("hello world"); | 195 | 195 | } | 196 | | - for (const chunks of [ | 197 | | - [block("alias.txt", encode(""), "1", 0, "missing.txt")], | 198 | | - [block("self.txt", encode(""), "1", 0, "self.txt")], | 199 | | - [block("alias.txt", encode(""), "1", 0, "later.txt"), block("later.txt", encode("hello"))], | 200 | | - [block("a.txt", encode(""), "1", 0, "b.txt"), block("b.txt", encode(""), "1", 0, "a.txt")], | 201 | | - [block("folder/", encode(""), "5"), block("alias.txt", encode(""), "1", 0, "folder/")], | 202 | | - [ | 203 | | - block("original.txt", encode("hello")), | 204 | | - block("Local", attributes({ linkpath: "" }), "x"), | 205 | | - block("alias.txt", encode(""), "1", 0, "original.txt"), | 206 | | - ], | 207 | | - [ | 208 | | - block("original.txt", encode("hello")), | 209 | | - block("LongLink", encode("original.txt\0"), "K"), | 210 | | - block("Global", attributes({ linkpath: "" }), "g"), | 211 | | - block("alias.txt", encode(""), "1", 0, "original.txt"), | 212 | | - ], | 213 | | - [ | | 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/"), | 214 | 203 | block("replaced.txt", encode("stale")), | 215 | 204 | block("replaced.txt", encode(""), "2", 0, "elsewhere.txt"), | 216 | | - block("alias.txt", encode(""), "1", 0, "replaced.txt"), | 217 | | - ], | 218 | | - [ | 219 | | - block("folder", encode("stale")), | 220 | | - block("folder/", encode(""), "5"), | 221 | | - block("alias.txt", encode(""), "1", 0, "folder"), | 222 | | - ], | 223 | | - ]) { | 224 | | - const archive = new Uint8Array(await new Blob(chunks).arrayBuffer()); | 225 | | - expect( | 226 | | - await request({ type: "open", kind: "archive", name: "link.tar", bytes: archive.buffer }), | 227 | | - ).toEqual({ | 228 | | - type: "error", | 229 | | - message: "A TAR hard link does not refer to an earlier file in this archive.", | 230 | | - }); | 231 | | - } | | 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 | + }); | 232 | 227 | } finally { | 233 | 228 | worker.terminate(); | 234 | 229 | } | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/files/binaryPreview.worker.ts +8-5Lines changed: 8 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -122,16 +122,19 @@ function tarEntries(bytes: Uint8Array<ArrayBuffer>) { | 122 | 122 | } | 123 | 123 | cursor = end; | 124 | 124 | } | 125 | | - } else if (type === "" || type === "0" || type === "1" || 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 | + ) { | 126 | 132 | if (attributes.mtime && !/^[+-]?\d+(?:\.\d+)?$/.test(attributes.mtime)) | 127 | 133 | throw new Error("The TAR metadata is damaged."); | 128 | 134 | const modified = | 129 | 135 | (attributes.mtime ? Number(attributes.mtime) : number(offset + 136, 12)) * 1000; | 130 | 136 | const date = new Date(modified); | 131 | | - // TAR hard links name an earlier member; sharing its bytes also resolves chains in one pass. | 132 | | - const linked = type === "1" && linkpath ? files.get(linkpath) : undefined; | 133 | | - if (type === "1" && !linked) | 134 | | - throw new Error("A TAR hard link does not refer to an earlier file in this archive."); | | 137 | + const linked = type === "1" ? files.get(linkpath) : undefined; | 135 | 138 | entries.push({ | 136 | 139 | path, | 137 | 140 | size: linked?.length ?? size, | |
|