Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes-entries/mod_headers-edit_all.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*) mod_headers: Fix a possible infinite recursion with the edit* action and
empty matches. [Yann Ylavic]
6 changes: 6 additions & 0 deletions changes-entries/regex_backrefs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*) core, mod_rewrite, mod_proxy_fcgi, mod_substitute, mod_proxy_html:
Fix backreferences ($N, %N) to groups that did not participate in
a match, or left over from a previous condition when an expression
is true without a capturing match, and fix regex patterns that can
match the empty string not making progress in mod_substitute and
mod_proxy_html. [Eric Covener]
5 changes: 4 additions & 1 deletion include/ap_mmn.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,14 +612,17 @@
* 20120211.140 (2.4.64-dev) Add ap_set_time_process_request() to scoreboard.h
* 20120211.141 (2.4.64-dev) add ap_stat_check() to httpd.h
* 20120211.142 (2.4.64-dev) Add ap_*_timingsafe() to httpd.h
* 20120211.143 (2.4.72-dev) Add ap_regexec_ex() and the AP_REG_NOTEMPTY,
* AP_REG_NOTEMPTY_ATSTART and AP_REG_ANCHORED
* options
*/

#define MODULE_MAGIC_COOKIE 0x41503234UL /* "AP24" */

#ifndef MODULE_MAGIC_NUMBER_MAJOR
#define MODULE_MAGIC_NUMBER_MAJOR 20120211
#endif
#define MODULE_MAGIC_NUMBER_MINOR 142 /* 0...n */
#define MODULE_MAGIC_NUMBER_MINOR 143 /* 0...n */

/**
* Determine if the server's current MODULE_MAGIC_NUMBER is at least a
Expand Down
32 changes: 28 additions & 4 deletions include/ap_regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ extern "C" {
#define AP_REG_NOMEM 0x20 /* nomem in our code */
#define AP_REG_DOTALL 0x40 /* perl's /s flag */

#define AP_REG_NOTEMPTY 0x080 /**< Empty match not valid */
#define AP_REG_ANCHORED 0x100 /**< Match at the first position */

#define AP_REG_DOLLAR_ENDONLY 0x200 /* '$' matches at end of subject string only */

#define AP_REG_NO_DEFAULT 0x400 /**< Don't implicitely add AP_REG_DEFAULT options */

#define AP_REG_NOTEMPTY_ATSTART 0x800 /**< Empty match not valid at first position */

#define AP_REG_MATCH "MATCH_" /**< suggested prefix for ap_regname */

#define AP_REG_DEFAULT (AP_REG_DOTALL|AP_REG_DOLLAR_ENDONLY)
Expand Down Expand Up @@ -169,7 +174,8 @@ AP_DECLARE(int) ap_regcomp(ap_regex_t *preg, const char *regex, int cflags);
* @return 0 for successful match, \p AP_REG_NOMATCH otherwise
*/
AP_DECLARE(int) ap_regexec(const ap_regex_t *preg, const char *string,
apr_size_t nmatch, ap_regmatch_t *pmatch, int eflags);
apr_size_t nmatch, ap_regmatch_t *pmatch,
int eflags);

/**
* Match a string with given length against a pre-compiled regex. The string
Expand All @@ -183,10 +189,28 @@ AP_DECLARE(int) ap_regexec(const ap_regex_t *preg, const char *string,
* other flags are ignored)
* @return 0 for successful match, AP_REG_NOMATCH otherwise
*/
AP_DECLARE(int) ap_regexec_len(const ap_regex_t *preg, const char *buff,
apr_size_t len, apr_size_t nmatch,
ap_regmatch_t *pmatch, int eflags);
AP_DECLARE(int) ap_regexec_len(const ap_regex_t *preg,
const char *buff, apr_size_t len,
apr_size_t nmatch, ap_regmatch_t *pmatch,
int eflags);

/**
* Match a string with given length and at a given offset against a
* pre-compiled regex. The string does not need to be NUL-terminated.
* @param preg The pre-compiled regex
* @param buff The string to match
* @param len Length of the string to match
* @param pos Offset in the string where the match should (re)start
* @param nmatch Provide information regarding the location of any matches
* @param pmatch Provide information regarding the location of any matches
* @param eflags Bitwise OR of AP_REG_* flags (NOTBOL and NOTEOL supported,
* other flags are ignored)
* @return 0 for successful match, AP_REG_NOMATCH otherwise
*/
AP_DECLARE(int) ap_regexec_ex(const ap_regex_t *preg,
const char *buff, apr_size_t len, apr_size_t pos,
apr_size_t nmatch, ap_regmatch_t *pmatch,
int eflags);
/**
* Return the error code returned by regcomp or regexec into error messages
* @param errcode the error code returned by regexec or regcomp
Expand Down
90 changes: 83 additions & 7 deletions modules/filters/mod_proxy_html.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,29 @@ static void pcharacters(void *ctxt, const xmlChar *uchars, int length)
FLUSH;
}

/* Whether a successful match's offsets are within [start, len] and in
* order. Some PCRE versions accept \K in a lookaround, which can report
* a match starting after its end or before the start offset; such a match
* is treated as no match.
*/
static int match_in_range(const ap_regmatch_t *m, apr_size_t start,
apr_size_t len)
{
int ok = (m->rm_so >= 0
&& (apr_size_t)m->rm_so >= start
&& m->rm_so <= m->rm_eo
&& (apr_size_t)m->rm_eo <= len);
AP_DEBUG_ASSERT(ok);
return ok;
}

static void dump_content(saxctxt *ctx)
{
urlmap *m;
char *found;
size_t s_from, s_to;
size_t match;
int nmatch;
int nmatch, eflags, i;
ap_regmatch_t pmatch[10];
size_t len, offs;
urlmap *themap = ctx->map;
Expand All @@ -224,7 +240,33 @@ static void dump_content(saxctxt *ctx)
temp.strlen = 0;
nmatch = 10;
offs = 0;
while (!ap_regexec(m->from.r, ctx->vb.buf+offs, nmatch, pmatch, 0)) {
eflags = 0;
len = strlen(ctx->vb.buf);
do {
/* Match from the current offset in the whole buffer (so that
* ^, \b, lookbehinds... keep their meaning), the offsets are
* then made relative to offs below.
*/
if (ap_regexec_ex(m->from.r, ctx->vb.buf, len, offs,
nmatch, pmatch, eflags)
|| !match_in_range(&pmatch[0], offs, len)) {
if (!eflags) {
break;
}
/* No non-empty match right after an empty one, consume
* the next char and retry a "normal" match from there.
*/
ap_varbuf_strmemcat(&temp, ctx->vb.buf+offs, 1);
++offs;
eflags = 0;
continue;
}
for (i = 0; i < nmatch; ++i) {
if (pmatch[i].rm_so >= 0) {
pmatch[i].rm_so -= offs;
pmatch[i].rm_eo -= offs;
}
}
match = pmatch[0].rm_so;
s_from = pmatch[0].rm_eo - match;
VERBOSEB(
Expand All @@ -240,7 +282,14 @@ static void dump_content(saxctxt *ctx)
ap_varbuf_regsub(&temp, m->to, ctx->vb.buf+offs, nmatch, pmatch, 0);
/* Continue past the match */
offs += pmatch[0].rm_eo;
}
/* After an empty match, the same (empty) match at the same
* position would loop forever: like pcre2_substitute() or
* Perl's /g, ask for a non-empty match at this position
* first, otherwise skip a char (above).
*/
eflags = (s_from == 0) ? AP_REG_NOTEMPTY_ATSTART | AP_REG_ANCHORED
: 0;
} while (ctx->vb.buf[offs]);
/* Copy any remaining text */
ap_varbuf_strcat(&temp, ctx->vb.buf+offs);
/* Replace the original buffer with the result */
Expand Down Expand Up @@ -354,6 +403,7 @@ static void pstartElement(void *ctxt, const xmlChar *uname,
char *found;
saxctxt *ctx = (saxctxt*) ctxt;
size_t nmatch;
int eflags;
ap_regmatch_t pmatch[10];
#ifndef GO_FASTER
int verbose = APLOGrtrace1(ctx->f->r);
Expand Down Expand Up @@ -459,7 +509,9 @@ static void pstartElement(void *ctxt, const xmlChar *uname,
if (m->flags & M_REGEX) {
nmatch = 10;
if (!ap_regexec(m->from.r, ctx->vb.buf, nmatch,
pmatch, 0)) {
pmatch, 0)
&& match_in_range(&pmatch[0], 0,
strlen(ctx->vb.buf))) {
++num_match;
match = pmatch[0].rm_so;
s_from = pmatch[0].rm_eo - match;
Expand Down Expand Up @@ -522,8 +574,29 @@ static void pstartElement(void *ctxt, const xmlChar *uname,
temp.strlen = 0;
nmatch = 10;
offs = 0;
while (!ap_regexec(m->from.r, ctx->vb.buf+offs,
nmatch, pmatch, 0)) {
eflags = 0;
len = strlen(ctx->vb.buf);
do {
/* See dump_content() */
if (ap_regexec_ex(m->from.r, ctx->vb.buf, len,
offs, nmatch, pmatch,
eflags)
|| !match_in_range(&pmatch[0], offs,
len)) {
if (!eflags) {
break;
}
ap_varbuf_strmemcat(&temp, ctx->vb.buf+offs, 1);
++offs;
eflags = 0;
continue;
}
for (i = 0; i < (int)nmatch; ++i) {
if (pmatch[i].rm_so >= 0) {
pmatch[i].rm_so -= offs;
pmatch[i].rm_eo -= offs;
}
}
match = pmatch[0].rm_so;
s_from = pmatch[0].rm_eo - match;
VERBOSE({
Expand All @@ -542,7 +615,10 @@ static void pstartElement(void *ctxt, const xmlChar *uname,
/* Continue past the match */
offs += pmatch[0].rm_eo;
++num_match;
}
eflags = (s_from == 0)
? AP_REG_NOTEMPTY_ATSTART | AP_REG_ANCHORED
: 0;
} while (ctx->vb.buf[offs]);
/* Copy any remaining text */
ap_varbuf_strcat(&temp, ctx->vb.buf+offs);
/* Replace buffer */
Expand Down
71 changes: 69 additions & 2 deletions modules/filters/mod_substitute.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ static void *merge_substitute_dcfg(apr_pool_t *p, void *basev, void *overv)

#define CAP2LINEMAX(n) ((n) < (apr_size_t)200 ? (int)(n) : 200)

/* Whether a successful match's offsets are within [start, len] and in
* order. Some PCRE versions accept \K in a lookaround, which can report
* a match starting after its end or before the start offset; such a match
* is treated as no match.
*/
static int match_in_range(const ap_regmatch_t *m, apr_size_t start,
apr_size_t len)
{
int ok = (m->rm_so >= 0
&& (apr_size_t)m->rm_so >= start
&& m->rm_so <= m->rm_eo
&& (apr_size_t)m->rm_eo <= len);
AP_DEBUG_ASSERT(ok);
return ok;
}

static apr_status_t do_pattmatch(ap_filter_t *f, apr_bucket *inb,
apr_bucket_brigade *mybb,
apr_pool_t *pool)
Expand Down Expand Up @@ -283,9 +299,48 @@ static apr_status_t do_pattmatch(ap_filter_t *f, apr_bucket *inb,
const char *pos = buff;
char *repl;
apr_size_t space_left = cfg->max_line_length;
while (!ap_regexec_len(script->regexp, pos, left,
AP_MAX_REG_MATCH, regm, 0)) {
int eflags = 0;
for (;;) {
apr_status_t rv;
int n;
/* Match from the current position in the whole line
* (so that ^, \b, lookbehinds... keep their meaning),
* the offsets are then made relative to pos below.
*/
if (ap_regexec_ex(script->regexp, buff, bytes,
pos - buff, AP_MAX_REG_MATCH, regm,
eflags)
|| !match_in_range(&regm[0], pos - buff, bytes)) {
if (!eflags || !left) {
break;
}
/* No non-empty match right after an empty one,
* consume the next char and retry a "normal"
* match from there (see below).
*/
if (script->flatten && !force_quick) {
if (vb.strlen >= cfg->max_line_length)
return APR_ENOMEM;
ap_varbuf_strmemcat(&vb, pos, 1);
}
else {
if (space_left <= 1)
return APR_ENOMEM;
space_left--;
apr_bucket_split(b, 1);
b = APR_BUCKET_NEXT(b);
}
pos++;
left--;
eflags = 0;
continue;
}
for (n = 0; n < AP_MAX_REG_MATCH; ++n) {
if (regm[n].rm_so >= 0) {
regm[n].rm_so -= pos - buff;
regm[n].rm_eo -= pos - buff;
}
}
ap_log_rerror(APLOG_MARK, APLOG_TRACE8, 0, f->r,
"Matching found");
have_match = 1;
Expand Down Expand Up @@ -336,6 +391,18 @@ static apr_status_t do_pattmatch(ap_filter_t *f, apr_bucket *inb,
*/
pos += regm[0].rm_eo;
left -= regm[0].rm_eo;
if (!left) {
break;
}
/* After an empty match, the same (empty) match at
* the same position would loop forever: like
* pcre2_substitute() or Perl's /g, ask for a
* non-empty match at this position first, otherwise
* skip a char (above).
*/
eflags = (regm[0].rm_eo == regm[0].rm_so)
? AP_REG_NOTEMPTY_ATSTART | AP_REG_ANCHORED
: 0;
}
if (have_match && script->flatten && !force_quick) {
char *copy;
Expand Down
14 changes: 11 additions & 3 deletions modules/mappers/mod_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -2593,6 +2593,7 @@ static char *do_expand(char *input, rewrite_ctx *ctx, rewriterule_entry *entry,

/* see ap_pregsub() in server/util.c */
if (bri->source && n < AP_MAX_REG_MATCH
&& bri->regmatch[n].rm_so >= 0
&& bri->regmatch[n].rm_eo > bri->regmatch[n].rm_so) {
span = bri->regmatch[n].rm_eo - bri->regmatch[n].rm_so;
if (entry && (entry->flags & RULEFLAG_ESCAPEBACKREF)) {
Expand Down Expand Up @@ -4275,7 +4276,11 @@ static cond_return_type apply_rewrite_cond(rewritecond_entry *p, rewrite_ctx *ct

case CONDPAT_AP_EXPR:
{
const char *err, *source;
/* source (and regmatch) are only set by the expression if a
* capturing regex is actually evaluated, e.g. not when the
* expression short-circuits before reaching it.
*/
const char *err, *source = NULL;
rc = ap_expr_exec_re(r, p->expr, AP_MAX_REG_MATCH, regmatch,
&source, &err);
if (rc < 0 || err) {
Expand All @@ -4288,8 +4293,11 @@ static cond_return_type apply_rewrite_cond(rewritecond_entry *p, rewrite_ctx *ct
rc = (rc > 0) ? COND_RC_MATCH : COND_RC_NOMATCH;
}

/* update briRC backref info */
if (rc && !(p->flags & CONDFLAG_NOTMATCH)) {
/* update briRC backref info, if the expression produced some
* (i.e. a capturing regex matched), otherwise keep the ones from
* the last matching RewriteCond
*/
if (rc && !(p->flags & CONDFLAG_NOTMATCH) && source) {
ctx->briRC.source = source;
memcpy(ctx->briRC.regmatch, regmatch, sizeof(regmatch));
}
Expand Down
Loading
Loading