Keep the connect link's rel to Base's own origin; add browser test
Card.CleanAt keeps rel oficina_connect only for BASE_ORIGIN plus /ask/oficina/connect, so another host with that path gets no same-tab treatment on the server either. A browser test covers the same-tab click and the lookalike. The new webtest cases share one app each.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+79
−29
internal/browsertest/oficina_connect_test.go+43 −0internal/equipo/chat.go+8 −10internal/web/ask.go+5 −2internal/web/ask_relay.go+7 −4internal/webtest/oficina_connect_test.go+16 −13
diff --git a/internal/browsertest/oficina_connect_test.go b/internal/browsertest/oficina_connect_test.go| new file mode 100644 |
| index 0000000..b5f20f7 |
| --- /dev/null |
| +++ b/internal/browsertest/oficina_connect_test.go |
| @@ -0,0 +1,43 @@ |
| +//go:build browser |
| + |
| +package browsertest |
| + |
| +import ( |
| + "strconv" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/chromedp/chromedp" |
| + |
| + "amadan.net/rastrillo/rastrillo/harness" |
| + |
| + "amadan.net/carlos/base/internal/equipo" |
| +) |
| + |
| +// A card's Connect link (rel oficina_connect) to Base's own connect page |
| +// opens in the same tab; the same path on another host gets no such |
| +// treatment and keeps its new tab. |
| +func TestAskOficinaConnectLinkIsSameTab(t *testing.T) { |
| + rig, f := askBase(t) |
| + openPanel(t, rig) |
| + f.OficinaConsent(f.URL + "/consent") |
| + id := f.AskOutcome("Connect Oficina to go on.", nil, []equipo.Link{ |
| + {Label: "Connect", URL: rig.Origin + "/ask/oficina/connect", Rel: "oficina_connect"}, |
| + {Label: "Lookalike", URL: "https://evil.example.test/ask/oficina/connect", Rel: "oficina_connect"}, |
| + }) |
| + card := `#ask-panel [data-card="` + id + `"]` |
| + drive(t, rig, chromedp.Poll[chromedp.Void](`document.querySelectorAll(`+strconv.Quote(card+` a.card-link`)+`).length === 2`, |
| + chromedp.WithPollingTimeout(10*time.Second))) |
| + got := harness.Evaluate[string](rig, `[...document.querySelectorAll(`+strconv.Quote(card+` a.card-link`)+`)] |
| + .map(a => a.textContent + ":" + (a.getAttribute("target") || "-")).join("|")`) |
| + if got != "Connect:-|Lookalike:_blank" { |
| + t.Fatalf("link targets %q, want Connect:-|Lookalike:_blank", got) |
| + } |
| + // Clicking it leaves this tab for the consent address. |
| + drive(t, rig, chromedp.Click(chromedp.CSS(card+` a.card-link`)), chromedp.Sleep(500*time.Millisecond)) |
| + drive(t, rig, chromedp.Poll[chromedp.Void](`location.href.startsWith(`+strconv.Quote(f.URL)+`)`, chromedp.WithPollingTimeout(10*time.Second))) |
| + if n := strings.Count(harness.Evaluate[string](rig, `location.href`), f.URL); n != 1 { |
| + t.Errorf("not on the consent address") |
| + } |
| +} |
diff --git a/internal/equipo/chat.go b/internal/equipo/chat.go| index 9e85387..f513dfe 100644 |
| --- a/internal/equipo/chat.go |
| +++ b/internal/equipo/chat.go |
| @@ -113,11 +113,16 @@ const OficinaConnectPath = "/ask/oficina/connect" |
| // Clean is the card as Base passes it on: its own fields only, never-null |
| // options and links, and a link only if it is an http(s) URL (WebURL), |
| -// with a rel only if it is LinkEquipo. A job card has no options, and is |
| +// with a rel only if it is LinkEquipo (or, for CleanAt, Base's own connect link). A job card has no options, and is |
| // kept only in a state Base knows; Detail and StartedAt are kept on a job |
| // card only, and Job only when it looks like a card id (validJob). ok is |
| // false for every other kind of card. |
| -func (c Card) Clean() (Card, bool) { |
| +func (c Card) Clean() (Card, bool) { return c.CleanAt("") } |
| + |
| +// CleanAt is Clean for Base at origin base (no trailing slash): a link with |
| +// rel LinkOficinaConnect keeps it only when it goes to exactly base plus |
| +// OficinaConnectPath. With base "" none does. |
| +func (c Card) CleanAt(base string) (Card, bool) { |
| switch c.Kind { |
| case CardQuestion, CardConfirm, CardOutcome, CardConnect: |
| case CardJob: |
| @@ -144,7 +149,7 @@ func (c Card) Clean() (Card, bool) { |
| for _, l := range c.Links { |
| if u := WebURL(l.URL); u != "" { |
| link := Link{Label: l.Label, URL: u} |
| - if l.Rel == LinkEquipo || l.Rel == LinkOficinaConnect && isConnectPath(u) { |
| + if l.Rel == LinkEquipo || l.Rel == LinkOficinaConnect && base != "" && u == base+OficinaConnectPath { |
| link.Rel = l.Rel |
| } |
| out.Links = append(out.Links, link) |
| @@ -153,13 +158,6 @@ func (c Card) Clean() (Card, bool) { |
| return out, true |
| } |
| -// isConnectPath reports whether raw, a URL WebURL accepted, has the path |
| -// of Base's connect route and no more than that. |
| -func isConnectPath(raw string) bool { |
| - u, err := url.Parse(raw) |
| - return err == nil && u.Path == OficinaConnectPath && u.RawQuery == "" && u.Fragment == "" |
| -} |
| - |
| // validJob reports whether id is 1 to 128 of [A-Za-z0-9_-]. |
| func validJob(id string) bool { |
| if id == "" || len(id) > 128 { |
diff --git a/internal/web/ask.go b/internal/web/ask.go| index cba514b..2a2d35d 100644 |
| --- a/internal/web/ask.go |
| +++ b/internal/web/ask.go |
| @@ -188,6 +188,9 @@ type askView struct { |
| Unread bool |
| // Notice is a line the chat says once (how connecting Oficina ended). |
| Notice string |
| + // base is Base's own origin: the only place a connect-Oficina link may |
| + // go and keep its rel. |
| + base string |
| } |
| // jobView is a job card as the chat draws it, a pill, with the cards that |
| @@ -285,7 +288,7 @@ func homeState(state string) string { |
| } |
| func (a *app) newAskView(state, next string) askView { |
| - v := askView{State: state, Next: next, Equipo: a.ask.client.Origin() + "/", Key: equipo.NewKey()} |
| + v := askView{base: a.cfg.Origin, State: state, Next: next, Equipo: a.ask.client.Origin() + "/", Key: equipo.NewKey()} |
| if a.cfg.Timezone != nil { |
| v.Zone = a.cfg.Timezone.String() |
| } |
| @@ -313,7 +316,7 @@ func (v *askView) fill(s equipo.Snapshot) { |
| var shown []equipo.Card |
| jobs := map[string]int{} |
| for _, c := range s.Cards { |
| - p, ok := c.Clean() |
| + p, ok := c.CleanAt(v.base) |
| switch { |
| case !ok || !p.Shown(): |
| case p.Kind == equipo.CardJob: |
diff --git a/internal/web/ask_relay.go b/internal/web/ask_relay.go| index 349ec98..72125c6 100644 |
| --- a/internal/web/ask_relay.go |
| +++ b/internal/web/ask_relay.go |
| @@ -159,7 +159,7 @@ func (a *app) askAnswer(w http.ResponseWriter, r *http.Request) { |
| Message *equipo.Message `json:"message"` |
| Turn *equipo.Turn `json:"turn"` |
| } |
| - out.Card, _ = in.Card.Clean() |
| + out.Card, _ = in.Card.CleanAt(a.cfg.Origin) |
| if in.Message.ID != "" { |
| out.Message = &in.Message |
| } |
| @@ -225,6 +225,7 @@ func (a *app) askEvents(w http.ResponseWriter, r *http.Request) { |
| if down.send("retry: 3000\n\n") != nil { |
| return |
| } |
| + seen.base = a.cfg.Origin |
| relay(down, res.Body, streamIdle, cancel, seen) |
| } |
| @@ -382,7 +383,7 @@ func (e sseEvent) frame(seen *turnsSeen) string { |
| if e.typ == "glance" { |
| data, ok = seen.glance(env.ID, env.Data) |
| } else { |
| - data, ok = project(e.typ, env.Data) |
| + data, ok = project(e.typ, env.Data, seen.base) |
| } |
| if !ok { |
| return "" |
| @@ -405,7 +406,7 @@ func (e sseEvent) frame(seen *turnsSeen) string { |
| // /ask/state uses; ok is false for every other kind of event, and for |
| // cards of a kind Base does not draw, or jobs in a state it does not know |
| // (equipo.Card.Clean). |
| -func project(typ string, raw json.RawMessage) (any, bool) { |
| +func project(typ string, raw json.RawMessage, base string) (any, bool) { |
| switch typ { |
| case "message": |
| var m equipo.Message |
| @@ -418,7 +419,7 @@ func project(typ string, raw json.RawMessage) (any, bool) { |
| if json.Unmarshal(raw, &c) != nil || c.ID == "" { |
| return nil, false |
| } |
| - return c.Clean() |
| + return c.CleanAt(base) |
| case "delta": |
| var d delta |
| return d, json.Unmarshal(raw, &d) == nil && d.TurnID != "" |
| @@ -460,6 +461,8 @@ const maxGlance = 60 |
| // no glance for a turn it does not know. |
| type turnsSeen struct { |
| live, past map[string]bool |
| + // base is Base's origin, for Card.CleanAt. |
| + base string |
| } |
| // seedTurns is what the snapshot s says of its turns. |
diff --git a/internal/webtest/oficina_connect_test.go b/internal/webtest/oficina_connect_test.go| index bd79328..f2a024f 100644 |
| --- a/internal/webtest/oficina_connect_test.go |
| +++ b/internal/webtest/oficina_connect_test.go |
| @@ -41,9 +41,9 @@ func TestOficinaConnectStartsAndRedirects(t *testing.T) { |
| } |
| func TestOficinaConnectNeverRedirectsAnywhereOdd(t *testing.T) { |
| + a, f := newAskApp(t) |
| + a.connectEquipo(f) |
| for _, bad := range []string{"http://evil.example/x", "javascript:alert(1)", "//evil.example/x", "/relative", "https://user:pw@evil.example/", "https:///nohost"} { |
| - a, f := newAskApp(t) |
| - a.connectEquipo(f) |
| f.OficinaConsent(bad) |
| r := a.get("/ask/oficina/connect") |
| if r.Code == 303 && r.Location() == bad || r.Code != 303 || r.Location() != "/" { |
| @@ -142,6 +142,8 @@ func TestOficinaDoneFinishesWithTheBearer(t *testing.T) { |
| } |
| func TestOficinaDoneOutcomes(t *testing.T) { |
| + a, f := newAskApp(t) |
| + a.connectEquipo(f) |
| for _, c := range []struct{ finish, want string }{ |
| {"connected", "Oficina connected."}, |
| {"denied", "Oficina wasn't connected."}, |
| @@ -149,8 +151,6 @@ func TestOficinaDoneOutcomes(t *testing.T) { |
| {"unavailable", "Couldn't reach Oficina. Try again later."}, |
| {"surprise", "Couldn't reach Oficina. Try again later."}, |
| } { |
| - a, f := newAskApp(t) |
| - a.connectEquipo(f) |
| f.FinishWith(c.finish) |
| if r := a.get(doneURL(fakeState(), fakeCode())); r.Code != 303 || r.Location() != "/" { |
| t.Fatalf("%s: %d %q", c.finish, r.Code, r.Location()) |
| @@ -162,28 +162,26 @@ func TestOficinaDoneOutcomes(t *testing.T) { |
| } |
| func TestOficinaDoneReturnedOutcomeWithoutCode(t *testing.T) { |
| + a, f := newAskApp(t) |
| + a.connectEquipo(f) |
| for param, want := range map[string]string{"denied": "Oficina wasn't connected.", "expired": "That link expired. Try again.", "unavailable": "Couldn't reach Oficina. Try again later."} { |
| - a, f := newAskApp(t) |
| - a.connectEquipo(f) |
| if r := a.get("/ask/oficina/done?oficina=" + param); r.Code != 303 || r.Location() != "/" { |
| t.Fatalf("%s: %d %q", param, r.Code, r.Location()) |
| } |
| - if n := len(f.Calls("/api/v1/assistant/oficina/connect/finish")); n != 0 { |
| - t.Errorf("%s: finished with no code", param) |
| - } |
| if home := a.get("/"); !strings.Contains(strings.ReplaceAll(home.Body, "'", "'"), want) { |
| t.Errorf("%s: home lacks %q", param, want) |
| } |
| } |
| + if n := len(f.Calls("/api/v1/assistant/oficina/connect/finish")); n != 0 { |
| + t.Errorf("finished with no code") |
| + } |
| // Only finishing connects: a bare "connected" says nothing. |
| - a, f := newAskApp(t) |
| + a, f = newAskApp(t) |
| a.connectEquipo(f) |
| if r := a.get("/ask/oficina/done?oficina=connected"); r.Code != 303 || r.Header.Get("Set-Cookie") != "" { |
| t.Errorf("bare connected: %d %v", r.Code, r.Header) |
| } |
| // Anything else says nothing. |
| - a, f = newAskApp(t) |
| - a.connectEquipo(f) |
| if r := a.get("/ask/oficina/done?oficina=<b>x</b>"); r.Code != 303 || r.Header.Get("Set-Cookie") != "" { |
| t.Errorf("odd outcome: %d %v", r.Code, r.Header) |
| } |
| @@ -215,7 +213,9 @@ func TestOficinaConnectCardLinkKeepsItsRel(t *testing.T) { |
| a.connectEquipo(f) |
| good := equipo.Link{Label: "Connect", URL: testOrigin + "/ask/oficina/connect", Rel: "oficina_connect"} |
| elsewhere := equipo.Link{Label: "Other", URL: "https://evil.example/x", Rel: "oficina_connect"} |
| - f.AskOutcome("Connect Oficina to go on.", nil, []equipo.Link{good, elsewhere}) |
| + // The same path on another host gets no same-tab treatment either. |
| + lookalike := equipo.Link{Label: "Lookalike", URL: "https://evil.example/ask/oficina/connect", Rel: "oficina_connect"} |
| + f.AskOutcome("Connect Oficina to go on.", nil, []equipo.Link{good, elsewhere, lookalike}) |
| body := a.get("/ask/state").Body |
| if !strings.Contains(body, `"rel":"oficina_connect"`) || strings.Count(body, `"rel":"oficina_connect"`) != 1 { |
| t.Fatalf("state: %s", body) |
| @@ -224,6 +224,9 @@ func TestOficinaConnectCardLinkKeepsItsRel(t *testing.T) { |
| if !strings.Contains(page, `<a class="card-link" href="`+testOrigin+`/ask/oficina/connect">Connect</a>`) { |
| t.Errorf("same-tab link missing:\n%s", page) |
| } |
| + if !strings.Contains(page, `href="https://evil.example/ask/oficina/connect" target="_blank" rel="noopener">Lookalike`) { |
| + t.Errorf("lookalike link got same-tab treatment:\n%s", page) |
| + } |
| if !strings.Contains(page, `href="https://evil.example/x" target="_blank" rel="noopener"`) { |
| t.Errorf("other link not opened in a new tab:\n%s", page) |
| } |