Skip to content

feat: store API keys as hashes - #161

Merged
VibeSWE merged 4 commits into
mainfrom
feat/hash-api-keys
Nov 24, 2026
Merged

VibeSWE merged 4 commits into
mainfrom
feat/hash-api-keys

Conversation

@VibeSWE

@VibeSWE VibeSWE commented Nov 24, 2026 •

Copy link
Copy Markdown

Description

API keys were stored, held in memory and sent between nodes as the keys themselves, and compared as plain strings. Anyone who could read parseable.json in the object store or the staging directory could use every key.

The server now keeps the SHA-256 of each key and its last four characters.

  • Hash. Keys are generated by the server from 122 random bits, so a plain fast hash is enough; the salt and cost of a password hash defend against guessable secrets. The presented key is hashed once per request and compared in constant time.
  • Create returns the key, as before. This is now the only time it can be read.
  • GET /api/prism/v1/apikeys/{id} returns the key masked (****abcd), as the list does. It used to return the full key.
  • Validate and request authentication compare hashes. The hash itself and the masked value do not authenticate.
  • UI. The API keys list no longer has a "Copy API key" action, and the creation sheet says the key is shown this once. getApiKey is removed from the client.

Existing keys

Existing keys keep working. A record stored with the key itself is still read, and is written back hashed:

  • for the default tenant, by the metadata write every query or standalone node already makes at start, which also rewrites that node's staging copy;
  • for other tenants, by a write added to the tenant loader (storage and the staging copy), made once per tenant that still has an unhashed key. The loader runs only where validate_multi_tenancy is called at start, which this repository's own main does not do. In this build a non-default tenant's keys are hashed in storage at that tenant's next metadata write.

So the first start of a query or standalone node on this version removes the default tenant's keys from storage and from the staging copy.

Hashing does not reach copies made earlier: object-store versions, backups, and the bucket's parseable.json after an import into the Postgres metastore still hold the old keys. If any of those may have been read, rotate the keys.

A stored hash that is not a SHA-256 in hex (a hand-edited record) loads, never matches, and is logged as a warning.

Difference from upstream

Upstream Parseable returns the key itself from GET /api/prism/v1/apikeys/{id}, and its integration suite (Quest) asserts that. This server cannot, so that one assertion failed in CI. The workflow now patches it in the pinned Quest source to expect the mask (scripts/ci/quest-api-key-hash.patch), which keeps the test and covers both the PR smoke run and the full runs on main. Any other client written against upstream that reads the key from this endpoint will get the mask.

One-way change

Older versions cannot read the hashed records: they fail to parse the whole metadata as soon as one API key exists.

  • Upgrade ingestors before query nodes. The first query or standalone node on this version rewrites the metadata. An ingestor still on the old version then fails every user and role sync, does not accept new keys, and cannot restart.
  • No rollback. An older version cannot start against the rewritten metadata until the API key records are removed from parseable.json by hand, and those keys are then gone.
  • A key that was lost cannot be recovered; delete it and create another.

Verified

  • cargo test --lib: 1,431 passed, 0 failed. Clippy and rustfmt add no warnings to main's.

  • Frontend: type check, 2,428 unit tests, and the 51 team and API-key-expiration browser tests pass.

  • Against a running server with a local store:

    • a created key is absent from the stored and staging metadata, and the stored hash is the SHA-256 of the key;
    • the key authenticates by X-API-Key and by Authorization: Bearer; a wrong key, the stored hash and the masked value each get 401;
    • get and list return the mask;
    • a record rewritten to the old format (key stored as apiKey) is hashed in both files by the next start, and that key still authenticates;
    • a deleted key gets 401 and stays deleted after a restart.
  • With all six PRs of this set merged together (feat: accept OTLP/HTTP protobuf on the logs, metrics and traces endpoints #156–feat: store API keys as hashes #161): cargo test --lib 1,515 passed, 0 failed, and on a running server every one of 17 protobuf requests (15 realistic shapes filling the 10 MiB payload limit, 2 crafted) returned 200 and stored the expected number of rows, authenticated with an API key.

Reviewed

Two independent read-only reviews (Claude Opus 5.5 and GLM 5.3) found the hashing, the comparison, the serde handling and every response and sync payload sound. Their findings are addressed in the last commit (the tenant staging copy, the malformed-hash warning) and in the notes above (upgrade order, earlier copies, where the tenant loader runs).

Not verified

  • The tenant-loader write is not reached by this repository's binary and has no test.
  • A mixed-version cluster was not run; the upgrade-order note comes from reading the old record type.
  • A cluster was not run, so key creation and deletion syncing between nodes on this version is covered by unit tests only.

This PR has:

  • been tested to ensure log ingestion and log query works.
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added documentation for new or modified features or behaviors.

🤖 Generated with Claude Code

VibeSWE and others added 2 commits November 24, 2026 08:08
API keys were stored, held in memory and sent between nodes as the keys
themselves, so anyone who could read parseable.json could use every key.
Keep the SHA-256 of each key and its last four characters instead. Keys
are generated by the server from 122 random bits, so an unsalted fast
hash is enough, and it is computed once per request.

The key is returned by the request that creates it and cannot be read
back: GET /api/prism/v1/apikeys/{id} now returns it masked, as the list
does. Validation and request authentication compare hashes.

Records stored with the key are still read, and are written back hashed:
for the default tenant by the write every query or standalone node makes
at start, for other tenants by a write added to the tenant loader.
Existing keys keep working. Older versions cannot read the hashed
records, so this is a one-way change.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The server no longer keeps API keys, so the key list cannot copy one
later. Remove the copy action and the client call behind it, and say on
the creation sheet that the key is shown this once.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
VibeSWE and others added 2 commits November 24, 2026 08:41
…rmed stored hash

Review follow-ups to storing API keys as hashes.

- The rewrite of a tenant's metadata in the tenant loader updated storage
  but not this node's staging copy, which kept the key until the tenant's
  next change. Write both.
- A stored hash in another form (uppercase, wrong length) loaded and never
  matched, without a word. Read it in any case, and warn when it is not a
  SHA-256 in hex. It still loads: refusing it would fail the whole metadata.
- Say in the loader that it runs only where validate_multi_tenancy is
  called at start, which this repository's own main does not do.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Quest, upstream's integration suite, checks that GET
/api/prism/v1/apikeys/{id} returns the key itself. This server now keeps
only a hash and returns the mask, so that one assertion fails in the smoke
and the full runs. Patch the assertion in the pinned Quest source before
the test image is built, so the test still covers get, with this server's
contract. The patch step fails if the pinned source stops matching.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@VibeSWE
VibeSWE merged commit d901a6f into main Nov 24, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant