Skip to content

fix(flags): warn once when the secret key can't read flag definitions - #413

Draft
Silthus wants to merge 1 commit into
mainfrom
fix/flag-definitions-scope-warning
Draft

Silthus wants to merge 1 commit into
mainfrom
fix/flag-definitions-scope-warning

Conversation

@Silthus

@Silthus Silthus commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

When you set SecretKey, the client turns on local evaluation and polls /flags/definitions with it, every 5 minutes by default. Transactional email asks for a Project Secret API Key with only the transactional_email:write scope. The flags service answers that key with 401 authentication_failed ("Secret API token is invalid."), the same answer as a wrong key, because its key lookup only finds keys with a flag scope. A personal API key without the scope gets 403 permission_denied.

So a client that only sends email logged Unable to fetch feature flags, status: 401 Unauthorized as an error on every poll, for as long as it ran, about feature flags it never uses.

Merge Danger

Low, two-way door. Only the logging for 401 and 403 from /flags/definitions changes. Polling, flag state, the cache provider path, other statuses, network errors and remote evaluation are unchanged.

What changes

  • On 401 or 403, the poller logs one warning. It says the key is invalid or lacks the Feature flags: Read scope and that adding the scope turns local evaluation on.
  • Later rejected polls log at debug. The next 200 or 304 re-arms the warning, so a key that breaks later warns again.
  • The poller keeps its interval. A user who adds the scope gets local evaluation back on the next successful poll, and after a restart.
  • The warning names no opt-out, because SecretKey is the only switch for local evaluation in this SDK. Adding one would change the public config, so it stays out of this fix.

This PR is independent of the email PR #406. It touches only the flag poller and its tests, and applies cleanly on that PR's head.

💚 How did you test it?

  • New tests in featureflags_test.go: five polls answered with 401 or 403 log one warning and four debug lines, a 200 or a 304 between rejections warns again, and a load from the shared cache provider doesn't. They failed before the fix and pass now, and each fails when its reset or debug line is removed. definitionsSequenceServer now answers any numeric body as a status, which replaces its special cases for 304 and 402.
  • go test ./..., go vet ./... and gofmt -l . pass.
  • Test seam: FeatureFlagsPoller.fetchFlagDefinitions against an httptest server, read through a logger that records levels.
  • Live run: a real client from NewWithConfig with SecretKey and a 1 s poll interval polled a local stub of /flags/definitions that sends the flags service's 401 and 403 bodies. Over 8 polls (three 401s, a 200, then 401s) it logged two warnings, one before and one after the load, and debug lines for the rest. Five 403s logged one warning. The same run on main logged 7 errors.
  • Gap: no run against PostHog itself with a scope-less key. The stub's 401 and 403 come from reading rust/feature-flags/src/api/auth.rs and errors.rs in PostHog/posthog.

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Claude Code (Claude Opus 5.5) wrote the change test-first in a T3 Code thread, as a side fix from the transactional email work. GPT-6.1 Sol reviewed the diff before the push. The same fix is going to posthog-node, posthog-python and posthog-elixir.

Question Answer Who Where
Which responses count as a key that can't read flags? 401 and 403, because the flags service answers a scope-less project secret key with the same 401 as a wrong key 🤖 Agent comment
Stop polling or keep polling quietly? Keep polling at the configured interval and stay quiet, so a transient 401 or an added scope recovers without a restart 🤖 Agent comment
Which channel reports it? One Logger.Warnf, then Logger.Debugf 🤖 Agent comment
Add a local evaluation opt-out to name in the warning? No, that changes the public config; the warning names the scope only 🤖 Agent comment

Involvement: two cross-family review rounds (GPT-6.1 Sol), no grilling rounds, live-run proof against a local stub of the flags service.

🤖 Generated with Claude Code

A secret key without the Feature flags: Read scope gets 401 from
/flags/definitions, so the poller logged an error on every poll. Warn
once for 401 and 403 with the missing scope, log repeats at debug, and
warn again only after definitions load from the API. Polling, flag
state, the cache provider path and other statuses are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Silthus Silthus self-assigned this Nov 5, 2026
@Silthus

Silthus commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Note

🤖 Claude Opus 5.5 responding on behalf of Michael

Decisions made while building this fix:

  • 🤖 Which responses count as a key that can't read flags? 401 and 403. The flags service answers a project secret key without the flag scope with the same 401 as a wrong key, so the SDK can't tell them apart and the message names both causes.
  • 🤖 Stop polling or keep polling quietly? Keep polling at the existing cadence and log nothing more until definitions load. Stopping would turn a transient 401 into a dead poller until restart.
  • 🤖 Which channel reports it? The SDK's existing warning or error channel, once. Other statuses keep their current logging.
  • 🤖 Add a local evaluation opt-out to name in the warning? No. In posthog-go, SecretKey is the only switch for local evaluation, and a new option would change the public config. The warning names the missing scope only.

@posthog posthog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostHog Review · Standard · reviewed c3ba554: no findings.

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

posthog-go Compliance Report

Date: 2026-11-05 12:23:13 UTC
Duration: 169317ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

✅ 94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint ✅ 18ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 8ms
Required Headers.Has Authorization Bearer Header ✅ 7ms
Required Headers.Has Content Type Json ✅ 8ms
Required Headers.Has Posthog Sdk Info Format ✅ 6ms
Required Headers.Has Posthog Attempt Header ✅ 7ms
Required Headers.Has Posthog Request Id ✅ 8ms
Required Headers.Has Posthog Request Timestamp ✅ 7ms
Required Headers.Has User Agent ✅ 7ms
Body Format.Body Has Created At And Batch ✅ 7ms
Body Format.No Api Key In Body ✅ 7ms
Body Format.No Sent At In Body ✅ 7ms
Event Format.Event Has Required Root Fields ✅ 8ms
Event Format.Event Uuid Is Valid ✅ 6ms
Event Format.Event Timestamp Is Rfc3339 ✅ 7ms
Event Format.Distinct Id Is String ✅ 7ms
Event Format.Distinct Id At Root Not Properties ✅ 7ms
Event Format.Custom Properties Preserved ✅ 6ms
Event Format.Set Properties Preserved ✅ 7ms
Event Format.Set Once Properties Preserved ✅ 7ms
Event Format.Groups Properties Preserved ✅ 7ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 6ms
Event Format.Event Has Required Root Fields Batch ✅ 10ms
Event Format.Event Uuid Is Valid Batch ✅ 10ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 9ms
Event Format.Distinct Id Is String Batch ✅ 10ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 9ms
Event Format.Custom Properties Preserved Batch ✅ 9ms
Event Format.Set Properties Preserved Batch ✅ 10ms
Event Format.Set Once Properties Preserved Batch ✅ 9ms
Event Format.Groups Properties Preserved Batch ✅ 10ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 10ms
Batch Behavior.Multiple Events In Single Batch ✅ 32ms
Batch Behavior.Batch Envelope Smoke ✅ 12ms
Batch Behavior.Flush With No Events Sends Nothing ✅ 3ms
Batch Behavior.Flush At Triggers Batch ✅ 1008ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 19ms
Deduplication.Generates Unique Uuids ✅ 19ms
Deduplication.Different Events Same Content Different Uuids ✅ 9ms
Deduplication.Preserves Uuid On Retry ✅ 5135ms
Deduplication.Preserves Timestamp On Retry ✅ 5134ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 5136ms
Deduplication.No Duplicate Events In Batch ✅ 33ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 8ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 10140ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 5136ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 2014ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 5136ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 9ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 29ms
Response Format Validation.Success No Retry After When All Ok ✅ 27ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 132ms
Response Format Validation.Success No Retry After When Drop Only ✅ 29ms
Response Format Validation.Response Echoes Request Id ✅ 7ms
Retry Behavior.Retries On 408 ✅ 5132ms
Retry Behavior.Retries On 500 ✅ 5137ms
Retry Behavior.Retries On 503 ✅ 5136ms
Retry Behavior.Retries On 504 ✅ 5137ms
Retry Behavior.Retryable Errors Have Retry After ✅ 2132ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 8140ms
Retry Behavior.Does Not Retry On 400 ✅ 2133ms
Retry Behavior.Does Not Retry On 401 ✅ 2133ms
Retry Behavior.Does Not Retry On 402 ✅ 2133ms
Retry Behavior.Does Not Retry On 413 ✅ 2133ms
Retry Behavior.Does Not Retry On 415 ✅ 2134ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 2132ms
Retry Behavior.Implements Backoff ✅ 15134ms
Retry Behavior.Max Retries Respected ✅ 15131ms
Partial Batch Handling.Handles 200 Full Success ✅ 2021ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3033ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3032ms
Partial Batch Handling.Does Not Retry Limited Events ✅ 3030ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 5133ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 5137ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 5137ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 5136ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 5136ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 5135ms
Partial Batch Handling.Respects Retry After On Partial ✅ 5134ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3031ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry ✅ 3034ms
Compression.Sends Gzip Content Encoding ✅ 17ms
Compression.No Content Encoding When Disabled ✅ 7ms
Compression.Compressed Body Is Decompressible ✅ 17ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 2133ms
Event Options.Cookieless Mode Override ✅ 8ms
Event Options.Disable Skew Correction Override ✅ 6ms
Event Options.Process Person Profile Override ✅ 6ms
Event Options.Product Tour Id Override ✅ 17ms
Event Options.Unset Options Omitted ✅ 8ms
Event Options.Options Override In Batch ✅ 9ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties ✅ 6ms
Geoip And Historical Migration.Historical Migration Set In Body ✅ 6ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 6ms

Feature_Flags Tests

✅ 17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 16ms
Request Payload.Flags Request Uses V2 Query Param ✅ 17ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 16ms
Request Payload.Flags Request Omits Authorization Header ✅ 16ms
Request Payload.Token In Flags Body Matches Init ✅ 16ms
Request Payload.Groups Round Trip ✅ 16ms
Request Payload.Groups Default To Empty Object ✅ 16ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 16ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 17ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 17ms
Request Lifecycle.No Flags Request On Init Alone ✅ 3ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 7ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 29ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 17ms
Retry Behavior.Retries Flags On 502 ✅ 18ms
Retry Behavior.Retries Flags On 504 ✅ 19ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 19ms

This branch has not been deployed

No deployments
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