Skip to content

fix(security): apply cloud tracking quota to /tracking/check - #252

Merged
gkhngyk merged 1 commit into
mainfrom
fix/gate-tracking-check-cloud-quota
Jun 13, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/gate-tracking-check-cloud-quota

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Summary

POST /api/tracking/check enqueued a real tracking run (full-brand when no promptId is passed) after only an ownership check, while POST /api/tracking/analyze-new gated the same kind of run behind three cloud-mode controls: an active/trialing subscription (402), a per-brand daily on-demand cap (429), and a per-brand cooldown (429).

So any authenticated user could call /check to bypass billing and trigger unbounded Cloro + LLM spend — repeatedly, and even with no active subscription.

Change

  • Extract the shared guard into server/src/lib/tracking-quota.js — enforceOnDemandTrackingQuota(brandId, organizationId) + TrackingQuotaError, preserving the exact 402/429 status codes and response bodies.
  • Call it from both /check and /analyze-new (the inline block in analyze-new is replaced by the helper — single source of truth, no drift).
  • /check's job is now tagged onDemand: true so its runs count toward the daily cap and cooldown (previously untracked).

Why it's safe — no impact on legitimate flows

  • Self-hosted (isCloud() === false): the guard is a no-op — unrestricted, unchanged.
  • Active/trialing (paying) orgs: pass the guard exactly as they pass /analyze-new today — unaffected.
  • Onboarding is not broken:
    • Cloud onboarding triggers the first tracking run via the internal /api/internal/trigger-tracking endpoint (CRON_SECRET) after payment, which this guard does not touch; the org is trialing/active by then anyway.
    • The only /check call in the onboarding flow runs in self-hosted mode only (cloud returns to the subscription step before it), where the guard is a no-op.
  • Non-active orgs that could previously abuse /check are now blocked consistently — they were already blocked on /analyze-new, so this is the existing policy applied uniformly, not a new restriction.

Verification

  • node --check, eslint, prettier clean on both files.
  • Read-only confirmed against the codebase + DB: onboarding path, the active/trialing pass-through, and that the guard logic is a faithful extraction (identical status codes/bodies).

Type of change

  • fix — security / billing-bypass

POST /api/tracking/check enqueued a real (full-brand when no promptId)
tracking run after only an ownership check, while POST /api/tracking/analyze-new
gated the same kind of run behind the cloud subscription check, the daily
on-demand limit and the cooldown. Any authenticated user could therefore use
/check to bypass billing and trigger unbounded Cloro/LLM spend.

Extract the shared cloud-mode guard into lib/tracking-quota.js
(enforceOnDemandTrackingQuota + TrackingQuotaError, preserving the exact 402/429
status codes and bodies) and call it from both /check and /analyze-new. /check's
job is now tagged onDemand:true so its runs count toward the daily cap and
cooldown like analyze-new's.

Self-hosted (isCloud()===false) stays unrestricted. Onboarding is unaffected:
cloud onboarding triggers tracking via the internal CRON_SECRET endpoint after
payment, and the only /check call in onboarding runs in self-hosted mode where
the guard is a no-op.
@gkhngyk
gkhngyk merged commit 402b1b0 into main Jun 13, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the fix/gate-tracking-check-cloud-quota branch June 13, 2026 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant