Skip to content

invalidate_legacy_secrets_store deletes all custom secrets on GET /settings #102

Open

Description

@simonrosenberg

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() writes secrets.model_dump_json(...) wholesale, so custom_secrets becomes {}.
  • SaasSecretsStore.store() deletes every row for (keycloak_user_id, org_id) and re-inserts only what the payload names — and it dels provider_tokens first, 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 whole Settings model and load() reconstructs it, so a settings.json written by a version that kept provider tokens there round-trips the legacy value. The migration fires on the next GET /settings.
  • SaaS: SaasSettingsStore has no secrets_store handling at all, so the field is always the default empty Secrets() 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 drops provider_tokens entirely, 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 GET that 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

  1. linear commented on Jul 30, 2026

    @linear
  2. simonrosenberg commented on Aug 3, 2026

    @simonrosenberg
    MemberAuthor

    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:

    openhands/app_server/secrets/secrets_store.py:16
    PROTECTED_CREDENTIAL_NAMES = frozenset({'CODEX_AUTH_JSON'})
    

    and applies it by narrowing the delete rather than fixing the writer (enterprise/storage/saas_secrets_store.py:92):

    delete(StoredCustomSecrets).filter(
        StoredCustomSecrets.keycloak_user_id == self.user_id,
        StoredCustomSecrets.org_id == org_id,
        StoredCustomSecrets.secret_name.not_in(PROTECTED_CREDENTIAL_NAMES),
    )

    git diff ent/main...protected-credential-writes -- openhands/app_server/settings/settings_router.py is empty — invalidate_legacy_secrets_store is untouched. So after #101 merges, GET /api/v1/settings still deletes every custom secret except CODEX_AUTH_JSON. The two secrets this issue's own reproduction used, MY_API_KEY and DEPLOY_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:

    # invalidate_legacy_secrets_store, openhands/app_server/settings/settings_router.py:383
    if len(settings.secrets_store.provider_tokens.items()) > 0:
        existing = await secrets_store.load()
        user_secrets = Secrets(
            provider_tokens=settings.secrets_store.provider_tokens,
            custom_secrets=(existing.custom_secrets if existing else {}),
        )
        await secrets_store.store(user_secrets)

    Properties worth noting for sequencing:

    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_JSON at 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.

  3. simonrosenberg commented on Aug 4, 2026

    @simonrosenberg
    MemberAuthor

    Follow-up on the "Why this matters beyond Codex" section above: that hypothesis is ruled out, definitively — not just unlikely, timeline-impossible.

    CODEX_AUTH_JSON and 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:

    • settings table dropped (no data backfill): 523b40dbf / SAAS: drop deprecated table OpenHands#11469, 2025-10-22.
    • First ACP support at all: ca621a423 ("Enable ACPAgent on RemoteRuntime API"), 2026-03-13.
    • First 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_JSON secret and a still-populated settings.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_JSON bucket 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.

  4. all-hands-bot commented on Oct 6, 2026

    @all-hands-bot
    Contributor

    [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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions