package server import ( "encoding/base64" "encoding/hex" "encoding/json" "errors" "net/url" "sort" "strings" ) const externalResourceApprovalsConfig = "external_resource_approvals_v1" const maxExternalResourceApprovals = 128 type externalResourceApproval struct { Kind string `json:"kind"` URL string `json:"url"` Integrity string `json:"integrity,omitempty"` } type externalResourcePolicy struct { items []externalResourceApproval byKind map[string]map[string]externalResourceApproval } func normalizeExternalResourceURL(raw string) (string, error) { raw = strings.TrimSpace(raw) if raw == "" || len(raw) > 2048 || strings.ContainsAny(raw, " \\\r\n\t") { return "", errors.New("invalid external resource URL") } u, err := url.Parse(raw) if err != nil || !u.IsAbs() || !strings.EqualFold(u.Scheme, "https") || u.Host == "" || u.User != nil || u.Fragment != "" { return "", errors.New("external resources require an absolute HTTPS URL without credentials or fragments") } u.Scheme = "https" u.Host = strings.ToLower(u.Host) u.RawFragment = "" return u.String(), nil } func normalizeExternalResourceIntegrity(raw string) (string, error) { raw = strings.TrimSpace(raw) if len(raw) == 64 { decoded, err := hex.DecodeString(raw) if err == nil && len(decoded) == 32 { return "sha256-" + base64.StdEncoding.EncodeToString(decoded), nil } } if !strings.HasPrefix(raw, "sha256-") { return "", errors.New("SHA-256 integrity is required") } decoded, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(raw, "sha256-")) if err != nil || len(decoded) != 32 { return "", errors.New("invalid SHA-256 integrity") } return "sha256-" + base64.StdEncoding.EncodeToString(decoded), nil } func normalizeExternalResourceApproval(item externalResourceApproval) (externalResourceApproval, error) { item.Kind = strings.ToLower(strings.TrimSpace(item.Kind)) if item.Kind != "image" && item.Kind != "stylesheet" && item.Kind != "script" { return externalResourceApproval{}, errors.New("invalid external resource kind") } var err error item.URL, err = normalizeExternalResourceURL(item.URL) if err != nil { return externalResourceApproval{}, err } if item.Kind == "image" { item.Integrity = "" return item, nil } item.Integrity, err = normalizeExternalResourceIntegrity(item.Integrity) if err != nil { return externalResourceApproval{}, err } return item, nil } func newExternalResourcePolicy(items []externalResourceApproval) externalResourcePolicy { p := externalResourcePolicy{byKind: map[string]map[string]externalResourceApproval{}} seen := map[string]bool{} for _, raw := range items { item, err := normalizeExternalResourceApproval(raw) if err != nil { continue } key := item.Kind + "\x00" + item.URL if seen[key] || len(p.items) >= maxExternalResourceApprovals { continue } seen[key] = true if p.byKind[item.Kind] == nil { p.byKind[item.Kind] = map[string]externalResourceApproval{} } p.byKind[item.Kind][item.URL] = item p.items = append(p.items, item) } sort.Slice(p.items, func(i, j int) bool { if p.items[i].Kind == p.items[j].Kind { return p.items[i].URL < p.items[j].URL } return p.items[i].Kind < p.items[j].Kind }) return p } func (p externalResourcePolicy) approved(kind, rawURL string) (externalResourceApproval, bool) { normalized, err := normalizeExternalResourceURL(rawURL) if err != nil { return externalResourceApproval{}, false } item, ok := p.byKind[kind][normalized] return item, ok } func (s *Server) externalResourcePolicy(domainUUID string) externalResourcePolicy { var items []externalResourceApproval raw := strings.TrimSpace(s.getDomainConfig(externalResourceApprovalsConfig, domainUUID)) if raw != "" { _ = json.Unmarshal([]byte(raw), &items) } return newExternalResourcePolicy(items) } func (s *Server) saveExternalResourceApprovals(domainUUID string, items []externalResourceApproval) error { policy := newExternalResourcePolicy(items) if len(policy.items) != len(items) { return errors.New("invalid or duplicate external resource approval") } raw, err := json.Marshal(policy.items) if err != nil { return err } return s.store.SetConfigInDomain(domainUUID, externalResourceApprovalsConfig, string(raw)) }