fix(security): generate API keys from the CSPRNG instead of the creation time - #283
Merged
Merged
Conversation
…ion time API keys were sqids(digits of the creation timestamp + row id). ApiCredential uses the uuid as its primary key with incrementing disabled, so the auto-increment id is never read back on insert and the observer always saw null: every key created in the same second, on any organization, was identical. AuthenticateOnceWithBasicAuth resolves a key with first(), so a holder of one organization's key could authenticate as another's. Even with the id, a key built from a timestamp and a sequential id is guessable. generateKeys() now returns 32 random alphanumeric characters (Str::random, backed by random_bytes). Its argument is ignored and optional; the observer and the roll endpoint no longer build a seed. Found when the k6 release benchmark minted three keys within a second and two came out identical.
Merged
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## release/v1.6.67 #283 +/- ##
===================================================
Coverage 100.00% 100.00%
Complexity 7772 7772
===================================================
Files 436 436
Lines 25337 25334 -3
===================================================
- Hits 25337 25334 -3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
API keys were
sqids(digits of the creation timestamp + row id).ApiCredentialuses the uuid as its primary key with$incrementing = false, so the auto-incrementidis never read back on insert andApiCredentialObserveralways sawnull. Every API key created in the same second, on any organization, was identical.AuthenticateOnceWithBasicAuthresolves a key withfirst(), so one organization's key could authenticate as another's credential. Sandbox (flb_test_) keys have the same flaw. Even with the id, a key derived from a timestamp and a sequential id is guessable.Reproduced on the dev stack: two credentials created in the same second got the same key, and the observer saw
id = NULL. Found when the k6 release benchmark minted three keys within a second and two came out identical (fleetbase/fleetbase performance run for v0.7.66).Fix
ApiCredential::generateKeys()returns 32 random alphanumeric characters (Str::random, backed byrandom_bytes, about 190 bits). Its argument is now optional and ignored, so existing callers don't break.ApiCredentialObserver::created()andApiCredentialController::roll()no longer build a timestamp seed.flb_live_…/flb_test_…) and the hashed secret are unchanged.Tests
^flb_live_[A-Za-z0-9]{32}$; the observer gives credentials created in the same second, with a null id, different keys.test:lintandtest:date-driftare clean.After merging
Existing duplicates stay until they are rolled. Check both the live and sandbox databases: