Skip to content

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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

DennisDyallo
Copy link
Collaborator

@DennisDyallo DennisDyallo commented Jul 7, 2025

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:

Copy link

github-actions bot commented Jul 7, 2025

Test Results: Windows

    2 files      2 suites   10s ⏱️
3 918 tests 3 918 ✅ 0 💤 0 ❌
3 920 runs  3 920 ✅ 0 💤 0 ❌

Results for commit f077510.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Jul 7, 2025

Test Results: Ubuntu

    2 files      2 suites   15s ⏱️
3 910 tests 3 910 ✅ 0 💤 0 ❌
3 912 runs  3 912 ✅ 0 💤 0 ❌

Results for commit f077510.

♻️ This comment has been updated with latest results.

Copy link

github-actions bot commented Jul 7, 2025

Test Results: MacOS

    2 files      2 suites   12s ⏱️
3 910 tests 3 910 ✅ 0 💤 0 ❌
3 912 runs  3 912 ✅ 0 💤 0 ❌

Results for commit f077510.

♻️ This comment has been updated with latest results.

@DennisDyallo DennisDyallo requested a review from LDVG July 7, 2025 09:23
@DennisDyallo DennisDyallo force-pushed the dennisdyallo/fixes-182 branch from 2f9f708 to 76e03fb Compare July 7, 2025 14:40
@DennisDyallo DennisDyallo requested a review from Copilot July 7, 2025 14:40
Copy link
Contributor

@Copilot Copilot AI left a 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 []; use Array.Empty<byte>() or new 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

@DennisDyallo DennisDyallo force-pushed the dennisdyallo/fixes-182 branch from 2bac238 to 3356905 Compare July 8, 2025 10:05
@DennisDyallo DennisDyallo requested a review from dainnilsson July 8, 2025 10:05
@DennisDyallo DennisDyallo force-pushed the dennisdyallo/fixes-182 branch from 3356905 to 8f24ae9 Compare July 8, 2025 10:26
@dainnilsson
Copy link
Member

Should GetCurrentStatus reuse InvokeCommand and ReadStatus? The InvokeCommand with an out parameter also seems maybe a bit overly complicated? I would probably do something along the lines of:

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);

@LDVG LDVG removed their request for review July 21, 2025 08:33
@DennisDyallo
Copy link
Collaborator Author

@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.

Copy link

github-actions bot commented Aug 4, 2025

Code Coverage

Package Line Rate Branch Rate Complexity Health
Yubico.Core 40% 31% 4371
Yubico.YubiKey 51% 46% 20748
Summary 49% (35571 / 72606) 44% (8727 / 19881) 25119

Minimum allowed line rate is 40%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] DeleteSlot / DeleteSlotConfiguration throws fault when not failing
2 participants