package summaryqa import ( "strings" "testing" ) func TestTicket60CitationNormalizationAndBounds(t *testing.T) { raw := []Citation{ {Title: " Useful \n source ", URL: " HTTPS://EXAMPLE.COM:443/report#part "}, {Title: "duplicate", URL: "https://example.com/report"}, } for _, u := range []string{"http://example.com", "javascript:alert(1)", "//example.com", "https:///missing", "https://user:secret@example.com", "https://bad_host.test", "https://-bad.test", "https://999.999.999.999/", "https://example.com/%00", "https://example.com/?q=%0a", "https://example.com/?q=%zz", "https://example.com:99999", "https://example.com/\x00", "https://example.com/" + strings.Repeat("x", MaxCitationURLBytes)} { raw = append(raw, Citation{Title: "invalid", URL: u}) } raw = append(raw, Citation{Title: strings.Repeat("é", MaxCitationTitleRunes+1), URL: "https://example.org"}) got := NormalizeCitations(raw) if len(got) != 2 || got[0] != (Citation{Title: "Useful source", URL: "https://example.com/report"}) || len([]rune(got[1].Title)) != MaxCitationTitleRunes { t.Fatalf("unsafe or unnormalized citations: %+v", got) } many := []Citation{} for i := 0; i < MaxCitations+10; i++ { many = append(many, Citation{Title: "source", URL: "https://example.com/" + strings.Repeat("x", i)}) } if len(NormalizeCitations(many)) != MaxCitations { t.Fatal("unbounded citations") } if NormalizeCitations(nil) == nil { t.Fatal("empty citations must encode as []") } } func TestTicket60CitationsRejectLocalHostsAndIPLiterals(t *testing.T) { for _, host := range []string{ "127.0.0.1", "127.1.2.3", "0.0.0.0", "10.0.0.1", "172.16.0.1", "172.31.255.254", "192.168.1.1", "169.254.169.254", "[::1]", "[::]", "[fc00::1]", "[fd12:3456::1]", "[fe80::1]", "[::ffff:127.0.0.1]", "localhost", "LOCALHOST:443", "app.localhost", "host.local", "host.LOCAL:8443", "printer", "intranet", "host.localdomain", "host.internal", "host.lan", "host.home", "host.corp", "host.intranet", "router.home.arpa", "host.test", "host.invalid", "host.example", "host.onion", "host.alt", "2130706433", "0177.0.0.1", "0x7f000001", "0x7f.0x0.0x0.0x1", "127.1", // The policy rejects every literal IP, including public addresses. "8.8.8.8", "[2606:4700:4700::1111]", } { t.Run(host, func(t *testing.T) { got := NormalizeCitations([]Citation{{Title: "source", URL: "https://" + host + "/report"}}) if len(got) != 0 { t.Fatalf("non-public host exposed as a citation: %s", host) } }) } for _, host := range []string{"www.reuters.com", "news.bbc.co.uk", "example.com", "xn--bcher-kva.de", "localhost.example.com"} { t.Run(host, func(t *testing.T) { if got := NormalizeCitations([]Citation{{Title: "source", URL: "https://" + host + "/report"}}); len(got) != 1 { t.Fatal("qualified DNS host rejected", host) } }) } }