aboutsummaryrefslogtreecommitdiffstats
path: root/src/libserver/css
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2021-03-29 14:16:06 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2021-03-29 14:16:06 +0100
commitf0cbfec0eed63d3ad36868835e93973efd383012 (patch)
tree4d6b9c1c3742bce31bfd2b8b3247d9aa37c82033 /src/libserver/css
parent90226f812c87dc598403dc5385d5736879c2ad6a (diff)
downloadrspamd-f0cbfec0eed63d3ad36868835e93973efd383012.tar.gz
rspamd-f0cbfec0eed63d3ad36868835e93973efd383012.zip
[Minor] Lowercase CSS
Diffstat (limited to 'src/libserver/css')
-rw-r--r--src/libserver/css/css_parser.cxx25
-rw-r--r--src/libserver/css/css_util.cxx16
2 files changed, 29 insertions, 12 deletions
diff --git a/src/libserver/css/css_parser.cxx b/src/libserver/css/css_parser.cxx
index f80386fc2..d828ffa46 100644
--- a/src/libserver/css/css_parser.cxx
+++ b/src/libserver/css/css_parser.cxx
@@ -18,6 +18,7 @@
#include "css_tokeniser.hxx"
#include "css_selector.hxx"
#include "css_rule.hxx"
+#include "css_util.hxx"
#include "css.hxx"
#include "fmt/core.h"
@@ -159,6 +160,9 @@ public:
return tl::make_unexpected(error);
}
+ /* Helper parser methods */
+ bool need_unescape(const std::string_view &sv);
+
private:
std::unique_ptr<css_style_sheet> style_object;
std::unique_ptr<css_tokeniser> tokeniser;
@@ -170,9 +174,6 @@ private:
const int max_rec = 20;
bool eof = false;
- /* Helper parser methods */
- bool need_unescape(const std::string_view &sv);
-
/* Consumers */
auto component_value_consumer(std::unique_ptr<css_consumed_block> &top) -> bool;
auto function_consumer(std::unique_ptr<css_consumed_block> &top) -> bool;
@@ -684,8 +685,24 @@ auto parse_css(rspamd_mempool_t *pool, const std::string_view &st) ->
tl::expected<std::unique_ptr<css_style_sheet>, css_parse_error>
{
css_parser parser(pool);
+ std::string_view processed_input;
+
+ if (parser.need_unescape(st)) {
+ processed_input = rspamd::css::unescape_css(pool, st);
+ }
+ else {
+ /* Lowercase inplace */
+ auto *nspace = reinterpret_cast<char *>(rspamd_mempool_alloc(pool, st.length()));
+ auto *p = nspace;
+
+ for (const auto c : st) {
+ *p++ = g_ascii_tolower(c);
+ }
+
+ processed_input = std::string_view{nspace, (std::size_t)(p - nspace)};
+ }
- if (parser.consume_input(st)) {
+ if (parser.consume_input(processed_input)) {
return parser.get_object_maybe();
}
diff --git a/src/libserver/css/css_util.cxx b/src/libserver/css/css_util.cxx
index 7388e49fd..7add8043c 100644
--- a/src/libserver/css/css_util.cxx
+++ b/src/libserver/css/css_util.cxx
@@ -23,7 +23,7 @@ namespace rspamd::css {
std::string_view unescape_css(rspamd_mempool_t *pool,
const std::string_view &sv)
{
- auto *nspace = reinterpret_cast<char *>(rspamd_mempool_alloc(pool, sv.length ()));
+ auto *nspace = reinterpret_cast<char *>(rspamd_mempool_alloc(pool, sv.length()));
auto *d = nspace;
auto nleft = sv.length ();
@@ -38,20 +38,20 @@ std::string_view unescape_css(rspamd_mempool_t *pool,
auto escape_offset = 0, i = 0;
#define MAYBE_CONSUME_CHAR(c) do { \
- if (c == '"' || c == '\'') { \
+ if ((c) == '"' || (c) == '\'') { \
state = quoted; \
- quote_char = c; \
+ quote_char = (c); \
nleft--; \
- *d++ = c; \
+ *d++ = (c); \
} \
- else if (c == '\\') { \
+ else if ((c) == '\\') { \
escape_offset = i; \
state = escape; \
} \
else { \
state = normal; \
nleft--; \
- *d++ = c; \
+ *d++ = g_ascii_tolower(c); \
} \
} while (0)
@@ -89,14 +89,14 @@ std::string_view unescape_css(rspamd_mempool_t *pool,
else {
if (val < 0x80) {
/* Trivial case: ascii character */
- *d++ = (unsigned char)val;
+ *d++ = (unsigned char)g_ascii_tolower(val);
nleft --;
}
else {
UChar32 uc = val;
auto off = 0;
UTF8_APPEND_CHAR_SAFE((uint8_t *) d, off,
- sv.length (), uc);
+ sv.length (), u_tolower(uc));
d += off;
nleft -= off;
}