Files
probo/pkg/coredata/detected_tracker.go
Sacha Al Himdani 4c57d201a4 Make license declarations consistently MIT
The source headers, LICENSE files, and license metadata had drifted
apart. Align the entire project to MIT:

- Convert every source-file header to the MIT text across all comment
  styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including
  SPDX-License-Identifier tags
- Set the root and cookie-banner LICENSE files to the MIT text with a
  "MIT License" title line
- Switch the package.json license fields, Docker image label, and
  cookie-banner README to MIT
- Update docs and the genmodels header generator accordingly
- Normalize copyright lines to a single format
  (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the
  hello@getprobo.com and hello@probo.inc emails to hello@probo.com and
  the comma-separated years to a hyphenated range

Genuine third-party references are intentionally left untouched: the
Lucide icon attributions (Lucide is ISC) and the trivy dependency
license allowlist.

Signed-off-by: Sacha Al Himdani <sacha@probo.com>
2026-07-13 16:21:14 +02:00

377 lines
9.2 KiB
Go

// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package coredata
import (
"context"
"fmt"
"maps"
"time"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/page"
)
type (
DetectedTracker struct {
ID gid.GID `db:"id"`
CookieBannerID gid.GID `db:"cookie_banner_id"`
TrackerPatternID *gid.GID `db:"tracker_pattern_id"`
TrackerType TrackerType `db:"tracker_type"`
Identifier string `db:"identifier"`
MaxAgeSeconds *int `db:"max_age_seconds"`
Source *CookieSource `db:"source"`
ValueSize *int `db:"value_size"`
InitiatorURL *string `db:"initiator_url"`
InitiatorDomain *string `db:"initiator_domain"`
LastDetectedAt time.Time `db:"last_detected_at"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
DetectedTrackers []*DetectedTracker
)
func (dt *DetectedTracker) CursorKey(field DetectedTrackerOrderField) page.CursorKey {
switch field {
case DetectedTrackerOrderFieldInitiatorURL:
if dt.InitiatorURL == nil {
return page.NewCursorKey(dt.ID, "")
}
return page.NewCursorKey(dt.ID, *dt.InitiatorURL)
case DetectedTrackerOrderFieldLastDetectedAt:
return page.NewCursorKey(dt.ID, dt.LastDetectedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", field))
}
func (dt *DetectedTracker) Upsert(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) (bool, error) {
q := `
INSERT INTO detected_trackers (
id,
tenant_id,
cookie_banner_id,
tracker_pattern_id,
tracker_type,
identifier,
max_age_seconds,
source,
value_size,
initiator_url,
initiator_domain,
last_detected_at,
created_at,
updated_at
) VALUES (
@id,
@tenant_id,
@cookie_banner_id,
@tracker_pattern_id,
@tracker_type,
@identifier,
@max_age_seconds,
@source,
@value_size,
@initiator_url,
@initiator_domain,
@last_detected_at,
@created_at,
@updated_at
)
ON CONFLICT (cookie_banner_id, tracker_type, identifier) DO UPDATE
SET last_detected_at = EXCLUDED.last_detected_at,
source = CASE WHEN detected_trackers.source IS NULL OR (
detected_trackers.source != @source_script AND EXCLUDED.source = @source_script
) THEN EXCLUDED.source
ELSE detected_trackers.source
END,
initiator_url = COALESCE(EXCLUDED.initiator_url, detected_trackers.initiator_url),
initiator_domain = COALESCE(EXCLUDED.initiator_domain, detected_trackers.initiator_domain),
updated_at = EXCLUDED.updated_at
`
args := pgx.StrictNamedArgs{
"id": dt.ID,
"tenant_id": scope.GetTenantID(),
"cookie_banner_id": dt.CookieBannerID,
"tracker_pattern_id": dt.TrackerPatternID,
"tracker_type": dt.TrackerType,
"identifier": dt.Identifier,
"max_age_seconds": dt.MaxAgeSeconds,
"source": dt.Source,
"source_script": CookieSourceScript,
"value_size": dt.ValueSize,
"initiator_url": dt.InitiatorURL,
"initiator_domain": dt.InitiatorDomain,
"last_detected_at": dt.LastDetectedAt,
"created_at": dt.CreatedAt,
"updated_at": dt.UpdatedAt,
}
result, err := tx.Exec(ctx, q, args)
if err != nil {
return false, fmt.Errorf("cannot upsert detected tracker: %w", err)
}
return result.RowsAffected() > 0, nil
}
func (dts *DetectedTrackers) CountByTrackerPatternID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
trackerPatternID gid.GID,
) (int, error) {
q := `
SELECT
COUNT(id)
FROM
detected_trackers
WHERE
%s
AND tracker_pattern_id = @tracker_pattern_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"tracker_pattern_id": trackerPatternID}
maps.Copy(args, scope.SQLArguments())
row := conn.QueryRow(ctx, q, args)
var count int
if err := row.Scan(&count); err != nil {
return 0, fmt.Errorf("cannot scan count: %w", err)
}
return count, nil
}
func (dts *DetectedTrackers) LoadInitiatorDomainsByTrackerPatternID(
ctx context.Context,
conn pg.Querier,
trackerPatternID gid.GID,
limit int,
) ([]string, error) {
q := `
SELECT DISTINCT initiator_domain
FROM detected_trackers
WHERE tracker_pattern_id = @tracker_pattern_id
AND initiator_domain IS NOT NULL
LIMIT @limit;
`
args := pgx.StrictNamedArgs{
"tracker_pattern_id": trackerPatternID,
"limit": limit,
}
rows, err := conn.Query(ctx, q, args)
if err != nil {
return nil, fmt.Errorf("cannot load initiator domains: %w", err)
}
domains, err := pgx.CollectRows(rows, pgx.RowTo[string])
if err != nil {
return nil, fmt.Errorf("cannot collect initiator domains: %w", err)
}
return domains, nil
}
func (dts *DetectedTrackers) LoadByTrackerPatternID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
trackerPatternID gid.GID,
cursor *page.Cursor[DetectedTrackerOrderField],
) error {
q := `
SELECT
id,
cookie_banner_id,
tracker_pattern_id,
tracker_type,
identifier,
max_age_seconds,
source,
value_size,
initiator_url,
initiator_domain,
last_detected_at,
created_at,
updated_at
FROM
detected_trackers
WHERE
%s
AND tracker_pattern_id = @tracker_pattern_id
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{
"tracker_pattern_id": trackerPatternID,
}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query detected trackers: %w", err)
}
trackers, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[DetectedTracker])
if err != nil {
return fmt.Errorf("cannot collect detected trackers: %w", err)
}
*dts = trackers
return nil
}
func (dts *DetectedTrackers) LoadSiblingPatternIDsByInitiatorDomains(
ctx context.Context,
conn pg.Querier,
cookieBannerID gid.GID,
domains []string,
excludePatternID gid.GID,
limit int,
) ([]gid.GID, error) {
if len(domains) == 0 {
return nil, nil
}
q := `
SELECT DISTINCT tracker_pattern_id
FROM detected_trackers
WHERE cookie_banner_id = @cookie_banner_id
AND initiator_domain = ANY(@domains)
AND tracker_pattern_id IS NOT NULL
AND tracker_pattern_id != @exclude_pattern_id
ORDER BY tracker_pattern_id
LIMIT @limit;
`
args := pgx.StrictNamedArgs{
"cookie_banner_id": cookieBannerID,
"domains": domains,
"exclude_pattern_id": excludePatternID,
"limit": limit,
}
rows, err := conn.Query(ctx, q, args)
if err != nil {
return nil, fmt.Errorf("cannot load sibling pattern ids: %w", err)
}
ids, err := pgx.CollectRows(rows, pgx.RowTo[gid.GID])
if err != nil {
return nil, fmt.Errorf("cannot collect sibling pattern ids: %w", err)
}
return ids, nil
}
// UpdateTrackerPatternID repoints a single detected tracker at another
// pattern. It is the per-row counterpart of RelinkByTrackerPatternID,
// used by the banner-reset rebuild where each detection of a glob moves
// to its own recreated exact pattern.
func (dt *DetectedTracker) UpdateTrackerPatternID(
ctx context.Context,
tx pg.Tx,
scope Scoper,
) error {
q := `
UPDATE detected_trackers
SET
tracker_pattern_id = @tracker_pattern_id,
updated_at = @updated_at
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": dt.ID,
"tracker_pattern_id": dt.TrackerPatternID,
"updated_at": time.Now(),
}
maps.Copy(args, scope.SQLArguments())
result, err := tx.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update detected tracker pattern: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}
func (dts *DetectedTrackers) RelinkByTrackerPatternID(
ctx context.Context,
tx pg.Tx,
scope Scoper,
sourcePatternID gid.GID,
targetPatternID gid.GID,
) error {
q := `
UPDATE detected_trackers
SET
tracker_pattern_id = @target_pattern_id,
updated_at = @updated_at
WHERE
%s
AND tracker_pattern_id = @source_pattern_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"source_pattern_id": sourcePatternID,
"target_pattern_id": targetPatternID,
"updated_at": time.Now(),
}
maps.Copy(args, scope.SQLArguments())
_, err := tx.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot relink detected trackers to pattern: %w", err)
}
return nil
}