Skip to content

fix: preserve [\s\S] instead of expanding to [\x00-\x{10ffff}] for PCRE compatibility#291

Closed
fzipi with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-pcre-modsecurity-compatibility
Closed

fix: preserve [\s\S] instead of expanding to [\x00-\x{10ffff}] for PCRE compatibility#291
fzipi with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-pcre-modsecurity-compatibility

Conversation

Copilot AI commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

[\s\S] in .ra files was being expanded to [\x00-\x{10ffff}] by rassemble-go, which is valid RE2 but illegal in PCRE non-UTF mode (ModSecurity v2/Apache).

what

  • Add replaceAnyCharClass post-processing step in assembler.go that normalizes rassemble-go's two output forms back to [\s\S]:
    • [\x00-\x{10ffff}][\s\S] (produced for quantified patterns, e.g. [\s\S]+)
    • (?s:.)[\s\S] (produced for a standalone [\s\S]; previously became . after flag stripping, silently dropping newline matching)
  • Step runs just before dontUseFlagsForMetaCharacters in the cleanup pipeline
  • Replace sb.WriteString(fmt.Sprintf(...)) with fmt.Fprintf(&sb, ...) in useHexEscapes to satisfy staticcheck QF1012

why

  • [\x00-\x{10ffff}] uses \x{...} wide-char syntax which PCRE rejects in non-UTF mode, causing Apache/ModSecurity v2 to fail on rule compilation
  • The standalone [\s\S](?s:.). path was also semantically wrong: . without the s flag does not match newlines
  • [\s\S] is valid in both PCRE and RE2 and is the idiomatic cross-engine "any char including newline" class

references


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix crs-toolchain expansion of [\s\S] for PCRE compatibility Fix: preserve [\s\S] instead of expanding to [\x00-\x{10ffff}] for PCRE compatibility Mar 1, 2026
Copilot finished work on behalf of fzipi March 1, 2026 21:02
@fzipi

fzipi commented Mar 1, 2026

Copy link
Copy Markdown
Member

Copilot AI commented Mar 1, 2026

Copy link
Copy Markdown
Contributor Author

@copilot fix the golanci-lint run: https://github.com/coreruleset/crs-toolchain/actions/runs/22552572995/job/65325552097?pr=291

Fixed in 2ed7fa7 — replaced both sb.WriteString(fmt.Sprintf(...)) calls in useHexEscapes with fmt.Fprintf(&sb, ...) as required by staticcheck QF1012.

Copilot finished work on behalf of fzipi March 1, 2026 21:22
@fzipi fzipi changed the title Fix: preserve [\s\S] instead of expanding to [\x00-\x{10ffff}] for PCRE compatibility fix: preserve [\s\S] instead of expanding to [\x00-\x{10ffff}] for PCRE compatibility Mar 1, 2026
@fzipi
fzipi requested review from Copilot and theseion March 1, 2026 21:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts the regex assembly cleanup pipeline to preserve the cross-engine “match any char including newline” idiom [\s\S], avoiding PCRE non-UTF compilation failures and preventing silent newline-matching regressions.

Changes:

  • Add a post-processing step to normalize rassemble-go outputs like [\x00-\x{10ffff}] and (?s:.) back to [\s\S] before flag stripping.
  • Extend/update assembler tests to cover standalone, quantified, and embedded [\s\S] cases (and update an existing flag-group test expectation).
  • Refactor useHexEscapes to use fmt.Fprintf instead of sb.WriteString(fmt.Sprintf(...)) (staticcheck QF1012).

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.

File Description
regex/operators/assembler.go Adds replaceAnyCharClass cleanup step and refactors hex-escape writing.
regex/operators/assembler_test.go Adds coverage for [\s\S] preservation and updates expected output where newline matching must be retained.
go.mod Tidies indirect requirements.
go.sum Updates dependency checksums consistent with module tidy/update.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread regex/operators/assembler.go Outdated
@fzipi
fzipi marked this pull request as ready for review March 1, 2026 23:13
@theseion

theseion commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

Where did you get this? The toolchain usually generates . from [\s\S], so this must be some special case.

@fzipi

fzipi commented Mar 2, 2026

Copy link
Copy Markdown
Member

@theseion

theseion commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

This only happens because of the quantifier after the character class. The quick fix here is obviously to use . instead of [\s\S], which is equivalent in our case anyway. Then we have time to investigate the actual issue.

@theseion

theseion commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Replaced by #300

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.

4 participants