[openstreetmap/openstreetmap-website] Fix exception uploading a unicode preference value (PR #4803)

Tom Hughes notifications at github.com
Thu May 16 16:58:27 UTC 2024


Anonymised log extract for an error captured in production:

```
Started PUT "/api/0.6/user/preferences/key" for a.b.c.d at 2024-05-16 15:53:45 +0000
Processing by Api::UserPreferencesController#update as */*
   Parameters: {"preference_key"=>"key"}
API threw unexpected Encoding::CompatibilityError exception: incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string)
/srv/www.openstreetmap.org/rails/app/validators/characters_validator.rb:6:in `match?'
/srv/www.openstreetmap.org/rails/app/validators/characters_validator.rb:6:in `validate_each'
/var/lib/gems/3.0.0/gems/activemodel-7.1.3.2/lib/active_model/validator.rb:155:in `block in validate'
/var/lib/gems/3.0.0/gems/activemodel-7.1.3.2/lib/active_model/validator.rb:151:in `each'
/var/lib/gems/3.0.0/gems/activemodel-7.1.3.2/lib/active_model/validator.rb:151:in `validate'
```

The problem is that user_preferences#upload is weird and uses the raw request body as the value but that does not have a well defined encoding and rails treats it as ASCII which causes the above exception when the model tries to validate it.

This PR simply forces the body to be treated as UTF-8 instead, and adds tests.
You can view, comment on, or merge this pull request online at:

  https://github.com/openstreetmap/openstreetmap-website/pull/4803

-- Commit Summary --

  * Test unicode values in user preference keys and values
  * Treat the body as UTF-8 for user_preferences#update

-- File Changes --

    M app/controllers/api/user_preferences_controller.rb (2)
    M test/controllers/api/user_preferences_controller_test.rb (22)

-- Patch Links --

https://github.com/openstreetmap/openstreetmap-website/pull/4803.patch
https://github.com/openstreetmap/openstreetmap-website/pull/4803.diff

-- 
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/4803
You are receiving this because you are subscribed to this thread.

Message ID: <openstreetmap/openstreetmap-website/pull/4803 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20240516/15d145f9/attachment.htm>


More information about the rails-dev mailing list