diff options
author | grunfink <grunfink@noreply.codeberg.org> | 2023-07-11 17:11:21 +0000 |
---|---|---|
committer | grunfink <grunfink@noreply.codeberg.org> | 2023-07-11 17:11:21 +0000 |
commit | a5272a21e6e5d288fd0fc5f6de65051fd40910b3 (patch) | |
tree | 1cba1e46167512e9e3a6b8f8e278d84fbdb29bf2 /xs.h | |
parent | eb7bee953ba101deeff30b16ccd8c0dae3914eb8 (diff) | |
parent | 63741cb66a6146c16898e0cafde1722a327a0059 (diff) |
Merge pull request 'Attempt to prevent XSS.' (#64) from yonle/snac2:master into master
Reviewed-on: https://codeberg.org/grunfink/snac2/pulls/64
Diffstat (limited to 'xs.h')
-rw-r--r-- | xs.h | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -72,6 +72,7 @@ xs_str *xs_replace_in(xs_str *str, const char *sfrom, const char *sto, int times xs_str *xs_fmt(const char *fmt, ...); int xs_str_in(const char *haystack, const char *needle); int _xs_startsorends(const char *str, const char *xfix, int ends); +xs_str *xs_encode_html(const xs_str *str); #define xs_startswith(str, prefix) _xs_startsorends(str, prefix, 0) #define xs_endswith(str, postfix) _xs_startsorends(str, postfix, 1) xs_str *xs_crop_i(xs_str *str, int start, int end); @@ -506,6 +507,20 @@ int _xs_startsorends(const char *str, const char *xfix, int ends) return !!(ssz >= psz && memcmp(xfix, str + (ends ? ssz - psz : 0), psz) == 0); } +xs_str *xs_encode_html(const char *str) +/* escapes html characters */ +{ + xs_str *encoded = xs_replace(str, "&", "&"); + encoded = xs_replace(encoded, "<", "<"); + encoded = xs_replace(encoded, ">", ">"); + encoded = xs_replace(encoded, "\"", """); + encoded = xs_replace(encoded, "'", "'"); + + // Restore only <br>. Probably safe. Let's hope nothing goes wrong with this. + encoded = xs_replace(encoded, "<br>", "<br>"); + + return encoded; +} xs_str *xs_crop_i(xs_str *str, int start, int end) /* crops the d_char to be only from start to end */ |