<p dir="auto">Anonymised log extract for an error captured in production:</p>
<pre class="notranslate"><code class="notranslate">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'
</code></pre>
<p dir="auto">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.</p>
<p dir="auto">This PR simply forces the body to be treated as UTF-8 instead, and adds tests.</p>

<hr>

<h4>You can view, comment on, or merge this pull request online at:</h4>
<p>  <a href='https://github.com/openstreetmap/openstreetmap-website/pull/4803'>https://github.com/openstreetmap/openstreetmap-website/pull/4803</a></p>

<h4>Commit Summary</h4>
<ul>
  <li><a href="https://github.com/openstreetmap/openstreetmap-website/pull/4803/commits/334c8560217cd20ae5ef74aa43dc1a934d3ae6f8" class="commit-link">334c856</a>  Test unicode values in user preference keys and values</li>
  <li><a href="https://github.com/openstreetmap/openstreetmap-website/pull/4803/commits/64af2816a3f788c360434a73a234d41ef4c9f66e" class="commit-link">64af281</a>  Treat the body as UTF-8 for user_preferences#update</li>
</ul>

<h4 style="display: inline-block">File Changes </h4> <p style="display: inline-block">(<a href="https://github.com/openstreetmap/openstreetmap-website/pull/4803/files">2 files</a>)</p>
<ul>
  <li>
    <strong>M</strong>
    <a href="https://github.com/openstreetmap/openstreetmap-website/pull/4803/files#diff-c264648051a5016bf8ab7161c8a8ac50aa4aee854789e1b327b84b0667ae1429">app/controllers/api/user_preferences_controller.rb</a>
    (2)
  </li>
  <li>
    <strong>M</strong>
    <a href="https://github.com/openstreetmap/openstreetmap-website/pull/4803/files#diff-8423ac0d20c682130833ce91a974a158c585f45101d176ab38107652b84762f5">test/controllers/api/user_preferences_controller_test.rb</a>
    (22)
  </li>
</ul>

<h4>Patch Links:</h4>
<ul>
  <li><a href='https://github.com/openstreetmap/openstreetmap-website/pull/4803.patch'>https://github.com/openstreetmap/openstreetmap-website/pull/4803.patch</a></li>
  <li><a href='https://github.com/openstreetmap/openstreetmap-website/pull/4803.diff'>https://github.com/openstreetmap/openstreetmap-website/pull/4803.diff</a></li>
</ul>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />Reply to this email directly, <a href="https://github.com/openstreetmap/openstreetmap-website/pull/4803">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AAK2OLOOGEQWWP62C3AZPGLZCTQTHAVCNFSM6AAAAABH2TMMM2VHI2DSMVQWIX3LMV43ASLTON2WKOZSGMYDAOJQGUYDEMI">unsubscribe</a>.<br />You are receiving this because you are subscribed to this thread.<img src="https://github.com/notifications/beacon/AAK2OLJBTIKLSTEWSJL5K63ZCTQTHA5CNFSM6AAAAABH2TMMM2WGG33NNVSW45C7OR4XAZNFJFZXG5LFVJRW63LNMVXHIX3JMTHISJIGHU.gif" height="1" width="1" alt="" /><span style="color: transparent; font-size: 0; display: none; visibility: hidden; overflow: hidden; opacity: 0; width: 0; height: 0; max-width: 0; max-height: 0; mso-hide: all">Message ID: <span><openstreetmap/openstreetmap-website/pull/4803</span><span>@</span><span>github</span><span>.</span><span>com></span></span></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/openstreetmap/openstreetmap-website/pull/4803",
"url": "https://github.com/openstreetmap/openstreetmap-website/pull/4803",
"name": "View Pull Request"
},
"description": "View this Pull Request on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>