oficina / base Public

Abilities phase 1: Astra round 4's P2 (READY)

Round 4 found no P1. Its one P2: a source's address reached ask.js as
Equipo gave it but was percent-encoded by the server's template, so
"/wiki/Foo_(bar)" drew differently. Clean now normalizes a source URL
the way html/template does, so every client is handed the same address;
the browser parity test has the case.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 3a89adf9f450da0fcfec4fce7f7d59522acdcc1b parent 1659aa1
3 files changed, +24 −4
  • internal/browsertest/ask_lookup_test.go +1 −1
  • internal/equipo/lookups.go +20 −1
  • internal/equipo/lookups_test.go +3 −2
diff --git a/internal/browsertest/ask_lookup_test.go b/internal/browsertest/ask_lookup_test.go
index 4bd2bbd..165e455 100644
--- a/internal/browsertest/ask_lookup_test.go
+++ b/internal/browsertest/ask_lookup_test.go
@@ -51,7 +51,7 @@ func TestAskLookupMatchesServer(t *testing.T) {
order := []string{"Here it is.", "Both of them.", ""}
rig := sayLookups(t, map[string][]equipo.Lookup{
"Here it is.": {{Answer: "**Rain** later, see [Met](https://met.test/x) & <b>more</b>.",
- Sources: []equipo.Source{{URL: "https://www.met.test/dublin?d=1&e=2", Title: "Met Éireann · 日本 😀"}, {URL: "https://b.test/"}, {URL: "https://c.test/", Title: "Met\rNow\x00"}}}},
+ Sources: []equipo.Source{{URL: "https://www.met.test/dublin?d=1&e=2", Title: "Met Éireann · 日本 😀"}, {URL: "https://b.test/"}, {URL: "https://c.test/", Title: "Met\rNow\x00"}, {URL: "https://d.test/wiki/Foo_(bar)'s", Title: "Wiki"}}}},
"Both of them.": {
{Answer: "First:\n- one\n- *two*", Sources: []equipo.Source{{URL: "https://one.test/a", Title: "One"}}},
{Answer: "Second, https://two.test/x.", Sources: []equipo.Source{{URL: "https://two.test/", Title: "Two"}}},
diff --git a/internal/equipo/lookups.go b/internal/equipo/lookups.go
index f864323..4ebd507 100644
--- a/internal/equipo/lookups.go
+++ b/internal/equipo/lookups.go
@@ -1,6 +1,7 @@
package equipo
import (
+ "fmt"
"net/url"
"regexp"
"strings"
@@ -97,7 +98,25 @@ func sourceURL(raw string) string {
if u == "" || err != nil || p.Scheme != "https" || p.Port() != "" || !sourceHostRE.MatchString(strings.ToLower(p.Host)) {
return ""
}
- return u
+ return normalizeURL(u)
+}
+
+// normalizeURL percent-encodes every byte of u that Go's html/template
+// would encode in an href (all but letters, digits, -._~, the reserved
+// !#$&*+,/:;=?@[] and %), so the address the server draws and the one
+// ask.js and the app are handed are the same.
+func normalizeURL(u string) string {
+ var b strings.Builder
+ for i := 0; i < len(u); i++ {
+ c := u[i]
+ switch {
+ case 'a' <= c && c <= 'z', 'A' <= c && c <= 'Z', '0' <= c && c <= '9', strings.IndexByte("-._~!#$&*+,/:;=?@[]%", c) >= 0:
+ b.WriteByte(c)
+ default:
+ fmt.Fprintf(&b, "%%%02X", c)
+ }
+ }
+ return b.String()
}
// sourceHostRE is a source's host: a DNS name of two labels or more in
diff --git a/internal/equipo/lookups_test.go b/internal/equipo/lookups_test.go
index 6cfbc1e..d8fda38 100644
--- a/internal/equipo/lookups_test.go
+++ b/internal/equipo/lookups_test.go
@@ -64,11 +64,12 @@ func TestCleanLookupHTTPSOnly(t *testing.T) {
{Answer: "Kept", Sources: []equipo.Source{{URL: "http://plain.test/", Title: "a"}, {URL: "javascript:alert(1)", Title: "b"},
{URL: "/relative", Title: "c"}, {URL: "https://user:pw@x.test/", Title: "d"}, {URL: "https://café.test/", Title: "idn"},
{URL: "https://[::1]/", Title: "v6"}, {URL: "https://127.1/", Title: "v4"}, {URL: "https://ok.test:8443/", Title: "port"},
- {URL: "https://localhost/", Title: "bare name"}, {URL: "https://-x.test/", Title: "hyphen"}, {URL: "https://ok.test/a?b=1", Title: "e"}}},
+ {URL: "https://localhost/", Title: "bare name"}, {URL: "https://-x.test/", Title: "hyphen"},
+ {URL: "https://ok.test/wiki/Foo_(bar)'s", Title: "f"}, {URL: "https://ok.test/a?b=1", Title: "e"}}},
{Answer: "No sources", Sources: []equipo.Source{{URL: "http://plain.test/"}}},
{Answer: " ", Sources: []equipo.Source{src(1)}},
}}.Clean()
- want := []equipo.Lookup{{Answer: "Kept", Sources: []equipo.Source{{URL: "https://ok.test/a?b=1", Title: "e"}}}}
+ want := []equipo.Lookup{{Answer: "Kept", Sources: []equipo.Source{{URL: "https://ok.test/wiki/Foo_%28bar%29%27s", Title: "f"}, {URL: "https://ok.test/a?b=1", Title: "e"}}}}
if !reflect.DeepEqual(m.Lookups, want) {
t.Errorf("lookups %+v, want %+v", m.Lookups, want)
}