Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Concurrent token refresh can corrupt stored credentials (keychain key and nais-credentials.json.enc out of sync)

Open
#816 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
cli, security

Research direction

Start with getOIDCUser and storeOIDCUser in internal/naisapi/auth/oidcuser.go, including the decryption and file-write paths. Reproduce with several nais alpha mcp serve processes after token expiry, then verify nais auth print-access-token. Done means concurrent refreshes preserve a matching keychain key and encrypted credentials file, with locked read-check-refresh-store behavior and an atomic file replacement.

Written by the indexing model from the issue text.

Description

getOIDCUser (internal/naisapi/auth/oidcuser.go:68) refreshes an expired token inside the process, with no lock across processes. storeOIDCUser (:75-105) does three things on every call:

  1. Generates a fresh random encryption key.
  2. Stores that key in the keychain (:98).
  3. Writes nais-credentials.json.enc with a non-atomic os.WriteFile (:163).

When two nais processes refresh at the same time, the keychain key and the file can end up written by different processes. Decryption then fails (:57-59, ErrNeedsOIDCLogin), and the user has to run nais login again. A torn read during the write has the same effect.

This happens in practice with several nais alpha mcp serve instances, one per AI agent session (navikt/cplt runs one per sandboxed session).

Suggested fix:

  • Take an exclusive file lock (for example flock on <configDir>/nais-credentials.lock) around the whole sequence: read, check validity, refresh, store.
  • After getting the lock, re-read the credentials, and skip the refresh if another process has already done it.
  • Write the file through a temp file and rename it into place.
  • Consider reusing the existing keychain key instead of rotating it on every refresh, so the key and the file can't drift apart.

Repro idea: let the access token expire, start N nais alpha mcp serve processes, and call one tool on each at the same time. Afterwards, check whether nais auth print-access-token still works.

Dominant language
Go
Stars
2
Forks
5
Avg merge
2h 55m
Merged PRs (30d)
30

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nais/cli

All issues in nais/cli

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.