Skip to content

Convert a UUID user id from the session token back to UUID - #146

Open
RClaudiuM wants to merge 1 commit into
vsdudakov:mainfrom
RClaudiuM:fix/144-uuid-user-id-session
Open

RClaudiuM wants to merge 1 commit into
vsdudakov:mainfrom
RClaudiuM:fix/144-uuid-user-id-session

Conversation

@RClaudiuM

Copy link
Copy Markdown

References

Fixes #144

Summary

ApiService.sign_in stores a UUID user id in the JWT as a string. get_user_id_from_session_id then returned that string unchanged and passed it to admin_model.get_obj. SQLAlchemy's Uuid type calls .hex on the bound parameter, so for a model with a UUID primary key (for example on SQLite) the lookup raised AttributeError: 'str' object has no attribute 'hex', and /api/me and /api/configuration returned 500 right after a successful sign-in.

The function is annotated -> UUID | int | None, so this change makes it honour that: a string user id from the token is parsed back into a UUID before the lookup. A string that is not a valid UUID is treated as an invalid session and returns None. Integer user ids are unchanged. Only int | UUID ids can be issued by sign_in, so a string id in a token always came from a UUID.

Tests added in tests/api/test_service.py:

  • a UUID user id round-trips through sign_in and get_user_id_from_session_id as a UUID, and get_obj receives the UUID
  • an integer user id is still returned as an int
  • a malformed string user id returns None without calling get_obj
  • a real SQLAlchemy model with a Uuid primary key on in-memory SQLite can load the signed-in user (the case from the issue)

Before the change, the UUID, malformed-id and SQLAlchemy tests fail (the latter with the .hex error from the issue). After it, they pass.

Checked locally: pytest -n auto tests (2110 passed), ruff check, ruff format --check, ty check fastadmin.

Are there any open tasks or blockers?

None.

sign_in stores a UUID user id in the JWT as a string, but get_user_id_from_session_id returned it unchanged and passed it to get_obj. SQLAlchemy's Uuid type calls .hex on the bound value, so loading the signed-in user for a model with a UUID primary key raised AttributeError and /api/me and /api/configuration returned 500.

Parse a string user id back into a UUID before the lookup, and treat a value that is not a valid UUID as an invalid session. Integer user ids are unaffected.

This branch has not been deployed

No deployments
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.

JWT payload: a UUID is encoded as a string but the string is not decoded as a UUID

1 participant