BYNT-1365: Webauthn device name validation using form extension#119
BYNT-1365: Webauthn device name validation using form extension#119
Conversation
There was a problem hiding this comment.
Pull Request Overview
Adds a custom WebAuthn registration form that sanitizes device names to prevent XSS and wires it into the application’s security settings.
- Introduces
SanitizedWebAuthnRegisterFormwith validation and sanitization logic for thenamefield. - Imports
sanitize_stringand integratesValidationErrorfor consistent error handling. - Configures the new form under Flask-Security options in
app.py.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| enferno/user/forms.py | Added SanitizedWebAuthnRegisterForm to sanitize and validate device names. |
| enferno/app.py | Registered SanitizedWebAuthnRegisterForm in the security options. |
Comments suppressed due to low confidence (1)
enferno/user/forms.py:21
- No tests cover this new validation logic. Please add unit tests verifying empty, HTML-only, overlength, and valid name cases for
validate_name.
def validate_name(self, field):
level09
left a comment
There was a problem hiding this comment.
Can we do 2 modifications :
-
I don't like the current Silent modification UX, Users get no feedback when HTML is stripped, creating confusion. Let's raise a validation error if the input is not valid or contains HTML (explicit rejection)
-
for this use case as mentioned in the comment, we can't use the sanitize string, Let's instead build a custom method or util to validate and reject :
- html tags
- html entities
- complex nested html
- long strings
here is an example
if not field.data or not field.data.strip():
raise ValidationError("Device name cannot be empty.")
import re
from html import unescape
# Reject HTML tags
if re.search(r'<[^>]*>', field.data):
raise ValidationError(
"Device name cannot contain HTML tags. Please enter plain text only."
)
# Reject HTML entities
if unescape(field.data) != field.data:
raise ValidationError(
"Device name cannot contain HTML entities. Please enter plain text only."
)
# Normalize whitespace and validate length
clean_name = ' '.join(field.data.split())
if len(clean_name) > 64:
raise ValidationError("Device name is too long (maximum 64 characters).")
example cases to be rejected :
[
"My <script>alert('xss')</script> Phone",
"Device <script>",
"Phone<!--comment-->",
"My <div>Phone</div>",
"&Device&",
"Phone<svg onload=alert()>"
]
- also very long strings
we can re-use this util method everywhere we need (feel free to parameterize it with string length etc .. )
…or WebAuthn device names. Addresses XSS concerns by rejecting HTML tags/entities instead of cleaning them, providing clear user feedback.
level09
left a comment
There was a problem hiding this comment.
Excellent. All boxes checked ✅
Jira Issue
Description
Sanitize WebAuthn device name to prevent xss
Checklist
API Changes (if applicable)
Additional Notes
[Any other relevant information]