package cfbrowser import ( "bytes" "context" "encoding/json" "errors" "image" "image/png" "io" "os" "path/filepath" "strings" "sync" "sync/atomic" "testing" "time" "unicode/utf8" "clusterflow.dev/cfbrowser/api" "clusterflow.dev/cfbrowser/policy" ) var testOwner = api.Owner{EnvironmentID: "e", ThreadID: "t", RunID: "r", SSHSessionID: "ssh1"} type fakeDriver struct { open func(context.Context, api.EngineOptions) (api.EngineSession, error) trusted bool } func (d *fakeDriver) Probe(ctx context.Context) (api.Capabilities, error) { if e := ctx.Err(); e != nil { return api.Capabilities{}, e } s := "unsupported" if d.trusted { s = "supported" } return api.Capabilities{Engine: "fake", Features: map[string]api.Capability{"trustedInput": {Status: s}}}, nil } func (d *fakeDriver) Open(ctx context.Context, o api.EngineOptions) (api.EngineSession, error) { if d.open != nil { return d.open(ctx, o) } return &fakeSession{page: &fakePage{}}, nil } type fakeSession struct { page *fakePage downloads []api.Download closed atomic.Bool failNew bool } func (s *fakeSession) NewPage(context.Context) (api.EnginePage, error) { if s.failNew { return nil, errors.New("new page failed") } return s.page, nil } func (s *fakeSession) Downloads() []api.Download { return s.downloads } func (s *fakeSession) Close() error { s.closed.Store(true); return nil } type fakePage struct { eval func(context.Context, string) (json.RawMessage, error) inputErr error png []byte inputCount atomic.Int32 } func (p *fakePage) Navigate(context.Context, string, string) (string, error) { return "https://example.org", nil } func (p *fakePage) Evaluate(ctx context.Context, script string) (json.RawMessage, error) { if p.eval != nil { return p.eval(ctx, script) } op, a := scriptArgs(script) var out any switch op { case "snapshot": out = api.Snapshot{SnapshotID: a["id"].(string), DocumentID: "doc", URL: "https://example.org", Kind: "dom-semantic", Elements: []api.Element{{Ref: "e1", Role: "textbox", Name: "Query"}}} case "prepare": out = map[string]any{"x": 1, "y": 2, "tag": "INPUT"} case "wait": out = true case "read": out = api.ReadResult{Text: "example"} default: out = map[string]any{} } b, e := json.Marshal(out) return b, e } func (p *fakePage) Input(context.Context, string, string, float64, float64) error { p.inputCount.Add(1) return p.inputErr } func (p *fakePage) Capture(context.Context) ([]byte, error) { if p.png != nil { return p.png, nil } var b bytes.Buffer _ = png.Encode(&b, image.NewRGBA(image.Rect(0, 0, 8, 8))) return b.Bytes(), nil } func (p *fakePage) Close() error { return nil } func scriptArgs(s string) (string, map[string]any) { s = s[strings.LastIndex(s, ";return run(")+12:] s = strings.TrimSuffix(s, ");})()") var args []json.RawMessage _ = json.Unmarshal([]byte("["+s+"]"), &args) var op string var a map[string]any if len(args) == 2 { _ = json.Unmarshal(args[0], &op) _ = json.Unmarshal(args[1], &a) } return op, a } func fixture(t *testing.T, trusted bool) (*Manager, *fakeSession, api.SessionInfo, api.PageInfo) { t.Helper() s := &fakeSession{page: &fakePage{}} d := &fakeDriver{trusted: trusted, open: func(context.Context, api.EngineOptions) (api.EngineSession, error) { return s, nil }} m, e := New(Config{Policy: policy.URLPolicy{AllowOrigins: []string{"https://example.org"}}}, d) if e != nil { t.Fatal(e) } t.Cleanup(func() { _ = m.Close() }) info, e := m.CreateSession(context.Background(), testOwner) if e != nil { t.Fatal(e) } p, e := m.NewPage(context.Background(), testOwner, info.ID) if e != nil { t.Fatal(e) } return m, s, info, p } func requireCode(t *testing.T, err error, code string) { t.Helper() var e *api.Error if !errors.As(err, &e) || e.Code != code { t.Fatalf("expected %s, got %v", code, err) } } func TestOwnershipAndQuotas(t *testing.T) { m, _, s, p := fixture(t, false) ctx := context.Background() other := testOwner other.RunID = "child" _, e := m.Snapshot(ctx, other, s.ID, p.PageID, api.SnapshotRequest{}) requireCode(t, e, "BROWSER_SCOPE_MISMATCH") if len(m.Sessions(other)) != 0 || len(m.Sessions(testOwner)) != 1 { t.Fatal("session ownership leak") } if _, e = m.CreateSession(ctx, api.Owner{}); e == nil { t.Fatal("invalid owner accepted") } if _, e = m.CreateSession(ctx, other); e != nil { t.Fatal(e) } if _, e = m.CreateSession(ctx, other); e == nil { t.Fatal("session quota ignored") } if _, e = m.NewPage(ctx, testOwner, s.ID); e != nil { t.Fatal(e) } if _, e = m.NewPage(ctx, testOwner, s.ID); e != nil { t.Fatal(e) } if _, e = m.NewPage(ctx, testOwner, s.ID); e == nil { t.Fatal("page quota ignored") } if e = m.CloseSession(other, s.ID); e == nil { t.Fatal("cross-owner close accepted") } if e = m.CloseSession(testOwner, s.ID); e != nil { t.Fatal(e) } _, e = m.Read(ctx, testOwner, s.ID, p.PageID, api.ReadRequest{}) requireCode(t, e, "BROWSER_SCOPE_MISMATCH") } func TestReferencesAndActions(t *testing.T) { for _, trusted := range []bool{false, true} { t.Run(map[bool]string{false: "synthetic", true: "browser"}[trusted], func(t *testing.T) { m, s, info, p := fixture(t, trusted) ctx := context.Background() snap, e := m.Snapshot(ctx, testOwner, info.ID, p.PageID, api.SnapshotRequest{}) if e != nil { t.Fatal(e) } r := api.ActionRequest{SnapshotID: snap.SnapshotID, Ref: "e1", Action: "fill", Value: "test"} result, e := m.Act(ctx, testOwner, info.ID, p.PageID, r) if e != nil || result.Effect != "dispatched" { t.Fatalf("action: %+v %v", result, e) } if trusted && s.page.inputCount.Load() != 1 { t.Fatal("browser input was not used") } r.Ref = "guessed" _, e = m.Act(ctx, testOwner, info.ID, p.PageID, r) requireCode(t, e, "BROWSER_STALE_REF") r.Ref = "e1" _, e = m.Snapshot(ctx, testOwner, info.ID, p.PageID, api.SnapshotRequest{}) if e != nil { t.Fatal(e) } _, e = m.Act(ctx, testOwner, info.ID, p.PageID, r) requireCode(t, e, "BROWSER_STALE_REF") if _, e = m.Navigate(ctx, testOwner, info.ID, p.PageID, "https://example.org", "goto"); e != nil { t.Fatal(e) } _, e = m.Act(ctx, testOwner, info.ID, p.PageID, r) requireCode(t, e, "BROWSER_STALE_REF") }) } } func TestUnknownActionOutcomeIsNotRetried(t *testing.T) { m, s, info, p := fixture(t, true) ctx := context.Background() snap, e := m.Snapshot(ctx, testOwner, info.ID, p.PageID, api.SnapshotRequest{}) if e != nil { t.Fatal(e) } s.page.inputErr = context.DeadlineExceeded r, e := m.Act(ctx, testOwner, info.ID, p.PageID, api.ActionRequest{SnapshotID: snap.SnapshotID, Ref: "e1", Action: "click"}) requireCode(t, e, "BROWSER_ACTION_OUTCOME_UNKNOWN") if r.Effect != "unknown" || s.page.inputCount.Load() != 1 { t.Fatal("unsafe replay or false success") } s.page.inputErr = api.Err("BROWSER_WORKER_EXITED", "worker disconnected") r, e = m.Act(ctx, testOwner, info.ID, p.PageID, api.ActionRequest{SnapshotID: snap.SnapshotID, Ref: "e1", Action: "click"}) requireCode(t, e, "BROWSER_ACTION_OUTCOME_UNKNOWN") if r.Effect != "unknown" || s.page.inputCount.Load() != 2 { t.Fatal("worker loss was reported as not dispatched") } } func TestClosePageReclaimsQuotaAndInvalidatesHandles(t *testing.T) { m, _, info, p := fixture(t, false) ctx := context.Background() if e := m.ClosePage(ctx, testOwner, info.ID, p.PageID); e != nil { t.Fatal(e) } if _, e := m.Snapshot(ctx, testOwner, info.ID, p.PageID, api.SnapshotRequest{}); e == nil { t.Fatal("closed page remained callable") } if len(m.Sessions(testOwner)[0].Pages) != 0 { t.Fatal("closed page retained quota") } if _, e := m.NewPage(ctx, testOwner, info.ID); e != nil { t.Fatal(e) } } func TestReadWaitAndInvalidRequests(t *testing.T) { m, _, s, p := fixture(t, false) ctx := context.Background() if v, e := m.Read(ctx, testOwner, s.ID, p.PageID, api.ReadRequest{}); e != nil || v.Text != "example" { t.Fatalf("read: %+v %v", v, e) } if e := m.Wait(ctx, testOwner, s.ID, p.PageID, api.WaitRequest{Kind: "ready"}); e != nil { t.Fatal(e) } for _, r := range []api.WaitRequest{{Kind: "bad"}, {Kind: "text", TimeoutMS: -1}} { if m.Wait(ctx, testOwner, s.ID, p.PageID, r) == nil { t.Fatal("invalid wait accepted") } } for _, r := range []api.SnapshotRequest{{Mode: "html"}, {MaxBytes: 12}, {MaxElements: 151}} { if _, e := m.Snapshot(ctx, testOwner, s.ID, p.PageID, r); e == nil { t.Fatal("invalid snapshot accepted") } } for _, r := range []api.ReadRequest{{Mode: "html"}, {MaxBytes: 1}, {Ref: "e1", SnapshotID: "bad"}} { if _, e := m.Read(ctx, testOwner, s.ID, p.PageID, r); e == nil { t.Fatal("invalid read accepted") } } if _, e := m.Navigate(ctx, testOwner, s.ID, p.PageID, "file:///etc/passwd", "goto"); e == nil { t.Fatal("unsafe navigation accepted") } if _, e := m.Navigate(ctx, testOwner, s.ID, p.PageID, "", "unknown"); e == nil { t.Fatal("invalid navigation accepted") } if _, e := m.Read(ctx, testOwner, s.ID, "missing", api.ReadRequest{}); e == nil { t.Fatal("unknown page accepted") } } func TestCancellationOfWaitAndPageQueue(t *testing.T) { m, s, info, p := fixture(t, false) started := make(chan struct{}) var once sync.Once s.page.eval = func(ctx context.Context, _ string) (json.RawMessage, error) { once.Do(func() { close(started) }) <-ctx.Done() return nil, ctx.Err() } ctx, cancel := context.WithCancel(context.Background()) done := make(chan error, 1) go func() { done <- m.Wait(ctx, testOwner, info.ID, p.PageID, api.WaitRequest{Kind: "text", Value: "never"}) }() <-started queued, stop := context.WithTimeout(context.Background(), 20*time.Millisecond) defer stop() _, e := m.Snapshot(queued, testOwner, info.ID, p.PageID, api.SnapshotRequest{}) if !errors.Is(e, context.DeadlineExceeded) { t.Fatal(e) } cancel() select { case e := <-done: if !errors.Is(e, context.Canceled) { t.Fatal(e) } case <-time.After(time.Second): t.Fatal("wait did not cancel") } } func TestConcurrentQuotaReservationAndShutdown(t *testing.T) { started := make(chan struct{}) d := &fakeDriver{open: func(ctx context.Context, _ api.EngineOptions) (api.EngineSession, error) { close(started) <-ctx.Done() return nil, ctx.Err() }} m, e := New(Config{MaxSessions: 1}, d) if e != nil { t.Fatal(e) } defer m.Close() done := make(chan error, 1) go func() { _, e := m.CreateSession(context.Background(), testOwner); done <- e }() <-started _, e = m.CreateSession(context.Background(), testOwner) requireCode(t, e, "BROWSER_LIMIT") if e = m.Close(); e != nil { t.Fatal(e) } if e = m.Close(); e != nil { t.Fatal(e) } select { case e := <-done: if e == nil { t.Fatal("canceled open succeeded") } case <-time.After(time.Second): t.Fatal("shutdown blocked") } _, e = m.CreateSession(context.Background(), testOwner) requireCode(t, e, "BROWSER_CLOSED") } func TestBudgetsAndUTF8(t *testing.T) { s := api.Snapshot{SnapshotID: "id", URL: strings.Repeat("u", 8000), Title: strings.Repeat("题", 200), Text: strings.Repeat("科学\"\n", 2000), Elements: []api.Element{}} for i := 0; i < 150; i++ { s.Elements = append(s.Elements, api.Element{Ref: "e", Name: strings.Repeat("名", 256)}) } if e := fitSnapshot(&s, 1024); e != nil { t.Fatal(e) } b, _ := json.Marshal(s) if len(b) > 1024 || !s.Truncated || !utf8.ValidString(s.Text) { t.Fatal("invalid snapshot budget") } for i := 0; i < 10; i++ { if !utf8.ValidString(truncate("科学test", i)) { t.Fatal("UTF-8 split") } } if fitSnapshot(&api.Snapshot{}, 1) == nil { t.Fatal("impossible budget accepted") } if decode(json.RawMessage(`{"error":"BROWSER_STALE_REF"}`), new(any)) == nil { t.Fatal("page error ignored") } if decode(json.RawMessage(strings.Repeat(" ", 513*1024)), new(any)) == nil { t.Fatal("oversized result accepted") } } func TestConfigurationAndEngineFailure(t *testing.T) { if _, e := New(Config{}, nil); e == nil { t.Fatal("nil driver accepted") } for _, cfg := range []Config{{MaxSessions: -1}, {MaxPages: 99}, {MaxResultBytes: 1}, {MaxDownloadBytes: -1}, {Timeout: 6 * time.Minute}} { if _, e := New(cfg, &fakeDriver{}); e == nil { t.Fatal("invalid config accepted") } } m, e := New(Config{DataDir: t.TempDir()}, &fakeDriver{open: func(context.Context, api.EngineOptions) (api.EngineSession, error) { return nil, errors.New("startup failed") }}) if e != nil { t.Fatal(e) } defer m.Close() if _, e = m.CreateSession(context.Background(), testOwner); e == nil { t.Fatal("engine failure hidden") } if _, e = m.Capabilities(context.Background()); e != nil { t.Fatal(e) } } func TestManagedFilesAndSecureExport(t *testing.T) { m, _, s, p := fixture(t, false) ctx := context.Background() file, e := m.Capture(ctx, testOwner, s.ID, p.PageID) if e != nil { t.Fatal(e) } if file.Size == 0 || file.SHA256 == "" { t.Fatal("missing file metadata") } files, e := m.Files(ctx, testOwner, s.ID) if e != nil || len(files) != 1 { t.Fatal(files, e) } dest := t.TempDir() if _, e = m.Export(ctx, testOwner, s.ID, file.ID, dest, "capture.png"); e != nil { t.Fatal(e) } if _, e = m.Export(ctx, testOwner, s.ID, file.ID, dest, "capture.png"); e == nil { t.Fatal("overwrite allowed") } for _, path := range []string{"../escape", "/tmp/escape", "a/../b", "a\\b", "a//b"} { if _, e = m.Export(ctx, testOwner, s.ID, file.ID, dest, path); e == nil { t.Fatalf("unsafe path accepted: %s", path) } } outside := t.TempDir() if os.Symlink(outside, filepath.Join(dest, "link")) == nil { if _, e = m.Export(ctx, testOwner, s.ID, file.ID, dest, "link/escaped.png"); e == nil { t.Fatal("symlink escape allowed") } } if _, e = m.Export(ctx, testOwner, s.ID, "unknown", dest, "x"); e == nil { t.Fatal("unknown file exported") } if safeName("../../a\\b\x00.cif") != "b.cif" { t.Fatal("unsafe suggested name") } } func TestFileLimitsDownloadImportAndCanceledCopy(t *testing.T) { m, s, info, p := fixture(t, false) ctx := context.Background() owned, _ := m.owned(testOwner, info.ID) if _, e := m.store(owned, "big.bin", "", strings.NewReader("1234"), 3); e == nil { t.Fatal("file limit ignored") } if e := os.WriteFile(filepath.Join(owned.downloadDir, "guid"), []byte("download"), 0600); e != nil { t.Fatal(e) } s.downloads = []api.Download{{ID: "guid", Name: "../../data.cif", Path: filepath.Join(owned.downloadDir, "guid"), State: "completed"}} files, e := m.Files(ctx, testOwner, info.ID) if e != nil || len(files) != 1 || files[0].Name != "data.cif" { t.Fatal(files, e) } files, e = m.Files(ctx, testOwner, info.ID) if e != nil || len(files) != 1 { t.Fatal("duplicate import", e) } s.page.png = []byte("bad") if _, e = m.Capture(ctx, testOwner, info.ID, p.PageID); e == nil { t.Fatal("invalid PNG stored") } stopped, cancel := context.WithCancel(ctx) cancel() if _, e = copyContext(stopped, io.Discard, strings.NewReader("data")); !errors.Is(e, context.Canceled) { t.Fatal(e) } var dst bytes.Buffer n, e := copyContext(ctx, &dst, strings.NewReader(strings.Repeat("x", 70000))) if e != nil || n != 70000 { t.Fatal(n, e) } }