From: Alan T. DeKok Date: Wed, 1 Jun 2022 14:23:58 +0000 (-0400) Subject: fixes and tests for run-time regular expressions X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5ccea91fbf3c051288abf99c6145a77707ff3f05;p=thirdparty%2Ffreeradius-server.git fixes and tests for run-time regular expressions --- diff --git a/src/lib/unlang/xlat_expr.c b/src/lib/unlang/xlat_expr.c index 79d4df9a7fb..b0adcef4c54 100644 --- a/src/lib/unlang/xlat_expr.c +++ b/src/lib/unlang/xlat_expr.c @@ -405,7 +405,7 @@ static int xlat_instantiate_regex(xlat_inst_ctx_t const *xctx) /* * The RHS is more then just one regex node, it has to be dynamically expanded. */ - if (xlat_exp_next(rhs->group, regex)) { + if (xlat_exp_next(rhs->group, regex) || !tmpl_contains_regex(regex->vpt)) { inst->xlat = talloc_steal(inst, rhs); return 0; } @@ -525,7 +525,20 @@ static xlat_action_t xlat_regex_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, * LHS should already have been expanded. RHS was just expanded by us. */ lhs = fr_dlist_head(in); + + /* + * Because we expanded the RHS ourselves, the "concat" + * flag to the RHS argument is ignored. So we just + * concatenate it here. + */ rhs = fr_dlist_head(&rctx->list); + if (fr_value_box_list_concat_in_place(rctx, rhs, &rctx->list, FR_TYPE_STRING, + FR_VALUE_BOX_LIST_FREE, true, SIZE_MAX) < 0) { + RPEDEBUG("Failed concatenating regular expression string"); + return XLAT_ACTION_FAIL; + } + + fr_assert(rhs != NULL); slen = regex_compile(rctx, &preg, rhs->vb_strvalue, rhs->vb_length, NULL, true, true); /* no flags, allow subcaptures, at runtime */ @@ -549,7 +562,7 @@ static xlat_action_t xlat_regex_op(TALLOC_CTX *ctx, fr_dcursor_t *out, /* * Just run precompiled regexes. */ - if (!tmpl_is_regex_xlat(inst->regex->vpt)) { + if (inst->regex) { preg = tmpl_regex(inst->regex->vpt); return xlat_regex_match(ctx, request, lhs, &preg, out, op); diff --git a/src/tests/xlat/expr.txt b/src/tests/xlat/expr.txt index 959330d8cb9..2c54e2251b2 100644 --- a/src/tests/xlat/expr.txt +++ b/src/tests/xlat/expr.txt @@ -30,6 +30,21 @@ match 2 xlat_expr (uint32) 1 << 31 match 2147483648 +xlat_expr %n +match {0} + +xlat_expr &Packet-Src-IP-Address =~ /^127.0/ +match true + +xlat_expr &Packet-Src-IP-Address =~ /^128.0/ +match false + +xlat_expr &Packet-Src-IP-Address =~ /foo %{Packet-Dst-IP-Address} foo/ +match false + +xlat_expr &Packet-Src-IP-Address =~ /%{Packet-Dst-IP-Address}/ +match true + # @todo - failure? xlat_expr (uint32) 1 << 32 match NULL