Join our Newsletter — 33% off our NHI Course

How should security teams govern refresh tokens in CLI tools?

Treat refresh tokens as the durable credential in the session lifecycle, store them with local protections that match the runtime, and replace them whenever rotation returns a new value. If the CLI supports multiple organisations, keep token state separated by tenant so one session cannot spill into another.

How refresh tokens should be governed in CLI tools

Refresh tokens in a CLI are not just a convenience feature, they are the long-lived session credential that can keep reissuing access without a fresh login. Governance should therefore focus on storage durability, rotation handling, tenant separation, and revocation paths. The practical question is not whether the CLI can cache them, but whether the cache is protected well enough for the runtime and the user’s blast radius.

A CLI that treats refresh tokens casually creates a standing access path even when short-lived access tokens are well managed. The right control posture is to assume the refresh token has value equivalent to a reusable login session and to make the storage, replacement, and invalidation behaviour explicit. For a broader identity-control view, NHIMG’s Ultimate Guide to NHIs places OAuth tokens and service-style credentials in the wider identity model, while Token and Session Security Guide covers the lifecycle rules that matter most for refresh and session tokens.

How to store and rotate CLI refresh tokens safely

Storage should match the trust level of the host and the expected attacker model. On a local developer machine that usually means OS-backed secure storage, limited file permissions, and avoiding world-readable config paths. If the CLI runs in a more constrained or ephemeral environment, the safer answer may be to avoid persistent refresh-token storage entirely and require a narrower session design. The governance rule is simple: if the token can renew access without user interaction, it deserves stronger protection than ordinary application config.

Rotation is the second control that often fails in practice. If the authorization server returns a new refresh token, the old one should be treated as obsolete immediately, because the old value may still work unless the server enforces one-time use or family invalidation. That means the CLI must update token state atomically and handle failures carefully, otherwise it can overwrite a newer token with an older one or leave a valid predecessor lying around. NHIMG’s Guide to NHI Rotation Challenges is useful here because the operational problem is the same at scale, even when the token belongs to a human operator using a command line.

When a token is revoked, expired, or replaced, the local cache must be purged quickly and deterministically. Treat refresh-token deletion as part of session teardown, not as housekeeping. This matters because a stale token in a hidden config file often survives longer than the access token it was meant to protect, which turns local storage into the weakest link in the session chain. The right operational habit is to couple token issuance, replacement, and deletion to a single lifecycle policy instead of letting each CLI command improvise its own behaviour.

Why tenant separation and audience scoping matter in CLI sessions

CLI tools that support multiple organisations need explicit tenant separation, because refresh tokens can otherwise become a cross-tenant pivot. A user who authenticated to one tenant should not be able to silently reuse the same session state for another tenant, even if the CLI presents a shared profile or cached sign-in experience. This is not just a usability concern, it is a governance boundary: the token cache should be keyed so identity context, tenant context, and target environment cannot blur together.

Audience restriction also matters. If a CLI uses broad tokens that can call multiple downstream services, compromise of one session can spread farther than the operator intended. The best pattern is to keep the token audience as narrow as the tool’s actual task allows and to separate profiles when the same user operates across environments with different trust levels. NHIMG’s SaaS-to-SaaS and OAuth App Governance Guide reinforces the same boundary-setting principle for OAuth grants, and the OAuth 2.0 Authorization Framework defines the grant structure that underpins these token flows.

The practical test is whether one cached session can accidentally authorize another tenant or another organisation. If the answer is yes, the design is too coarse. If the CLI supports profile switching, each profile should have its own token state, renewal record, and revocation path so users can recover cleanly from a leak or account change without risking cross-contamination.

Risk and Threat Considerations

Refresh tokens are attractive to attackers because they often outlive access tokens and can be replayed to mint fresh access after the initial sign-in window has passed. In a CLI, that risk is amplified by local storage patterns, developer convenience, and the habit of reusing the same tool across multiple tenants or environments.

Failure mechanism: An attacker steals a refresh token from a config file, cache, backup, terminal artifact, or synced profile, then uses it to obtain new access tokens until rotation, revocation, or expiration stops the chain.

Impact: The attacker can maintain persistent access, bypass short-lived access-token controls, and move laterally across any tenant or service that the stolen session can still reach.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10 addresses the attack and risk surface, while NIST SP 800-53 Rev 5 sets the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP Non-Human Identity Top 10 NHI-01 — Improper Offboarding Refresh tokens must be revoked and removed when CLI sessions end or change owners.
NHI-02 — Secret Leakage CLI refresh tokens are sensitive bearer material that can leak from local storage or sync paths.
NHI-05 — Overprivileged NHI CLI token scope and audience determine how far a stolen refresh token can reach.
Recommendation — Revoke cached refresh tokens when users leave, profiles change, or access is no longer required. Store refresh tokens in protected local storage and exclude them from logs, backups, and shared paths. Minimise token scope and audience so a stolen session cannot access unrelated tenants or services.
NIST SP 800-53 Rev 5 IA-5 — Authenticator Management Refresh tokens are authenticators whose lifecycle, rotation, and revocation need explicit control.
IA-9 — Service Identification and Authentication CLI sessions often authenticate to services and APIs using reusable token credentials.
Recommendation — Manage refresh-token issuance, rotation, storage, and revocation as controlled authenticators. Use service-oriented authentication patterns that limit replay and bind tokens to expected usage.

Practitioner Guidance

What to verify: Confirm that the CLI stores refresh tokens only in the smallest durable location needed for the runtime, and that the storage path inherits the platform’s strongest local protections. Verify that token replacement is atomic so a newly issued refresh token cannot be lost or overwritten by a stale value.

What to measure: Track refresh-token age, renewal frequency, and the number of active token profiles per tenant. A growing population of long-lived, rarely rotated tokens is usually a sign that the CLI is caching sessions more aggressively than the security model can justify.

Common mistake: Treating refresh tokens like harmless cache entries because they are not the access token itself. In practice, the refresh token is often the more sensitive object because it can regenerate access repeatedly, so the governance bar should be higher, not lower.

Practitioner takeaway: The safest CLI design is the one that makes token persistence narrow, token replacement deterministic, and tenant boundaries impossible to confuse.