| 1 | //go:build browser |
| 2 | |
| 3 | package equipo |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "sync/atomic" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/rastrillo/rastrillo/harness" |
| 18 | "github.com/chromedp/chromedp" |
| 19 | ) |
| 20 | |
| 21 | func TestBrowserPreviewBlocksNavigation(t *testing.T) { |
| 22 | var hits atomic.Int32 |
| 23 | outside := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { hits.Add(1); fmt.Fprint(w, "escaped") })) |
| 24 | defer outside.Close() |
| 25 | for _, attack := range []string{"refresh", "location", "top"} { |
| 26 | t.Run(attack, func(t *testing.T) { |
| 27 | loaded := make(chan struct{}, 1) |
| 28 | preview := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 29 | select { |
| 30 | case loaded <- struct{}{}: |
| 31 | default: |
| 32 | } |
| 33 | switch attack { |
| 34 | case "refresh": |
| 35 | fmt.Fprintf(w, `<meta http-equiv="refresh" content="0;url=%s/?secret=fixture"><h1>Preview</h1>`, outside.URL) |
| 36 | case "location": |
| 37 | fmt.Fprintf(w, `<h1>Preview</h1><script>location=%q</script>`, outside.URL+"/?secret=fixture") |
| 38 | case "top": |
| 39 | fmt.Fprintf(w, `<h1>Preview</h1><script>top.location=%q</script>`, outside.URL+"/?secret=fixture") |
| 40 | } |
| 41 | })) |
| 42 | defer preview.Close() |
| 43 | policy, page, err := previewWrapper(preview.URL+"/", true) |
| 44 | if err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | parent := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 48 | w.Header().Set("Content-Security-Policy", policy) |
| 49 | w.Header().Set("Content-Type", "text/html") |
| 50 | fmt.Fprint(w, page) |
| 51 | })) |
| 52 | defer parent.Close() |
| 53 | profile := t.TempDir() |
| 54 | opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.ExecPath(harness.ChromePath(t)), chromedp.NoSandbox, chromedp.Flag("ignore-certificate-errors", true), chromedp.UserDataDir(profile)) |
| 55 | alloc, stop := chromedp.NewExecAllocator(context.Background(), opts...) |
| 56 | defer stop() |
| 57 | ctx, cancel := chromedp.NewContext(alloc) |
| 58 | defer cancel() |
| 59 | ctx, timeout := context.WithTimeout(ctx, 20*time.Second) |
| 60 | defer timeout() |
| 61 | if _, err := chromedp.Run(ctx, chromedp.Navigate(parent.URL)); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | select { |
| 65 | case <-loaded: |
| 66 | case <-ctx.Done(): |
| 67 | t.Fatal("preview never loaded") |
| 68 | } |
| 69 | var location string |
| 70 | // Two animation frames let the loaded document execute its navigation. |
| 71 | location, err = chromedp.Run(ctx, chromedp.Evaluate[string](`new Promise(r=>requestAnimationFrame(()=>requestAnimationFrame(()=>r(location.href))))`, chromedp.EvalAwaitPromise)) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if hits.Load() != 0 || !strings.HasPrefix(location, parent.URL) { |
| 76 | t.Fatalf("browser escaped wrapper: hits=%d location=%s", hits.Load(), location) |
| 77 | } |
| 78 | if dir := os.Getenv("EQUIPO_PREVIEW_SHOTS"); dir != "" { |
| 79 | var shot []byte |
| 80 | shot, err = chromedp.Run(ctx, chromedp.CaptureScreenshot()) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | _ = os.WriteFile(filepath.Join(dir, attack+".png"), shot, 0600) |
| 85 | } |
| 86 | }) |
| 87 | } |
| 88 | } |
| 89 | |