Repository navigation
fix(auth): write the oauth token cache private and atomically - #429
Merged
Merged
Conversation
The token cache wrote the bearer token with Path.write_text and narrowed the file to 0600 only afterwards, so it existed at the process umask -- 0644 with the common default -- for the window in between, inside a cache directory created at 0755 that any local user could traverse and list. On Windows chmod does not restrict access at all, so there the window never closed. The 401 tarpit record, which names the client_id and the token-endpoint error, had the same shape. Both files are now written through tempfile.mkstemp in the cache directory, which creates them readable and writable by the creating user only, and moved into place with os.replace, so a write that dies part-way can no longer leave a truncated entry or destroy the entry already on disk. The cache directory is created with mode 0700; an existing directory is left as the user made it. The new tests neutralise os.chmod, as Windows effectively does, and sweep every file the cache writes rather than the token file alone, so a new cache file added later is covered without being named. Closes #424 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Bearer-token cache files may expose credentials to other Windows users because user-only access controls are not guaranteed.
3 open issues
What changed in this PR
This PR addresses #424 by hardening OAuth disk-cache writes in the SDK runtime, without changing the public API or cache format.
Changes:
- Routes token and 401 tarpit writes through a shared temporary-file and atomic-replacement helper for both providers.
- Requests restrictive POSIX permissions at creation instead of applying
chmodafter writing. - Adds permission, cleanup, and failed-replacement regression tests, plus regenerated runtime and stub output.
| File | Description |
|---|---|
tests/acceptance/test_token_cache_permissions.py |
Adds cache-permission, cleanup, and previous-entry preservation tests. |
stubs/camunda_orchestration_sdk/runtime/auth.pyi |
Adds the private write-helper signature. |
runtime/auth.py |
Implements atomic cache writes and restrictive creation modes. |
generated/camunda_orchestration_sdk/runtime/auth.py |
Carries the runtime changes into the published SDK. |
Review effort: Balanced
From Copilot's review of this PR. The docstring claimed the private-create fix held on Windows. It does not: a mode argument does not become an access-control list there, so the file inherits the directory's ACL exactly as it did under write_text. That is long-standing rather than a regression, so this PR's claim is corrected and the Windows half is filed as #436 with the options it needs a decision on. The temporary name no longer embeds the destination's basename, so a long cache filename cannot push the temporary one past the filesystem's component limit and silently disable token persistence. The async cache-write diagnostic is now suppressed on failure: a logger that raises must not take down a request that already holds a valid token. Refs #436 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda
The temp-file prefix and the suppressed cache-write diagnostic landed in 82ef5ee without a test. Both tests fail against the previous implementation (temp name embedding the destination's basename; unsuppressed logger error) and pass on the current source. The skip reason no longer claims a Windows guarantee, matching the corrected docstring. Refs #436 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda
Contributor
|
Successfully created backport PR for |
Contributor
|
Created backport PR for
Please cherry-pick the changes locally and resolve any conflicts. git fetch origin backport-429-to-stable/9
git worktree add --checkout .worktree/backport-429-to-stable/9 backport-429-to-stable/9
cd .worktree/backport-429-to-stable/9
git reset --hard HEAD^
git cherry-pick -x 45cab63c942904138d9b53b4b2f5b358e41404c0 |
camunda-sdk-automation Bot
pushed a commit
that referenced
this pull request
Nov 30, 2026
Cherry-picked from #429 onto stable/9 with conflicts. The conflict markers are committed as-is and must be resolved before this backport is merged.
Member
|
/backport |
jwulf
pushed a commit
that referenced
this pull request
Nov 30, 2026
…519) * fix(auth): write the oauth token cache private and atomically The token cache wrote the bearer token with Path.write_text and narrowed the file to 0600 only afterwards, so it existed at the process umask -- 0644 with the common default -- for the window in between, inside a cache directory created at 0755 that any local user could traverse and list. On Windows chmod does not restrict access at all, so there the window never closed. The 401 tarpit record, which names the client_id and the token-endpoint error, had the same shape. Both files are now written through tempfile.mkstemp in the cache directory, which creates them readable and writable by the creating user only, and moved into place with os.replace, so a write that dies part-way can no longer leave a truncated entry or destroy the entry already on disk. The cache directory is created with mode 0700; an existing directory is left as the user made it. The new tests neutralise os.chmod, as Windows effectively does, and sweep every file the cache writes rather than the token file alone, so a new cache file added later is covered without being named. Closes #424 Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda * chore(generation): regenerate SDK for private atomic token cache writes Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda * chore(auth): stop claiming a Windows guarantee, and harden two edges From Copilot's review of this PR. The docstring claimed the private-create fix held on Windows. It does not: a mode argument does not become an access-control list there, so the file inherits the directory's ACL exactly as it did under write_text. That is long-standing rather than a regression, so this PR's claim is corrected and the Windows half is filed as #436 with the options it needs a decision on. The temporary name no longer embeds the destination's basename, so a long cache filename cannot push the temporary one past the filesystem's component limit and silently disable token persistence. The async cache-write diagnostic is now suppressed on failure: a logger that raises must not take down a request that already holds a valid token. Refs #436 Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda * chore(generation): regenerate SDK for the token cache write edges Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda * test(auth): pin the long-filename and raising-logger token cache edges The temp-file prefix and the suppressed cache-write diagnostic landed in 82ef5ee without a test. Both tests fail against the previous implementation (temp name embedding the destination's basename; unsuppressed logger error) and pass on the current source. The skip reason no longer claims a Windows guarantee, matching the corrected docstring. Refs #436 Claude-Session: https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda --------- (cherry picked from commit 45cab63) Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
Member
|
/backport |
jwulf
added a commit
that referenced
this pull request
Nov 30, 2026
#429 to stable/9) (#520) * fix(auth): write the oauth token cache private and atomically Cherry-picked from #429 onto stable/9 with conflicts. The conflict markers are committed as-is and must be resolved before this backport is merged. * chore: resolve backport conflict in runtime/auth.py Drop stable/9's post-write chmod calls (the private atomic write replaces them) and import contextlib, which the backported code uses. * chore: make the backported token cache tests pass stable/9's strict pyright Type the no-op chmod stub and mark the _OAuthToken test import as intended private use. Also carry the resolved runtime/auth.py into generated/ until the backport workflow regenerates it. --------- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Josh Wulf <josh.wulf@camunda.com>
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.


Requested by Joshua Wulf · Slack thread
Before: The OAuth token cache wrote the bearer token with
Path.write_textand narrowed the file to0600only afterwards, so the token sat on disk at the process umask —0644with the common default — for the window in between, inside a cache directory created at0755that any local user could traverse and list.chmoddoes not restrict access on Windows, so there the window never closed. The write was also not atomic, so a crash part-way through left a truncated entry, and the exposure recurred on every refresh. The 401 tarpit record, which names theclient_idand the token-endpoint error, had the same shape.After: On POSIX both files are created readable and writable by the creating user only, and are moved into place in one step; the cache directory is created
0700. The exposure window and the non-atomic write are gone.This closes the audit gap with no change to the public API or to observable client behaviour: the same files, with the same names and contents, in the same place.
What this does not fix, called out after Copilot's review: on Windows a mode argument does not become an access-control list, so neither
0700nor0600restricts anything there — the files inherit the directory's ACL, exactly as they did underwrite_text. That is long-standing rather than a regression, and restricting the cache by ACL needs a platform-specific call and a cross-SDK decision, so it is filed as #436 rather than guessed at here. An earlier draft of this PR claimed the fix held on Windows; it does not, and the docstring now says so.How: A single
_write_private_filehelper writes throughtempfile.mkstempin the cache directory — which documents the file as readable and writable by the creating user ID only — and thenos.replaces it into place, which is atomic on POSIX and on Windows. The four write sites (token cache and tarpit, sync and async providers) now go through it, and theiros.chmodcalls are gone._ensure_dirpassesmode=0o700tomkdir; a mode argument only ever clears bits through the umask and never adds them, so that is0700whatever the umask is, and an existing directory is left as the user made it.runtime/tls.pyalready used this shape for private key material; this brings the token cache in line with it. The temporary name uses a short fixed prefix rather than the destination's basename, so a long cache filename cannot push the temporary one past the filesystem's component limit and silently disable persistence.Red/green: the tests were written first and six of the eight fail against the previous implementation (directory
0777instead of0700under an open umask, files0666instead of0600oncechmodis neutralised, and the previous cache entry destroyed by a write that fails part-way). The two that pass on both are guards against over-correcting — no temporary file left behind, and a pre-existing cache directory's mode not rewritten.Test scope is the defect class, not the instance: the permission test drives a token fetch and a 401 and then sweeps every file in the cache directory, so a cache file added later is covered without being named.
os.chmodis replaced with a no-op for those tests, which is what Windows effectively does, so nothing after the fact can rescue a file that was created too wide.Verification on Python 3.13 (
make generate-only, then the Makefile validation steps):ruff check .,ty check,ty check examples/and the README snippet check are clean;pytest tests/acceptanceis 850 passed, 1 skipped. The mode assertions are POSIX-only and skip elsewhere.Commits follow the repo's split rule: the
runtime/and test change, then the regenerated output, then the same pair again for the review fix-ups (chore, so they do not reach the changelog).Known overlap: #422 also touches
runtime/auth.py, to stop_OAuthToken.__repr__printing the access token. Different region of the file; the two are independent and can merge in either order.Closes #424
🤖 Generated with Claude Code
https://claude.ai/code/session_01RcVhnekJQeXnMfPhs9pYda