mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-18 22:56:22 +00:00
Merge pull request #2116 from profanity-im/sanitzexml
feat: Sanitize illegal XML characters from outgoing messages
This commit is contained in:
27
src/common.c
27
src/common.c
@@ -418,6 +418,33 @@ utf8_display_len(const char* const str)
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes illegal XML 1.0 characters from a string.
|
||||
*
|
||||
* This function creates a new string that excludes characters in the range
|
||||
* U+0000 to U+001F, except for U+0009 (TAB), U+000A (LF), and U+000D (CR).
|
||||
*/
|
||||
gchar*
|
||||
str_xml_sanitize(const char* const str)
|
||||
{
|
||||
if (str == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
GString* sanitized = g_string_new_len(NULL, strlen(str));
|
||||
const char* curr = str;
|
||||
|
||||
while (*curr != '\0') {
|
||||
gunichar c = g_utf8_get_char(curr);
|
||||
if ((c >= 0x20) || (c == 0x09) || (c == 0x0A) || (c == 0x0D)) {
|
||||
g_string_append_unichar(sanitized, c);
|
||||
}
|
||||
curr = g_utf8_next_char(curr);
|
||||
}
|
||||
|
||||
return g_string_free(sanitized, FALSE);
|
||||
}
|
||||
|
||||
char*
|
||||
release_get_latest(void)
|
||||
{
|
||||
|
||||
@@ -159,6 +159,7 @@ gboolean copy_file(const char* const src, const char* const target, const gboole
|
||||
char* str_replace(const char* string, const char* substr, const char* replacement);
|
||||
gboolean strtoi_range(const char* str, int* saveptr, int min, int max, char** err_msg);
|
||||
int utf8_display_len(const char* const str);
|
||||
gchar* str_xml_sanitize(const char* const str);
|
||||
|
||||
gboolean string_matches_one_of(const char* what, const char* is, gboolean is_can_be_null, const char* first, ...) __attribute__((sentinel));
|
||||
gboolean valid_tls_policy_option(const char* is);
|
||||
|
||||
@@ -115,7 +115,8 @@ cl_ev_send_msg_correct(ProfChatWin* chatwin, const char* const msg, const char*
|
||||
}
|
||||
|
||||
auto_char char* plugin_msg = plugins_pre_chat_message_send(chatwin->barejid, msg);
|
||||
const char* const message = plugin_msg ?: msg;
|
||||
auto_gchar gchar* sanitized_msg = str_xml_sanitize(plugin_msg ?: msg);
|
||||
const char* message = sanitized_msg;
|
||||
|
||||
char* replace_id = NULL;
|
||||
if (correct_last_msg) {
|
||||
@@ -177,7 +178,8 @@ void
|
||||
cl_ev_send_muc_msg_corrected(ProfMucWin* mucwin, const char* const msg, const char* const oob_url, gboolean correct_last_msg)
|
||||
{
|
||||
auto_char char* plugin_msg = plugins_pre_room_message_send(mucwin->roomjid, msg);
|
||||
const char* const message = plugin_msg ?: msg;
|
||||
auto_gchar gchar* sanitized_msg = str_xml_sanitize(plugin_msg ?: msg);
|
||||
const char* message = sanitized_msg;
|
||||
|
||||
char* replace_id = NULL;
|
||||
if (correct_last_msg) {
|
||||
@@ -218,7 +220,8 @@ cl_ev_send_priv_msg(ProfPrivateWin* privwin, const char* const msg, const char*
|
||||
privwin_message_left_room(privwin);
|
||||
} else {
|
||||
auto_char char* plugin_msg = plugins_pre_priv_message_send(privwin->fulljid, msg);
|
||||
const char* const message = plugin_msg ?: msg;
|
||||
auto_gchar gchar* sanitized_msg = str_xml_sanitize(plugin_msg ?: msg);
|
||||
const char* message = sanitized_msg;
|
||||
auto_jid Jid* jidp = jid_create(privwin->fulljid);
|
||||
|
||||
auto_char char* id = message_send_private(privwin->fulljid, message, oob_url);
|
||||
|
||||
Reference in New Issue
Block a user