matrix-org / synapse

Synapse: Matrix homeserver written in Python/Twisted.
https://matrix-org.github.io/synapse
Apache License 2.0
11.83k stars 2.13k forks source link

Cannot search for non-ascii displaynames using the admin API #16370

Open niudin opened 1 year ago

niudin commented 1 year ago

I searched for Chinese nicknames in the background and reported an error. {errcode: "M_UNKNOWN", error: "Query parameter 'name' must be ascii"} errcode : "M_UNKNOWN" error : "Query parameter 'name' must be ascii"

DMRobertson commented 1 year ago

Exception is from

https://github.com/matrix-org/synapse/blob/d38d0dffc94b6269ed7ff5163d60958be3e6c304/synapse/http/servlet.py#L493-L496

and I would guess this comes from

https://github.com/matrix-org/synapse/blob/eef2b9e34418e902baab1e730eb805eb56034cc2/synapse/rest/admin/users.py#L102

though I cannot say for certain without server side logs.

I would guess that passing encoding="utf-8" would suffice here, like we do in

https://github.com/matrix-org/synapse/blob/dd44ee00b6cf4d900e56857039320660400cff37/synapse/rest/admin/rooms.py#L229

(Does it make sense to interpret query parameters as utf-8 by default?)

DMRobertson commented 1 year ago

If anyone wants to try this:

  1. Write a test reproducing the issue.
    • Set a user's displayname to a non-ascii string.
    • Call GET /_synapse/admin/v2/users?name=.... with the displayname you chose
    • Assert that the response is 200 OK and includes the user you were searching for.
  2. Run the test locally and check that it fails.
  3. Try adding encoding=utf-8 to the parse_string call for name I highlighted above.
  4. Rerun the test and check it succeeds.
niudin commented 1 year ago

Thank you very much, I solved it according to your method

niudin commented 1 year ago

Another question is, can I limit the number of registrations with the same IP? I looked in the database and it seems that the registered IPs are not stored in a separate table.

clokep commented 1 year ago

Thank you very much, I solved it according to your method

Would you mind contributing back a pull request with the improvements?