ChickenBenny opened a new pull request, #77234:
URL: https://github.com/apache/airflow/pull/77234

   ## Why
   
   `PATCH /auth/fab/v1/users/{username}` can return 409 for requests that 
should just work. There are
   two ways this happens.
   
   **1. Your own username counts as "taken".** The service only looked up the 
new username if it was
   different from the one in the path, and that comparison was case-sensitive. 
But `find_user` matches
   usernames case-insensitively. So if the two differ only in case, the lookup 
finds the user you're
   updating and calls it a conflict:
   
   | Request (user stored as) | Expected | Before this PR |
   |---|---|---|
   | `PATCH /users/alice` `{"username": "Alice"}` (`alice`), just changing the 
case | 200 | 409 `The username 'Alice' already exists` |
   | `PATCH /users/carol` `{"username": "Carol", "first_name": "Caro"}` 
(`Carol`), not renaming at all | 200 | 409 `The username 'Carol' already 
exists` |
   
   The second one is easy to hit: a client GETs the user, changes one field and 
PATCHes it back, but
   builds the URL from a lower-cased name. Email has the same problem on 
databases where the lookup
   ignores case, like MySQL with a `_ci` collation.
   
   **2. Fields you're not updating still get checked.** The username and email 
checks ran before
   `update_mask` was parsed, so a field you left out of the mask could still 
fail the request:
   
   | Request | Expected | Before this PR |
   |---|---|---|
   | `PATCH /users/bob?update_mask=first_name` `{"email": "[email protected]", 
"first_name": "Bobby"}` (email belongs to another user) | 200, only 
`first_name` changes | 409 `The email '[email protected]' already exists` |
   
   Everything else in `update_user` already follows the mask (the writes and 
the password session
   reset), so these two checks were the odd ones out.
   
   ## What
   
   - Parse `update_mask` first, and only check `username` and `email` if 
they're in it.
   - Only call it a conflict when the lookup finds a *different* user 
(`existing.id != user.id`). That
     way the service doesn't need its own idea of how usernames match. It 
leaves that to `find_user`,
     and it handles the case-insensitive email lookup too.
   
   One small behaviour change: if a request has an unknown field in 
`update_mask` *and* a conflicting
   username or email, it now gets 400 `Unknown update masks` instead of 409, 
since the mask is checked
   first.
   
   ## Follow-up
   
   Why didn't the existing tests catch this? The service tests mock 
`find_user`, so they never see how
   it actually matches: ignoring case for usernames, and depending on the 
collation for emails. The
   route tests mock the whole service. Nothing runs `PATCH /users` against a 
real DB, which is why it
   took a repro in Breeze to find. A follow-up could add `db_test` tests for 
`PATCH /users` (and
   `PATCH /roles`) covering a case-only rename, a PATCH that keeps the same 
username, and a conflict
   with another user.
   
   ## Related
   
   - #76972 fixes the same kind of rename conflict for `PATCH /roles/{name}`, 
the same way: compare
     ids, and only check the name when it's in `update_mask`.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to