Default cookie consent to GDPR and track its source

When IP geolocation does not resolve a country, or resolves one with no
known cookie-consent regulation (common on localhost and unmapped
regions), the banner previously fell back to OPT_OUT with no recorded
regulation. Apply GDPR (OPT_IN) as the safe default in that case so the
strictest consent model wins when origin is unknown.

To keep consent records auditable, stamp each one with a regulation
source of DETECTED (resolved from geolocation) or DEFAULT (fell back to
GDPR). The shared cookiebanner.ResolveRegulation helper centralizes the
decision for both the config and consent endpoints, and the new value is
exposed through GraphQL, MCP, the CLI, the n8n node, and the console
consent-records views.

Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
Émile Ré
2026-06-10 17:48:07 +02:00
parent e3ccb76b03
commit ed9831a734
18 changed files with 315 additions and 64 deletions

View File

@@ -36,11 +36,32 @@ const (
RegulationPDPL = coredata.RegulationPDPL
)
type RegulationSource = coredata.RegulationSource
const (
RegulationSourceDetected = coredata.RegulationSourceDetected
RegulationSourceDefault = coredata.RegulationSourceDefault
)
const (
ConsentModeOptIn = "OPT_IN"
ConsentModeOptOut = "OPT_OUT"
)
// ResolveRegulation returns the regulation to apply for a visitor along
// with its source. It defaults to GDPR when geolocation is unresolved
// (cc is nil) or when the resolved country maps to no known regulation,
// ensuring the strictest opt-in consent model applies by default.
func ResolveRegulation(cc *coredata.CountryCode) (Regulation, RegulationSource) {
if cc != nil {
if reg := RegulationForCountry(*cc); reg != RegulationNone {
return reg, RegulationSourceDetected
}
}
return RegulationGDPR, RegulationSourceDefault
}
// RegulationForCountry maps a country code to the applicable privacy
// regulation. For countries with no known cookie-consent regulation it
// returns RegulationNone.

View File

@@ -0,0 +1,74 @@
// Copyright (c) 2026 Probo Inc <hello@probo.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.
package cookiebanner
import (
"testing"
"github.com/stretchr/testify/require"
"go.probo.inc/probo/pkg/coredata"
)
func TestResolveRegulation(t *testing.T) {
t.Parallel()
tests := []struct {
name string
countryCode *coredata.CountryCode
wantRegulation Regulation
wantSource RegulationSource
}{
{
name: "unresolved geolocation defaults to GDPR",
countryCode: nil,
wantRegulation: RegulationGDPR,
wantSource: RegulationSourceDefault,
},
{
name: "country with no known regulation defaults to GDPR",
countryCode: new(coredata.CountryCodeAQ),
wantRegulation: RegulationGDPR,
wantSource: RegulationSourceDefault,
},
{
name: "EU country resolves to GDPR as detected",
countryCode: new(coredata.CountryCodeFR),
wantRegulation: RegulationGDPR,
wantSource: RegulationSourceDetected,
},
{
name: "US resolves to CCPA as detected",
countryCode: new(coredata.CountryCodeUS),
wantRegulation: RegulationCCPA,
wantSource: RegulationSourceDetected,
},
{
name: "UK resolves to UK GDPR as detected",
countryCode: new(coredata.CountryCodeGB),
wantRegulation: RegulationUKGDPR,
wantSource: RegulationSourceDetected,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
regulation, source := ResolveRegulation(tt.countryCode)
require.Equal(t, tt.wantRegulation, regulation)
require.Equal(t, tt.wantSource, source)
})
}
}

View File

@@ -96,16 +96,17 @@ type (
}
RecordConsentRequest struct {
Version int
VisitorID string
IPAddress *string
UserAgent *string
ConsentData json.RawMessage
Action coredata.CookieConsentAction
SdkVersion string
Regulation *Regulation
CountryCode *coredata.CountryCode
ConsentMode *coredata.CookieConsentMode
Version int
VisitorID string
IPAddress *string
UserAgent *string
ConsentData json.RawMessage
Action coredata.CookieConsentAction
SdkVersion string
Regulation *Regulation
RegulationSource coredata.RegulationSource
CountryCode *coredata.CountryCode
ConsentMode *coredata.CookieConsentMode
}
DetectedCookie struct {
@@ -2076,6 +2077,7 @@ func (s *Service) RecordConsent(
Action: req.Action,
SdkVersion: req.SdkVersion,
Regulation: req.Regulation,
RegulationSource: &req.RegulationSource,
CountryCode: req.CountryCode,
ConsentMode: req.ConsentMode,
CreatedAt: time.Now(),