| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "maps" |
| 8 | "net/http" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/wire" |
| 15 | ) |
| 16 | |
| 17 | func TestPreviewFixedHeaders(t *testing.T) { |
| 18 | files := maps.Clone(mockups) |
| 19 | files["mockups/_headers"] = "/*\n Content-Security-Policy: default-src *\n" |
| 20 | e, fc, _, r := previewRig(t, files, nil) |
| 21 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 22 | if rep.State != wire.StepDone { |
| 23 | t.Fatalf("preview: %+v", rep) |
| 24 | } |
| 25 | for _, dir := range []string{fc.served, fc.calls[1][len(fc.calls[1])-1]} { |
| 26 | raw, err := os.ReadFile(filepath.Join(dir, "_headers")) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | for _, want := range []string{"frame-ancestors https://equipo.example", "default-src 'none'", "connect-src 'self'", "script-src 'self'", "img-src 'self' data:", "form-action 'none'", "frame-src 'none'", "base-uri 'none'", "object-src 'none'", "X-DNS-Prefetch-Control: off"} { |
| 31 | if !strings.Contains(string(raw), want) { |
| 32 | t.Fatalf("missing %q: %s", want, raw) |
| 33 | } |
| 34 | } |
| 35 | if strings.Contains(string(raw), "default-src *") { |
| 36 | t.Fatal("candidate weakened headers") |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestPreviewSecretScanBeforePublish(t *testing.T) { |
| 42 | for _, private := range []bool{false, true} { |
| 43 | for _, unavailable := range []bool{false, true} { |
| 44 | name := "public" |
| 45 | if private { |
| 46 | name = "private" |
| 47 | } |
| 48 | if unavailable { |
| 49 | name += "-unavailable" |
| 50 | } |
| 51 | t.Run(name, func(t *testing.T) { |
| 52 | files := maps.Clone(mockups) |
| 53 | // Assemble a test token so the repository scan does not treat it as a credential. |
| 54 | files["mockups/config.js"] = `const token = "` + "ghp_" + strings.Join([]string{"Vw3K", "8pRt", "a9Qz", "6LmN", "2sDx", "7bJh", "4cFv", "0uEy", "5nWi"}, "") + `"; // gitleaks:allow` + "\n" |
| 55 | files["mockups/gitleaks.toml"] = "[allowlist]\nregexes = ['.*']\n" |
| 56 | e, fc, _, r := previewRig(t, files, nil) |
| 57 | var fd *fakeDevbox |
| 58 | if private { |
| 59 | fd = &fakeDevbox{t: t} |
| 60 | e.Devbox = &DevboxOptions{Carlos: "/carlos", State: filepath.Join(e.O.Runner.StateDir, "devbox"), Run: fd.run} |
| 61 | if err := os.MkdirAll(e.Devbox.State, 0700); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | } |
| 65 | want := "preview_secrets" |
| 66 | if unavailable { |
| 67 | e.O.Runner.Executable = filepath.Join(t.TempDir(), "missing-runner") |
| 68 | want = "preview_scanner" |
| 69 | } |
| 70 | built := false |
| 71 | rep := e.Exec(context.Background(), r, func(v wire.StepReport) { built = built || v.Preview != nil }) |
| 72 | if rep.State != wire.StepFailed || rep.Reason != want || !rep.Termination || built { |
| 73 | t.Fatalf("published unsafe export: %+v, built=%v", rep, built) |
| 74 | } |
| 75 | if len(fc.calls) != 0 || fd != nil && len(fd.calls) != 0 { |
| 76 | t.Fatalf("host called before scan: %v %+v", fc.calls, fd) |
| 77 | } |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestPreviewRefusalKeepsReason(t *testing.T) { |
| 84 | for _, reason := range []string{"preview_public_disabled", "preview_host"} { |
| 85 | if got := leaseReason(leaseRefusal(reason)); got != reason { |
| 86 | t.Fatalf("refusal %q became %q", reason, got) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestPermanentSiteKeepsHeaders(t *testing.T) { |
| 92 | const policy = "/*\n Content-Security-Policy: default-src 'self'; script-src 'self' https://scripts.example\n" |
| 93 | r := newSiteRig(t, map[string]string{"release/preview.json": `{"output":"site"}`, "site/index.html": "<h1>Site</h1>", "site/_headers": policy}) |
| 94 | rep := r.e.Exec(context.Background(), r.r, func(wire.StepReport) {}) |
| 95 | if rep.State != wire.StepDone { |
| 96 | t.Fatalf("site: %+v", rep) |
| 97 | } |
| 98 | raw, err := os.ReadFile(filepath.Join(filepath.Dir(r.calls), "served", "_headers")) |
| 99 | if err != nil || string(raw) != policy { |
| 100 | t.Fatalf("permanent site policy changed: %q, %v", raw, err) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestPreviewUnsupportedHeaders(t *testing.T) { |
| 105 | for _, host := range []string{"public-deploy", "public-ship", "private"} { |
| 106 | t.Run(host, func(t *testing.T) { |
| 107 | e, fc, _, r := previewRig(t, mockups, nil) |
| 108 | const refusal = "pack preview: _headers: line 2: header \"Content-Security-Policy\" is not supported yet" |
| 109 | if host == "private" { |
| 110 | e.Devbox = &DevboxOptions{Carlos: "/carlos", State: filepath.Join(e.O.Runner.StateDir, "devbox"), Run: func(context.Context, string, []string, []string) (string, error) { |
| 111 | return refusal, errors.New("unsupported headers") |
| 112 | }} |
| 113 | if err := os.MkdirAll(e.Devbox.State, 0700); err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | } else if host == "public-deploy" { |
| 117 | fc.fail["deploy"] = refusal |
| 118 | } else { |
| 119 | fc.fail["ship"] = refusal |
| 120 | } |
| 121 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 122 | if rep.Reason != "preview_headers" || rep.Preview != nil && rep.Preview.Outcome == wire.PreviewDeployed { |
| 123 | t.Fatalf("unsupported policy: %+v", rep) |
| 124 | } |
| 125 | }) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func TestPreviewRestartRejectsLegacyMarker(t *testing.T) { |
| 130 | e, fc, _, r := previewRig(t, mockups, nil) |
| 131 | fc.fail["ship"] = "interrupted" |
| 132 | e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 133 | stage := filepath.Join(e.root(), stepKey(r), "preview") |
| 134 | markPath := filepath.Join(stage, previewMarker) |
| 135 | raw, err := os.ReadFile(markPath) |
| 136 | if err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | var mark map[string]any |
| 140 | if err := json.Unmarshal(raw, &mark); err != nil { |
| 141 | t.Fatal(err) |
| 142 | } |
| 143 | delete(mark, "security") |
| 144 | raw, _ = json.Marshal(mark) |
| 145 | if err := os.WriteFile(markPath, raw, 0600); err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | fc.served = stage |
| 149 | before := len(fc.calls) |
| 150 | r.Phase = phasePreviewPublishing |
| 151 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 152 | if rep.Preview == nil || rep.Preview.Outcome != wire.PreviewUnconfirmed || rep.Preview.URL != "" || len(fc.calls) != before { |
| 153 | t.Fatalf("legacy preview certified after restart: %+v", rep.Preview) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func TestPreviewBundledScannerNoModuleCache(t *testing.T) { |
| 158 | e, fc, _, r := previewRig(t, mockups, nil) |
| 159 | e.O.Runner.GoBinary = "/missing/go" |
| 160 | t.Setenv("GOMODCACHE", filepath.Join(t.TempDir(), "absent-modules")) |
| 161 | t.Setenv("PATH", "/usr/bin:/bin") |
| 162 | t.Setenv("GITLEAKS_CONFIG", "/candidate/ignore.toml") |
| 163 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 164 | if rep.State != wire.StepDone || fc.served == "" { |
| 165 | t.Fatalf("bundle needed host Go cache: %+v", rep) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | func TestPreviewRequiresServedSafetyHeader(t *testing.T) { |
| 170 | for _, policy := range []string{"", "default-src *"} { |
| 171 | e, fc, _, r := previewRig(t, mockups, nil) |
| 172 | e.Preview.Client = &http.Client{Transport: headerTransport{base: http.DefaultTransport, policy: policy}} |
| 173 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 174 | if rep.Preview == nil || rep.Preview.Outcome != wire.PreviewUnconfirmed || rep.Reason != "preview_header_missing" || rep.Preview.URL != "" { |
| 175 | t.Fatalf("missing/changed served CSP certified: %+v", rep) |
| 176 | } |
| 177 | _ = fc |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | type headerTransport struct { |
| 182 | base http.RoundTripper |
| 183 | policy string |
| 184 | } |
| 185 | |
| 186 | func (t headerTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
| 187 | r, e := t.base.RoundTrip(req) |
| 188 | if e == nil { |
| 189 | r.Header.Set("Content-Security-Policy", t.policy) |
| 190 | } |
| 191 | return r, e |
| 192 | } |
| 193 | |
| 194 | func TestPreviewUnsafeTapStillScansAndBindsExport(t *testing.T) { |
| 195 | e, fc, _, r := previewRig(t, mockups, nil) |
| 196 | first := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 197 | if first.Preview == nil { |
| 198 | t.Fatal(first) |
| 199 | } |
| 200 | r.StepID = "s_pv2" |
| 201 | r.PreviewUnsafeDigest = first.Preview.Digest |
| 202 | if err := e.O.State.AcceptDispatch(context.Background(), "unsafe", r.DispatchCommand); err != nil { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | e.Preview.Client = &http.Client{Transport: headerTransport{base: http.DefaultTransport}} |
| 206 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 207 | if rep.Preview == nil || !rep.Preview.Unsafe || rep.Preview.Outcome != wire.PreviewDeployed { |
| 208 | t.Fatalf("owner tap refused: %+v", rep) |
| 209 | } |
| 210 | if _, err := os.Stat(filepath.Join(fc.shipped, "_headers")); !os.IsNotExist(err) { |
| 211 | t.Fatal("unsupported header still published", err) |
| 212 | } |
| 213 | r.StepID = "s_pv3" |
| 214 | r.PreviewUnsafeDigest = strings.Repeat("b", 64) |
| 215 | if err := e.O.State.AcceptDispatch(context.Background(), "wrong", r.DispatchCommand); err != nil { |
| 216 | t.Fatal(err) |
| 217 | } |
| 218 | before := len(fc.calls) |
| 219 | rep = e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 220 | if rep.Reason != "withdrawn" || len(fc.calls) != before { |
| 221 | t.Fatal("tap authorized another export", rep) |
| 222 | } |
| 223 | r.StepID = "s_pv4" |
| 224 | r.PreviewUnsafeDigest = first.Preview.Digest |
| 225 | e.O.Runner.Executable = "/missing/runner" |
| 226 | if err := e.O.State.AcceptDispatch(context.Background(), "scanner", r.DispatchCommand); err != nil { |
| 227 | t.Fatal(err) |
| 228 | } |
| 229 | rep = e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 230 | if rep.Reason != "preview_scanner" || len(fc.calls) != before { |
| 231 | t.Fatal("tap skipped scan", rep) |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | func TestPreviewAncestorOrigin(t *testing.T) { |
| 236 | for _, origin := range []string{"https://equipo.example", "https://other.example:8443", "http://127.0.0.1:4321"} { |
| 237 | if !strings.Contains(previewHeaders(origin), "frame-ancestors "+origin+"\n") { |
| 238 | t.Fatalf("origin missing: %s", previewHeaders(origin)) |
| 239 | } |
| 240 | } |
| 241 | for _, bad := range []string{"", "https://*.example", "https://eq.example/; default-src *", "https://user@eq.example", "https://eq.example?extra=1"} { |
| 242 | if !strings.Contains(previewHeaders(bad), "frame-ancestors 'none'\n") { |
| 243 | t.Fatalf("unsafe ancestor: %s", previewHeaders(bad)) |
| 244 | } |
| 245 | } |
| 246 | if previewSecurityStamp("https://one.example") == previewSecurityStamp("https://two.example") { |
| 247 | t.Fatal("restart certificate did not bind Equipo origin") |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | func TestPreviewRejectsDifferentAncestor(t *testing.T) { |
| 252 | e, _, _, r := previewRig(t, mockups, nil) |
| 253 | e.Preview.Client = &http.Client{Transport: headerTransport{base: http.DefaultTransport, policy: previewCSP("https://another-equipo.example")}} |
| 254 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 255 | if rep.Preview == nil || rep.Preview.Outcome != wire.PreviewUnconfirmed || rep.Reason != "preview_header_missing" { |
| 256 | t.Fatalf("different ancestor accepted: %+v", rep) |
| 257 | } |
| 258 | } |
| 259 | |