Skip to content

Parse capture groups in if-blocks (fixed for crossplane)#91

Merged
dvershinin merged 1 commit intomasterfrom
pr-51-fixed
Dec 6, 2025
Merged

Parse capture groups in if-blocks (fixed for crossplane)#91
dvershinin merged 1 commit intomasterfrom
pr-51-fixed

Conversation

@dvershinin
Copy link
Owner

@dvershinin dvershinin commented Dec 6, 2025

Based on the work from #51 by @MegaManSec, rebased and adapted for the crossplane-based parser.

This PR enables the IfBlock to provide variables from regex capture groups when using ~, ~*, !~, or !~* operators. For example:

if ($request_uri ~ "^/foo/(.*)$") {
    # $1 is now available as a variable
    return 301 /bar/$1;
}

Key improvement: Capture groups now correctly inherit the boundary (safety constraints) from the source variable being matched. This is critical for HTTP splitting detection:

  • if ($uri ~* ^/echo/(.*)$)$1 inherits $uri's boundary /[^\x20\t]*can contain \r\n → warning triggered
  • if ($request_uri ~* ^/echo/(.*)$)$1 inherits $request_uri's boundary /[^\s]*cannot contain \r\n → safe

This distinction matters because:

  • $uri contains decoded bytes (nginx decodes %0d%0a → actual \r\n)
  • $request_uri contains raw percent-encoded form (%0d%0a stays as-is)

Changes:

  • Add provide_variables = True to IfBlock
  • Add is_regex property to check for regex operators
  • Add variables property that extracts capture groups from the if condition regex with proper boundary inheritance

Fixes #50

- Import compile_script to get source variable's boundary
- Capture groups from if-block regex now inherit the boundary from the
  source variable being tested (e.g., $request_uri)
- This fixes false positives where capture groups were incorrectly flagged
  as potentially containing newlines when the source was safe
- Fixes #50
@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 6, 2025

@dvershinin dvershinin merged commit cdadcb9 into master Dec 6, 2025
19 checks passed
@dvershinin dvershinin deleted the pr-51-fixed branch December 6, 2025 19:24
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.

capture groups in if() are not defined as variables

1 participant