fix(security): generate referral codes with secrets CSPRNG#356
Open
kevinnft wants to merge 1 commit into
Open
Conversation
Replace non-crypto random.choices with secrets.choice over the 62-symbol alphanumeric alphabet so referral codes cannot be predicted from prior outputs of the Mersenne Twister stream. Closes Quantarq#196 Signed-off-by: NossXBT <kevinnft@users.noreply.github.com>
| from web_app.api import referal | ||
|
|
||
| source = inspect.getsource(referal.generate_random_string) | ||
| assert "secrets" in source |
|
|
||
| source = inspect.getsource(referal.generate_random_string) | ||
| assert "secrets" in source | ||
| assert "random.choices" not in source |
| source = inspect.getsource(referal.generate_random_string) | ||
| assert "secrets" in source | ||
| assert "random.choices" not in source | ||
| assert "random.random" not in source |
|
|
||
| alphabet = string.ascii_letters + string.digits | ||
| code = referal.generate_random_string(16) | ||
| assert len(code) == 16 |
| alphabet = string.ascii_letters + string.digits | ||
| code = referal.generate_random_string(16) | ||
| assert len(code) == 16 | ||
| assert all(ch in alphabet for ch in code) |
|
|
||
| # Sanity: many draws stay unique enough that collision is rare. | ||
| codes = {referal.generate_random_string(16) for _ in range(200)} | ||
| assert len(codes) == 200 |
| # ±0.5% absolute frequency band around 1/62 as specified in #196. | ||
| for ch in alphabet: | ||
| freq = counts[ch] / total | ||
| assert abs(freq - (1 / len(alphabet))) <= 0.005, ( |
| import math | ||
|
|
||
| entropy_bits = length * math.log2(len(alphabet)) | ||
| assert entropy_bits >= 95 |
4 tasks
| counts.update(generate_random_string(length)) | ||
|
|
||
| total = draws * length | ||
| expected = total / len(alphabet) |
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.
Summary
Closes #196.
Referral codes were generated with
random.choices(Mersenne Twister). That stream is not cryptographically secure; prior outputs can let an attacker predict future codes. This PR switches tosecrets.choiceover the 62-symbol alphanumeric alphabet.Changes
quantara/web_app/api/referal.py:secrets.choice+ shared alphabetquantara/web_app/tests/test_create_referal_link.py: source/CSPRNG checks, uniqueness, distribution/entropy sanityTest plan
random.choices/random.random