A bug report discusses issues with tags containing Cyrillic characters in Discourse, leading to a 500 Internal Server Error. Users Stranik and mpalmer identify the problem as an Encoding::Undefined ConversionError when JSON encoding ASCII-8BIT strings containing UTF-8 encoded Cyrillic text. The issue arises because params are encoded as ASCII-8BIT, which fails during JSON serialization. mpalmer explains that while URLs can use percent-encoded UTF-8, the internal handling of these parameters causes the error. A fix is proposed in a GitHub commit 7ee861f4571f6e7259e631d0404e0d958501dcf0, but a second issue appears with a different error. mpalmer provides another fix in commit 67882ec37da6dac2ec0ce69e110014a6fe11882c, which resolves the issue for Stranik, who confirms it works.
Encoding::UndefinedConversionError ("\xD1" from ASCII-8BIT to UTF-8) /var/www/discourse/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.9/lib/active_support/core_ext/object/json.rb:34:in encode’`
In theory, URLs can only contain 7-bit ASCII characters. In practice, everyone’s pretty much decided that percent-encoded UTF-8 is , which is why, if you look in the “env” for the log message, you’ll see something like
So, the problem is occuring because the contents of params is being encoded as ASCII-8BIT, and while most everything manages to figure out what’s going on and roll with it, the JSON encoding of the result, containing as it does an ASCII-8BIT string whose individual bytes don’t translate into valid UTF-8 codepoints, explodes.
It must be the additional complexity of the data you’ve got that’s causing the bug to appear, whilst my trivial example DB doesn’t trip it up.
Since I can’t reproduce it myself, I’ve had to take a bit of a speculative bug fix attempt in Hunt-and-kill a few more mis-encoded params · discourse/discourse@67882ec · GitHub; please let me know if it does/doesn’t work for you. I think we’re going to have to do a larger and more comprehensive fix for this, along the lines of this initializer; playing whack-a-mole with parameter encodings one-by-one seems like a good way to go prematurely bald.