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

Fix a pydantic crash while enrolling #4

Closed

Conversation

jacopotediosi
Copy link

In my setup, OctoPrint 1.11.0.dev287 (latest maintanance branch), Windows and Python 3.12.7, OctoPrint-MfaTotp crashes when clicking the Enroll button, with the following stacktrace:

octoprint.server.api - ERROR - Error while executing SimpleApiPlugin mfa_totp
Traceback (most recent call last):
  File "OctoPrint\src\octoprint\server\api\__init__.py", line 157, in pluginCommand
    response = api_plugin.on_api_command(command, data)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "OctoPrint\src\octoprint\util\__init__.py", line 1683, in wrapper
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "OctoPrint-MfaTotp\octoprint_mfa_totp\__init__.py", line 160, in on_api_command
    key, uri = self._enroll_user(userid)
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "OctoPrint-MfaTotp\octoprint_mfa_totp\__init__.py", line 85, in _enroll_user
    self._data.users[userid] = MfaTotpUserSettings(
                               ^^^^^^^^^^^^^^^^^^^^
  File "OctoPrint\venv\Lib\site-packages\pydantic\main.py", line 212, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pydantic_core._pydantic_core.ValidationError: 1 validation error for MfaTotpUserSettings
last_used
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.9/v/string_type

I believe the error is due to the MfaTotpUserSettings.last_used field, which is declared as str but initialized to None.
This PR changes the field declaration to str|None. I'm not sure if this is the best change based on the context of the code, but at least it fixes the crash.

@foosel foosel closed this in 329185a Oct 28, 2024
@foosel
Copy link
Member

foosel commented Oct 28, 2024

I have no idea when this broke, because that definitely used to work, but regardless, your diagnosis was spot on and it's now fixed. Thanks!

@jacopotediosi jacopotediosi deleted the fix/pydantic-crash branch October 29, 2024 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants