ms-teams-cli icon indicating copy to clipboard operation
ms-teams-cli copied to clipboard

Windows: login succeeds but token storage fails — bundle exceeds Credential Manager's 2560-char blob limit

Open jimzip93 opened this issue 3 months ago • 0 comments

Summary

On Windows, teams auth login completes the OAuth flow successfully but fails at the token-persistence step. The whole token bundle is written to Windows Credential Manager as a single credential blob, and Entra token bundles routinely exceed the platform's blob-size limit.

{
  "success": false,
  "error": {
    "code": "KEYRING_ERROR",
    "message": "Keyring error: Failed to store token: Attribute 'password encoded as UTF-16' is longer than platform limit of 2560 chars"
  }
}

The result: authentication genuinely succeeded in Entra, but the CLI is left unauthenticated. teams auth status reports authenticated: false and no entry appears in Credential Manager. The CLI is unusable on this machine — every login is discarded.

Environment

  • teams-cli 0.4.0, x86_64-pc-windows-msvc (release zip)
  • Windows 11 Pro 26200
  • BYO app registration (auth_app = "byo"), device-code flow
  • Non-admin user, per-user install

Measurements

I ran the device-code flow manually against the same tenant and app registration with the CLI's exact default_delegated_scopes, and measured the returned tokens:

Component Size
access_token 2931 chars
refresh_token 1524 chars
Serialised JSON bundle as stored 4875 chars
Credential Manager limit 2560 chars

The access token alone (2931) already exceeds the limit, before the refresh token is concatenated.

Why reducing scopes does not help

The obvious mitigation — request fewer scopes to shrink the scp claim — does not work. Entra issues an access token covering all consented scopes for the resource, not just those requested. Requesting the 10 default delegated scopes returned a token whose scp claim also contained Files.ReadWrite.All, OnlineMeetings.Read, People.Read and others consented on the app registration. Token size is therefore a property of the tenant's consent grant, not of what the CLI asks for, and is not controllable from the client side.

Any Windows user in a tenant with a moderately broad consent grant will hit this. It is not an edge case.

Repro

  1. Windows, BYO app registration with several consented delegated Graph permissions.
  2. teams auth login --device-code --client-id <id> --tenant-id <id>
  3. Complete the browser sign-in.
  4. Observe KEYRING_ERROR; then teams auth status → authenticated: false.

Notes on the fix

docs/teams-cli-prd.md:172 and :894 both specify an encrypted-file fallback for exactly this situation:

Encrypted local cache: Tokens stored via OS keyring (macOS Keychain, Windows Credential Manager, Linux Secret Service/libsecret) with fallback to encrypted file (~/.config/teams-cli/tokens.enc).

but the Phase 1 checklist item "Token caching with keyring + encrypted file fallback" is unchecked and the fallback does not appear to exist in the shipped binary.

Two approaches that would resolve it:

  1. Chunk the blob across multiple Credential Manager entries (teams-cli:<profile>:0, :1, …), reassembling on read. Keeps everything in the OS credential store. Note that store_token currently writes one item; chunking needs matching cleanup in auth logout so stale chunks are not orphaned.
  2. Implement the specced fallback, using DPAPI (CryptProtectData, CurrentUser scope) on Windows so the file is bound to the user account rather than relying on a hand-rolled key derivation.

Chunking is probably the smaller change and keeps the existing security properties.

A narrower interim improvement regardless of which path is taken: the error currently surfaces as a generic KEYRING_ERROR after a successful sign-in, which makes it look like the login failed. Detecting the size overflow specifically and saying so — ideally alongside the TEAMS_CLI_ACCESS_TOKEN escape hatch — would save the next person the investigation.

jimzip93 avatar Aug 21 '26 11:08 jimzip93