Repository navigation
Description
Problem
invalidate_legacy_secrets_store (openhands/app_server/settings/settings_router.py:390) destroys all of a user's custom secrets. It runs inside GET /api/v1/settings, so no user action beyond opening the app is required.
The function migrates legacy provider tokens out of Settings.secrets_store into the secrets store:
if len(settings.secrets_store.provider_tokens.items()) > 0:
user_secrets = Secrets(provider_tokens=settings.secrets_store.provider_tokens)
await secrets_store.store(user_secrets)It builds a fresh Secrets carrying only provider_tokens, so custom_secrets is {}. Both store implementations treat a store() payload as the whole truth:
FileSecretsStore.store()writessecrets.model_dump_json(...)wholesale, socustom_secretsbecomes{}.SaasSecretsStore.store()deletes every row for(keycloak_user_id, org_id)and re-inserts only what the payload names — and itdelsprovider_tokensfirst, so on that path the migration deletes the custom secrets and saves nothing.
The migration then clears settings.secrets_store, so it is a one-shot, unrecoverable loss.
Reproduction
Against main (7cca8f8d7), calling the real function:
store = FileSecretsStore(LocalFileStore(root=tempfile.mkdtemp()))
await store.store(Secrets(custom_secrets={
'MY_API_KEY': CustomSecret.from_value({'secret': 'k1', 'description': 'prod key'}),
'DEPLOY_TOKEN': CustomSecret.from_value({'secret': 'k2', 'description': 'deploy'}),
}))
legacy = Secrets(provider_tokens={ProviderType.GITHUB: ProviderToken(token=SecretStr('ghp_x'))})
await invalidate_legacy_secrets_store(Settings(secrets_store=legacy), settings_store, store)before : ['DEPLOY_TOKEN', 'MY_API_KEY']
after : []
Who is affected
OSS only, once per affected user. The trigger is settings.secrets_store.provider_tokens being non-empty:
- OSS:
FileSettingsStore.store()serialises the wholeSettingsmodel andload()reconstructs it, so asettings.jsonwritten by a version that kept provider tokens there round-trips the legacy value. The migration fires on the nextGET /settings. - SaaS:
SaasSettingsStorehas nosecrets_storehandling at all, so the field is always the default emptySecrets()and the migration cannot fire. Not a production SaaS exposure.
No new legacy data is being created — secrets_store is in _SETTINGS_UPDATE_IGNORED_FIELDS, so the settings POST cannot write it. This only affects users carrying data from before that migration.
Suggested fix
Read-modify-write instead of constructing a fresh document:
existing = await secrets_store.load() or Secrets()
user_secrets = existing.model_copy(
update={'provider_tokens': settings.secrets_store.provider_tokens}
)
await secrets_store.store(user_secrets)Worth considering alongside it:
- The
SaasSecretsStore.store()path dropsprovider_tokensentirely, so if this migration were ever reachable in SaaS it would lose them silently. Either the migration should be OSS-scoped explicitly or the drop should be loud. - A
GETthat performs a destructive write is the underlying hazard. If legacy data is old enough to be gone in practice, deleting the migration is cheaper than fixing it.
Relationship to other work
This is the general form of a bug being fixed narrowly for one key. #101 makes store() structurally unable to write CODEX_AUTH_JSON, which immunises that one credential against exactly this path — but every other custom secret stays exposed. Full context in the triage on #77: #77 (comment)
Filing separately because it is pre-existing on main, independent of the Codex credential work, and shouldn't be bundled into that stack.
Activity
linear commentedon Jul 30, 2026
OHE-3015
simonrosenberg commentedon Aug 3, 2026
Confirmed still live on
ent/main@7cca8f8d7, and adding two findings from the telemetry investigation in #120 (this issue is now tracked as a sub-issue of it).1. The fix currently in flight does not fix this
#101 introduces an allowlist of one:
and applies it by narrowing the delete rather than fixing the writer (
enterprise/storage/saas_secrets_store.py:92):git diff ent/main...protected-credential-writes -- openhands/app_server/settings/settings_router.pyis empty —invalidate_legacy_secrets_storeis untouched. So after #101 merges,GET /api/v1/settingsstill deletes every custom secret exceptCODEX_AUTH_JSON. The two secrets this issue's own reproduction used,MY_API_KEYandDEPLOY_TOKEN, remain destroyed.That guard's stated purpose is write-back correctness — stopping a stale read-modify-write from restoring a credential a runtime has rotated. Preventing data loss is a side effect, which is why it inherited the Codex scope of the #70 design and why it sits on top of #100's CAS.
2. The credential-agnostic fix is about three lines
The root cause is that the migration builds a whole document from one field and hands it to a store that treats any payload as the whole truth:
Properties worth noting for sequencing:
CODEX_AUTH_JSON.FileSecretsStoreandSaasSecretsStore).maindirectly, so it is testable againstent/main— fix(secrets): make the managed-credential write guard structural #101 is not, since it "deletes machinery that only exists" on feat(secrets): add versioned compare-and-swap to the secrets stores #100.Recommend landing this independently of #100/#101 rather than waiting on the write-back design, which per #120 addresses a mechanism observed in at most 1 of 11 production failures.
Why this matters beyond Codex
In #120's 15-day production window, the largest single Codex ACP failure bucket was 21 sandboxes where the user selected the Codex agent and the runtime found no
CODEX_AUTH_JSONat all — no materialisation log of any kind. This is the only mechanism found that is reproduced against real code and produces that shape: "user configured a credential, later conversation has none."Two caveats, stated plainly: this fires only for users who still had legacy
provider_tokens, and only once per user; and there is no user-level telemetry linking it to any of those 21 sandboxes. The connection is mechanism-and-shape, not traced causation. The data-loss bug stands on its own reproduction regardless.Residual design flaw
Fixing the migration stops the current bleeding but leaves the class of bug representable: any caller holding one field can still delete the rest via
store(). A partial-update contract (or rejecting whole-document overwrites) would make that impossible. Tracked as R8 in #120 — a separate, larger decision.simonrosenberg commentedon Aug 4, 2026
Follow-up on the "Why this matters beyond Codex" section above: that hypothesis is ruled out, definitively — not just unlikely, timeline-impossible.
CODEX_AUTH_JSONand ACP (the agent protocol Codex runs under) didn't exist until well after the only mechanism that could have destroyed a custom secret via this migration was already gone from the codebase:settingstable dropped (no data backfill):523b40dbf/ SAAS: drop deprecated table OpenHands#11469, 2025-10-22.ca621a423("Enable ACPAgent on RemoteRuntime API"), 2026-03-13.CODEX_AUTH_JSON:d01935765("feat(acp): add ChatGPT-subscription auth for Codex via auth.json injection"), 2026-05-15.That's a 5–7 month gap, and migrations apply strictly in order — any codebase containing Codex ACP support necessarily already contains the migration 077 table drop, on every deployment (cloud and replicated alike; there's no upgrade path that gets you Codex ACP while still being pre-077). So there is no point in time, on any install, where a user could have had a
CODEX_AUTH_JSONsecret and a still-populatedsettings.secrets_store.provider_tokens— the guard this migration needs to fire and the secret it was hypothesized to destroy never coexisted. This mechanism cannot be any part of the 21-sandbox no-CODEX_AUTH_JSONbucket from #120.This doesn't affect the core finding above — the custom-secrets data-loss bug is real and reproducible, fixed in #121 — only the "why this matters beyond Codex" framing, which should be treated as closed/ruled out rather than an open lead.
all-hands-bot commentedon Oct 6, 2026
[Automatic Post]: This issue has had no activity for 63 days (labels: none). Could someone take a look when they have a chance and help figure out next steps?
Generated by OpenHands automation on behalf of the user.