fix(security): generate OTP with SecureRandom, uniform draw, zero-padded (A2) - #136
Open
pallakartheekreddy wants to merge 1 commit into
Open
fix(security): generate OTP with SecureRandom, uniform draw, zero-padded (A2)#136pallakartheekreddy wants to merge 1 commit into
pallakartheekreddy wants to merge 1 commit into
Conversation
…ded (A2) KeycloakSmsAuthenticatorUtil.getSmsCode used java.util.Random + nextFloat(): Random r = new Random(); long code = (long) (r.nextFloat() * maxValue); return Long.toString(code); Three defects made the OTP guessable/undersized: (1) java.util.Random is a time-seeded LCG, predictable from a few outputs; (2) nextFloat()'s ~24-bit mantissa cannot uniformly address a 10^8 space (gaps/clustering); (3) Long.toString drops leading zeros, shrinking effective length and leaking high-zero digits. Replace with a shared SecureRandom, a uniform nextInt(10^n) draw, and %0Nd zero-padding so every code is exactly nrOfDigits long. Guard nrOfDigits to 1..9 (10^9 fits in int) and throw IllegalArgumentException (still a RuntimeException, so existing catch sites are unaffected). Pure utility change, no Keycloak SPI contract impact. Verified: sms-provider module test-compiles under JDK 11. Implements A2 from implementation-designs/sunbird-auth.md. A1 (OTP expiry/first-use invalidation), A3 (attempt lockout) and A4 (msg91 cleartext HTTP) are separate, higher-touch changes deferred to their own PRs.
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
Fixes the predictable-OTP finding A2 (HIGH) in
implementation-designs/sunbird-auth.md.The vulnerability
KeycloakSmsAuthenticatorUtil.getSmsCodegenerated the OTP with:Three compounding defects:
java.util.Randomis a time-seeded LCG — not cryptographic, predictable from a couple of outputs.nextFloat()'s ~24-bit mantissa can't uniformly address a10^8space → gaps/clustering, some codes unreachable.Long.toString(code)drops leading zeros, shrinking effective length and leaking that the high digits were zero.The fix
private static final SecureRandom.SECURE_RANDOM.nextInt(10^n)draw.String.format("%0Nd", code)zero-padding so every OTP is exactlynrOfDigitslong.nrOfDigitsto1..9(fits inint); throwIllegalArgumentException(still aRuntimeException, so existing catch sites are unaffected).Pure utility change — no Keycloak SPI contract impact.
SecureRandom.nextIntis non-blocking at OTP volumes.Verification
sms-providermodule test-compiles under JDK 11 (exit 0; class recompiled).Scope / deferred
A1 (OTP expiry never enforced / no first-use invalidation), A3 (attempt lockout/rate-limit), and A4 (msg91 over cleartext HTTP, authkey in URL) are separate, higher-touch changes and get their own PRs.