Skip to content

Commit

Permalink
Merge pull request #309 from bshifter/filterx-invalid-utf8-fix
Browse files Browse the repository at this point in the history
Filterx invalid utf8 fix for unset_empties
  • Loading branch information
alltilla authored Sep 27, 2024
2 parents 116def9 + 72321d3 commit d53d84f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/filterx/func-unset-empties.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ static gboolean _should_unset_string(FilterXFunctionUnsetEmpties *self, FilterXO
if (check_flag(self->flags, FILTERX_FUNC_UNSET_EMPTIES_FLAG_REPLACE_EMPTY_STRING) && (str_len == 0))
return TRUE;

if (!g_utf8_validate(str, -1, NULL))
return FALSE;
if (check_flag(self->flags, FILTERX_FUNC_UNSET_EMPTIES_FLAG_IGNORECASE))
{
casefold_str = g_utf8_casefold(str, str_len);
Expand Down Expand Up @@ -380,6 +382,12 @@ _handle_target_object(FilterXFunctionUnsetEmpties *self, FilterXObject *target,
set_flag(&self->flags, FILTERX_FUNC_UNSET_EMPTIES_FLAG_REPLACE_EMPTY_STRING, TRUE);
return TRUE;
}
if (!g_utf8_validate(str, -1, NULL))
{
g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
FILTERX_FUNC_UNSET_EMPTIES_ARG_NAME_TARGETS" strings must be valid utf8 strings! " FILTERX_FUNC_UNSET_EMPTIES_USAGE);
return FALSE;
}
if (check_flag(self->flags, FILTERX_FUNC_UNSET_EMPTIES_FLAG_IGNORECASE))
{
g_ptr_array_add(self->targets, g_utf8_casefold(str, len));
Expand Down
27 changes: 27 additions & 0 deletions tests/light/functional_tests/filterx/test_filterx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,33 @@ def test_vars(config, syslog_ng):
assert file_true.read_log() == '{"logmsg_variable":"foo","pipeline_level_variable":"baz","log":{"body":"foobar","attributes":{"attribute":42}},"js_array":[1,2,3,[4,5,6]]}\n'


def test_unset_empties_invalid_utf8(config, syslog_ng):
(file_true, file_false) = create_config(
config, r"""
invalid_utf8 = "ba\xC080az";
dict = {"foo":invalid_utf8};
unset_empties(dict, targets=["baz"]);
$MSG = dict;
""",
)
syslog_ng.start(config)

assert file_true.get_stats()["processed"] == 1
assert "processed" not in file_false.get_stats()


def test_unset_empties_invalid_utf8_target(config, syslog_ng):
(file_true, file_false) = create_config(
config, r"""
dict = {};
unset_empties(dict, targets=["b\xC080az"], recursive=true);
$MSG = dict;
""",
)
with pytest.raises(Exception):
syslog_ng.start(config)


def test_unset_empties(config, syslog_ng):
(file_true, file_false) = create_config(
config, r"""
Expand Down

0 comments on commit d53d84f

Please sign in to comment.