Canonicalize cookie banner origin on save

Strip www. prefix and trailing slash from origin when creating or
updating a cookie banner so CORS lookups match regardless of whether
the customer's site redirects www to the apex domain.

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-13 16:50:43 +04:00
parent d05c3591d3
commit 41b57a61de

View File

@@ -19,6 +19,8 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"net/url"
"strings"
"time" "time"
"go.gearno.de/kit/pg" "go.gearno.de/kit/pg"
@@ -154,6 +156,23 @@ func (r *CreateCookieConsentRecordRequest) Validate() error {
return v.Error() return v.Error()
} }
func canonicalizeOrigin(raw string) string {
u, err := url.Parse(raw)
if err != nil {
return raw
}
host := u.Hostname()
host = strings.TrimPrefix(host, "www.")
port := u.Port()
if port != "" {
return u.Scheme + "://" + host + ":" + port
}
return u.Scheme + "://" + host
}
func buildSnapshot( func buildSnapshot(
banner *coredata.CookieBanner, banner *coredata.CookieBanner,
categories coredata.CookieCategories, categories coredata.CookieCategories,
@@ -250,7 +269,7 @@ func (s *Service) CreateCookieBanner(
ID: gid.New(scope.GetTenantID(), coredata.CookieBannerEntityType), ID: gid.New(scope.GetTenantID(), coredata.CookieBannerEntityType),
OrganizationID: req.OrganizationID, OrganizationID: req.OrganizationID,
Name: req.Name, Name: req.Name,
Origin: req.Origin, Origin: canonicalizeOrigin(req.Origin),
State: coredata.CookieBannerStateActive, State: coredata.CookieBannerStateActive,
PrivacyPolicyURL: req.PrivacyPolicyURL, PrivacyPolicyURL: req.PrivacyPolicyURL,
ConsentExpiryDays: req.ConsentExpiryDays, ConsentExpiryDays: req.ConsentExpiryDays,
@@ -413,7 +432,7 @@ func (s *Service) UpdateCookieBanner(
banner.Name = *req.Name banner.Name = *req.Name
} }
if req.Origin != nil { if req.Origin != nil {
banner.Origin = *req.Origin banner.Origin = canonicalizeOrigin(*req.Origin)
} }
if req.PrivacyPolicyURL != nil { if req.PrivacyPolicyURL != nil {
banner.PrivacyPolicyURL = *req.PrivacyPolicyURL banner.PrivacyPolicyURL = *req.PrivacyPolicyURL