comparison mod_cloud_notify_encrypted/mod_cloud_notify_encrypted.lua @ 4651:8231774f5bfd

mod_cloud_notify_encrypted: Ensure body substring remains valid UTF-8 The `body:sub()` call risks splitting the string in the middle of a multi-byte UTF-8 sequence. This should have been caught by util.stanza validation, but that would have caused some havoc, at the very least causing the notification to not be sent. There have been no reports of this happening. Likely because this module isn't widely deployed among users with languages that use many longer UTF-8 sequences. The util.encodings.utf8.valid() function is O(n) where only the last sequence really needs to be checked, but it's in C and expected to be fast.
author Kim Alvefur <zash@zash.se>
date Sun, 22 Aug 2021 13:22:59 +0200
parents 44af84178cea
children 62480053c87b
comparison
equal deleted inserted replaced
4650:44af84178cea 4651:8231774f5bfd
1 local array = require "util.array"; 1 local array = require "util.array";
2 local base64 = require "util.encodings".base64; 2 local base64 = require "util.encodings".base64;
3 local valid_utf8 = require "util.encodings".utf8.valid;
3 local ciphers = require "openssl.cipher"; 4 local ciphers = require "openssl.cipher";
4 local jid = require "util.jid"; 5 local jid = require "util.jid";
5 local json = require "util.json"; 6 local json = require "util.json";
6 local random = require "util.random"; 7 local random = require "util.random";
7 local set = require "util.set"; 8 local set = require "util.set";
76 body = "You have received an encrypted message"; 77 body = "You have received an encrypted message";
77 else 78 else
78 body = original_stanza:get_child_text("body"); 79 body = original_stanza:get_child_text("body");
79 if body and #body > 255 then 80 if body and #body > 255 then
80 body = body:sub(1, 255); 81 body = body:sub(1, 255);
82 if not valid_utf8(body) then
83 body = body:gsub("[\194-\244][\128-\191]*$", "");
84 end
81 end 85 end
82 end 86 end
83 87
84 local push_payload = { 88 local push_payload = {
85 unread = tonumber(push_summary["message-count"]) or 1; 89 unread = tonumber(push_summary["message-count"]) or 1;