575: fix double hashing issue#576
Conversation
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
|
Warning Review limit reached
More reviews will be available in 49 minutes and 7 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more credits in the billing tab to continue. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughTokenIDGenerator.generateTokenID changes how HMAC digests are computed. Instead of wrapping input bytes with ChangesToken ID generation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java (1)
30-30: 💤 Low valuePotential
StringIndexOutOfBoundsExceptionif hash output is shorter thantokenIDLength.If
tokenIDLengthexceeds the length of the numeric string derived fromhash.getBytes(),substring(0, tokenIDLength)will throw. While this may be unlikely given typical hash lengths and configured token lengths, a defensive check would improve robustness.🛡️ Defensive fix
- return new BigInteger(hash.getBytes()).toString().substring(0, tokenIDLength); + String numericHash = new BigInteger(hash.getBytes()).toString(); + return numericHash.substring(0, Math.min(tokenIDLength, numericHash.length()));🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java` at line 30, The return expression in TokenIDGenerator that does new BigInteger(hash.getBytes()).toString().substring(0, tokenIDLength) can throw StringIndexOutOfBoundsException if the numeric string is shorter than tokenIDLength; fix it in the TokenIDGenerator method by first converting to String (e.g., String numeric = new BigInteger(hash.getBytes()).toString()), then if numeric.length() < tokenIDLength pad (e.g., prepend zeros) to reach tokenIDLength, otherwise return numeric.substring(0, tokenIDLength); update the return site to use this guarded value so substring never receives an out-of-range end index.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java`:
- Around line 28-29: The hashing uses String.getBytes() which is
platform-dependent; update TokenIDGenerator where uinHash and hash are computed
to call getBytes with an explicit charset (e.g., StandardCharsets.UTF_8) before
passing to HMACUtils2.digestAsPlainText, and add the necessary import for
java.nio.charset.StandardCharsets so the produced HMACs are deterministic across
platforms.
- Around line 28-29: The token derivation was changed: you removed the
intermediate HMACUtils2.generateHash call which alters downstream Token IDs and
also used platform-default charset via String.getBytes(); to fix, restore the
original intermediate hash step by computing uinHash with
HMACUtils2.generateHash(...) (same method previously used) and then compute the
final hash from that value, and ensure all byte conversions use
StandardCharsets.UTF_8 (e.g., replace getBytes() with
getBytes(StandardCharsets.UTF_8)); look for TokenIDGenerator and the
HMACUtils2.digestAsPlainText/generateHash usages to apply this change so token
outputs remain deterministic and backward-compatible.
---
Nitpick comments:
In
`@kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java`:
- Line 30: The return expression in TokenIDGenerator that does new
BigInteger(hash.getBytes()).toString().substring(0, tokenIDLength) can throw
StringIndexOutOfBoundsException if the numeric string is shorter than
tokenIDLength; fix it in the TokenIDGenerator method by first converting to
String (e.g., String numeric = new BigInteger(hash.getBytes()).toString()), then
if numeric.length() < tokenIDLength pad (e.g., prepend zeros) to reach
tokenIDLength, otherwise return numeric.substring(0, tokenIDLength); update the
return site to use this guarded value so substring never receives an
out-of-range end index.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: dbca8ce2-6628-4a87-8819-8958afed30eb
📒 Files selected for processing (1)
kernel/kernel-keymanager-service/src/main/java/io/mosip/kernel/tokenidgenerator/generator/TokenIDGenerator.java
Signed-off-by: nagendra0721 <nagendra0718@gmail.com>
[#575]
Summary by CodeRabbit