| 1 | package pi |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // The committed lockfile pins Pi at Version with the integrity npm |
| 12 | // published, and every package in it carries an integrity hash, so npm |
| 13 | // ci installs exactly these bytes or nothing. |
| 14 | func TestLockfilePinsPi(t *testing.T) { |
| 15 | pkg, lock := Package() |
| 16 | var p struct { |
| 17 | Dependencies map[string]string `json:"dependencies"` |
| 18 | } |
| 19 | if err := json.Unmarshal(pkg, &p); err != nil || p.Dependencies[PackageName] != Version || len(p.Dependencies) != 1 { |
| 20 | t.Fatalf("package.json: %v %v", p.Dependencies, err) |
| 21 | } |
| 22 | var l struct { |
| 23 | Packages map[string]struct { |
| 24 | Version, Integrity, Resolved string |
| 25 | HasInstallScript bool |
| 26 | } `json:"packages"` |
| 27 | } |
| 28 | if err := json.Unmarshal(lock, &l); err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | if got := l.Packages["node_modules/"+PackageName]; got.Version != Version || got.Integrity != Integrity { |
| 32 | t.Fatalf("lockfile pins %+v", got) |
| 33 | } |
| 34 | for name, p := range l.Packages { |
| 35 | if name == "" { |
| 36 | continue |
| 37 | } |
| 38 | if len(p.Integrity) <= len("sha512-") || p.Resolved == "" { |
| 39 | t.Errorf("%s is not pinned by integrity", name) |
| 40 | } |
| 41 | } |
| 42 | if !Pinned() { |
| 43 | t.Fatal("Pinned says no") |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // The tree digest is over every entry's path, mode and content, links |
| 48 | // included; a link must resolve inside the tree; a writable entry is |
| 49 | // refused (the install is read-only). |
| 50 | func TestTreeDigest(t *testing.T) { |
| 51 | make := func(t *testing.T) string { |
| 52 | dir := filepath.Join(t.TempDir(), "node_modules") |
| 53 | t.Cleanup(func() { unseal(dir) }) |
| 54 | must(t, os.MkdirAll(filepath.Join(dir, "a/bin"), 0755)) |
| 55 | must(t, os.MkdirAll(filepath.Join(dir, ".bin"), 0755)) |
| 56 | must(t, os.WriteFile(filepath.Join(dir, "a/index.js"), []byte("x"), 0644)) |
| 57 | must(t, os.WriteFile(filepath.Join(dir, "a/bin/cli.js"), []byte("y"), 0755)) |
| 58 | must(t, os.Symlink("../a/bin/cli.js", filepath.Join(dir, ".bin/a"))) |
| 59 | must(t, Seal(dir)) |
| 60 | return dir |
| 61 | } |
| 62 | base, err := TreeSHA256(make(t)) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | if again, _ := TreeSHA256(make(t)); again != base { |
| 67 | t.Fatal("the digest is not deterministic") |
| 68 | } |
| 69 | for _, tc := range []struct { |
| 70 | name string |
| 71 | change func(dir string) error |
| 72 | refuse bool |
| 73 | }{ |
| 74 | {"content", func(d string) error { |
| 75 | return writeSealed(filepath.Join(d, "a/index.js"), "z", 0444) |
| 76 | }, false}, |
| 77 | {"mode", func(d string) error { return os.Chmod(filepath.Join(d, "a/index.js"), 0555) }, false}, |
| 78 | {"link target", func(d string) error { |
| 79 | os.Chmod(filepath.Join(d, ".bin"), 0755) |
| 80 | os.Remove(filepath.Join(d, ".bin/a")) |
| 81 | err := os.Symlink("../a/index.js", filepath.Join(d, ".bin/a")) |
| 82 | os.Chmod(filepath.Join(d, ".bin"), 0555) |
| 83 | return err |
| 84 | }, false}, |
| 85 | {"new file", func(d string) error { |
| 86 | os.Chmod(filepath.Join(d, "a"), 0755) |
| 87 | defer os.Chmod(filepath.Join(d, "a"), 0555) |
| 88 | return writeSealed(filepath.Join(d, "a/extra.js"), "", 0444) |
| 89 | }, false}, |
| 90 | {"link out of the tree", func(d string) error { |
| 91 | os.Chmod(filepath.Join(d, ".bin"), 0755) |
| 92 | os.Remove(filepath.Join(d, ".bin/a")) |
| 93 | err := os.Symlink("../../../etc/passwd", filepath.Join(d, ".bin/a")) |
| 94 | os.Chmod(filepath.Join(d, ".bin"), 0555) |
| 95 | return err |
| 96 | }, true}, |
| 97 | {"absolute link", func(d string) error { |
| 98 | os.Chmod(filepath.Join(d, ".bin"), 0755) |
| 99 | os.Remove(filepath.Join(d, ".bin/a")) |
| 100 | err := os.Symlink(filepath.Join(d, "a/index.js"), filepath.Join(d, ".bin/a")) |
| 101 | os.Chmod(filepath.Join(d, ".bin"), 0555) |
| 102 | return err |
| 103 | }, true}, |
| 104 | {"writable file", func(d string) error { return os.Chmod(filepath.Join(d, "a/index.js"), 0644) }, true}, |
| 105 | // Containment is resolved, not lexical: b's text stays inside, but |
| 106 | // through the link a it leaves the tree. |
| 107 | {"a chain out of the tree", func(d string) error { |
| 108 | os.Chmod(d, 0755) |
| 109 | defer os.Chmod(d, 0555) |
| 110 | for _, dir := range []string{"sub", "real"} { |
| 111 | if err := os.Mkdir(filepath.Join(d, dir), 0555); err != nil { |
| 112 | return err |
| 113 | } |
| 114 | } |
| 115 | os.Chmod(filepath.Join(d, "sub"), 0755) |
| 116 | defer os.Chmod(filepath.Join(d, "sub"), 0555) |
| 117 | if err := os.Symlink("../real", filepath.Join(d, "sub/a")); err != nil { |
| 118 | return err |
| 119 | } |
| 120 | return os.Symlink("a/../../outside", filepath.Join(d, "sub/b")) |
| 121 | }, true}, |
| 122 | {"a dangling link", func(d string) error { |
| 123 | os.Chmod(d, 0755) |
| 124 | defer os.Chmod(d, 0555) |
| 125 | return os.Symlink("a/missing.js", filepath.Join(d, "gone")) |
| 126 | }, true}, |
| 127 | {"a cycle", func(d string) error { |
| 128 | os.Chmod(d, 0755) |
| 129 | defer os.Chmod(d, 0555) |
| 130 | if err := os.Symlink("y", filepath.Join(d, "x")); err != nil { |
| 131 | return err |
| 132 | } |
| 133 | return os.Symlink("x", filepath.Join(d, "y")) |
| 134 | }, true}, |
| 135 | {"a link to a directory inside", func(d string) error { |
| 136 | os.Chmod(d, 0755) |
| 137 | defer os.Chmod(d, 0555) |
| 138 | return os.Symlink("a/bin", filepath.Join(d, "bin-link")) |
| 139 | }, false}, |
| 140 | {"a second name for a file", func(d string) error { |
| 141 | os.Chmod(d, 0755) |
| 142 | defer os.Chmod(d, 0555) |
| 143 | return os.Link(filepath.Join(d, "a/index.js"), filepath.Join(d, "alias.js")) |
| 144 | }, true}, |
| 145 | {"writable directory", func(d string) error { return os.Chmod(filepath.Join(d, "a"), 0755) }, true}, |
| 146 | } { |
| 147 | t.Run(tc.name, func(t *testing.T) { |
| 148 | dir := make(t) |
| 149 | must(t, tc.change(dir)) |
| 150 | got, err := TreeSHA256(dir) |
| 151 | if tc.refuse { |
| 152 | if !errors.Is(err, ErrUnpinned) { |
| 153 | t.Fatalf("accepted: %s %v", got, err) |
| 154 | } |
| 155 | return |
| 156 | } |
| 157 | if err != nil || got == base { |
| 158 | t.Fatalf("the digest did not move: %v", err) |
| 159 | } |
| 160 | }) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // The committed tree digests: one per supported platform, and the |
| 165 | // platforms are the runner's (linux/amd64) and CI's (linux/arm64). The |
| 166 | // tree has no platform-specific package, so they are one digest. |
| 167 | func TestTreePinsByPlatform(t *testing.T) { |
| 168 | for _, arch := range []string{"amd64", "arm64"} { |
| 169 | tree, node, ok := Pins(arch) |
| 170 | if !ok || len(tree) != 64 || len(node.SHA256) != 64 || len(node.BinarySHA256) != 64 || node.Version != NodeVersion { |
| 171 | t.Fatalf("%s: %q %+v %v", arch, tree, node, ok) |
| 172 | } |
| 173 | } |
| 174 | if _, _, ok := Pins("riscv64"); ok { |
| 175 | t.Fatal("a platform with no pins") |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | func writeSealed(path, content string, mode os.FileMode) error { |
| 180 | os.Chmod(filepath.Dir(path), 0755) |
| 181 | os.Chmod(path, 0644) |
| 182 | err := os.WriteFile(path, []byte(content), 0644) |
| 183 | if err == nil { |
| 184 | err = os.Chmod(path, mode) |
| 185 | } |
| 186 | os.Chmod(filepath.Dir(path), 0555) |
| 187 | return err |
| 188 | } |
| 189 | |
| 190 | // unseal lets t.TempDir remove a sealed tree. |
| 191 | func unseal(dir string) { |
| 192 | filepath.Walk(dir, func(p string, info os.FileInfo, err error) error { |
| 193 | if err == nil && info.IsDir() { |
| 194 | os.Chmod(p, 0755) |
| 195 | } |
| 196 | return nil |
| 197 | }) |
| 198 | } |
| 199 | |
| 200 | func must(t *testing.T, err error) { |
| 201 | t.Helper() |
| 202 | if err != nil { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // The pinned Pi this machine has (CI's box installs it) is exactly the |
| 208 | // pinned one; a changed file in it is not. |
| 209 | func TestPinnedInstallVerifies(t *testing.T) { |
| 210 | inst := realInstall(t) |
| 211 | root := filepath.Dir(filepath.Dir(inst.Tree)) |
| 212 | if err := Verify(root); err != nil { |
| 213 | t.Fatalf("the pinned install does not verify: %v", err) |
| 214 | } |
| 215 | if err := Verify(t.TempDir()); err == nil { |
| 216 | t.Fatal("an empty root verified") |
| 217 | } |
| 218 | // The right Node beside another tree. |
| 219 | other := t.TempDir() |
| 220 | node, tree := Paths(other) |
| 221 | must(t, os.MkdirAll(filepath.Dir(node), 0755)) |
| 222 | must(t, os.Symlink(inst.Node, node)) |
| 223 | must(t, os.MkdirAll(tree, 0755)) |
| 224 | must(t, os.WriteFile(filepath.Join(tree, "index.js"), []byte("x"), 0444)) |
| 225 | must(t, Seal(tree)) |
| 226 | sealed := tree |
| 227 | t.Cleanup(func() { unseal(sealed) }) |
| 228 | if err := Verify(other); err == nil { |
| 229 | t.Fatal("another tree verified") |
| 230 | } |
| 231 | // The right tree beside another Node. |
| 232 | third := t.TempDir() |
| 233 | node, tree = Paths(third) |
| 234 | must(t, os.MkdirAll(filepath.Dir(node), 0755)) |
| 235 | must(t, os.WriteFile(node, []byte("#!/bin/sh\n"), 0555)) |
| 236 | copyTree(t, inst.Tree, tree) |
| 237 | copied := tree |
| 238 | t.Cleanup(func() { unseal(copied) }) |
| 239 | if sum, err := TreeSHA256(tree); err != nil || sum != inst.TreeSHA256 { |
| 240 | t.Fatalf("the copy is not the pinned tree: %v", err) |
| 241 | } |
| 242 | if err := Verify(third); err == nil { |
| 243 | t.Fatal("another Node verified") |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // copyTree copies a sealed tree, links and modes as they are. |
| 248 | func copyTree(t *testing.T, from, to string) { |
| 249 | t.Helper() |
| 250 | err := filepath.Walk(from, func(p string, info os.FileInfo, err error) error { |
| 251 | if err != nil { |
| 252 | return err |
| 253 | } |
| 254 | rel, _ := filepath.Rel(from, p) |
| 255 | dst := filepath.Join(to, rel) |
| 256 | switch { |
| 257 | case info.Mode()&os.ModeSymlink != 0: |
| 258 | target, err := os.Readlink(p) |
| 259 | if err != nil { |
| 260 | return err |
| 261 | } |
| 262 | return os.Symlink(target, dst) |
| 263 | case info.IsDir(): |
| 264 | return os.MkdirAll(dst, 0755) |
| 265 | } |
| 266 | raw, err := os.ReadFile(p) |
| 267 | if err != nil { |
| 268 | return err |
| 269 | } |
| 270 | return os.WriteFile(dst, raw, info.Mode().Perm()) |
| 271 | }) |
| 272 | must(t, err) |
| 273 | must(t, Seal(to)) |
| 274 | } |
| 275 | |