package provider import ( "encoding/json" "os" "path/filepath" "strings" "testing" ) func TestCheckGoPathPins(t *testing.T) { for _, target := range []string{"root", "modules"} { t.Run(target, func(t *testing.T) { root, mods := t.TempDir(), t.TempDir() in := CheckPolicyInputs{Workspace: t.TempDir(), Bwrap: "/usr/bin/true", GoRoot: root, ModuleCache: mods, Argv: []string{"/usr/bin/true"}} p, err := BuildCheckPolicy(in) if err != nil { t.Fatal(err) } l := Launch{AttemptKey: "check", Workspace: in.Workspace, Policy: p} if _, err = AttestLaunch(l, in.Argv); err != nil { t.Fatal(err) } dir := root if target == "modules" { dir = mods } if err = os.WriteFile(filepath.Join(dir, "changed"), []byte("changed"), 0600); err != nil { t.Fatal(err) } if _, err = AttestLaunch(l, in.Argv); err != nil { t.Fatal("trusted host cache update refused", err) } }) } } func TestEngineeringGoScope(t *testing.T) { for _, action := range []string{"inspect", "converse", "research", "answer", ""} { t.Run(action, func(t *testing.T) { b, in := policyFixture(t) in.Action = action in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: t.TempDir(), Target: "/opt/go", ReadOnly: true}) if _, err := BuildPolicy(b, in); err == nil { t.Fatal("Go trees admitted outside Engineering") } }) } for _, target := range []string{"/opt/go", "/opt/go-mod"} { for _, source := range []string{"workspace", "scratch", "account", "ancestor", "symlink", "internal-symlink", "writable", "owner"} { t.Run(filepath.Base(target)+"/"+source, func(t *testing.T) { b, in := policyFixture(t) in.Action = "implement" path := t.TempDir() ro := true switch source { case "workspace": path = in.Workspace case "scratch": path = in.Scratch case "account": path = b.CredentialHome case "ancestor": path = filepath.Dir(b.CredentialHome) case "symlink": link := filepath.Join(t.TempDir(), "link") if err := os.Symlink(path, link); err != nil { t.Fatal(err) } path = link case "owner": if target == "/opt/go" { t.Skip("Go may be installed by root") } if os.Getuid() == 0 { t.Skip("needs a directory owned by another user") } path = "/usr/share" case "internal-symlink": if target == "/opt/go-mod" { t.Skip("module cache is path pinned") } writeFake(t, filepath.Join(path, "VERSION"), "fixture") if err := os.Symlink("VERSION", filepath.Join(path, "link")); err != nil { t.Fatal(err) } case "writable": ro = false } in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: path, Target: target, ReadOnly: ro}) if _, err := BuildPolicy(b, in); err == nil { t.Fatal("unsafe Go tree admitted") } }) } } } func TestEngineeringGitEnvironment(t *testing.T) { b, in := policyFixture(t) in.Action = "implement" in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: t.TempDir(), Target: "/opt/go", ReadOnly: true}) p := mustPolicy(t, b, in) for k, want := range map[string]string{"GIT_CONFIG_GLOBAL": "/dev/null", "GIT_CONFIG_NOSYSTEM": "1", "PATH": "/opt/go/bin:/usr/bin:/bin:/opt"} { if p.Env[k] != want { t.Errorf("%s=%q, want %q", k, p.Env[k], want) } } if _, ok := p.Env["XDG_CONFIG_HOME"]; ok { t.Error("provider receives writable Git config home") } } func TestEngineeringGoPinReuse(t *testing.T) { b, in := policyFixture(t) in.Action = "implement" root, mods := t.TempDir(), t.TempDir() in.RuntimeFiles = append(in.RuntimeFiles, Mount{root, "/opt/go", true}, Mount{mods, "/opt/go-mod", true}) p := mustPolicy(t, b, in) var manifest effectiveManifest if err := json.Unmarshal(p.Manifest, &manifest); err != nil { t.Fatal(err) } for _, f := range manifest.Files { if f.Mount.Target == "/opt/go-mod" && f.SHA256 != "" { t.Fatal("module cache was content hashed") } } // Trusted host edits do not trigger another content scan in this launch. writeFake(t, filepath.Join(root, "new"), "host update") writeFake(t, filepath.Join(mods, "new"), "host update") if err := p.ValidateSources(b); err != nil { t.Fatal("rehashed launch sources", err) } if q := mustPolicy(t, b, in); q.ManifestHash != p.ManifestHash { t.Fatal("rehashed policy rebuild") } held := b.provenance.goPins[in.RuntimeFiles[len(in.RuntimeFiles)-2]].file if err := b.provenance.Close(); err != nil { t.Fatal(err) } if _, err := held.Stat(); err == nil { t.Fatal("lease leaked its Go descriptor") } if f, err := p.OpenGoTree(b, in.RuntimeFiles[len(in.RuntimeFiles)-2]); err == nil { f.Close() t.Fatal("closed lease supplied a Go tree") } } func TestEngineeringGoManifestTampering(t *testing.T) { for _, field := range []string{"PATH", "digest"} { t.Run(field, func(t *testing.T) { b, in := policyFixture(t) in.Action = "implement" in.RuntimeFiles = append(in.RuntimeFiles, Mount{t.TempDir(), "/opt/go", true}) p := mustPolicy(t, b, in) var m effectiveManifest if err := json.Unmarshal(p.Manifest, &m); err != nil { t.Fatal(err) } if field == "PATH" { m.Env["PATH"] = "/workspace:/usr/bin" p.Env["PATH"] = m.Env["PATH"] } else { for i := range m.Files { if m.Files[i].Mount.Target == "/opt/go" { m.Files[i].SHA256 = strings.Repeat("0", 64) } } } p.Manifest, p.ManifestHash, _ = hashJSON(m) _, p.Hash, _ = hashJSON(policyTemplate(m)) l := Launch{AttemptKey: "tamper", Workspace: in.Workspace, Binding: b, Selection: Selection{Provider: b.Provider, AccountID: b.AccountID}, Policy: p} if _, err := AttestLaunch(l, in.Argv); err == nil { t.Fatal("forged " + field + " accepted") } if field == "digest" { if f, err := p.OpenGoTree(b, in.RuntimeFiles[len(in.RuntimeFiles)-1]); err == nil { f.Close() t.Fatal("forged descriptor digest accepted") } } }) } }