Skip to content

fix(auth): write the oauth token cache private and atomically - #429

Merged
emilyoram merged 5 commits into
mainfrom
fix/424-token-cache-permissions
Nov 20, 2026
Merged

emilyoram merged 5 commits into
mainfrom
fix/424-token-cache-permissions

Conversation

@claude

@claude claude Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Joshua Wulf · Slack thread

Before: The OAuth token cache wrote the bearer token with Path.write_text and narrowed the file to 0600 only afterwards, so the token sat on disk 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. chmod does 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 the client_id and 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 0700 nor 0600 restricts anything there — the files inherit the directory's ACL, exactly as they did under write_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_file helper writes through tempfile.mkstemp in the cache directory — which documents the file as readable and writable by the creating user ID only — and then os.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 their os.chmod calls are gone. _ensure_dir passes mode=0o700 to mkdir; a mode argument only ever clears bits through the umask and never adds them, so that is 0700 whatever the umask is, and an existing directory is left as the user made it. runtime/tls.py already 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 0777 instead of 0700 under an open umask, files 0666 instead of 0600 once chmod is 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.chmod is 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/acceptance is 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

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

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.

🟡 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 chmod after 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

Comment thread runtime/auth.py Resolved
Comment thread runtime/auth.py Resolved
Comment thread runtime/auth.py Resolved
@claude
claude Bot marked this pull request as ready for review November 13, 2026 06:46
@claude
claude Bot requested a review from a team as a code owner November 13, 2026 06:46
@claude
claude Bot marked this pull request as draft November 13, 2026 06:53
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
@claude
claude Bot marked this pull request as ready for review November 13, 2026 07:19
@claude
claude Bot marked this pull request as draft November 13, 2026 07:25
@claude
claude Bot marked this pull request as ready for review November 13, 2026 07:32
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

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.

🟢 Approval recommended

No active review issues block the changes introducing private, atomic token-cache and 401 tarpit writes.

No open issues 🎉

Review effort: Balanced

@emilyoram
emilyoram merged commit 45cab63 into main Nov 20, 2026
27 checks passed
@jwulf jwulf added backport stable/10 Backport to stable/10 after merge backport stable/9 Backport to stable/9 after merge labels Nov 30, 2026
@camunda-sdk-automation

Copy link
Copy Markdown
Contributor

Successfully created backport PR for stable/10:

@camunda-sdk-automation

Copy link
Copy Markdown
Contributor

Created backport PR for stable/9:

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.
@jwulf

jwulf commented Nov 30, 2026

Copy link
Copy Markdown
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>
@jwulf

jwulf commented Nov 30, 2026

Copy link
Copy Markdown
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport stable/9 Backport to stable/9 after merge backport stable/10 Backport to stable/10 after merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(auth): OAuth token cache file is world-readable between write and chmod, in a 0755 cache directory

4 participants