Skip to content
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

feat(webauthn): add partial unsupported json v2 compat #327

Merged
merged 1 commit into from
Nov 15, 2024

Conversation

seankhliao
Copy link
Contributor

Using the candidate json/v2 encoder
https://github.com/go-json-experiment/json SessionData doesn't roundtrip properly through json as an empty []byte is encoded as "". Mark compatible fields as omitempty, and use len(field) == 0 instead of nil checks to be more resilient.

@seankhliao seankhliao requested a review from a team as a code owner November 9, 2024 21:55
Copy link

coderabbitai bot commented Nov 9, 2024

Walkthrough

The changes made in the webauthn/login.go file focus on the ValidatePasskeyLogin method, refining the validation logic for user credentials. The method now checks the lengths of session.UserID and parsedResponse.Response.UserHandle instead of performing direct nil comparisons. This adjustment enhances the reliability of the validation process without altering the overall structure or core logic of the login functionality.

Changes

File Change Summary
webauthn/login.go Updated ValidatePasskeyLogin method to check lengths of session.UserID and parsedResponse.Response.UserHandle instead of nil comparisons.

Poem

In the code where bunnies play,
Validations now hold sway.
Lengths are checked, not nils in sight,
Ensuring logins work just right.
Hops of joy, we celebrate,
For better checks, we elevate! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7fbd1ee and 33c0a2c.

📒 Files selected for processing (1)
  • webauthn/login.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • webauthn/login.go

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
webauthn/login.go (1)

218-220: LGTM: Improved validation check for UserHandle

The change from nil check to length check is consistent with the previous change and handles both nil and empty byte slices properly.

Consider making the error message more specific by changing "blank" to "empty or nil" to better reflect the actual check being performed.

-    return nil, nil, protocol.ErrBadRequest.WithDetails("Client-side Discoverable Assertion was attempted with a blank User Handle")
+    return nil, nil, protocol.ErrBadRequest.WithDetails("Client-side Discoverable Assertion was attempted with an empty or nil User Handle")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 865f160 and 7fbd1ee.

📒 Files selected for processing (2)
  • webauthn/login.go (1 hunks)
  • webauthn/types.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • webauthn/types.go
🔇 Additional comments (1)
webauthn/login.go (1)

214-216: LGTM: Improved validation check for UserID

The change from nil check to length check is more robust as it handles both nil and empty byte slices consistently, which aligns with the PR's goal of fixing JSON encoding issues.

@james-d-elliott
Copy link
Member

What specific scenario does the omitempty change fix? I wouldn't consider the challenge to be at all compatible with this change as it's arbitrarily required.

@seankhliao
Copy link
Contributor Author

A login looks like:

  1. initial request from client triggers BeginDiscoverableLogin, which creates the SessionData
  2. SessionData is persisted
  3. client completes the challenge and sends the response back to the server
  4. SessionData is retrieved and together with the client response passed to ValidatePasskeyLogin

If the SessionData is not just held in memory (e.g. stored in a secure cookie, or shared cache between server instances), it needs to be serialized, which is most naturally done through json.

Before this PR, for the field UserID []byte set to nil.

  • encoding/json marshals it as "user_id": null
  • github.com/go-json-experiment/json marshals it as "user_id": ""

Unmarshaling "" gets turned into a 0 len []byte, which fails the check for == nil even though it's functionally equivalent, ref https://google.github.io/styleguide/go/decisions#nil-slices

This PR makes 2 changes: setting omitempty and changing the check to len() == 0.
Either alone is enough solve the issue, though as a matter of style, I think both should apply.

@seankhliao
Copy link
Contributor Author

As for compatibility, this package checks the UserID field with bytes.Equal, e.g. here https://github.com/seankhliao/webauthn/blob/7fbd1ee8072860d0588934d171c8f844a53a5a65/webauthn/login.go#L192

The docs for bytes.Equal note:

A nil argument is equivalent to an empty slice.

@james-d-elliott
Copy link
Member

I think we should wait on the change to omit empty since the other change actually fixes the edge case issue. My reasoning is this experimental package is subject to sudden breaking changes without warning, and in addition both the challenge and relying party ID may not be empty for any reason, leaving the only change which is reasonable being the change to the user ID field's tags.

@james-d-elliott james-d-elliott changed the title fix(webauthn): SessionData empty values through json feat(webauthn): add partial unsupported json v2 comapt Nov 9, 2024
@james-d-elliott james-d-elliott changed the title feat(webauthn): add partial unsupported json v2 comapt feat(webauthn): add partial unsupported json v2 compat Nov 9, 2024
Using the candidate json/v2 encoder
https://github.com/go-json-experiment/json SessionData doesn't
roundtrip properly through json as an empty []byte is encoded as "".
Use len(field) == 0 instead of nil checks to be more resilient.
@seankhliao
Copy link
Contributor Author

Sure, I've dropped the omitempty change, leaving just the change to check against length instead of nil.

@james-d-elliott james-d-elliott merged commit bf37040 into go-webauthn:master Nov 15, 2024
@seankhliao seankhliao deleted the json-marshal branch November 15, 2024 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants