Skip to content

Commit

Permalink
Fix handling of optional fields such as user_handle.
Browse files Browse the repository at this point in the history
UserHandle is tagged as Optional[bytes] so field.annotation == bytes doesn't match - add explicit match for user_handle.

The conversion from base64 should only be done if input was json - there was a comment to this effect that that didn't work when using parse_raw - but
recent releases, using Pydantic 2.0 no longer use those deprecated methods.
  • Loading branch information
jwag956 committed Oct 6, 2023
1 parent efc5345 commit b3e550c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ target/
# Jupyter Notebook
.ipynb_checkpoints

# PyCharm
.idea

# pyenv
.python-version

Expand Down
8 changes: 3 additions & 5 deletions webauthn/helpers/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,11 @@ def _pydantic_v2_validate_bytes_fields(
"""
field = cls.model_fields[info.field_name] # type: ignore[attr-defined]

if field.annotation != bytes:
# UserHandle is defined as Optional[bytes]
if field.annotation != bytes and info.field_name != 'user_handle':
return v

if isinstance(v, str):
# NOTE:
# Ideally we should only do this when info.mode == "json", but
# that does not work when using the deprecated parse_raw method
if isinstance(v, str) and info.mode == "json":
return base64url_to_bytes(v)

return _to_bytes(v)
Expand Down

0 comments on commit b3e550c

Please sign in to comment.