-
Notifications
You must be signed in to change notification settings - Fork 60
fix(otp): Handle case when last slot is deleted #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
Test Results: Windows 2 files 2 suites 10s ⏱️ Results for commit f077510. ♻️ This comment has been updated with latest results. |
Test Results: Ubuntu 2 files 2 suites 15s ⏱️ Results for commit f077510. ♻️ This comment has been updated with latest results. |
Test Results: MacOS 2 files 2 suites 12s ⏱️ Results for commit f077510. ♻️ This comment has been updated with latest results. |
2f9f708
to
76e03fb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR enhances the OTP error transform pipeline by centralizing failed-APDU creation and updating sequence validation to correctly handle the “last slot deleted” edge case.
- Added a
FailedApdu
helper to simplify constructing error responses. - Introduced
IsValidSequenceProgression
to encapsulate sequence-number checks, including wrap-around when deleting the final slot. - Updated the main
Invoke
method and exception catch block to use the new helper and validation logic.
Comments suppressed due to low confidence (3)
Yubico.YubiKey/src/Yubico/YubiKey/Pipelines/OtpErrorTransform.cs:85
- Add unit tests for
IsValidSequenceProgression
to cover normal increments, wrap-around cases, and the 'last slot deletion' scenario.
private static bool IsValidSequenceProgression(int beforeSequence, int nextSequence, OtpStatus afterStatus)
Yubico.YubiKey/src/Yubico/YubiKey/Pipelines/OtpErrorTransform.cs:75
- C# does not support array literal
[]
; useArray.Empty<byte>()
ornew byte[0]
to create an empty byte array.
return FailedApdu([]);
Yubico.YubiKey/src/Yubico/YubiKey/Pipelines/OtpErrorTransform.cs:89
- This comparison doesn’t handle wrap-around properly. Consider using
(beforeSequence + 1) % 0x100
to mirror the original sequence logic.
return nextSequence == beforeSequence + 1 || // Normal increment
Yubico.YubiKey/src/Yubico/YubiKey/Pipelines/OtpErrorTransform.cs
Outdated
Show resolved
Hide resolved
2bac238
to
3356905
Compare
3356905
to
8f24ae9
Compare
Should private OtpStatus GetCurrentStatus() =>
ReadStatus(InvokeCommand(new ReadStatusCommand().CreateCommandApdu(), typeof(ReadStatusCommand), typeof(ReadStatusResponse)); and var beforeStatus = ReadStatus(InvokeCommand(new ReadStatusCommand().CreateCommandApdu(), typeof(ReadStatusCommand), typeof(ReadStatusResponse)));
var responseApdu = InvokeCommand(command, commandType, responseType);
var afterStatus = ReadStatus(responseApdu); |
@dainnilsson Thanks, I've simplified the code in the latest commit. I reused the ReadStatus, but I figured there's no need to reuse invokecommand as it doesn't really do much but add another layer in the stack trace. |
This pull request modifies the error handling logic in the
OtpErrorTransform
pipeline to improve clarity and handle edge cases more effectively. The changes include introducing a helper method for failed APDU responses and refining sequence validation logic.Fixes: #182
Error handling improvements:
Yubico.YubiKey/src/Yubico/YubiKey/Pipelines/OtpErrorTransform.cs
: Added aFailedApdu
helper method to streamline the creation of failed APDU responses. This simplifies the code and avoids redundancy.Yubico.YubiKey/src/Yubico/YubiKey/Pipelines/OtpErrorTransform.cs
: Updated the sequence validation logic to account for cases where the programming sequence is set to0
when deleting the last slot, ensuring proper handling of this edge case.