Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
Émile Ré
2026-05-05 17:49:13 +04:00
parent 85fb899220
commit d0141c8dcf
3 changed files with 89 additions and 43 deletions

View File

@@ -1211,7 +1211,6 @@ func (s *Service) CreateCookiePattern(
return fmt.Errorf("cannot insert cookie pattern: %w", err) return fmt.Errorf("cannot insert cookie pattern: %w", err)
} }
src := coredata.CookieSourceScript
tp := &coredata.TrackerPattern{ tp := &coredata.TrackerPattern{
ID: gid.New(scope.GetTenantID(), coredata.TrackerPatternEntityType), ID: gid.New(scope.GetTenantID(), coredata.TrackerPatternEntityType),
OrganizationID: category.OrganizationID, OrganizationID: category.OrganizationID,
@@ -1223,7 +1222,7 @@ func (s *Service) CreateCookiePattern(
DisplayName: req.DisplayName, DisplayName: req.DisplayName,
MaxAgeSeconds: req.MaxAgeSeconds, MaxAgeSeconds: req.MaxAgeSeconds,
Description: req.Description, Description: req.Description,
Source: &src, Source: new(coredata.CookieSourceScript),
CreatedAt: now, CreatedAt: now,
UpdatedAt: now, UpdatedAt: now,
} }
@@ -2350,9 +2349,12 @@ func (s *Service) ReportDetectedCookies(
bannerID gid.GID, bannerID gid.GID,
req ReportDetectedCookiesRequest, req ReportDetectedCookiesRequest,
) error { ) error {
return s.ReportDetectedTrackers(ctx, bannerID, ReportDetectedTrackersRequest{ return s.ReportDetectedTrackers(ctx,
bannerID,
ReportDetectedTrackersRequest{
Cookies: req.Cookies, Cookies: req.Cookies,
}) },
)
} }
func (s *Service) ReportDetectedTrackers( func (s *Service) ReportDetectedTrackers(
@@ -2383,8 +2385,18 @@ func (s *Service) ReportDetectedTrackers(
for _, dc := range req.Cookies { for _, dc := range req.Cookies {
if err := s.reportDetectedTracker( if err := s.reportDetectedTracker(
ctx, tx, scope, &banner, uncategorised.ID, now, ctx,
coredata.TrackerTypeCookie, dc.Name, dc.MaxAgeSeconds, &dc.Source, nil, tx,
scope,
&banner,
uncategorised.ID,
now,
detectedTrackerInfo{
TrackerType: coredata.TrackerTypeCookie,
Identifier: dc.Name,
MaxAgeSeconds: dc.MaxAgeSeconds,
Source: &dc.Source,
},
&inserted, &inserted,
); err != nil { ); err != nil {
return err return err
@@ -2393,8 +2405,17 @@ func (s *Service) ReportDetectedTrackers(
for _, ds := range req.Storage { for _, ds := range req.Storage {
if err := s.reportDetectedTracker( if err := s.reportDetectedTracker(
ctx, tx, scope, &banner, uncategorised.ID, now, ctx,
ds.StorageType, ds.Key, nil, nil, ds.ValueSize, tx,
scope,
&banner,
uncategorised.ID,
now,
detectedTrackerInfo{
TrackerType: ds.StorageType,
Identifier: ds.Key,
ValueSize: ds.ValueSize,
},
&inserted, &inserted,
); err != nil { ); err != nil {
return err return err
@@ -2403,8 +2424,16 @@ func (s *Service) ReportDetectedTrackers(
for _, dr := range req.Resources { for _, dr := range req.Resources {
if err := s.reportDetectedTracker( if err := s.reportDetectedTracker(
ctx, tx, scope, &banner, uncategorised.ID, now, ctx,
dr.ResourceType, dr.Origin, nil, nil, nil, tx,
scope,
&banner,
uncategorised.ID,
now,
detectedTrackerInfo{
TrackerType: dr.ResourceType,
Identifier: dr.Origin,
},
&inserted, &inserted,
); err != nil { ); err != nil {
return err return err
@@ -2422,6 +2451,14 @@ func (s *Service) ReportDetectedTrackers(
) )
} }
type detectedTrackerInfo struct {
TrackerType coredata.TrackerType
Identifier string
MaxAgeSeconds *int
Source *coredata.CookieSource
ValueSize *int
}
func (s *Service) reportDetectedTracker( func (s *Service) reportDetectedTracker(
ctx context.Context, ctx context.Context,
tx pg.Tx, tx pg.Tx,
@@ -2429,15 +2466,11 @@ func (s *Service) reportDetectedTracker(
banner *coredata.CookieBanner, banner *coredata.CookieBanner,
uncategorisedID gid.GID, uncategorisedID gid.GID,
now time.Time, now time.Time,
trackerType coredata.TrackerType, info detectedTrackerInfo,
identifier string,
maxAgeSeconds *int,
source *coredata.CookieSource,
valueSize *int,
inserted *int, inserted *int,
) error { ) error {
var matchedPattern coredata.TrackerPattern var matchedPattern coredata.TrackerPattern
err := matchedPattern.FindMatchingPattern(ctx, tx, scope, banner.ID, trackerType, identifier) err := matchedPattern.FindMatchingPattern(ctx, tx, scope, banner.ID, info.TrackerType, info.Identifier)
if err != nil && !errors.Is(err, coredata.ErrResourceNotFound) { if err != nil && !errors.Is(err, coredata.ErrResourceNotFound) {
return fmt.Errorf("cannot find matching tracker pattern: %w", err) return fmt.Errorf("cannot find matching tracker pattern: %w", err)
} }
@@ -2460,13 +2493,13 @@ func (s *Service) reportDetectedTracker(
OrganizationID: banner.OrganizationID, OrganizationID: banner.OrganizationID,
CookieBannerID: banner.ID, CookieBannerID: banner.ID,
CookieCategoryID: uncategorisedID, CookieCategoryID: uncategorisedID,
TrackerType: trackerType, TrackerType: info.TrackerType,
Pattern: identifier, Pattern: info.Identifier,
MatchType: coredata.CookiePatternMatchTypeExact, MatchType: coredata.CookiePatternMatchTypeExact,
DisplayName: identifier, DisplayName: info.Identifier,
Description: "", Description: "",
MaxAgeSeconds: maxAgeSeconds, MaxAgeSeconds: info.MaxAgeSeconds,
Source: source, Source: info.Source,
CreatedAt: now, CreatedAt: now,
UpdatedAt: now, UpdatedAt: now,
} }
@@ -2479,7 +2512,7 @@ func (s *Service) reportDetectedTracker(
*inserted++ *inserted++
} else { } else {
var existingPattern coredata.TrackerPattern var existingPattern coredata.TrackerPattern
if err := existingPattern.FindMatchingPattern(ctx, tx, scope, banner.ID, trackerType, identifier); err != nil { if err := existingPattern.FindMatchingPattern(ctx, tx, scope, banner.ID, info.TrackerType, info.Identifier); err != nil {
return fmt.Errorf("cannot load existing tracker pattern: %w", err) return fmt.Errorf("cannot load existing tracker pattern: %w", err)
} }
patternID = &existingPattern.ID patternID = &existingPattern.ID
@@ -2490,11 +2523,11 @@ func (s *Service) reportDetectedTracker(
ID: gid.New(scope.GetTenantID(), coredata.DetectedTrackerEntityType), ID: gid.New(scope.GetTenantID(), coredata.DetectedTrackerEntityType),
CookieBannerID: banner.ID, CookieBannerID: banner.ID,
TrackerPatternID: patternID, TrackerPatternID: patternID,
TrackerType: trackerType, TrackerType: info.TrackerType,
Identifier: identifier, Identifier: info.Identifier,
MaxAgeSeconds: maxAgeSeconds, MaxAgeSeconds: info.MaxAgeSeconds,
Source: source, Source: info.Source,
ValueSize: valueSize, ValueSize: info.ValueSize,
LastDetectedAt: now, LastDetectedAt: now,
CreatedAt: now, CreatedAt: now,
UpdatedAt: now, UpdatedAt: now,

View File

@@ -78,7 +78,11 @@ INSERT INTO detected_trackers (
) )
ON CONFLICT (cookie_banner_id, tracker_type, identifier) DO UPDATE ON CONFLICT (cookie_banner_id, tracker_type, identifier) DO UPDATE
SET last_detected_at = EXCLUDED.last_detected_at, 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, 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,
updated_at = EXCLUDED.updated_at updated_at = EXCLUDED.updated_at
` `

View File

@@ -329,11 +329,14 @@ func (h *Handler) handleReportDetectedTrackers(w http.ResponseWriter, r *http.Re
source = coredata.CookieSourceScript source = coredata.CookieSourceScript
} }
req.Cookies = append(req.Cookies, cookiebanner.DetectedCookie{ req.Cookies = append(
req.Cookies,
cookiebanner.DetectedCookie{
Name: name, Name: name,
MaxAgeSeconds: c.MaxAgeSeconds, MaxAgeSeconds: c.MaxAgeSeconds,
Source: source, Source: source,
}) },
)
} }
for _, s := range body.Storage { for _, s := range body.Storage {
@@ -354,11 +357,14 @@ func (h *Handler) handleReportDetectedTrackers(w http.ResponseWriter, r *http.Re
continue continue
} }
req.Storage = append(req.Storage, cookiebanner.DetectedStorageItem{ req.Storage = append(
req.Storage,
cookiebanner.DetectedStorageItem{
Key: key, Key: key,
StorageType: storageType, StorageType: storageType,
ValueSize: s.ValueSize, ValueSize: s.ValueSize,
}) },
)
} }
for _, res := range body.Resources { for _, res := range body.Resources {
@@ -377,10 +383,13 @@ func (h *Handler) handleReportDetectedTrackers(w http.ResponseWriter, r *http.Re
continue continue
} }
req.Resources = append(req.Resources, cookiebanner.DetectedResourceItem{ req.Resources = append(
req.Resources,
cookiebanner.DetectedResourceItem{
Origin: origin, Origin: origin,
ResourceType: resourceType, ResourceType: resourceType,
}) },
)
} }
if len(req.Cookies)+len(req.Storage)+len(req.Resources) == 0 { if len(req.Cookies)+len(req.Storage)+len(req.Resources) == 0 {