oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "crypto/sha256"
6 "fmt"
7 "net"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "sync"
12 "testing"
13
14 "amadan.net/oficina/equipo/internal/runner"
15)
16
17var scannerBuild struct {
18 sync.Mutex
19 binary, pin string
20}
21
22func previewScannerFixture(t *testing.T) string {
23 t.Helper()
24 scannerBuild.Lock()
25 defer scannerBuild.Unlock()
26 if scannerBuild.binary == "" {
27 dir, err := os.MkdirTemp(os.Getenv("TMPDIR"), "equipo-scanner-test-")
28 if err != nil {
29 t.Fatal(err)
30 }
31 // Re-execute the already compiled test binary's pinned gitleaks command.
32 // No compiler, module cache or network is used to prepare the scanner.
33 binary := copySelf(t, dir, "gitleaks")
34 raw, err := os.ReadFile(binary)
35 if err != nil {
36 t.Fatal(err)
37 }
38 scannerBuild.binary = binary
39 scannerBuild.pin = fmt.Sprintf("%x", sha256.Sum256(raw))
40 }
41 old := previewScannerTool
42 previewScannerTool = func(c runner.Config) (string, string) {
43 return filepath.Join(filepath.Dir(c.Executable), "gitleaks"), scannerBuild.pin
44 }
45 t.Cleanup(func() { previewScannerTool = old })
46 return filepath.Join(filepath.Dir(scannerBuild.binary), "equipo-runner")
47}
48
49func TestPreviewScannerSandboxDeniesNetworkAndHostFiles(t *testing.T) {
50 root := t.TempDir()
51 outside, err := net.Listen("tcp", "127.0.0.1:0")
52 if err != nil {
53 t.Fatal(err)
54 }
55 defer outside.Close()
56 secret := filepath.Join(root, "host-only.txt")
57 if err := os.WriteFile(secret, []byte("host data"), 0600); err != nil {
58 t.Fatal(err)
59 }
60 source := fmt.Sprintf(`package main
61import("os";"net";"time")
62func main(){
63 found:=false
64 if c,e:=net.DialTimeout("tcp",%q,300*time.Millisecond);e==nil{found=true;c.Close()}
65 if _,e:=os.ReadFile(%q);e==nil{found=true}
66 report:="";for i,a:=range os.Args{if a=="--report-path"{report=os.Args[i+1]}}
67 body:="[]";if found{body="[{}]"};if os.WriteFile(report,[]byte(body),0600)!=nil{os.Exit(2)}
68}`, outside.Addr().String(), secret)
69 file := filepath.Join(root, "scanner.go")
70 if err := os.WriteFile(file, []byte(source), 0600); err != nil {
71 t.Fatal(err)
72 }
73 binary := filepath.Join(root, "scanner")
74 cmd := exec.Command("go", "build", "-o", binary, file)
75 cmd.Env = append(os.Environ(), "CGO_ENABLED=0")
76 if out, err := cmd.CombinedOutput(); err != nil {
77 t.Fatalf("fixture build: %v %s", err, out)
78 }
79 raw, err := os.ReadFile(binary)
80 if err != nil {
81 t.Fatal(err)
82 }
83 old := previewScannerTool
84 t.Cleanup(func() { previewScannerTool = old })
85 previewScannerTool = func(runner.Config) (string, string) { return binary, fmt.Sprintf("%x", sha256.Sum256(raw)) }
86 stage := filepath.Join(root, "export")
87 if err := os.Mkdir(stage, 0700); err != nil {
88 t.Fatal(err)
89 }
90 if why := (&Engineer{}).scanPreview(context.Background(), stage); why != "" {
91 t.Fatal("scanner reached network or host files:", why)
92 }
93}
94