Skip to content

fix(clerk): reject a secret key containing a return character - #107332

Open
Gilbert09 wants to merge 1 commit into
masterfrom
posthog/clerk-secret-key-return-character
Open

Gilbert09 wants to merge 1 commit into
masterfrom
posthog/clerk-secret-key-return-character

Conversation

@Gilbert09

Copy link
Copy Markdown
Member

Problem

Pasting a secret key that contains a carriage return or newline (for example, a whole .env snippet pasted into the secret key field instead of just the key) crashes credential validation for the Clerk data warehouse source, reported via error tracking.

A return character is valid ASCII, so it passes the existing isascii() guard, but requests still refuses to send it as a header value (InvalidHeader). That exception is a RequestException subclass, so it was swallowed by the generic network-error handler and reported back as "Couldn't reach Clerk to validate your secret key. Please try again in a moment." — a message that tells the user to retry a key that will never validate.

Changes

  • validate_credentials now rejects a secret key containing \r or \n before making any request, alongside the existing non-ASCII check, and explains it with the same "unsupported character" message.
  • Mechanical: extended the existing parametrized ASCII-guard test with a return-character case.

How did you test this code?

Added test_key_with_return_character_is_rejected_before_any_request, which asserts the key is rejected before any request is sent and the session mock is never called — the same pattern as the existing non-ASCII test.

Ran pytest products/warehouse_sources/backend/temporal/data_imports/sources/clerk/ locally: 174 passed. Not run: the full CI matrix (relies on this PR's checks).

Release status

  • No feature flag controls this change

Automatic notifications

  • Publish to changelog?

Docs update

None — this is an internal validation fix with no documented behavior change.

🤖 Agent context

Autonomy: Fully autonomous

Agent: Claude Code, Sonnet 5

Triaged from a live error tracking issue for the Clerk data warehouse source. Confirmed the failure originates in validate_credentials (not just referenced in serialized context), read the requests library's header-validation regex to identify the exact class of character it rejects, and scoped the fix to that guard rather than widening NonRetryableErrors (this needed a code fix, not a retry-policy change, since retrying the same malformed key would never succeed and the existing UX message actively told users to retry). Searched open PRs (by keyword and by this account's own PR list) for a duplicate; none matched.


🤖 Generated with Claude Code

A carriage return or newline is valid ASCII, so it passed the existing character guard, but requests still rejects it as an invalid header value when the key is validated. That exception is a RequestException subclass, so it was swallowed by the generic network-error handler and reported as a transient failure instead of explaining the malformed key.

Generated-By: PostHog Desktop
Task-Id: dddbb8df-805b-4cb2-8d5e-53a568522829
Copilot AI lite review requested due to automatic review settings September 27, 2026 00:00
@Gilbert09 Gilbert09 added the stamphog Request AI approval (no full review) label Sep 27, 2026 — with Talyn App
@trunk-io

trunk-io Bot commented Sep 27, 2026

Copy link
Copy Markdown

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@stamphog stamphog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approved.

Trivial, well-tested validation fix by an owning-team author with strong familiarity; no risky territory touched.

  • Author wrote 100% of the modified lines and has 6 merged PRs in these paths (familiarity STRONG).
  • copilot-pull-request-reviewer[bot] reviewed the current head.
Gate mechanics and policy version
Gate Result
prerequisites ✓ all clear
deny-list ✓ no deny categories matched
size ✓ 6L, 1F substantive, 19L/2F incl. docs/generated/snapshots — within ceiling
tier ✓ T1-agent / T1a-trivial (19L, 2F, single-area, fix)
stamphog 2.2.0 .stamphog/policy.yml @ 3006b51 · reviewed head 3006b51

@github-actions

github-actions Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

🤖 CI report

⚠️ Trunk lane — backend Python lane (py:product:warehouse_sources)

This PR is assigned to the backend Python lane (py:product:warehouse_sources). It runs backend Python tests and may merge in parallel with PRs in other lanes.

✅ Duplication (Python) — clean

New Python code duplication introduced by this branch. Fails at 70+ tokens in app code, or 150+ tokens when both copies live in test files. Advisory while the gate proves itself: extract a shared helper instead of copying.

✅ Duplication (TypeScript) — clean

New TypeScript code duplication introduced by this branch. Fails at 70+ tokens in app code, or 150+ tokens when both copies live in test files. Advisory while the gate proves itself: extract a shared helper instead of copying.

@coderabbitai

coderabbitai Bot commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: PostHog/posthog/.coderabbit.yaml

Review profile: QUIET

Plan: Enterprise

Run ID: 27b9d147-0cc3-4e0c-a547-fb84121cd070

📥 Commits

Reviewing files that changed from the base of the PR and between f264381 and 3006b51.

📒 Files selected for processing (2)
  • products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py
  • products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.


📝 Walkthrough

Walkthrough

validate_credentials now rejects secret keys containing carriage returns or newlines, alongside non-ASCII characters. Parameterized tests verify that these keys fail validation with a “Copy the key again” message and do not trigger a request.

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 3006b

Clerk keys containing pasted line breaks are rejected before a request, preventing invalid headers from being reported as transient connectivity failures. No actionable merge risk remains.

🚥 Pre-merge checks | ✅ 1
✅ Passed checks (1 passed)
Check name Status Explanation
Description check ✅ Passed The description clearly states the problem, user impact, code changes, tests, release status, documentation status, and agent context. It also records the duplicate-PR search and the locally executed …
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

@trunk-io

trunk-io Bot commented Sep 27, 2026

Copy link
Copy Markdown

Static Badge   Static Badge   Static Badge

View Full Report ↗︎ ⋅ Docs

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

stamphog Request AI approval (no full review)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants