Skip to content

[2.4.x] Merge changes to regex backreferences and empty matches - #1097

Open
covener wants to merge 10 commits into
apache:2.4.xfrom
covener:2.4.x-regex-backrefs
Open

covener wants to merge 10 commits into
apache:2.4.xfrom
covener:2.4.x-regex-backrefs

Conversation

@covener

@covener covener commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Merge r1915267, r1915268, r1915271, 4d328d9, 8347e21 and #1101 (test only, not yet merged on trunk) from trunk:

regex: Add ap_regexec_ex() which can take a starting offset to match from.

  • include/ap_mmn.h:
    Bump MMN minor.

  • include/ap_regex.h:
    Declare ap_regexec_ex().

  • server/util_pcre.c(ap_regexec, ap_regexec_len, ap_regexec_ex):
    Reuse existing ap_regexec_len() code to implement ap_regexec_ex() where the
    offset is given instead of zero, then implement ap_regexec{,len}() in terms
    of ap_regexec_ex().

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1915267 13f79535-47bb-0310-9956-ffa450edef68

regex: Add AP_REG_NOTEMPTY_ATSTART maching option.

  • include/ap_mmn.h:
    Bump MMN minor.

  • include/ap_regex.h:
    Define AP_REG_NOTEMPTY_ATSTART bit.

  • server/util_pcre.c(ap_regexec_ex):
    Map AP_REG_NOTEMPTY_ATSTART to native PCRE_NOTEMPTY_ATSTART.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1915268 13f79535-47bb-0310-9956-ffa450edef68

mod_headers: Avoid infinite recursion with the edit* action and empty matches.

Change the recursion used for edit* to a loop using the new ap_regexec_ex()
function taking the current position (offset) in the subject string.

After an empty match do the same thing as pcre2_substitute() (or Perl's /g),
that is: don't allow for another empty match at the same positition by setting
the AP_REG_NOTEMPTY_ATSTART option. If there is a non-empty match use it,
otherwise skip/consume the first character and continue from there.

  • modules/metadata/mod_headers.c:
    Rename the hdr_edit_r enum for edit* to hdr_edit_all to better express what
    is does (and since the action is not recursive anymore).

  • modules/metadata/mod_headers.c(push_string, push_match):
    New helpers to consume the subject and substitutions in an array of iovec.

  • modules/metadata/mod_headers.c(process_regexp):
    Implement the new logic, using push_match() and push_string() to fill the
    iovec array finally passed to apr_strcatv() for the resulting string.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1915271 13f79535-47bb-0310-9956-ffa450edef68

Backreferences without a capturing match, and empty regex matches (#1096)

  • Backreferences without a capturing match, and empty regex matches:

An expression only fills the caller's match vector and source when a
capturing regex is evaluated and matches, so an expression that is true
via a '||' short-circuit, a regex without groups, or '!~' left $N / %N
to be expanded from whatever the vector held before: a previous
condition or entry, or nothing at all. mod_substitute and
mod_proxy_html did not advance past an empty match, so a pattern that
can match the empty string (x*, ^, \b, a lookahead) never progressed.

  • server/util_expr_eval.c (ap_expr_eval_re_backref): Expand only groups
    with rm_so >= 0 and a non-empty span.
    (ap_expr_eval_comp): Reset the match vector when a capturing regex
    does not match.

  • modules/mappers/mod_rewrite.c (apply_rewrite_cond): Start with no
    source, and update the condition backreferences only when the
    expression produced captures, so %N keeps referring to the last
    matching RewriteCond as with the other condition types.
    (do_expand): Require rm_so >= 0.

  • modules/proxy/mod_proxy_fcgi.c (fix_cgivars): Reset the source and
    match vector before each ProxyFCGISetEnvIf entry.

  • server/util.c (regsub_core): Require rm_so >= 0.

  • server/util_regex.c (ap_rxplus_exec): Don't recurse on an empty match
    at the start of the string.
    (ap_rxplus_match): Refuse groups that did not participate.

  • modules/filters/mod_substitute.c (do_pattmatch),
    modules/filters/mod_proxy_html.c (dump_content, pstartElement):
    Match from an absolute offset in the line with ap_regexec_ex(), so
    ^, \b and lookbehinds keep their meaning after a previous match, and
    after an empty match retry with AP_REG_NOTEMPTY_ATSTART|AP_REG_ANCHORED
    or copy one byte, as pcre2_substitute() and Perl's /g do.

  • test/modules/filters/test_010_substitute_empty_match.py,
    test/modules/metadata/test_006_expr_backref.py,
    test/modules/proxy/test_13_fcgi_setenvif.py,
    test/modules/proxy/fcgi_faker.py, test/unit/regex.c: New tests.

  • test/modules/proxy/env.py: Load mod_proxy_fcgi.

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01PUN2EPhoNvc4rfibgy6H72

    • test/modules/filters/test_010_substitute_empty_match.py: Write the
      documents with newline="" so the multi-line cases stay LF-only on
      Windows.

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01PUN2EPhoNvc4rfibgy6H72

    • changes-entries/regex_backrefs.txt: Add.

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01PUN2EPhoNvc4rfibgy6H72


Co-authored-by: Claude Opus 5.5 noreply@anthropic.com

Regex matches with out of order or out of range offsets (#1098)

  • Regex matches with out of order or out of range offsets:

PCRE versions which accept \K in a lookaround (PCRE1, PCRE2 before
10.38) can report a match whose start is after its end ("a(?=bc\K)"),
or before the offset the match was started from ("(?<=\K.)b" from a
later position). mod_headers' edit/edit* aborted the child on the
first with ap_assert() and used a wrapped-around length for the second,
and mod_substitute and mod_proxy_html used the offsets as-is.

  • modules/metadata/mod_headers.c (push_match): Replace ap_assert() with
    AP_DEBUG_ASSERT() plus a runtime check, also requiring the match to
    start at or after *pos and end within the string, and treat a match
    failing it as no match.

  • modules/filters/mod_substitute.c (match_in_range): New helper.
    (do_pattmatch): Treat a match out of range as no match.

  • modules/filters/mod_proxy_html.c (match_in_range): New helper.
    (dump_content, pstartElement): Treat a match out of range as no match.

  • changes-entries/regex_match_offsets.txt: Add.

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01PUN2EPhoNvc4rfibgy6H72

  • Delete changes-entries/regex_match_offsets.txt

no changes-entry


Co-authored-by: Claude Opus 5.5 noreply@anthropic.com

Plus one 2.4.x-only commit, "regex: Add AP_REG_NOTEMPTY and AP_REG_ANCHORED matching options.", placed before r1915271: trunk got those two options as a small part of r1810605 (a large ap_expr change not being backported), and both r1915271 and 4d328d9 use AP_REG_ANCHORED. A final 2.4.x-only commit collapses the MMN bumps from r1915267, r1915268 and that commit into a single one, 142 → 143.

Conflicts were resolved against 2.4.x context only (no functional change from trunk): include/ap_mmn.h and server/util_pcre.c for r1915267/r1915268, modules/metadata/mod_headers.c for r1915271 (2.4.x's 'note' action check), and modules/filters/mod_substitute.c for 4d328d9 (2.4.x has no expression replacement).

8347e21 applied cleanly. #1101 makes mod_proxy_ajp, mod_proxy_uwsgi and mod_proxy_fcgi optional modules in the proxy tests, so a build without one skips only the tests that use it. On 2.4.x the uwsgi check goes in test_03_uwsgi.py's single setup fixture, and the AGENTS.md part is omitted. With the three modules removed, modules/proxy gives 51 passed, 14 skipped.

Tested on macOS: the new tests (test_010_substitute_empty_match, test_006_expr_backref, test_13_fcgi_setenvif) pass, and modules/filters, metadata, proxy and http1 give 365 passed, 67 skipped, 34 xfailed, no XPASS. Each commit through r1915271 builds on its own.

🤖 Generated with Claude Code

ylavic and others added 6 commits November 26, 2026 12:46
…rver/util_pcre.c):

regex: Add ap_regexec_ex() which can take a starting offset to match from.

* include/ap_mmn.h:
  Bump MMN minor.

* include/ap_regex.h:
  Declare ap_regexec_ex().

* server/util_pcre.c(ap_regexec, ap_regexec_len, ap_regexec_ex):
  Reuse existing ap_regexec_len() code to implement ap_regexec_ex() where the
  offset is given instead of zero, then implement ap_regexec{,len}() in terms
  of ap_regexec_ex().

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1915267 13f79535-47bb-0310-9956-ffa450edef68

(cherry picked from commit e52a206)
…rver/util_pcre.c):

regex: Add AP_REG_NOTEMPTY_ATSTART maching option.

* include/ap_mmn.h:
  Bump MMN minor.

* include/ap_regex.h:
  Define AP_REG_NOTEMPTY_ATSTART bit.

* server/util_pcre.c(ap_regexec_ex):
  Map AP_REG_NOTEMPTY_ATSTART to native PCRE_NOTEMPTY_ATSTART.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1915268 13f79535-47bb-0310-9956-ffa450edef68

(cherry picked from commit 97a1873)
2.4.x-only change, not a trunk merge.  Trunk got these two options as
a small part of r1810605 (ap_expr: open string expressions to the
<word>), a large ap_expr change that is not being backported.  The
mod_headers edit* fix (r1915271) and the empty-match handling in
mod_substitute and mod_proxy_html (4d328d9) retry an empty match
with AP_REG_NOTEMPTY_ATSTART|AP_REG_ANCHORED, so 2.4.x needs the
option, with the same values as on trunk.

* include/ap_mmn.h:
  Bump MMN minor.

* include/ap_regex.h:
  Define AP_REG_NOTEMPTY and AP_REG_ANCHORED bits, as on trunk.

* server/util_pcre.c (ap_regexec_ex):
  Map them to native PCRE_NOTEMPTY and PCRE_ANCHORED.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…_headers.c):

mod_headers: Avoid infinite recursion with the edit* action and empty matches.

Change the recursion used for edit* to a loop using the new ap_regexec_ex()
function taking the current position (offset) in the subject string.

After an empty match do the same thing as pcre2_substitute() (or Perl's /g),
that is: don't allow for another empty match at the same positition by setting
the AP_REG_NOTEMPTY_ATSTART option. If there is a non-empty match use it,
otherwise skip/consume the first character and continue from there.

* modules/metadata/mod_headers.c:
  Rename the hdr_edit_r enum for edit* to hdr_edit_all to better express what
  is does (and since the action is not recursive anymore).

* modules/metadata/mod_headers.c(push_string, push_match):
  New helpers to consume the subject and substitutions in an array of iovec.

* modules/metadata/mod_headers.c(process_regexp):
  Implement the new logic, using push_match() and push_string() to fill the
  iovec array finally passed to apr_strcatv() for the resulting string.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1915271 13f79535-47bb-0310-9956-ffa450edef68

(cherry picked from commit 64ecf00)
…ubstitute.c):

Backreferences without a capturing match, and empty regex matches (apache#1096)

* Backreferences without a capturing match, and empty regex matches:

An expression only fills the caller's match vector and source when a
capturing regex is evaluated and matches, so an expression that is true
via a '||' short-circuit, a regex without groups, or '!~' left $N / %N
to be expanded from whatever the vector held before: a previous
condition or entry, or nothing at all.  mod_substitute and
mod_proxy_html did not advance past an empty match, so a pattern that
can match the empty string (x*, ^, \b, a lookahead) never progressed.

* server/util_expr_eval.c (ap_expr_eval_re_backref): Expand only groups
  with rm_so >= 0 and a non-empty span.
  (ap_expr_eval_comp): Reset the match vector when a capturing regex
  does not match.

* modules/mappers/mod_rewrite.c (apply_rewrite_cond): Start with no
  source, and update the condition backreferences only when the
  expression produced captures, so %N keeps referring to the last
  matching RewriteCond as with the other condition types.
  (do_expand): Require rm_so >= 0.

* modules/proxy/mod_proxy_fcgi.c (fix_cgivars): Reset the source and
  match vector before each ProxyFCGISetEnvIf entry.

* server/util.c (regsub_core): Require rm_so >= 0.

* server/util_regex.c (ap_rxplus_exec): Don't recurse on an empty match
  at the start of the string.
  (ap_rxplus_match): Refuse groups that did not participate.

* modules/filters/mod_substitute.c (do_pattmatch),
  modules/filters/mod_proxy_html.c (dump_content, pstartElement):
  Match from an absolute offset in the line with ap_regexec_ex(), so
  ^, \b and lookbehinds keep their meaning after a previous match, and
  after an empty match retry with AP_REG_NOTEMPTY_ATSTART|AP_REG_ANCHORED
  or copy one byte, as pcre2_substitute() and Perl's /g do.

* test/modules/filters/test_010_substitute_empty_match.py,
  test/modules/metadata/test_006_expr_backref.py,
  test/modules/proxy/test_13_fcgi_setenvif.py,
  test/modules/proxy/fcgi_faker.py, test/unit/regex.c: New tests.

* test/modules/proxy/env.py: Load mod_proxy_fcgi.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PUN2EPhoNvc4rfibgy6H72

* * test/modules/filters/test_010_substitute_empty_match.py: Write the
  documents with newline="" so the multi-line cases stay LF-only on
  Windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PUN2EPhoNvc4rfibgy6H72

* * changes-entries/regex_backrefs.txt: Add.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PUN2EPhoNvc4rfibgy6H72

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 4d328d9)
2.4.x-only change, not a trunk merge.  The merges of r1915267 and
r1915268 and the 2.4.x-only AP_REG_NOTEMPTY/AP_REG_ANCHORED commit each
bumped the minor (143, 144, 145), following the trunk commits.  They
land in 2.4.x together, so one bump to 143 covering all of the new
API is enough.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@covener

covener commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

Working on ditching the ap_assert

covener and others added 2 commits November 26, 2026 14:45
* Regex matches with out of order or out of range offsets:

PCRE versions which accept \K in a lookaround (PCRE1, PCRE2 before
10.38) can report a match whose start is after its end ("a(?=bc\K)"),
or before the offset the match was started from ("(?<=\K.)b" from a
later position).  mod_headers' edit/edit* aborted the child on the
first with ap_assert() and used a wrapped-around length for the second,
and mod_substitute and mod_proxy_html used the offsets as-is.

* modules/metadata/mod_headers.c (push_match): Replace ap_assert() with
  AP_DEBUG_ASSERT() plus a runtime check, also requiring the match to
  start at or after *pos and end within the string, and treat a match
  failing it as no match.

* modules/filters/mod_substitute.c (match_in_range): New helper.
  (do_pattmatch): Treat a match out of range as no match.

* modules/filters/mod_proxy_html.c (match_in_range): New helper.
  (dump_content, pstartElement): Treat a match out of range as no match.

* changes-entries/regex_match_offsets.txt: Add.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PUN2EPhoNvc4rfibgy6H72

* Delete changes-entries/regex_match_offsets.txt

no changes-entry

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 8347e21)
…modules/proxy/test_03_uwsgi.py, AGENTS.md changes omitted):

test: Load mod_proxy_ajp, mod_proxy_uwsgi and mod_proxy_fcgi as optional modules.

A module in ProxyTestSetup.add_modules() that the build lacks stops setup
with "Unable to find modules", failing every test under test/modules/proxy,
and the env.require() checks in the tests that use these modules could
never be reached. Load them with add_optional_modules() so a build without
one skips only those tests (or fails them under --strict-optional).

* test/modules/proxy/env.py: Move proxy_ajp, proxy_uwsgi and proxy_fcgi
  to add_optional_modules().
* test/modules/proxy/test_03_uwsgi.py: Check for mod_proxy_uwsgi with
  env.require() in the setup fixture, which had no check.
* test/README.pytest: Describe when a test module belongs in
  add_modules() and when in add_optional_modules().

(2.4.x's test_03_uwsgi.py has a single module-level setup fixture, so the
check goes there; 2.4.x has no AGENTS.md.)
covener added a commit to covener/httpd that referenced this pull request Nov 27, 2026
test/ is copied to 2.4.x as it is, so mark the tests from apache#1096 that fail
on a 2.4.x without the change, expecting the backport (apache#1097) in 2.4.72.
Marks follow a run against unfixed 2.4.x: a test that hangs the server,
crashes it or leaves errors in the error log is skipped, one that fails
cleanly is an expected failure, and one that passes is left unmarked.

* test/modules/filters/test_010_substitute_empty_match.py: Skip the cases
  that loop forever or log "Line too long"; anchor_nonempty fails cleanly;
  lookbehind passes already.
* test/modules/metadata/test_006_expr_backref.py: Skip the class: its
  Header edit* lines crash every request without r1915271.
* test/modules/proxy/test_13_fcgi_setenvif.py: Only the short-circuit test
  fails without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PUN2ETFf8d5srUT51exsTS
covener and others added 2 commits November 26, 2026 21:45
test: Mark the tests of the regex backreference changes not_backported.

test/ is copied to 2.4.x as it is, so mark the tests from apache#1096 that fail
on a 2.4.x without the change, expecting the backport (apache#1097) in 2.4.72.
Marks follow a run against unfixed 2.4.x: a test that hangs the server,
crashes it or leaves errors in the error log is skipped, one that fails
cleanly is an expected failure, and one that passes is left unmarked.

* test/modules/filters/test_010_substitute_empty_match.py: Skip the cases
  that loop forever or log "Line too long"; anchor_nonempty fails cleanly;
  lookbehind passes already.
* test/modules/metadata/test_006_expr_backref.py: Skip the class: its
  Header edit* lines crash every request without r1915271.
* test/modules/proxy/test_13_fcgi_setenvif.py: Only the short-circuit test
  fails without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PUN2ETFf8d5srUT51exsTS
(cherry picked from commit 4468c24)
test: Name the pull request in the not_backported examples.

Changes are made by pull request now, so the examples showing svn
revisions led to marks waiting for a revision that never comes. Show
"gh-NNNN" instead, and say that the "rNNNNNNN" names in older marks are
svn revisions from before the move to git.

* test/pyhttpd/backport.py, test/README.pytest: Use apachegh-1096 in the
  examples, and explain the older revision names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PUN2ETFf8d5srUT51exsTS
(cherry picked from commit c1185a1)
covener added a commit that referenced this pull request Nov 27, 2026
…nal modules (#1101)

* test: Load mod_proxy_ajp, mod_proxy_uwsgi and mod_proxy_fcgi as optional modules.

A module in ProxyTestSetup.add_modules() that the build lacks stops setup
with "Unable to find modules", failing every test under test/modules/proxy,
and the env.require() checks in the tests that use these modules could
never be reached. Load them with add_optional_modules() so a build without
one skips only those tests (or fails them under --strict-optional).

* test/modules/proxy/env.py: Move proxy_ajp, proxy_uwsgi and proxy_fcgi
  to add_optional_modules().
* test/modules/proxy/test_03_uwsgi.py: Check for mod_proxy_uwsgi with
  env.require() in both class fixtures; _proxy_uwsgi_setup had no check.
* test/README.pytest, AGENTS.md: Describe when a test module belongs in
  add_modules() and when in add_optional_modules().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PUN2EPhoNvc4rfibgy6H72

* test: Mark the tests of the regex backreference changes not_backported.

test/ is copied to 2.4.x as it is, so mark the tests from #1096 that fail
on a 2.4.x without the change, expecting the backport (#1097) in 2.4.72.
Marks follow a run against unfixed 2.4.x: a test that hangs the server,
crashes it or leaves errors in the error log is skipped, one that fails
cleanly is an expected failure, and one that passes is left unmarked.

* test/modules/filters/test_010_substitute_empty_match.py: Skip the cases
  that loop forever or log "Line too long"; anchor_nonempty fails cleanly;
  lookbehind passes already.
* test/modules/metadata/test_006_expr_backref.py: Skip the class: its
  Header edit* lines crash every request without r1915271.
* test/modules/proxy/test_13_fcgi_setenvif.py: Only the short-circuit test
  fails without the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PUN2ETFf8d5srUT51exsTS

* test: Name the pull request in the not_backported examples.

Changes are made by pull request now, so the examples showing svn
revisions led to marks waiting for a revision that never comes. Show
"gh-NNNN" instead, and say that the "rNNNNNNN" names in older marks are
svn revisions from before the move to git.

* test/pyhttpd/backport.py, test/README.pytest: Use gh-1096 in the
  examples, and explain the older revision names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PUN2ETFf8d5srUT51exsTS

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants