fix(gateway): generate device user_code with a CSPRNG#159
Open
arseniycodes wants to merge 1 commit into
Open
Conversation
The device-authorization user_code was built from Math.random(), a non-cryptographic PRNG, over an 8-char / 30-symbol alphabet. The user_code is the only secret an approver checks at /activate/approve, so a predictable generator lets an attacker who observes prior outputs predict in-flight codes and approve a victim's pending device — binding the attacker's org and a freshly minted session/ingest key onto the victim's device. Switch to crypto.randomInt over the same alphabet (unbiased, CSPRNG).
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
The device-authorization
user_code(humanCode()) was generated withMath.random()— a non-cryptographic PRNG — over an 8-character / 30-symbol alphabet. Theuser_codeis the sole secret an approver verifies at/activate/approve.Impact
Math.random()'s internal state is recoverable from observed outputs, so an attacker can predict in-flightuser_codes within the code's TTL and call/activate/approvefor a victim's pending device. Approval binds the attacker's org plus a freshly minted CLI session token and ingest key onto the victim's device session — silently redirecting the victim's CLI/telemetry into the attacker's project. (Thedevice_codetoken pickup already usednanoid, so only the human-visible code was affected.)Fix
Generate
user_codewithcrypto.randomIntover the same alphabet (CSPRNG, unbiased).Follow-up worth considering separately: rate-limit
/activate/approveby IP/code. With a CSPRNG over ~30^8 the code space is now infeasible to brute-force, so this is defense-in-depth.Summary by cubic
Generate the device-authorization user_code with a CSPRNG using
crypto.randomIntto remove predictability. This prevents attackers from guessing in-flight codes and approving a victim’s device.Written for commit aba7ab8. Summary will update on new commits.