package pi import ( "encoding/json" "errors" "os" "path/filepath" "testing" ) // The committed lockfile pins Pi at Version with the integrity npm // published, and every package in it carries an integrity hash, so npm // ci installs exactly these bytes or nothing. func TestLockfilePinsPi(t *testing.T) { pkg, lock := Package() var p struct { Dependencies map[string]string `json:"dependencies"` } if err := json.Unmarshal(pkg, &p); err != nil || p.Dependencies[PackageName] != Version || len(p.Dependencies) != 1 { t.Fatalf("package.json: %v %v", p.Dependencies, err) } var l struct { Packages map[string]struct { Version, Integrity, Resolved string HasInstallScript bool } `json:"packages"` } if err := json.Unmarshal(lock, &l); err != nil { t.Fatal(err) } if got := l.Packages["node_modules/"+PackageName]; got.Version != Version || got.Integrity != Integrity { t.Fatalf("lockfile pins %+v", got) } for name, p := range l.Packages { if name == "" { continue } if len(p.Integrity) <= len("sha512-") || p.Resolved == "" { t.Errorf("%s is not pinned by integrity", name) } } if !Pinned() { t.Fatal("Pinned says no") } } // The tree digest is over every entry's path, mode and content, links // included; a link must resolve inside the tree; a writable entry is // refused (the install is read-only). func TestTreeDigest(t *testing.T) { make := func(t *testing.T) string { dir := filepath.Join(t.TempDir(), "node_modules") t.Cleanup(func() { unseal(dir) }) must(t, os.MkdirAll(filepath.Join(dir, "a/bin"), 0755)) must(t, os.MkdirAll(filepath.Join(dir, ".bin"), 0755)) must(t, os.WriteFile(filepath.Join(dir, "a/index.js"), []byte("x"), 0644)) must(t, os.WriteFile(filepath.Join(dir, "a/bin/cli.js"), []byte("y"), 0755)) must(t, os.Symlink("../a/bin/cli.js", filepath.Join(dir, ".bin/a"))) must(t, Seal(dir)) return dir } base, err := TreeSHA256(make(t)) if err != nil { t.Fatal(err) } if again, _ := TreeSHA256(make(t)); again != base { t.Fatal("the digest is not deterministic") } for _, tc := range []struct { name string change func(dir string) error refuse bool }{ {"content", func(d string) error { return writeSealed(filepath.Join(d, "a/index.js"), "z", 0444) }, false}, {"mode", func(d string) error { return os.Chmod(filepath.Join(d, "a/index.js"), 0555) }, false}, {"link target", func(d string) error { os.Chmod(filepath.Join(d, ".bin"), 0755) os.Remove(filepath.Join(d, ".bin/a")) err := os.Symlink("../a/index.js", filepath.Join(d, ".bin/a")) os.Chmod(filepath.Join(d, ".bin"), 0555) return err }, false}, {"new file", func(d string) error { os.Chmod(filepath.Join(d, "a"), 0755) defer os.Chmod(filepath.Join(d, "a"), 0555) return writeSealed(filepath.Join(d, "a/extra.js"), "", 0444) }, false}, {"link out of the tree", func(d string) error { os.Chmod(filepath.Join(d, ".bin"), 0755) os.Remove(filepath.Join(d, ".bin/a")) err := os.Symlink("../../../etc/passwd", filepath.Join(d, ".bin/a")) os.Chmod(filepath.Join(d, ".bin"), 0555) return err }, true}, {"absolute link", func(d string) error { os.Chmod(filepath.Join(d, ".bin"), 0755) os.Remove(filepath.Join(d, ".bin/a")) err := os.Symlink(filepath.Join(d, "a/index.js"), filepath.Join(d, ".bin/a")) os.Chmod(filepath.Join(d, ".bin"), 0555) return err }, true}, {"writable file", func(d string) error { return os.Chmod(filepath.Join(d, "a/index.js"), 0644) }, true}, // Containment is resolved, not lexical: b's text stays inside, but // through the link a it leaves the tree. {"a chain out of the tree", func(d string) error { os.Chmod(d, 0755) defer os.Chmod(d, 0555) for _, dir := range []string{"sub", "real"} { if err := os.Mkdir(filepath.Join(d, dir), 0555); err != nil { return err } } os.Chmod(filepath.Join(d, "sub"), 0755) defer os.Chmod(filepath.Join(d, "sub"), 0555) if err := os.Symlink("../real", filepath.Join(d, "sub/a")); err != nil { return err } return os.Symlink("a/../../outside", filepath.Join(d, "sub/b")) }, true}, {"a dangling link", func(d string) error { os.Chmod(d, 0755) defer os.Chmod(d, 0555) return os.Symlink("a/missing.js", filepath.Join(d, "gone")) }, true}, {"a cycle", func(d string) error { os.Chmod(d, 0755) defer os.Chmod(d, 0555) if err := os.Symlink("y", filepath.Join(d, "x")); err != nil { return err } return os.Symlink("x", filepath.Join(d, "y")) }, true}, {"a link to a directory inside", func(d string) error { os.Chmod(d, 0755) defer os.Chmod(d, 0555) return os.Symlink("a/bin", filepath.Join(d, "bin-link")) }, false}, {"a second name for a file", func(d string) error { os.Chmod(d, 0755) defer os.Chmod(d, 0555) return os.Link(filepath.Join(d, "a/index.js"), filepath.Join(d, "alias.js")) }, true}, {"writable directory", func(d string) error { return os.Chmod(filepath.Join(d, "a"), 0755) }, true}, } { t.Run(tc.name, func(t *testing.T) { dir := make(t) must(t, tc.change(dir)) got, err := TreeSHA256(dir) if tc.refuse { if !errors.Is(err, ErrUnpinned) { t.Fatalf("accepted: %s %v", got, err) } return } if err != nil || got == base { t.Fatalf("the digest did not move: %v", err) } }) } } // The committed tree digests: one per supported platform, and the // platforms are the runner's (linux/amd64) and CI's (linux/arm64). The // tree has no platform-specific package, so they are one digest. func TestTreePinsByPlatform(t *testing.T) { for _, arch := range []string{"amd64", "arm64"} { tree, node, ok := Pins(arch) if !ok || len(tree) != 64 || len(node.SHA256) != 64 || len(node.BinarySHA256) != 64 || node.Version != NodeVersion { t.Fatalf("%s: %q %+v %v", arch, tree, node, ok) } } if _, _, ok := Pins("riscv64"); ok { t.Fatal("a platform with no pins") } } func writeSealed(path, content string, mode os.FileMode) error { os.Chmod(filepath.Dir(path), 0755) os.Chmod(path, 0644) err := os.WriteFile(path, []byte(content), 0644) if err == nil { err = os.Chmod(path, mode) } os.Chmod(filepath.Dir(path), 0555) return err } // unseal lets t.TempDir remove a sealed tree. func unseal(dir string) { filepath.Walk(dir, func(p string, info os.FileInfo, err error) error { if err == nil && info.IsDir() { os.Chmod(p, 0755) } return nil }) } func must(t *testing.T, err error) { t.Helper() if err != nil { t.Fatal(err) } } // The pinned Pi this machine has (CI's box installs it) is exactly the // pinned one; a changed file in it is not. func TestPinnedInstallVerifies(t *testing.T) { inst := realInstall(t) root := filepath.Dir(filepath.Dir(inst.Tree)) if err := Verify(root); err != nil { t.Fatalf("the pinned install does not verify: %v", err) } if err := Verify(t.TempDir()); err == nil { t.Fatal("an empty root verified") } // The right Node beside another tree. other := t.TempDir() node, tree := Paths(other) must(t, os.MkdirAll(filepath.Dir(node), 0755)) must(t, os.Symlink(inst.Node, node)) must(t, os.MkdirAll(tree, 0755)) must(t, os.WriteFile(filepath.Join(tree, "index.js"), []byte("x"), 0444)) must(t, Seal(tree)) sealed := tree t.Cleanup(func() { unseal(sealed) }) if err := Verify(other); err == nil { t.Fatal("another tree verified") } // The right tree beside another Node. third := t.TempDir() node, tree = Paths(third) must(t, os.MkdirAll(filepath.Dir(node), 0755)) must(t, os.WriteFile(node, []byte("#!/bin/sh\n"), 0555)) copyTree(t, inst.Tree, tree) copied := tree t.Cleanup(func() { unseal(copied) }) if sum, err := TreeSHA256(tree); err != nil || sum != inst.TreeSHA256 { t.Fatalf("the copy is not the pinned tree: %v", err) } if err := Verify(third); err == nil { t.Fatal("another Node verified") } } // copyTree copies a sealed tree, links and modes as they are. func copyTree(t *testing.T, from, to string) { t.Helper() err := filepath.Walk(from, func(p string, info os.FileInfo, err error) error { if err != nil { return err } rel, _ := filepath.Rel(from, p) dst := filepath.Join(to, rel) switch { case info.Mode()&os.ModeSymlink != 0: target, err := os.Readlink(p) if err != nil { return err } return os.Symlink(target, dst) case info.IsDir(): return os.MkdirAll(dst, 0755) } raw, err := os.ReadFile(p) if err != nil { return err } return os.WriteFile(dst, raw, info.Mode().Perm()) }) must(t, err) must(t, Seal(to)) }