Skip to content

WebAuthn: Avoid submitting the wrong form #769

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

Merged
merged 1 commit into from
Jul 9, 2025
Merged

Conversation

jkcgs
Copy link
Contributor

@jkcgs jkcgs commented Jul 9, 2025

Description

If the template is modified/overridden, there could be other forms before the two-factor one. This selects the form based on the tokenField input. This uses the Element.closest method that "traverses the element and its parents", which should be available to 95.2% of users according to Can I Use?.

Motivation and Context

I'm working on an internal project to integrate two-factor login to the Django admin (as others have tried: #169), and so far so good, but if the passkey setup is done inside the admin panel, the logout form (link) is triggered instead the two-factor form, since the Javascript selects the form based on the document.forms[0] line. This instead searches the closest form to the two-factor token input.

No, this small merge request does not integrate the project into the django-admin. But I could eventually work in a PR to do this, if everything goes as expected.

How Has This Been Tested?

I'm running this modification and testing it live in my django-admin environment. I haven't tested further since it's not a profound change.

Screenshots (if appropriate):

image

In the screenshot, you can see the WebAuthn setup integrated to the admin. Once the token is activated, it was triggering the logout action and I didn't understand why, until I saw how the form was being selected.

image

(congratulations screen)

With this small fix, I can successfully store the key on my account.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

If the template is modified/overridden, there could be other forms before the two-factor one. This selects the form based on the tokenField input.
Copy link
Contributor

@claudep claudep left a comment

Choose a reason for hiding this comment

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

Thanks, it looks totally reasonable.

@claudep claudep merged commit 625e98f into jazzband:master Jul 9, 2025
4 checks passed
@jkcgs jkcgs deleted the patch-1 branch July 9, 2025 23:57
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