Validate the csk_ API key prefix in the TS client - #37
Merged
rapguit merged 1 commit intoJun 26, 2026
Merged
Conversation
The Python client rejects keys not starting with csk_ at construction; the TS CodeSpar constructor only checked for a non-empty key, so a malformed key failed late with an opaque HTTP error instead of a clear constructor error. Adds the same prefix check, restoring TS/Python parity with the documented contract. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
rapguit
approved these changes
Jun 26, 2026
Contributor
|
Thanks for this, Andre — small surface, clean fix. Verified locally:
Behavior-change call-out in the PR body is appropriate — failing fast at construction with a clear message is strictly an improvement over the opaque first-request HTTP error, and the staging/prod prefix split is handled correctly by checking Approving. Will let a maintainer trigger the workflows + merge. |
rapguit
pushed a commit
that referenced
this pull request
Jun 26, 2026
Per-session `projectId` is validated against `^prj_[A-Za-z0-9]{16}$`
(Zod schema in `types.ts`), but the **client-level** `projectId` was
passed straight to the `x-codespar-project` header with no validation —
in both the TS and Python clients. A malformed client-level scope
reached the backend instead of failing fast at construction.
- TS: extracts `PROJECT_ID_REGEX` in `types.ts` (now reused by the Zod
schema and the `CodeSpar` constructor).
- Python: adds the matching `_PROJECT_ID_RE` check to
`AsyncCodeSpar.__init__` (covers the sync client via its factory).
- Fixes three Python test fixtures in `test_session.py` that used
17-char ids (never validated before).
**Behavior change**: a client constructed with a malformed `projectId`
now throws at construction. Tests added (TS + Python).
`npm test` + `tsc --noEmit` green for `packages/core`; `pytest` + `mypy`
+ `ruff` green for `packages/python`.
Note: touches `packages/core/src/index.ts`, which also changes in #37 —
a trivial merge conflict on the constructor is expected if both land;
whichever merges second rebases.
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Merged
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Python client rejects API keys not starting with
csk_at construction. The TSCodeSparconstructor only checked for a non-empty key, so a malformed key failed late with an opaque HTTP error on the first request instead of a clear constructor error.Adds the same
csk_prefix check to the TS constructor (both stagingcsk_and prodcsk_live_share the prefix), restoring TS/Python parity with the documented contract.Behavior change: a
CodeSparconstructed with a non-csk_key now throws at construction instead of on first request. Tests added for the reject and staging-key-accept paths.npm test+tsc --noEmitgreen forpackages/core.