Skip to content

Fix the review findings on refresh tokens that need no design decision - #2853

Merged
nhoening merged 4 commits into
FlexMeasures:sec/refresh-tokensfrom
joyshmitz:sec/refresh-tokens-review-fixes
Nov 20, 2026
Merged

nhoening merged 4 commits into
FlexMeasures:sec/refresh-tokensfrom
joyshmitz:sec/refresh-tokens-review-fixes

Conversation

@joyshmitz

@joyshmitz joyshmitz commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Fixes for points 1, 4, 5 and 6 of my review of #2849, as four commits on top of c212713. Points 2 (families piling up) and 3 (expires-in for a zero lifetime) are left out, because each needs a choice that is yours.

  • Lock the user row at the start of delete_user, so that deleting a user and refreshing that user's token lock in the same order (point 1)
  • Name the index in the migration refresh_token_tracker_user_id_idx, as the naming convention does (point 5)
  • Document 422 for a request that fails validation, and 400 only for JSON that cannot be parsed (point 4)
  • Add tests for the lock order, for both status codes, and for the two safeguards that no test protected (point 6)
  • Added changelog item in documentation/changelog.rst: not needed, the entry of Sec/refresh tokens #2849 covers this

A correction to my review. There I suggested passive_deletes=True on the backref for point 1. That is not enough: it only helps while the user's families are not loaded in the session. With user.refresh_trackers loaded, the ORM still deletes the families before the user, and the deadlock is back. I reproduced that on PostgreSQL, so this PR takes the lock in delete_user instead and leaves the relationship as it was. The lock is taken without autoflush, because a flush at that point would write pending changes to the families before the user is locked.

Look & Feel

flexmeasures db check, after upgrading with this branch, no longer mentions refresh_token_tracker.

Status codes of POST /api/refreshAuthToken and POST /api/revokeRefreshToken, unchanged by this PR and now documented as they are:

request body status
{}, {"refresh-token": null}, {"refresh-token": 12}, {"refresh-token": ""}, an unknown field 422
no body, or a body that is not sent as JSON 422
{ sent as application/json 400

How to test

pytest flexmeasures/api/tests/test_refresh_tokens_fresh_db.py runs 42 tests: four new test functions, and test_malformed_refresh_requests now expects exactly 422. Each test for a safeguard fails when the code it covers is taken out:

changed in the code test that fails
no lock on the user in delete_user test_deleting_a_user_locks_the_user_before_the_families (all three cases)
that lock taken with autoflush the same test, case modified
no not user.active in exchange_refresh_token test_user_disabled_outside_the_orm_cannot_refresh
no db.session.commit() after an explicit revocation test_revocation_is_committed_by_the_revoking_request

The deadlock itself, on PostgreSQL 18, with a refresh paused between its two locks and delete_user started in that window:

code families loaded in the deleting session result
c212713 no deadlock detected, deletion aborted (3 of 3)
c212713 yes deadlock detected, deletion aborted
this branch no both commit
this branch yes both commit

The migration upgrades from bf22b21d0990, downgrades and upgrades again.

The full suite gives 3504 passed and 7 failed on my machine. The same seven fail on c212713 here: test_get_users_inactive and test_get_assets (two cases each, sorting by name, my database uses the C.UTF-8 collation), test_numerical_errors[cbc] and test_closest_sensor (two cases, cause not looked into).

Further Improvements

Points 2 and 3 of the review.

Related Items

Targets sec/refresh-tokens (#2849).


Sign-off

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on code under GPL or other license that is incompatible with FlexMeasures

Refreshing a token locks the user row and then the family row. Deleting a user let the ORM delete the family rows first and the user row after, so the two could deadlock, and PostgreSQL then aborted the deletion.

Take the lock on the user row at the start of delete_user, without autoflush, so that both paths lock in the same order, also when the session holds pending changes to the families.

Signed-off-by: Serhii <jshmitz@me.com>
The model's index=True yields refresh_token_tracker_user_id_idx under the project's naming convention, while the migration created ix_refresh_token_tracker_user_id, so flexmeasures db check reported the index as removed and added.

Signed-off-by: Serhii <jshmitz@me.com>
…ad requests

A request that fails validation, such as a missing, null, empty or non-string refresh-token, returns 422, as validation errors do elsewhere in the API. Only JSON that cannot be parsed returns 400.

Signed-off-by: Serhii <jshmitz@me.com>
…ded safeguards

Removing the inactive-user check from the token exchange, or the commit after an explicit revocation, left all tests green. The first is now covered by deactivating a user with a bulk update, which skips the listener that revokes the families. The second by rolling back the shared session after revoking, so that a later request cannot commit the pending revocation.

Signed-off-by: Serhii <jshmitz@me.com>
@joyshmitz joyshmitz mentioned this pull request Nov 10, 2026
5 tasks done
@nhoening
nhoening merged commit bf52eab into FlexMeasures:sec/refresh-tokens Nov 20, 2026
8 checks passed

@nhoening nhoening left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

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