Skip to content

Commit

Permalink
filterx: enforce string literal argument for is_sdata_from_enterprise…
Browse files Browse the repository at this point in the history
… function

Signed-off-by: Szilard Parrag <[email protected]>
  • Loading branch information
OverOrion committed Aug 26, 2024
1 parent 617c33d commit a08a8cb
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/filterx/filterx-globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ _simple_init(void)
g_assert(filterx_builtin_simple_function_register("vars", filterx_simple_function_vars));
g_assert(filterx_builtin_simple_function_register("lower", filterx_simple_function_lower));
g_assert(filterx_builtin_simple_function_register("upper", filterx_simple_function_upper));
g_assert(filterx_builtin_simple_function_register("is_sdata_from_enterprise",
filterx_simple_function_is_sdata_from_enterprise));
g_assert(filterx_builtin_simple_function_register("has_sdata",
filterx_simple_function_has_sdata));
}
Expand Down Expand Up @@ -134,6 +132,8 @@ _ctors_init(void)
g_assert(filterx_builtin_function_ctor_register("regexp_subst", filterx_function_regexp_subst_new));
g_assert(filterx_builtin_function_ctor_register("unset", filterx_function_unset_new));
g_assert(filterx_builtin_function_ctor_register("flatten", filterx_function_flatten_new));
g_assert(filterx_builtin_function_ctor_register("is_sdata_from_enterprise",
filterx_function_is_sdata_from_enterprise_new));
}

static void
Expand Down
78 changes: 64 additions & 14 deletions lib/filterx/func-sdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,43 @@

#define SDATA_PREFIX_LEN (7)

FilterXObject *
filterx_simple_function_is_sdata_from_enterprise(FilterXExpr *s, GPtrArray *args)
#define FILTERX_FUNC_IS_SDATA_FROM_ENTERPRISE_USAGE "Usage: is_sdata_from_enteprise(\"32473\")"

typedef struct FilterXFunctionIsSdataFromEnteprise_
{
FilterXFunction super;
gchar *str_literal;
gsize str_literal_len;
} FilterXFunctionIsSdataFromEnteprise;

static gboolean
_extract_args(FilterXFunctionIsSdataFromEnteprise *self, FilterXFunctionArgs *args, GError **error)
{
if (args == NULL || args->len != 1)
gsize len = filterx_function_args_len(args);
if (len != 1)
{
filterx_simple_function_argument_error(s, "Requires exactly one argument", FALSE);
return NULL;
g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
"invalid number of arguments. " FILTERX_FUNC_IS_SDATA_FROM_ENTERPRISE_USAGE);
return FALSE;
}

FilterXObject *obj = filterx_typecast_string(NULL, args);
if (!obj)
return NULL;
const gchar *str_value;
gsize len;
if (!filterx_object_extract_string(obj, &str_value, &len))
return NULL;
gsize str_literal_len;
const gchar *str_literal = filterx_function_args_get_literal_string(args, 0, &str_literal_len);
if (!str_literal)
{
g_set_error(error, FILTERX_FUNCTION_ERROR, FILTERX_FUNCTION_ERROR_CTOR_FAIL,
"argument must be a string literal. " FILTERX_FUNC_IS_SDATA_FROM_ENTERPRISE_USAGE);
return FALSE;
}
self->str_literal = g_strdup(str_literal);
self->str_literal_len = str_literal_len;
return TRUE;
}

static FilterXObject *
_eval(FilterXExpr *s)
{
FilterXFunctionIsSdataFromEnteprise *self = (FilterXFunctionIsSdataFromEnteprise *) s;

gboolean contains = FALSE;
FilterXEvalContext *context = filterx_eval_get_context();
Expand All @@ -60,12 +81,41 @@ filterx_simple_function_is_sdata_from_enterprise(FilterXExpr *s, GPtrArray *args
gchar *at_sign = strchr(value, '@');
if (!at_sign)
continue;
contains = strncmp(at_sign+1, str_value, len) == 0;
contains = strncmp(at_sign+1, self->str_literal, self->str_literal_len) == 0;
}
filterx_object_unref(obj);

return filterx_boolean_new(contains);
}

static void
_free(FilterXExpr *s)
{
FilterXFunctionIsSdataFromEnteprise *self = (FilterXFunctionIsSdataFromEnteprise *) s;

g_free(self->str_literal);
filterx_function_free_method(&self->super);
}


FilterXExpr *
filterx_function_is_sdata_from_enterprise_new(const gchar *function_name, FilterXFunctionArgs *args, GError **error)
{
FilterXFunctionIsSdataFromEnteprise *self = g_new0(FilterXFunctionIsSdataFromEnteprise, 1);
filterx_function_init_instance(&self->super, function_name);

if (!_extract_args(self, args, error) || !filterx_function_args_check(args, error))
goto error;
self->super.super.eval = _eval;
self->super.super.free_fn = _free;
filterx_function_args_free(args);
return &self->super.super;

error:
filterx_function_args_free(args);
filterx_expr_unref(&self->super.super);
return NULL;
}


FilterXObject *
filterx_simple_function_has_sdata(FilterXExpr *s, GPtrArray *args)
Expand Down
3 changes: 2 additions & 1 deletion lib/filterx/func-sdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

#include "filterx/expr-function.h"

FilterXObject *filterx_simple_function_is_sdata_from_enterprise(FilterXExpr *s, GPtrArray *args);
FilterXExpr *filterx_function_is_sdata_from_enterprise_new(const gchar *function_name, FilterXFunctionArgs *args,
GError **error);
FilterXObject *filterx_simple_function_has_sdata(FilterXExpr *s, GPtrArray *args);
FilterXExpr *filterx_generator_function_get_sdata_new(const gchar *function_name, FilterXFunctionArgs *args,
GError **error);
Expand Down

0 comments on commit a08a8cb

Please sign in to comment.