-
Notifications
You must be signed in to change notification settings - Fork 29
Description
When uploading a file that is not UTF-8 encoded, the database might error when inserting the file content. We might consider adding some validations to Rails or change the encoding prior to uploading the file. Attached is some more information with an example.
Sentry Issue: [CODEOCEAN-2]
PG::CharacterNotInRepertoire: ERROR: invalid byte sequence for encoding "UTF8": 0xf4 0x74 0x65 0x5f
app/controllers/concerns/common_behavior.rb:41:in `block in update_and_respond'
if @object.update(options[:params])
app/controllers/concerns/common_behavior.rb:40:in `update_and_respond'
respond_to do |format|
app/controllers/exercises_controller.rb:459:in `update'
update_and_respond(object: @exercise, params: myparam)
...
(185 additional frame(s) were not displayed)
ActiveRecord::StatementInvalid: PG::CharacterNotInRepertoire: ERROR: invalid byte sequence for encoding "UTF8": 0xf4 0x74 0x65 0x5f
: INSERT INTO "files" ("content", "context_id", "context_type", "file_type_id", "hidden", "name", "read_only", "created_at", "updated_at", "role", "hashed_content", "feedback_message", "path") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING "id"
Explanation (in German):
Und das mit der Côte d’Ivoire ist eine spannende Sache. Ich habe es auch geschafft, das nachzustellen. Von daher weiß ich jetzt, was passiert: Die Datei, die Du hochlädt, ist nicht UTF-8 codiert. Dort würde ein ô hexadezimal durch die beiden Zeichen 0xC3 0xB4 dargestellt werden. Stattdessen verwendet die Datei die Windows-typische Darstellung mit 0xF4, also nur ein Byte. In UTF-8 bedeutet der Beginn mit Präfix 0xF.. allerdings, dass ein Zeichen aus insgesamt vier Byte bestehend folgen wird, wie bspw. dieser Smiley 😎, Hex: 0xF0 0x9F 0x98 0x8E. Der Import schlägt also fehl, weil die folgenden Zeichen bereits normale weitere Zeichen sind (t = 0x74, e = 0x65, _ = 0x5f) und nicht, wie erwartet, die Fortsetzung des vorherigen Zeichens. Die Datenbank validiert dies und meldet einen Fehler, mit genau den vier Zeichen:
PG::CharacterNotInRepertoire: ERROR: invalid byte sequence for encoding "UTF8": 0xf4 0x74 0x65 0x5f
Leider ist das Encoding einer Datei (hier also Western / Windows-1252) nicht in der Datei selbst enthalten, man kann also nur mehr oder weniger geschickt raten. Und genau das schlägt hier fehl, die Datei wird nicht mit ihrem korrekten Encoding erkannt und so einfach standardmäßig als UTF-8 behandelt. Daher kommt es zu obigem Fehler. Du kannst also entweder die Datei vorher in echtes UTF-8 konvertieren oder das Zeichen entfernen und nach dem Upload dieses im Web-Interface wieder einfügen. Dann klappt es auf jeden Fall ;). In der Datei expenditure.txt hat es auch funktioniert (Zeile 51), dort wird es korrekt dargestellt.
Attachment: test.txt