Skip to content

Commit 7923de7

Browse files
committed
Use properly encoded message bodys for patch views
If it doesn't say utf8 / contains invalid characters, force it to utf8.
1 parent 40c9375 commit 7923de7

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

app/models/attachment.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ def patch?
1010
def decoded_body
1111
Base64.decode64(body) if body.present?
1212
end
13+
14+
def decoded_body_utf8
15+
raw = decoded_body
16+
return unless raw
17+
18+
utf8 = raw.dup
19+
utf8.force_encoding("UTF-8")
20+
return utf8 if utf8.valid_encoding?
21+
22+
raw.encode("UTF-8", invalid: :replace, undef: :replace, replace: "\uFFFD")
23+
end
1324

1425
private
1526

app/views/topics/_message.html.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
= link_to "Download", attachment_path(attachment), class: "attachment-download", download: attachment.file_name, data: { turbo: false }
6161
pre.attachment-content
6262
code.language-diff
63-
= attachment.decoded_body
63+
= attachment.decoded_body_utf8
6464
- else
6565
.attachment
6666
.attachment-info

0 commit comments

Comments
 (0)