package webread import ( "context" "debug/elf" "encoding/json" "io/fs" "os" "os/exec" "path/filepath" "runtime" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/testci" ) type probeReport struct { Paths []string Env []string Home bool } // probeMain runs inside the fetcher's sandbox in place of the fetcher and // reports what it can see. func probeMain(args []string) int { if len(args) != 1 { return 2 } var r probeReport filepath.WalkDir("/", func(p string, d fs.DirEntry, err error) error { if err != nil { return nil } if p == "/proc" || p == "/sys" || p == "/dev" { return fs.SkipDir } if r.Paths = append(r.Paths, p); len(r.Paths) > 10000 { return fs.SkipAll } return nil }) r.Env = os.Environ() _, err := os.Stat(os.Getenv("HOME")) r.Home = os.Getenv("HOME") != "" && err == nil b, _ := json.Marshal(r) if writeNew(filepath.Join(args[0], "probe.json"), b) != nil { return 1 } return 0 } func usableBwrap(t *testing.T) (bwrap, self string) { t.Helper() if runtime.GOOS != "linux" { t.Skip("the fetcher runs only on Linux") } testci.Tools(t, "bwrap") bwrap, err := exec.LookPath("bwrap") if err != nil { testci.Absent(t, "bwrap is not installed") } if out, err := exec.Command(bwrap, "--unshare-all", "--share-net", "--ro-bind", "/", "/", "true").CombinedOutput(); err != nil { testci.Absent(t, "bwrap cannot create namespaces here: "+strings.TrimSpace(string(out))) } self, err = os.Executable() if err != nil { t.Fatal(err) } if self, err = filepath.EvalSymlinks(self); err != nil { t.Fatal(err) } f, err := elf.Open(self) if err != nil { t.Fatal(err) } defer f.Close() for _, p := range f.Progs { if p.Type == elf.PT_INTERP { t.Skip("the test binary is dynamically linked; run with CGO_ENABLED=0 as make does") } } return bwrap, self } func outDir(t *testing.T) string { t.Helper() root, err := filepath.EvalSymlinks(t.TempDir()) if err != nil { t.Fatal(err) } dir := filepath.Join(root, "out") if err = os.Mkdir(dir, 0700); err != nil { t.Fatal(err) } return dir } // The fetcher's sandbox holds its binary, the CA bundle, resolv.conf and // the output directory: no runner state, home, credentials or host /etc. func TestLaunchHoldsNoRunnerState(t *testing.T) { bwrap, self := usableBwrap(t) state := t.TempDir() os.WriteFile(filepath.Join(state, "forge.json"), []byte(`{"token":"secret"}`), 0600) out := outDir(t) ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() cfg := LaunchConfig{Bwrap: bwrap, Binary: self, OutDir: out} if err := launch(ctx, cfg, plan(t, "https://shop.example.com/"), "webread-probe"); err != nil { t.Fatal(err) } raw, err := os.ReadFile(filepath.Join(out, "probe.json")) if err != nil { t.Fatal(err) } var r probeReport if err = json.Unmarshal(raw, &r); err != nil { t.Fatal(err) } allowed := map[string]bool{"/": true, "/webread": true, "/etc": true, "/etc/ssl": true, "/etc/ssl/certs": true, "/etc/ssl/certs/ca-certificates.crt": true, "/etc/resolv.conf": true, "/out": true, "/out/probe.json": true} for _, p := range r.Paths { if !allowed[p] { t.Errorf("the sandbox shows %s", p) } } // bubblewrap sets PWD itself after --chdir; nothing else may be there. if len(r.Env) > 1 || len(r.Env) == 1 && r.Env[0] != "PWD=/" || r.Home { t.Errorf("environment %v, home %v", r.Env, r.Home) } } // The real subcommand, under bubblewrap, writes a bundle the runner reads. // The only source is the instance's own host, refused before any lookup, // so the test needs no network. func TestLaunchWritesABundle(t *testing.T) { bwrap, self := usableBwrap(t) out := outDir(t) p := plan(t, "https://shop.example.com/") p.Instance = "shop.example.com" ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() if err := Launch(ctx, LaunchConfig{Bwrap: bwrap, Binary: self, OutDir: out}, p); err != nil { t.Fatal(err) } b, err := ReadBundle(out) if err != nil { t.Fatal(err) } if len(b.Log) != 1 || b.Log[0].Reason != ReasonHostRefused { t.Fatalf("log %+v", b.Log) } } func TestLaunchRefusesItsInputs(t *testing.T) { if runtime.GOOS != "linux" { if err := Launch(context.Background(), LaunchConfig{}, plan(t, "https://shop.example.com/")); err != ErrUnsupported { t.Fatalf("Launch off Linux: %v", err) } return } ok := plan(t, "https://shop.example.com/") self, _ := os.Executable() used := outDir(t) os.WriteFile(filepath.Join(used, "x"), nil, 0600) open := outDir(t) os.Chmod(open, 0755) link := filepath.Join(filepath.Dir(outDir(t)), "link") os.Symlink(outDir(t), link) for name, c := range map[string]struct { cfg LaunchConfig plan Plan }{ "bad plan": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: outDir(t)}, Plan{}}, "relative out": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: "out"}, ok}, "used out": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: used}, ok}, "open out": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: open}, ok}, "symlinked out": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: link}, ok}, "missing binary": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: "/nonexistent/equipo-runner", OutDir: outDir(t)}, ok}, "missing CA": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: outDir(t), CABundle: "/nonexistent.crt"}, ok}, "relative binary": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: "equipo-runner", OutDir: outDir(t)}, ok}, "relative bwrap": {LaunchConfig{Bwrap: "bwrap", Binary: self, OutDir: outDir(t)}, ok}, } { if err := Launch(context.Background(), c.cfg, c.plan); err == nil { t.Errorf("%s: launched", name) } } }