renderer: trailing / and /. on a link to a file fail, as on Linux
Astra task 4 review, round 1. 1. Both link resolvers (resolveRendererLink in internal/release and Tree.resolve in scripts/build-renderer-tree.py) dropped empty and "." parts before checking that each step is a directory. A link "f/" or "f/." to a regular file therefore resolved, though Linux refuses both with ENOTDIR. The build could pin such a link as the browser, and the verifier would pass it. Now every part after the first, empty and "." included, needs a directory before it, as in Linux path resolution. This also covers "f/..", which was already refused, and "f//". Directory targets with a trailing "/" or "/." still resolve. 2. The script tests loaded the script with spec_from_file_location / exec_module. That reads a cached .pyc even under -B, so a same-size edit within the cached mtime second ran stale code. The prelude now compiles the source bytes and execs them into a fresh module. Tests CGO_ENABLED=0 go test ./internal/release/ -run 'TestParseRendererManifest|TestVerifyRenderer|TestRendererTree|TestRendererHealth' -count=1 CGO_ENABLED=0 go test ./internal/release/ -count=1 ok 12.9s darwin/arm64 runner build and vet gates; rastrillo budget size ok New cases: - parser refusals: f/, f/., f/.., f//, a/ and a/. with a -> f, and b -> a with a -> f/; - parser acceptance: ../, ./../., and up/lib/ with up -> ..; - writer refusals: f/, f/., ld/ with ld -> f, and the browser's own link ending in /.; - verifier: four pinned MANIFESTs with such links are mismatch; - contract test: links ../lib/ and lib/. to directories build and verify. RED - Parser: six refusal cases were accepted. f/.. was already refused. - Writer: four cases built a tar and printed a pin. - Verifier: "[[usr/bad lib-extra/]]: <nil>". - Stale bytecode: compile a .pyc for the source, make the same-size edit 0o644 -> 0o664, restore the mtime, and run TestVerifyRendererAcceptsTheBuiltTree. It passed: "SURVIVED (stale bytecode ran)". GREEN: all of the above pass, and the stale-bytecode run is now caught: "MANIFEST: is group or world writable (0664)". Mutations (each restored byte-exact) - Go: dir-before-each-part check removed -> the 8 directory cases. - Go: empty and "." parts exempted (the old bug) -> the 6 slash cases. - Go: climb-out -> link_through_a_link_out (and a panic). - Go: names nothing -> link_names_nothing. - Go: intermediate links followed -> link_loop, link_through_a_link_out. - Go: links resolved -> every link case. - py: dir-before-each-part check removed -> the 4 writer cases. - py: empty and "." parts exempted -> the 4 writer cases. - py: climb-out -> escaping_link, escaping_through_a_link, absolute_link_climbing_out. - py: link following made lexical -> escaping_through_a_link. - py: names nothing -> absolute_link_outside_the_tree. - py: --tree MANIFEST chmod removed -> AcceptsTheBuiltTree. - py: --tree chmod 0o664 (same size), plain -> AcceptsTheBuiltTree. - py: the same, with a matching stale .pyc and mtime -> AcceptsTheBuiltTree. It survived before the prelude fix.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+121
−66
internal/release/renderer_manifest_test.go+57 −44internal/release/renderer_tree_test.go+19 −4internal/release/renderer_verify.go+11 −12internal/release/renderer_verify_linux_test.go+25 −1scripts/build-renderer-tree.py+9 −5
diff --git a/internal/release/renderer_manifest_test.go b/internal/release/renderer_manifest_test.go| index 0dbe2bdd..81dc9f2a 100644 |
| --- a/internal/release/renderer_manifest_test.go |
| +++ b/internal/release/renderer_manifest_test.go |
| @@ -26,6 +26,9 @@ func TestParseRendererManifest(t *testing.T) { |
| "file usr/lib-extra " + manifestSum + " 0644\n" + |
| "file usr/lib/browser " + manifestSum + " 0755\n" + |
| "link usr/lib/up ..\n" + |
| + "link usr/lib/up2 ../\n" + |
| + "link usr/lib/up3 ./../.\n" + |
| + "link usr/lib/via-up up/lib/\n" + |
| "dir usr/share\n" + |
| "file usr/share/f.conf " + manifestSum + " 0644\n" |
| got, err := parseRendererManifest([]byte(text)) |
| @@ -44,6 +47,9 @@ func TestParseRendererManifest(t *testing.T) { |
| "usr/lib-extra": {kind: "file", sha256: manifestSum, mode: 0o644}, |
| "usr/lib/browser": {kind: "file", sha256: manifestSum, mode: 0o755}, |
| "usr/lib/up": {kind: "link", target: ".."}, |
| + "usr/lib/up2": {kind: "link", target: "../"}, |
| + "usr/lib/up3": {kind: "link", target: "./../."}, |
| + "usr/lib/via-up": {kind: "link", target: "up/lib/"}, |
| "usr/share": {kind: "dir"}, |
| "usr/share/f.conf": {kind: "file", sha256: manifestSum, mode: 0o644}, |
| } |
| @@ -60,50 +66,57 @@ func TestParseRendererManifest(t *testing.T) { |
| func TestParseRendererManifestRefuses(t *testing.T) { |
| file := func(path, mode string) string { return "file " + path + " " + manifestSum + " " + mode + "\n" } |
| for name, c := range map[string]struct{ text, why string }{ |
| - "unknown kind": {"dir usr\nfifo usr/p\n", "unknown kind"}, |
| - "no kind": {"usr\n", "not an entry"}, |
| - "dir with a field more": {"dir usr x\n", "wrong number of fields"}, |
| - "file with a field less": {"dir usr\nfile usr/a " + manifestSum + "\n", "wrong number of fields"}, |
| - "file with a field more": {"dir usr\nfile usr/a " + manifestSum + " 0644 x\n", "wrong number of fields"}, |
| - "empty link target": {"dir usr\nlink usr/a \n", "not a relative link target"}, |
| - "link with no target": {"dir usr\nlink usr/a\n", "wrong number of fields"}, |
| - "two spaces": {"dir usr\nfile usr/a " + manifestSum + " 0644\n", "wrong number of fields"}, |
| - "upper-case hash": {"dir usr\n" + file("usr/a", "0644")[:len("file usr/a ")] + strings.ToUpper(manifestSum) + " 0644\n", "not a SHA-256"}, |
| - "short hash": {"dir usr\nfile usr/a abcd 0644\n", "not a SHA-256"}, |
| - "three-digit mode": {"dir usr\n" + file("usr/a", "644"), "not a mode"}, |
| - "group-writable mode": {"dir usr\n" + file("usr/a", "0664"), "not a mode"}, |
| - "world-writable mode": {"dir usr\n" + file("usr/a", "0646"), "not a mode"}, |
| - "setuid mode": {"dir usr\n" + file("usr/a", "4755"), "not a mode"}, |
| - "not octal": {"dir usr\n" + file("usr/a", "0855"), "not a mode"}, |
| - "duplicate": {"dir usr\ndir usr\n", "not sorted"}, |
| - "unsorted": {"dir usr\ndir etc\n", "not sorted"}, |
| - "absolute path": {"dir /usr\n", "not a clean relative path"}, |
| - "dot-dot path": {"dir usr\ndir usr/..\n", "not a clean relative path"}, |
| - "dot-dot inside": {"dir usr\ndir usr/../etc\n", "not a clean relative path"}, |
| - "dot path": {"dir usr\ndir usr/.\n", "not a clean relative path"}, |
| - "leading dot": {"dir ./usr\n", "not a clean relative path"}, |
| - "empty component": {"dir usr\ndir usr//lib\n", "not a clean relative path"}, |
| - "trailing slash": {"dir usr/\n", "not a clean relative path"}, |
| - "empty path": {"dir \n", "not a clean relative path"}, |
| - "no-break space in a path": {"dir usr\u00a0lib\n", "not a clean relative path"}, |
| - "tab in a path": {"dir usr\tlib\n", "not a clean relative path"}, |
| - "control character": {"dir usr\x01\n", "not a clean relative path"}, |
| - "delete character": {"dir usr\x7f\n", "not a clean relative path"}, |
| - "not UTF-8": {"dir usr\xff\n", "not UTF-8"}, |
| - "carriage return": {"dir usr\r\n", "not a clean relative path"}, |
| - "no final newline": {"dir usr", "does not end in a newline"}, |
| - "empty line": {"dir usr\n\ndir usr/lib\n", "not an entry"}, |
| - "lists itself": {"file MANIFEST " + manifestSum + " 0644\n", "not a clean relative path"}, |
| - "parent not listed": {"dir usr/lib\n", "its directory is not listed"}, |
| - "parent is a file": {file("usr", "0644") + "dir usr/lib\n", "its directory is not listed"}, |
| - "parent is a link": {"dir etc\nlink usr etc\ndir usr/lib\n", "its directory is not listed"}, |
| - "absolute link target": {"dir usr\nlink usr/a /usr\n", "not a relative link target"}, |
| - "link climbing out": {"dir usr\nlink usr/a ../../etc\n", "climbs out of the tree"}, |
| - "link names nothing": {"dir usr\nlink usr/a nothing\n", "names nothing in the tree"}, |
| - "link through a file": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/x\n", "is not a directory"}, |
| - "link through a link out": {"dir usr\nlink usr/out up/../..\nlink usr/up ..\n", "climbs out of the tree"}, |
| - "link loop": {"dir usr\nlink usr/a b\nlink usr/b a\n", "too many links"}, |
| - "link target with a tab": {"dir usr\nlink usr/a \tusr\n", "not a relative link target"}, |
| + "unknown kind": {"dir usr\nfifo usr/p\n", "unknown kind"}, |
| + "no kind": {"usr\n", "not an entry"}, |
| + "dir with a field more": {"dir usr x\n", "wrong number of fields"}, |
| + "file with a field less": {"dir usr\nfile usr/a " + manifestSum + "\n", "wrong number of fields"}, |
| + "file with a field more": {"dir usr\nfile usr/a " + manifestSum + " 0644 x\n", "wrong number of fields"}, |
| + "empty link target": {"dir usr\nlink usr/a \n", "not a relative link target"}, |
| + "link with no target": {"dir usr\nlink usr/a\n", "wrong number of fields"}, |
| + "two spaces": {"dir usr\nfile usr/a " + manifestSum + " 0644\n", "wrong number of fields"}, |
| + "upper-case hash": {"dir usr\n" + file("usr/a", "0644")[:len("file usr/a ")] + strings.ToUpper(manifestSum) + " 0644\n", "not a SHA-256"}, |
| + "short hash": {"dir usr\nfile usr/a abcd 0644\n", "not a SHA-256"}, |
| + "three-digit mode": {"dir usr\n" + file("usr/a", "644"), "not a mode"}, |
| + "group-writable mode": {"dir usr\n" + file("usr/a", "0664"), "not a mode"}, |
| + "world-writable mode": {"dir usr\n" + file("usr/a", "0646"), "not a mode"}, |
| + "setuid mode": {"dir usr\n" + file("usr/a", "4755"), "not a mode"}, |
| + "not octal": {"dir usr\n" + file("usr/a", "0855"), "not a mode"}, |
| + "duplicate": {"dir usr\ndir usr\n", "not sorted"}, |
| + "unsorted": {"dir usr\ndir etc\n", "not sorted"}, |
| + "absolute path": {"dir /usr\n", "not a clean relative path"}, |
| + "dot-dot path": {"dir usr\ndir usr/..\n", "not a clean relative path"}, |
| + "dot-dot inside": {"dir usr\ndir usr/../etc\n", "not a clean relative path"}, |
| + "dot path": {"dir usr\ndir usr/.\n", "not a clean relative path"}, |
| + "leading dot": {"dir ./usr\n", "not a clean relative path"}, |
| + "empty component": {"dir usr\ndir usr//lib\n", "not a clean relative path"}, |
| + "trailing slash": {"dir usr/\n", "not a clean relative path"}, |
| + "empty path": {"dir \n", "not a clean relative path"}, |
| + "no-break space in a path": {"dir usr\u00a0lib\n", "not a clean relative path"}, |
| + "tab in a path": {"dir usr\tlib\n", "not a clean relative path"}, |
| + "control character": {"dir usr\x01\n", "not a clean relative path"}, |
| + "delete character": {"dir usr\x7f\n", "not a clean relative path"}, |
| + "not UTF-8": {"dir usr\xff\n", "not UTF-8"}, |
| + "carriage return": {"dir usr\r\n", "not a clean relative path"}, |
| + "no final newline": {"dir usr", "does not end in a newline"}, |
| + "empty line": {"dir usr\n\ndir usr/lib\n", "not an entry"}, |
| + "lists itself": {"file MANIFEST " + manifestSum + " 0644\n", "not a clean relative path"}, |
| + "parent not listed": {"dir usr/lib\n", "its directory is not listed"}, |
| + "parent is a file": {file("usr", "0644") + "dir usr/lib\n", "its directory is not listed"}, |
| + "parent is a link": {"dir etc\nlink usr etc\ndir usr/lib\n", "its directory is not listed"}, |
| + "absolute link target": {"dir usr\nlink usr/a /usr\n", "not a relative link target"}, |
| + "link climbing out": {"dir usr\nlink usr/a ../../etc\n", "climbs out of the tree"}, |
| + "link names nothing": {"dir usr\nlink usr/a nothing\n", "names nothing in the tree"}, |
| + "link through a file": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/x\n", "is not a directory"}, |
| + "link to a file with a slash": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/\n", "is not a directory"}, |
| + "link to a file with /.": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/.\n", "is not a directory"}, |
| + "link to a file with /..": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f/..\n", "is not a directory"}, |
| + "link to a file with //": {"dir usr\n" + file("usr/f", "0644") + "link usr/l f//\n", "is not a directory"}, |
| + "slash through another link": {"dir usr\nlink usr/a f\nlink usr/b a/\n" + file("usr/f", "0644"), "is not a directory"}, |
| + "slash-dot through another link": {"dir usr\nlink usr/a f\nlink usr/b a/.\n" + file("usr/f", "0644"), "is not a directory"}, |
| + "link to a slash link to a file": {"dir usr\nlink usr/a f/\nlink usr/b a\n" + file("usr/f", "0644"), "is not a directory"}, |
| + "link through a link out": {"dir usr\nlink usr/out up/../..\nlink usr/up ..\n", "climbs out of the tree"}, |
| + "link loop": {"dir usr\nlink usr/a b\nlink usr/b a\n", "too many links"}, |
| + "link target with a tab": {"dir usr\nlink usr/a \tusr\n", "not a relative link target"}, |
| } { |
| t.Run(name, func(t *testing.T) { |
| got, err := parseRendererManifest([]byte(c.text)) |
diff --git a/internal/release/renderer_tree_test.go b/internal/release/renderer_tree_test.go| index ad542fb3..b05db76d 100644 |
| --- a/internal/release/renderer_tree_test.go |
| +++ b/internal/release/renderer_tree_test.go |
| @@ -166,13 +166,19 @@ func buildRendererTree(t *testing.T, pin string, urls map[string]string, out str |
| t.Fatal(err) |
| } |
| const prelude = ` |
| -import importlib.util, json, os, sys, time |
| +import json, os, sys, time, types |
| if float(sys.argv[2]): |
| time.time = lambda: float(sys.argv[2]) |
| os.umask(0o077) |
| -spec = importlib.util.spec_from_file_location("tree", "../../scripts/build-renderer-tree.py") |
| -m = importlib.util.module_from_spec(spec) |
| -spec.loader.exec_module(m) |
| +# The script's source bytes, compiled here: never a cached .pyc, which |
| +# could be stale (same size, same mtime second) after an edit. |
| +path = "../../scripts/build-renderer-tree.py" |
| +with open(path, "rb") as f: |
| + code = compile(f.read(), path, "exec", dont_inherit=True) |
| +m = types.ModuleType("tree") |
| +m.__file__ = path |
| +sys.modules["tree"] = m |
| +exec(code, m.__dict__) |
| urls = json.loads(sys.argv[1]) |
| m.fetch = lambda url: open(urls[url], "rb").read() |
| sys.exit(m.main(sys.argv[3:])) |
| @@ -356,6 +362,15 @@ func TestRendererTreeRefuses(t *testing.T) { |
| add("libc6", debMember{name: "./usr/up", link: "..", mode: 0o777})(f) |
| add("libc6", debMember{name: "./usr/out", link: "up/..", mode: 0o777})(f) |
| }}, |
| + {"a link to a file with a slash", "is not a directory", add("libc6", debMember{name: "./usr/lib64/ld", link: "ld-linux-x86-64.so.2/", mode: 0o777})}, |
| + {"a link to a file with /.", "is not a directory", add("libc6", debMember{name: "./usr/lib64/ld", link: "ld-linux-x86-64.so.2/.", mode: 0o777})}, |
| + {"a slash through another link", "is not a directory", func(f *rendererFixture) { |
| + add("libc6", debMember{name: "./usr/lib64/ld", link: "ld-linux-x86-64.so.2", mode: 0o777})(f) |
| + add("libc6", debMember{name: "./usr/lib64/via", link: "ld/", mode: 0o777})(f) |
| + }}, |
| + {"the browser's link with a slash", "is not a directory", func(f *rendererFixture) { |
| + f.packages["chromium-headless-shell"][5].link = "../lib/chromium/chromium-headless-shell/." |
| + }}, |
| {"absolute link outside the tree", "names nothing in the tree", add("libc6", debMember{name: "./usr/lib64/sh", link: "/bin/sh", mode: 0o777})}, |
| {"absolute link climbing out", "climbs out of the tree", add("libc6", debMember{name: "./usr/lib64/root", link: "/../../etc", mode: 0o777})}, |
| {"path climbing out", "not a path inside the tree", add("libc6", debMember{name: "./usr/../../evil", body: "x", mode: 0o644})}, |
diff --git a/internal/release/renderer_verify.go b/internal/release/renderer_verify.go| index 9cfc7764..1553a3dc 100644 |
| --- a/internal/release/renderer_verify.go |
| +++ b/internal/release/renderer_verify.go |
| @@ -188,21 +188,23 @@ func parseRendererManifest(b []byte) (map[string]rendererEntry, error) { |
| // resolveRendererLink is where target, read in directory here (names |
| // from the tree's root), leads: names from the root, each step an entry |
| -// of the tree, never above the root. It mirrors the build script's |
| -// Tree.resolve. |
| +// of the tree, never above the root. As Linux resolves a path, every |
| +// part after the first, including an empty or "." part (a trailing "/" |
| +// or "/."), needs a directory before it: "f/" and "f/." fail when f is a |
| +// file. It mirrors the build script's Tree.resolve. |
| func resolveRendererLink(entries map[string]rendererEntry, here []string, target string, depth int) ([]string, error) { |
| if depth > rendererLinkDepth { |
| return nil, errors.New("too many links") |
| } |
| here = append([]string(nil), here...) |
| - var names []string |
| - for _, n := range strings.Split(target, "/") { |
| - if n != "" && n != "." { |
| - names = append(names, n) |
| + for i, name := range strings.Split(target, "/") { |
| + if i > 0 && len(here) > 0 && entries[strings.Join(here, "/")].kind != "dir" { |
| + return nil, fmt.Errorf("%s is not a directory", strings.Join(here, "/")) |
| } |
| - } |
| - for i, name := range names { |
| - if name == ".." { |
| + switch name { |
| + case "", ".": |
| + continue |
| + case "..": |
| if len(here) == 0 { |
| return nil, errors.New("climbs out of the tree") |
| } |
| @@ -226,9 +228,6 @@ func resolveRendererLink(entries map[string]rendererEntry, here []string, target |
| } else { |
| here = append(here, name) |
| } |
| - if i < len(names)-1 && len(here) > 0 && entries[strings.Join(here, "/")].kind != "dir" { |
| - return nil, fmt.Errorf("%s is not a directory", strings.Join(here, "/")) |
| - } |
| } |
| return here, nil |
| } |
diff --git a/internal/release/renderer_verify_linux_test.go b/internal/release/renderer_verify_linux_test.go| index 41e21101..e361aceb 100644 |
| --- a/internal/release/renderer_verify_linux_test.go |
| +++ b/internal/release/renderer_verify_linux_test.go |
| @@ -132,7 +132,13 @@ func TestVerifyRendererAcceptsTheBuiltTree(t *testing.T) { |
| testci.Files(t, "../../scripts/build-renderer-tree.py") |
| testci.Tools(t, "python3") |
| dir := t.TempDir() |
| - pinFile, urls := newRendererFixture().write(t, dir) |
| + f := newRendererFixture() |
| + // A trailing slash or /. on a directory target is fine, for Linux, |
| + // the build and the verifier alike. |
| + f.packages["libc6"] = append(f.packages["libc6"], |
| + debMember{name: "./usr/lib64/lib", link: "../lib/", mode: 0o777}, |
| + debMember{name: "./usr/lib64/lib-dot", link: "lib/.", mode: 0o777}) |
| + pinFile, urls := f.write(t, dir) |
| tree := filepath.Join(dir, "tree") |
| pin, stderr, err := buildRendererTree(t, pinFile, urls, filepath.Join(dir, "renderer.tar.gz"), 0, "--tree", tree) |
| if err != nil { |
| @@ -311,6 +317,24 @@ func TestVerifyRendererRefusesAPinnedBadManifest(t *testing.T) { |
| t.Fatalf("escaping link: %v", err) |
| } |
| + // Linux refuses f/ and f/. (ENOTDIR) when f is a file, directly or |
| + // through another link; so does the verifier, even when pinned. |
| + for _, links := range [][][2]string{ |
| + {{"usr/bad", "lib-extra/"}}, |
| + {{"usr/bad", "lib-extra/."}}, |
| + {{"usr/bad", "via/"}, {"usr/via", "lib-extra"}}, |
| + {{"usr/bad", "via"}, {"usr/via", "lib-extra/."}}, |
| + } { |
| + root, _ := writeVerifyTree(t, t.TempDir()) |
| + for _, l := range links { |
| + treeMust(t, os.Symlink(l[1], filepath.Join(root, l[0]))) |
| + } |
| + err := VerifyRenderer(root, writeVerifyManifest(t, root)) |
| + if !errors.As(err, &re) || re.Path != "MANIFEST" || !strings.Contains(re.Reason, "is not a directory") { |
| + t.Fatalf("%v: %v", links, err) |
| + } |
| + } |
| + |
| big := make([]byte, rendererManifestMax+1) |
| for i := range big { |
| big[i] = '\n' |
diff --git a/scripts/build-renderer-tree.py b/scripts/build-renderer-tree.py| index b93bd911..bce6d954 100755 |
| --- a/scripts/build-renderer-tree.py |
| +++ b/scripts/build-renderer-tree.py |
| @@ -201,12 +201,18 @@ class Tree: |
| def resolve(self, start, target, depth=0): |
| """Where target, read in directory start (a list of names), leads: |
| - a list of names from the root, every step an entry of the tree.""" |
| + a list of names from the root, every step an entry of the tree. |
| + As Linux resolves a path, every part after the first, including an |
| + empty or "." part (a trailing "/" or "/."), needs a directory |
| + before it: "f/" and "f/." fail when f is a file.""" |
| if depth > LINK_DEPTH: |
| raise Refused("too many links") |
| here = list(start) |
| - names = [n for n in target.split("/") if n not in ("", ".")] |
| - for i, name in enumerate(names): |
| + for i, name in enumerate(target.split("/")): |
| + if i > 0 and self.kind(here) != "dir": |
| + raise Refused("%s is not a directory" % "/".join(here)) |
| + if name in ("", "."): |
| + continue |
| if name == "..": |
| if not here: |
| raise Refused("climbs out of the tree") |
| @@ -220,8 +226,6 @@ class Tree: |
| here = self.resolve(here, e[1], depth + 1) |
| else: |
| here.append(name) |
| - if i < len(names) - 1 and self.kind(here) != "dir": |
| - raise Refused("%s is not a directory" % "/".join(here)) |
| return here |
| def kind(self, names): |