Skip to content
Permalink
Browse files

change "allow_regex" to "require_regex"

which more closely matches our use-case

and pass the correct flag to tmpl_preparse()
  • Loading branch information
alandekok committed Aug 25, 2019
1 parent 9f245ef commit 05e3d022c90080c4107b58d600f8f8f04d4c7fa0
Showing with 16 additions and 10 deletions.
  1. +2 −3 src/lib/server/cond_tokenize.c
  2. +11 −6 src/lib/server/tmpl.c
  3. +1 −1 src/lib/server/tmpl.h
  4. +2 −0 src/tests/unit/regex.txt
@@ -595,7 +595,7 @@ static ssize_t cond_tokenize(TALLOC_CTX *ctx, fr_cond_t **pcond, char const **er
fr_cond_t *c;
size_t lhs_len, rhs_len;
FR_TOKEN op, lhs_type, rhs_type;

bool regex = false;
vp_tmpl_rules_t parse_rules;

/*
@@ -749,7 +749,6 @@ static ssize_t cond_tokenize(TALLOC_CTX *ctx, fr_cond_t **pcond, char const **er

} else { /* it's an operator */
#ifdef HAVE_REGEX
bool regex = false;
fr_regex_flags_t regex_flags;

memset(&regex_flags, 0, sizeof(regex_flags));
@@ -849,7 +848,7 @@ static ssize_t cond_tokenize(TALLOC_CTX *ctx, fr_cond_t **pcond, char const **er
return_P("Expected text after operator");
}

slen = tmpl_preparse(&rhs, &rhs_len, p, &rhs_type, error, NULL, true);
slen = tmpl_preparse(&rhs, &rhs_len, p, &rhs_type, error, NULL, regex);
if (slen <= 0) return_SLEN;

#ifdef HAVE_REGEX
@@ -2911,14 +2911,14 @@ void tmpl_verify(char const *file, int line, vp_tmpl_t const *vpt)
* @param[out] type token type of the string.
* @param[out] error string describing the error
* @param[out] castda NULL if casting is not allowed, otherwise the cast
* @param allow_regex whether or not to allow regular expressions
* @param require_regex whether or not to require regular expressions
* @return
* - > 0, amount of parsed string to skip, to get to the next token
* - <=0, -offset in 'start' where the parse error was located
*/
ssize_t tmpl_preparse(char const **out, size_t *outlen, char const *start,
FR_TOKEN *type, char const **error,
fr_dict_attr_t const **castda, bool allow_regex)
fr_dict_attr_t const **castda, bool require_regex)
{
char const *p = start;
char quote;
@@ -2973,17 +2973,22 @@ ssize_t tmpl_preparse(char const **out, size_t *outlen, char const *start,
fr_skip_spaces(p);
}

switch (*p) {
case '/':
if (!allow_regex) {
return_P("Unexpected regular expression");
if (require_regex) {
if (*p != '/') {
return_P("Expected regular expression");
}

if (castda && *castda) {
p++;
return_P("Invalid cast before regular expression");
}

} else if (*p == '/') {
return_P("Unexpected regular expression");
}

switch (*p) {
case '/':
quote = *(p++);
*type = T_OP_REG_EQ;
goto skip_string;
@@ -515,7 +515,7 @@ int tmpl_define_undefined_attr(fr_dict_t *dict, vp_tmpl_t *vpt,

ssize_t tmpl_preparse(char const **out, size_t *outlen, char const *start,
FR_TOKEN *type, char const **error,
fr_dict_attr_t const **castda, bool allow_regex) CC_HINT(nonnull(1,2,3,4,5));
fr_dict_attr_t const **castda, bool require_regex) CC_HINT(nonnull(1,2,3,4,5));

#ifdef __cplusplus
}
@@ -65,3 +65,5 @@ data %{1}
xlat %{33}
data ERROR offset 2 'Invalid regex reference. Must be in range 0-32'

condition &User-Name == /foo/
data ERROR offset 14 Unexpected regular expression

0 comments on commit 05e3d02

Please sign in to comment.
You can’t perform that action at this time.