package summaryqa import ( "net" "net/url" "strconv" "strings" "unicode" "unicode/utf8" ) const MaxCitations = 16 const MaxCitationTitleRunes = 256 const MaxCitationURLBytes = 2048 type Citation struct { Title string `json:"title"` URL string `json:"url"` } type AnswerResult struct { Answer string `json:"answer"` WebSearchPerformed bool `json:"web_search_performed"` Citations []Citation `json:"citations"` } // NormalizeCitations accepts metadata from structured provider annotations only. // It never discovers URLs in answer text and never fetches a cited URL. func NormalizeCitations(raw []Citation) []Citation { result := make([]Citation, 0, min(len(raw), MaxCitations)) seen := make(map[string]bool) for _, citation := range raw { value := strings.TrimSpace(citation.URL) if len(value) > MaxCitationURLBytes || !utf8.ValidString(value) || strings.ContainsAny(value, "\\") || strings.IndexFunc(value, unicode.IsControl) >= 0 { continue } u, err := url.Parse(value) if err != nil || !strings.EqualFold(u.Scheme, "https") || u.Opaque != "" || u.User != nil || u.Host == "" { continue } query, queryErr := url.QueryUnescape(u.RawQuery) if queryErr != nil || !utf8.ValidString(u.Path) || !utf8.ValidString(query) || strings.IndexFunc(u.Path, unicode.IsControl) >= 0 || strings.IndexFunc(query, unicode.IsControl) >= 0 { continue } host := strings.ToLower(u.Hostname()) if !validCitationHost(host) { continue } port := u.Port() if port != "" { n, err := strconv.Atoi(port) if err != nil || n < 1 || n > 65535 { continue } } u.Scheme = "https" u.Host = host if port != "" && port != "443" { u.Host = net.JoinHostPort(host, port) } u.Fragment = "" u.RawFragment = "" if u.Path == "" { u.Path = "/" } normalized := u.String() if len(normalized) > MaxCitationURLBytes || seen[normalized] { continue } if !utf8.ValidString(citation.Title) { continue } title := strings.Join(strings.FieldsFunc(citation.Title, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsControl(r) }), " ") if title == "" { continue } if utf8.RuneCountInString(title) > MaxCitationTitleRunes { title = string([]rune(title)[:MaxCitationTitleRunes]) } seen[normalized] = true result = append(result, Citation{Title: title, URL: normalized}) if len(result) == MaxCitations { break } } return result } func validCitationHost(host string) bool { if host == "" || len(host) > 253 { return false } // Citation links accept qualified DNS names only. Reject every IP literal, // including public ones, rather than relying on incomplete IP range checks. // This is purely syntactic validation: never resolve or fetch citation URLs. if net.ParseIP(host) != nil { return false } host = strings.ToLower(host) labels := strings.Split(host, ".") if len(labels) < 2 { return false } tld := labels[len(labels)-1] switch tld { case "localhost", "local", "localdomain", "internal", "lan", "home", "corp", "intranet", "arpa", "test", "invalid", "example", "onion", "alt": return false } // Alphabetic (or IDNA) top-level labels also reject abbreviated, integer, // octal and hexadecimal IP spellings browsers may interpret as addresses. if !strings.HasPrefix(tld, "xn--") { if len(tld) < 2 { return false } for _, r := range tld { if r < 'a' || r > 'z' { return false } } } for _, label := range labels { if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' { return false } for _, r := range label { if !((r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-') { return false } } } return true }