Add banner ID to probo_consent cookie

Include a `bid` field in the consent cookie so it explicitly
identifies which cookie banner it belongs to, making validation
direct instead of relying on the visitor ID as an implicit
discriminator. Existing cookies without `bid` self-heal on the
next load by falling through to the API fetch.

Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-04-24 18:36:45 +04:00
parent d9db93911b
commit d7eec08cd2
8 changed files with 54 additions and 1 deletions

View File

@@ -12,11 +12,17 @@
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE. // PERFORMANCE OF THIS SOFTWARE.
import { readFileSync } from "node:fs";
import * as esbuild from "esbuild"; import * as esbuild from "esbuild";
const { version } = JSON.parse(readFileSync("./package.json", "utf-8"));
const shared = { const shared = {
bundle: true, bundle: true,
target: "es2020", target: "es2020",
define: {
__SDK_VERSION__: JSON.stringify(version),
},
}; };
await Promise.all([ await Promise.all([

View File

@@ -132,7 +132,7 @@ export class CookieBannerClient {
this.startDetector(config); this.startDetector(config);
const cookie = getConsentCookie(); const cookie = getConsentCookie();
if (cookie && cookie.v === config.version && cookie.vid === this.visitorId) { if (cookie && cookie.bid === this.bannerId && cookie.v === config.version && cookie.vid === this.visitorId) {
this.consent = { this.consent = {
visitor_id: cookie.vid, visitor_id: cookie.vid,
version: cookie.v, version: cookie.v,
@@ -164,6 +164,7 @@ export class CookieBannerClient {
this._gpcApplied = apiConsent.action === "GPC"; this._gpcApplied = apiConsent.action === "GPC";
setConsentCookie( setConsentCookie(
{ {
bid: this.bannerId,
v: apiConsent.version, v: apiConsent.version,
vid: apiConsent.visitor_id, vid: apiConsent.visitor_id,
action: apiConsent.action, action: apiConsent.action,
@@ -270,6 +271,7 @@ export class CookieBannerClient {
setConsentCookie( setConsentCookie(
{ {
bid: this.bannerId,
v: cfg.version, v: cfg.version,
vid: this.visitorId, vid: this.visitorId,
action, action,
@@ -286,6 +288,7 @@ export class CookieBannerClient {
version: cfg.version, version: cfg.version,
action, action,
consent_data: consentData, consent_data: consentData,
sdk_version: __SDK_VERSION__,
}; };
void fetchJSON<ConsentRecord>(url, { method: "POST", body }) void fetchJSON<ConsentRecord>(url, { method: "POST", body })
.then(() => void flush(this.bannerId)) .then(() => void flush(this.bannerId))

View File

@@ -18,6 +18,7 @@ export const COOKIE_NAME = "probo_consent";
const SECONDS_PER_DAY = 86400; const SECONDS_PER_DAY = 86400;
export interface ConsentCookie { export interface ConsentCookie {
bid: string;
v: number; v: number;
vid: string; vid: string;
action: ConsentAction; action: ConsentAction;

15
packages/cookie-banner/src/globals.d.ts vendored Normal file
View File

@@ -0,0 +1,15 @@
// 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.
declare const __SDK_VERSION__: string;

View File

@@ -110,6 +110,7 @@ type (
UserAgent *string UserAgent *string
ConsentData json.RawMessage ConsentData json.RawMessage
Action coredata.CookieConsentAction Action coredata.CookieConsentAction
SdkVersion string
} }
RecordConsentRequest struct { RecordConsentRequest struct {
@@ -119,6 +120,7 @@ type (
UserAgent *string UserAgent *string
ConsentData json.RawMessage ConsentData json.RawMessage
Action coredata.CookieConsentAction Action coredata.CookieConsentAction
SdkVersion string
} }
DetectedCookie struct { DetectedCookie struct {
@@ -1675,6 +1677,7 @@ func (s *Service) CreateCookieConsentRecord(
UserAgent: req.UserAgent, UserAgent: req.UserAgent,
ConsentData: req.ConsentData, ConsentData: req.ConsentData,
Action: req.Action, Action: req.Action,
SdkVersion: req.SdkVersion,
CreatedAt: time.Now(), CreatedAt: time.Now(),
} }
@@ -2060,6 +2063,7 @@ func (s *Service) RecordConsent(
UserAgent: req.UserAgent, UserAgent: req.UserAgent,
ConsentData: req.ConsentData, ConsentData: req.ConsentData,
Action: req.Action, Action: req.Action,
SdkVersion: req.SdkVersion,
CreatedAt: time.Now(), CreatedAt: time.Now(),
} }

View File

@@ -39,6 +39,7 @@ type (
UserAgent *string `db:"user_agent"` UserAgent *string `db:"user_agent"`
ConsentData json.RawMessage `db:"consent_data"` ConsentData json.RawMessage `db:"consent_data"`
Action CookieConsentAction `db:"action"` Action CookieConsentAction `db:"action"`
SdkVersion string `db:"sdk_version"`
CreatedAt time.Time `db:"created_at"` CreatedAt time.Time `db:"created_at"`
} }
@@ -77,6 +78,7 @@ SELECT
user_agent, user_agent,
consent_data, consent_data,
action, action,
sdk_version,
created_at created_at
FROM FROM
cookie_consent_records cookie_consent_records
@@ -160,6 +162,7 @@ INSERT INTO cookie_consent_records (
user_agent, user_agent,
consent_data, consent_data,
action, action,
sdk_version,
created_at created_at
) VALUES ( ) VALUES (
@id, @id,
@@ -172,6 +175,7 @@ INSERT INTO cookie_consent_records (
@user_agent, @user_agent,
@consent_data, @consent_data,
@action, @action,
@sdk_version,
@created_at @created_at
) )
` `
@@ -187,6 +191,7 @@ INSERT INTO cookie_consent_records (
"user_agent": r.UserAgent, "user_agent": r.UserAgent,
"consent_data": r.ConsentData, "consent_data": r.ConsentData,
"action": r.Action, "action": r.Action,
"sdk_version": r.SdkVersion,
"created_at": r.CreatedAt, "created_at": r.CreatedAt,
} }
@@ -216,6 +221,7 @@ SELECT
user_agent, user_agent,
consent_data, consent_data,
action, action,
sdk_version,
created_at created_at
FROM FROM
cookie_consent_records cookie_consent_records

View File

@@ -0,0 +1,16 @@
-- 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 cookie_consent_records
ADD COLUMN sdk_version TEXT NOT NULL DEFAULT '';

View File

@@ -122,6 +122,7 @@ type (
Version int `json:"version"` Version int `json:"version"`
Action coredata.CookieConsentAction `json:"action"` Action coredata.CookieConsentAction `json:"action"`
ConsentData json.RawMessage `json:"consent_data"` ConsentData json.RawMessage `json:"consent_data"`
SdkVersion string `json:"sdk_version"`
} }
postConsentResponse struct { postConsentResponse struct {
@@ -155,6 +156,7 @@ func (h *Handler) handlePostConsent(w http.ResponseWriter, r *http.Request) {
UserAgent: &ua, UserAgent: &ua,
ConsentData: body.ConsentData, ConsentData: body.ConsentData,
Action: body.Action, Action: body.Action,
SdkVersion: body.SdkVersion,
} }
record, err := h.cookieBannerSvc.RecordConsent(r.Context(), bannerID, req) record, err := h.cookieBannerSvc.RecordConsent(r.Context(), bannerID, req)