package websource import ( "errors" "strings" "testing" ) func TestParseCanonicalParts(t *testing.T) { for _, c := range []struct { raw string want URL str string }{ {"https://Example.COM", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/"}, "https://example.com/"}, {"HTTPS://example.com./a/b?x=1#frag", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/a/b", RawQuery: "x=1"}, "https://example.com/a/b?x=1"}, {"https://example.com:443/", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/"}, "https://example.com/"}, {"http://shop.example.com:80/menu", URL{Scheme: "http", Host: "shop.example.com", Port: 80, EscapedPath: "/menu"}, "http://shop.example.com/menu"}, {"https://example.com/caf%C3%A9?q=a%20b", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/caf%C3%A9", RawQuery: "q=a%20b"}, "https://example.com/caf%C3%A9?q=a%20b"}, {"https://example.com/?", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/"}, "https://example.com/"}, {"https://xn--caf-dma.example/", URL{}, ""}, // .example refused, see below {"https://xn--caf-dma.com/", URL{Scheme: "https", Host: "xn--caf-dma.com", Port: 443, EscapedPath: "/"}, "https://xn--caf-dma.com/"}, {"https://example.com:/x", URL{Scheme: "https", Host: "example.com", Port: 443, EscapedPath: "/x"}, "https://example.com/x"}, } { got, err := Parse(c.raw) if c.str == "" { if err == nil { t.Errorf("Parse(%q) = %+v, want refusal", c.raw, got) } continue } if err != nil { t.Errorf("Parse(%q): %v", c.raw, err) continue } if got != c.want { t.Errorf("Parse(%q) = %+v, want %+v", c.raw, got, c.want) } if got.String() != c.str { t.Errorf("Parse(%q).String() = %q, want %q", c.raw, got.String(), c.str) } again, err := Parse(got.String()) if err != nil || again != got { t.Errorf("round trip of %q: %+v, %v", got.String(), again, err) } } } func TestParseRefusals(t *testing.T) { long := "https://example.com/" + strings.Repeat("a", MaxURL) for _, c := range []struct { raw string reason string }{ {"", "bad_url"}, {long, "url_too_long"}, {"ftp://example.com/", "scheme_refused"}, {"file:///etc/passwd", "scheme_refused"}, {"javascript:alert(1)", "scheme_refused"}, {"//example.com/", "scheme_refused"}, {"https:example.com", "bad_url"}, {"https:///path", "bad_url"}, {"https://user:pass@example.com/", "userinfo_refused"}, {"https://user@example.com/", "userinfo_refused"}, {"https://example.com:8080/", "port_refused"}, {"https://example.com:80/", "port_refused"}, {"http://example.com:443/", "port_refused"}, {"https://example.com:0443/", "port_refused"}, {"https://example.com/a b", "bad_url"}, {"https://example.com/a\\b", "bad_url"}, {"https://example.com/\x00", "bad_url"}, {"https://example.com/café", "bad_url"}, {"https://exämple.com/", "non_ascii_host"}, // IP literals in every form. {"https://127.0.0.1/", "ip_literal"}, {"https://93.184.216.34/", "ip_literal"}, {"http://2130706433/", "ip_literal"}, {"http://0177.0.0.1/", "ip_literal"}, {"http://0x7f.0.0.1/", "ip_literal"}, {"http://0x7f000001/", "ip_literal"}, {"http://1.0x7f/", "ip_literal"}, {"http://127.1/", "ip_literal"}, {"https://[::1]/", "ip_literal"}, {"https://[::ffff:10.0.0.1]/", "ip_literal"}, {"https://[fe80::1%25eth0]/", "ip_literal"}, {"https://%31%32%37.0.0.1/", "bad_url"}, // Names. {"https://localhost/", "host_refused"}, {"https://localhost./", "host_refused"}, {"https://app.localhost/", "host_refused"}, {"https://printer.local/", "host_refused"}, {"https://db.internal/", "host_refused"}, {"https://nas.lan/", "host_refused"}, {"https://router.home.arpa/", "host_refused"}, {"https://1.0.0.127.in-addr.arpa/", "host_refused"}, {"https://box.tailnet-example.ts.net/", "host_refused"}, {"https://abc.onion/", "host_refused"}, {"https://shop.test/", "host_refused"}, {"https://shop.invalid/", "host_refused"}, {"https://shop.example/", "host_refused"}, {"https://intranet/", "host_refused"}, {"https://example.123/", "host_refused"}, {"https://-bad.com/", "host_refused"}, {"https://bad-.com/", "host_refused"}, {"https://a..com/", "host_refused"}, {"https://example.com../", "host_refused"}, {"https://under_score.com/", "host_refused"}, {"https://" + strings.Repeat("a", 64) + ".com/", "host_refused"}, } { u, err := Parse(c.raw) if err == nil { t.Errorf("Parse(%q) = %+v, want %s", c.raw, u, c.reason) continue } if got := Reason(err); got != c.reason { t.Errorf("Parse(%q) reason %q (%v), want %q", c.raw, got, err, c.reason) } } } func TestHostname(t *testing.T) { for h, want := range map[string]bool{ "example.com": true, "a-b.example.com": true, "Example.com": false, "example": false, "1.2.3.4": false, "::1": false, "-a.com": false, "a_b.com": false, "": false, strings.Repeat("a.", 127) + "com": false, } { if got := Hostname(h); got != want { t.Errorf("Hostname(%q) = %v, want %v", h, got, want) } } } func TestResolve(t *testing.T) { base, err := Parse("https://shop.example.com/menu/today?x=1") if err != nil { t.Fatal(err) } for ref, want := range map[string]string{ "/style.css": "https://shop.example.com/style.css", "logo.png": "https://shop.example.com/menu/logo.png", "../about": "https://shop.example.com/about", "?page=2": "https://shop.example.com/menu/today?page=2", "//cdn.example.net/a.css": "https://cdn.example.net/a.css", "http://other.example.org/x#top": "http://other.example.org/x", "": "https://shop.example.com/menu/today?x=1", } { got, err := Resolve(base, ref) if err != nil { t.Errorf("Resolve(%q): %v", ref, err) continue } if got.String() != want { t.Errorf("Resolve(%q) = %q, want %q", ref, got.String(), want) } } for ref, reason := range map[string]string{ "javascript:alert(1)": "scheme_refused", "mailto:a@example.com": "scheme_refused", "data:image/png;base64,AA": "scheme_refused", "http://127.0.0.1/": "ip_literal", "//localhost/": "host_refused", "https://example.com:8080/": "port_refused", "https://u:p@example.com/": "userinfo_refused", "/" + strings.Repeat("a", MaxURL): "url_too_long", } { if u, err := Resolve(base, ref); err == nil || Reason(err) != reason { t.Errorf("Resolve(%q) = %+v, %v; want %s", ref, u, err, reason) } } } func TestProposal(t *testing.T) { if u, err := Proposal("https://shop.example.com/about"); err != nil || u.String() != "https://shop.example.com/about" { t.Fatalf("Proposal: %+v %v", u, err) } for raw, reason := range map[string]string{ "https://shop.example.com/?q=1": "query_refused", "https://shop.example.com/" + strings.Repeat("ab-", 34): "path_too_long", "https://u@shop.example.com/": "userinfo_refused", "https://shop.example.com/ghp_abcdefghijklmnopqrstuvwx": "secret_refused", "https://127.0.0.1/": "ip_literal", } { if u, err := Proposal(raw); err == nil || Reason(err) != reason { t.Errorf("Proposal(%q) = %+v, %v; want %s", raw, u, err, reason) } } // Exactly 100 bytes of path is allowed. if _, err := Proposal("https://shop.example.com/" + strings.Repeat("ab-", 33)); err != nil { t.Errorf("100-byte path: %v", err) } } func TestLooksSecret(t *testing.T) { for _, s := range []string{ "sk-ant-api03-abcdefghijklmnop", "sk_live_abcdefghijklmnop1234", "ghp_abcdefghijklmnopqrstuvwxyz0123", "github_pat_11ABCDEFG0123456789", "gho_abcdefghijklmnop1234", "glpat-abcdefghijklmnop1234", "xoxb-123456789012-abcdefghij", "AKIAIOSFODNN7EXAMPLE", "-----BEGIN OPENSSH PRIVATE KEY-----", "/x/-----BEGIN", "AIzaSyA1234567890abcdefghijklmnopqrstu", "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.sig", "/api/token=abc123", "/p/password:hunter2", "/a/Q2xhdWRlIGlzIGEgZ29vZCBib3QgMTIz", "/k/9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08", "/p/%67hp_abcdefghijklmnopqrstuvwxyz0123", } { if !LooksSecret(s) { t.Errorf("LooksSecret(%q) = false", s) } } for _, s := range []string{ "https://shop.example.com/about", "/blog/how-to-make-the-best-coffee-at-home-in-2026", "/products/supercalifragilisticexpialidocious", "/menu/opening-hours", "/images/logo-2024.png", "/skills/sk-ills", } { if LooksSecret(s) { t.Errorf("LooksSecret(%q) = true", s) } } } func TestReasonOfForeignError(t *testing.T) { if Reason(errors.New("x")) != "bad_url" || Reason(nil) != "" { t.Fatal("Reason fallback") } }