Store cookie durations as max_age_seconds

Replace the free-form duration TEXT column with a nullable
max_age_seconds INTEGER on both cookies and cookie_patterns
tables. The SDK detector now sends raw seconds instead of
humanized strings, eliminating locale-dependent comparisons
in the pattern merge worker. Humanization happens at display
time in the widget and console UI.

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-29 18:54:36 +04:00
parent b8ff3c66e1
commit 5cdaddf8b1
21 changed files with 308 additions and 151 deletions

View File

@@ -81,15 +81,15 @@ type (
CreateCookieRequest struct {
CookieCategoryID gid.GID
Name string
Duration string
MaxAgeSeconds *int
Description string
}
UpdateCookieRequest struct {
CookieID gid.GID
Name *string
Duration *string
Description *string
CookieID gid.GID
Name *string
MaxAgeSeconds **int
Description *string
}
ReorderCookieCategoryRequest struct {
@@ -107,14 +107,14 @@ type (
Pattern string
MatchType coredata.CookiePatternMatchType
DisplayName string
Duration string
MaxAgeSeconds *int
Description string
}
UpdateCookiePatternRequest struct {
CookiePatternID gid.GID
DisplayName *string
Duration *string
MaxAgeSeconds **int
Description *string
}
@@ -150,9 +150,9 @@ type (
}
DetectedCookie struct {
Name string
Duration string
Source coredata.CookieSource
Name string
MaxAgeSeconds *int
Source coredata.CookieSource
}
ReportDetectedCookiesRequest struct {
@@ -244,7 +244,6 @@ func (r *CreateCookieRequest) Validate() error {
v.Check(r.CookieCategoryID, "cookie_category_id", validator.Required(), validator.GID(coredata.CookieCategoryEntityType))
v.Check(r.Name, "name", validator.Required(), validator.SafeTextNoNewLine(255))
v.Check(r.Duration, "duration", validator.Required(), validator.SafeTextNoNewLine(255))
v.Check(r.Description, "description", validator.SafeText(1000))
return v.Error()
@@ -255,7 +254,6 @@ func (r *UpdateCookieRequest) Validate() error {
v.Check(r.CookieID, "cookie_id", validator.Required(), validator.GID(coredata.CookieEntityType))
v.Check(r.Name, "name", validator.SafeTextNoNewLine(255))
v.Check(r.Duration, "duration", validator.SafeTextNoNewLine(255))
v.Check(r.Description, "description", validator.SafeText(1000))
return v.Error()
@@ -286,7 +284,6 @@ func (r *CreateCookiePatternRequest) Validate() error {
}(),
))
v.Check(r.DisplayName, "display_name", validator.Required(), validator.SafeTextNoNewLine(255))
v.Check(r.Duration, "duration", validator.Required(), validator.SafeTextNoNewLine(255))
v.Check(r.Description, "description", validator.SafeText(1000))
return v.Error()
@@ -297,7 +294,6 @@ func (r *UpdateCookiePatternRequest) Validate() error {
v.Check(r.CookiePatternID, "cookie_pattern_id", validator.Required(), validator.GID(coredata.CookiePatternEntityType))
v.Check(r.DisplayName, "display_name", validator.SafeTextNoNewLine(255))
v.Check(r.Duration, "duration", validator.SafeTextNoNewLine(255))
v.Check(r.Description, "description", validator.SafeText(1000))
return v.Error()
@@ -411,9 +407,9 @@ func buildSnapshot(
cookiesByCategory[p.CookieCategoryID] = append(
cookiesByCategory[p.CookieCategoryID],
coredata.CookieItem{
Name: p.DisplayName,
Duration: p.Duration,
Description: p.Description,
Name: p.DisplayName,
MaxAgeSeconds: p.MaxAgeSeconds,
Description: p.Description,
},
)
}
@@ -662,6 +658,7 @@ func (s *Service) CreateCookieBanner(
slugToGID[dc.Slug] = category.ID
if dc.Kind == coredata.CookieCategoryKindNecessary {
consentMaxAge := req.ConsentExpiryDays * 86400
consentPattern := &coredata.CookiePattern{
ID: gid.New(scope.GetTenantID(), coredata.CookiePatternEntityType),
OrganizationID: banner.OrganizationID,
@@ -670,7 +667,7 @@ func (s *Service) CreateCookieBanner(
Pattern: "probo_consent",
MatchType: coredata.CookiePatternMatchTypeExact,
DisplayName: "probo_consent",
Duration: fmt.Sprintf("%d days", req.ConsentExpiryDays),
MaxAgeSeconds: &consentMaxAge,
Description: "Stores your cookie consent preferences for this website.",
Source: coredata.CookieSourceScript,
CreatedAt: now,
@@ -686,7 +683,7 @@ func (s *Service) CreateCookieBanner(
CookieBannerID: banner.ID,
CookiePatternID: consentPattern.ID,
Name: "probo_consent",
Duration: fmt.Sprintf("%d days", req.ConsentExpiryDays),
MaxAgeSeconds: &consentMaxAge,
Source: coredata.CookieSourceScript,
CreatedAt: now,
UpdatedAt: now,
@@ -1292,7 +1289,7 @@ func (s *Service) CreateCookie(
Pattern: req.Name,
MatchType: coredata.CookiePatternMatchTypeExact,
DisplayName: req.Name,
Duration: req.Duration,
MaxAgeSeconds: req.MaxAgeSeconds,
Description: req.Description,
Source: coredata.CookieSourceScript,
CreatedAt: now,
@@ -1312,7 +1309,7 @@ func (s *Service) CreateCookie(
CookieBannerID: category.CookieBannerID,
CookiePatternID: pattern.ID,
Name: req.Name,
Duration: req.Duration,
MaxAgeSeconds: req.MaxAgeSeconds,
Source: coredata.CookieSourceScript,
CreatedAt: now,
UpdatedAt: now,
@@ -1425,7 +1422,7 @@ func (s *Service) CreateCookiePattern(
Pattern: req.Pattern,
MatchType: req.MatchType,
DisplayName: req.DisplayName,
Duration: req.Duration,
MaxAgeSeconds: req.MaxAgeSeconds,
Description: req.Description,
Source: coredata.CookieSourceScript,
CreatedAt: now,
@@ -1477,8 +1474,8 @@ func (s *Service) UpdateCookiePattern(
if req.DisplayName != nil {
pattern.DisplayName = *req.DisplayName
}
if req.Duration != nil {
pattern.Duration = *req.Duration
if req.MaxAgeSeconds != nil {
pattern.MaxAgeSeconds = *req.MaxAgeSeconds
}
if req.Description != nil {
pattern.Description = *req.Description
@@ -1702,8 +1699,8 @@ func (s *Service) UpdateCookie(
return fmt.Errorf("cannot load cookie: %w", err)
}
if req.Duration != nil {
cookie.Duration = *req.Duration
if req.MaxAgeSeconds != nil {
cookie.MaxAgeSeconds = *req.MaxAgeSeconds
}
cookie.UpdatedAt = time.Now()
@@ -2637,7 +2634,7 @@ func (s *Service) ReportDetectedCookies(
Pattern: dc.Name,
MatchType: coredata.CookiePatternMatchTypeExact,
DisplayName: dc.Name,
Duration: dc.Duration,
MaxAgeSeconds: dc.MaxAgeSeconds,
Description: "",
Source: dc.Source,
CreatedAt: now,
@@ -2659,7 +2656,7 @@ func (s *Service) ReportDetectedCookies(
CookieBannerID: banner.ID,
CookiePatternID: patternID,
Name: dc.Name,
Duration: dc.Duration,
MaxAgeSeconds: dc.MaxAgeSeconds,
Source: dc.Source,
CreatedAt: now,
UpdatedAt: now,

View File

@@ -94,7 +94,7 @@ func (h *patternAnalysisHandler) Process(ctx context.Context, task coredata.Cook
merged := false
for prefix, group := range mergeGroups {
duration := mostCommonDuration(group)
maxAge := mostCommonMaxAge(group)
source := bestSource(group)
prefixPattern := &coredata.CookiePattern{
@@ -105,7 +105,7 @@ func (h *patternAnalysisHandler) Process(ctx context.Context, task coredata.Cook
Pattern: prefix,
MatchType: coredata.CookiePatternMatchTypePrefix,
DisplayName: prefix + "*",
Duration: duration,
MaxAgeSeconds: maxAge,
Description: "",
Source: source,
CreatedAt: time.Now(),
@@ -229,23 +229,35 @@ func bestSource(patterns []*coredata.CookiePattern) coredata.CookieSource {
return coredata.CookieSourcePreExisting
}
func mostCommonDuration(patterns []*coredata.CookiePattern) string {
counts := make(map[string]int)
func mostCommonMaxAge(patterns []*coredata.CookiePattern) *int {
type key struct {
valid bool
val int
}
counts := make(map[key]int)
for _, p := range patterns {
counts[p.Duration]++
k := key{}
if p.MaxAgeSeconds != nil {
k = key{valid: true, val: *p.MaxAgeSeconds}
}
counts[k]++
}
type entry struct {
duration string
count int
k key
count int
}
entries := make([]entry, 0, len(counts))
for d, c := range counts {
entries = append(entries, entry{d, c})
for k, c := range counts {
entries = append(entries, entry{k, c})
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].count > entries[j].count
})
return entries[0].duration
if !entries[0].k.valid {
return nil
}
v := entries[0].k.val
return &v
}

View File

@@ -35,7 +35,7 @@ type (
CookieBannerID gid.GID `db:"cookie_banner_id"`
CookiePatternID gid.GID `db:"cookie_pattern_id"`
Name string `db:"name"`
Duration string `db:"duration"`
MaxAgeSeconds *int `db:"max_age_seconds"`
Source CookieSource `db:"source"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
@@ -81,7 +81,7 @@ SELECT
cookie_banner_id,
cookie_pattern_id,
name,
duration,
max_age_seconds,
source,
created_at,
updated_at
@@ -130,7 +130,7 @@ SELECT
cookie_banner_id,
cookie_pattern_id,
name,
duration,
max_age_seconds,
source,
created_at,
updated_at
@@ -208,7 +208,7 @@ SELECT
cookie_banner_id,
cookie_pattern_id,
name,
duration,
max_age_seconds,
source,
created_at,
updated_at
@@ -289,7 +289,7 @@ SELECT
cookie_banner_id,
cookie_pattern_id,
name,
duration,
max_age_seconds,
source,
created_at,
updated_at
@@ -335,7 +335,7 @@ INSERT INTO cookies (
cookie_banner_id,
cookie_pattern_id,
name,
duration,
max_age_seconds,
source,
created_at,
updated_at
@@ -346,7 +346,7 @@ INSERT INTO cookies (
@cookie_banner_id,
@cookie_pattern_id,
@name,
@duration,
@max_age_seconds,
@source,
@created_at,
@updated_at
@@ -360,7 +360,7 @@ INSERT INTO cookies (
"cookie_banner_id": c.CookieBannerID,
"cookie_pattern_id": c.CookiePatternID,
"name": c.Name,
"duration": c.Duration,
"max_age_seconds": c.MaxAgeSeconds,
"source": c.Source,
"created_at": c.CreatedAt,
"updated_at": c.UpdatedAt,
@@ -392,7 +392,7 @@ INSERT INTO cookies (
cookie_banner_id,
cookie_pattern_id,
name,
duration,
max_age_seconds,
source,
created_at,
updated_at
@@ -403,7 +403,7 @@ INSERT INTO cookies (
@cookie_banner_id,
@cookie_pattern_id,
@name,
@duration,
@max_age_seconds,
@source,
@created_at,
@updated_at
@@ -420,7 +420,7 @@ ON CONFLICT (cookie_banner_id, name) DO UPDATE
"cookie_banner_id": c.CookieBannerID,
"cookie_pattern_id": c.CookiePatternID,
"name": c.Name,
"duration": c.Duration,
"max_age_seconds": c.MaxAgeSeconds,
"source": c.Source,
"source_script": CookieSourceScript,
"created_at": c.CreatedAt,
@@ -444,7 +444,7 @@ func (c *Cookie) Update(
UPDATE cookies
SET
cookie_pattern_id = @cookie_pattern_id,
duration = @duration,
max_age_seconds = @max_age_seconds,
updated_at = @updated_at
WHERE
%s
@@ -456,7 +456,7 @@ WHERE
args := pgx.StrictNamedArgs{
"id": c.ID,
"cookie_pattern_id": c.CookiePatternID,
"duration": c.Duration,
"max_age_seconds": c.MaxAgeSeconds,
"updated_at": c.UpdatedAt,
}
maps.Copy(args, scope.SQLArguments())

View File

@@ -31,9 +31,9 @@ import (
type (
CookieItem struct {
Name string `json:"name"`
Duration string `json:"duration"`
Description string `json:"description"`
Name string `json:"name"`
MaxAgeSeconds *int `json:"max_age_seconds"`
Description string `json:"description"`
}
CookieItems []CookieItem

View File

@@ -37,7 +37,7 @@ type (
Pattern string `db:"pattern"`
MatchType CookiePatternMatchType `db:"match_type"`
DisplayName string `db:"display_name"`
Duration string `db:"duration"`
MaxAgeSeconds *int `db:"max_age_seconds"`
Description string `db:"description"`
Source CookieSource `db:"source"`
CreatedAt time.Time `db:"created_at"`
@@ -86,7 +86,7 @@ SELECT
pattern,
match_type,
display_name,
duration,
max_age_seconds,
description,
source,
created_at,
@@ -138,7 +138,7 @@ SELECT
pattern,
match_type,
display_name,
duration,
max_age_seconds,
description,
source,
created_at,
@@ -201,7 +201,7 @@ SELECT
pattern,
match_type,
display_name,
duration,
max_age_seconds,
description,
source,
created_at,
@@ -281,7 +281,7 @@ SELECT
pattern,
match_type,
display_name,
duration,
max_age_seconds,
description,
source,
created_at,
@@ -330,7 +330,7 @@ INSERT INTO cookie_patterns (
pattern,
match_type,
display_name,
duration,
max_age_seconds,
description,
source,
created_at,
@@ -344,7 +344,7 @@ INSERT INTO cookie_patterns (
@pattern,
@match_type,
@display_name,
@duration,
@max_age_seconds,
@description,
@source,
@created_at,
@@ -361,7 +361,7 @@ INSERT INTO cookie_patterns (
"pattern": cp.Pattern,
"match_type": cp.MatchType,
"display_name": cp.DisplayName,
"duration": cp.Duration,
"max_age_seconds": cp.MaxAgeSeconds,
"description": cp.Description,
"source": cp.Source,
"created_at": cp.CreatedAt,
@@ -396,7 +396,7 @@ INSERT INTO cookie_patterns (
pattern,
match_type,
display_name,
duration,
max_age_seconds,
description,
source,
created_at,
@@ -410,7 +410,7 @@ INSERT INTO cookie_patterns (
@pattern,
@match_type,
@display_name,
@duration,
@max_age_seconds,
@description,
@source,
@created_at,
@@ -428,7 +428,7 @@ ON CONFLICT (cookie_banner_id, pattern) DO NOTHING
"pattern": cp.Pattern,
"match_type": cp.MatchType,
"display_name": cp.DisplayName,
"duration": cp.Duration,
"max_age_seconds": cp.MaxAgeSeconds,
"description": cp.Description,
"source": cp.Source,
"created_at": cp.CreatedAt,
@@ -453,7 +453,7 @@ UPDATE cookie_patterns
SET
cookie_category_id = @cookie_category_id,
display_name = @display_name,
duration = @duration,
max_age_seconds = @max_age_seconds,
description = @description,
updated_at = @updated_at
WHERE
@@ -467,7 +467,7 @@ WHERE
"id": cp.ID,
"cookie_category_id": cp.CookieCategoryID,
"display_name": cp.DisplayName,
"duration": cp.Duration,
"max_age_seconds": cp.MaxAgeSeconds,
"description": cp.Description,
"updated_at": cp.UpdatedAt,
}

View File

@@ -0,0 +1,19 @@
-- Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
--
-- Permission to use, copy, modify, and/or distribute this software for any
-- purpose with or without fee is hereby granted, provided that the above
-- copyright notice and this permission notice appear in all copies.
--
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
-- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
-- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
-- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-- PERFORMANCE OF THIS SOFTWARE.
ALTER TABLE cookies DROP COLUMN duration;
ALTER TABLE cookies ADD COLUMN max_age_seconds INTEGER;
ALTER TABLE cookie_patterns DROP COLUMN duration;
ALTER TABLE cookie_patterns ADD COLUMN max_age_seconds INTEGER;

View File

@@ -224,9 +224,9 @@ func (r *cookieBannerVersionResolver) Categories(ctx context.Context, obj *types
cookies := make([]*types.CookieBannerVersionCookie, len(cat.Cookies))
for j, c := range cat.Cookies {
cookies[j] = &types.CookieBannerVersionCookie{
Name: c.Name,
Duration: c.Duration,
Description: c.Description,
Name: c.Name,
MaxAgeSeconds: c.MaxAgeSeconds,
Description: c.Description,
}
}
@@ -754,7 +754,7 @@ func (r *mutationResolver) CreateCookiePattern(ctx context.Context, input types.
Pattern: input.Pattern,
MatchType: input.MatchType,
DisplayName: input.DisplayName,
Duration: input.Duration,
MaxAgeSeconds: input.MaxAgeSeconds,
Description: input.Description,
},
)
@@ -799,7 +799,7 @@ func (r *mutationResolver) UpdateCookiePattern(ctx context.Context, input types.
cookiebanner.UpdateCookiePatternRequest{
CookiePatternID: input.CookiePatternID,
DisplayName: input.DisplayName,
Duration: input.Duration,
MaxAgeSeconds: gqlutils.UnwrapOmittable(input.MaxAgeSeconds),
Description: input.Description,
},
)

View File

@@ -200,7 +200,7 @@ type CookiePattern implements Node {
pattern: String!
matchType: CookiePatternMatchType!
displayName: String!
duration: String!
maxAgeSeconds: Int
description: String!
source: CookieSource!
cookieCount: Int! @goField(forceResolver: true)
@@ -243,7 +243,7 @@ type CookieBannerVersionCategory {
type CookieBannerVersionCookie {
name: String!
duration: String!
maxAgeSeconds: Int
description: String!
}
@@ -434,14 +434,14 @@ input CreateCookiePatternInput {
pattern: String!
matchType: CookiePatternMatchType!
displayName: String!
duration: String!
maxAgeSeconds: Int
description: String!
}
input UpdateCookiePatternInput {
cookiePatternId: ID!
displayName: String
duration: String
maxAgeSeconds: Int @goField(omittable: true)
description: String
}

View File

@@ -69,13 +69,13 @@ func NewCookiePattern(cp *coredata.CookiePattern) *CookiePattern {
ID: cp.CookieBannerID,
},
},
Pattern: cp.Pattern,
MatchType: cp.MatchType,
DisplayName: cp.DisplayName,
Duration: cp.Duration,
Description: cp.Description,
Source: cp.Source,
CreatedAt: cp.CreatedAt,
UpdatedAt: cp.UpdatedAt,
Pattern: cp.Pattern,
MatchType: cp.MatchType,
DisplayName: cp.DisplayName,
MaxAgeSeconds: cp.MaxAgeSeconds,
Description: cp.Description,
Source: cp.Source,
CreatedAt: cp.CreatedAt,
UpdatedAt: cp.UpdatedAt,
}
}

View File

@@ -187,9 +187,9 @@ func (h *Handler) handlePostConsent(w http.ResponseWriter, r *http.Request) {
}
type detectedCookieEntry struct {
Name string `json:"name"`
Duration string `json:"duration"`
Source string `json:"source"`
Name string `json:"name"`
MaxAgeSeconds *int `json:"max_age_seconds"`
Source string `json:"source"`
}
type reportDetectedCookiesBody struct {
@@ -239,9 +239,9 @@ func (h *Handler) handleReportDetectedCookies(w http.ResponseWriter, r *http.Req
detected = append(
detected,
cookiebanner.DetectedCookie{
Name: name,
Duration: strings.TrimSpace(c.Duration),
Source: source,
Name: name,
MaxAgeSeconds: c.MaxAgeSeconds,
Source: source,
},
)
}