Repository navigation
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
b413e8b to
b9a1758
Compare
|
Hi @A13501350, thanks for this patch - could you add a new test case to the existing regression tests collection? Thanks! |
fzipi
left a comment
There was a problem hiding this comment.
Thanks @A13501350, this fixes a long-standing one (#729) and looks good to me.
I reviewed it by reading the code; I haven't built or run it. Windows CI is green.
modsecProcessRequestruns both phase 1 and phase 2, so this covers the default-phase-2 case from #729 (SecRule ARGS ... redirect:/).- At that point the only thing that puts
Locationintor->headers_outis the redirect branch ofperform_interception, so there's no risk of forwarding a stale value.
Two questions:
- The commit message says response phases never run on this connector. From what I can see,
OnSendResponsedoes callmodsecProcessResponsewhenSecResponseBodyAccessis On and there's a body. Its intercept path doesClear()+SetStatus()without aLocation, the same symptom as #729. Would you mind either applying the same lookup there, or opening a follow-up issue and rewording that line? I'd rather not have the "never run" claim in the history if it isn't accurate. - The redirect target can include macros (e.g.
redirect:/login?next=%{ARGS.next}), and ARGS are decoded, so the value could in theory contain CR/LF. Apache rejects such header values on output. I'm not sure IIS/HTTP.SYS does the same forSetHeader. Do you know? If not, a small guard (skip the header, or return 500, ifstrpbrk(location, "\r\n")matches) would settle it.
On tests: the existing redirect regression tests run on Apache, so they wouldn't catch this. Once the iis/tests harness from #3613 is in, a small FTW case expecting a 302 with a Location header would be great. I'm fine with that as a follow-up.
b9a1758 to
f83a0df
Compare
|
The original OnBeginRequest config-load path had two architectural
shortcomings that made fail-closed impossible to implement cleanly:
1. No single exit — every failure path before Finished was either
silently promoted to hr=S_OK or returned early from the function,
so the lock was released at N different call sites and no
centralized response construction existed.
2. No per-context sticky state — once config loading failed there was
nothing to stop every subsequent request on the same app pool from
re-entering the load path and hitting the same bug until a crash.
This commit restructures the function around a single Finished label
with a new bool configError that every failure path sets before
goto Finished. The Finished block now owns:
- LeaveCriticalSection (only one site in the function)
- Marking the context sticky-failed (configLoadingFailed + m_Config=NULL)
so subsequent requests also fail closed
- Writing the 500 response (without SetRequestHandled so IIS renders
its httpErrors page)
Bug fixes that fell out of the refactor:
- GlobalWideCharToMultiByte returns a Win32 DWORD positive error code,
which FAILED() sees as success. Replace two FAILED(hr) checks in this
function with hr != 0 (safe whether the signature stays DWORD or is
later changed to HRESULT).
- delete → delete[] at all six sites that free memory allocated via
new CHAR[] inside GlobalWideCharToMultiByte.
- GetConfig failure now includes a pConfig==NULL guard and a WriteEventViewerLog
instead of silently downgrading hr to S_OK.
- A previously-skipped branch (path[0]==0, empty config path on an
enabled module) now also fails closed instead of running with zero
rules.
New sticky flag: MODSECURITY_STORED_CONTEXT::configLoadingFailed. Once
set, every subsequent request returns 500 until the application pool
is recycled (IIS does not notify native modules when modsecurity.conf
changes, so no automatic recovery is possible).
Fixes owasp-modsecurity#729 (config-load side). Redirect-on-redirect is handled separately
by PR owasp-modsecurity#3689.
The original OnBeginRequest config-load path had two architectural
shortcomings that made fail-closed impossible to implement cleanly:
1. No single exit — every failure path before Finished was either
silently promoted to hr=S_OK or returned early from the function,
so the lock was released at N different call sites and no
centralized response construction existed.
2. No per-context sticky state — once config loading failed there was
nothing to stop every subsequent request on the same app pool from
re-entering the load path and hitting the same bug until a crash.
This commit restructures the function around a single Finished label
with a new bool configError that every failure path sets before
goto Finished. The Finished block now owns:
- LeaveCriticalSection (only one site in the function)
- Marking the context sticky-failed (configLoadingFailed + m_Config=NULL)
so subsequent requests also fail closed
- Writing the 500 response (without SetRequestHandled so IIS renders
its httpErrors page)
Bug fixes that fell out of the refactor:
- GlobalWideCharToMultiByte returns a Win32 DWORD positive error code,
which FAILED() sees as success. Replace two FAILED(hr) checks in this
function with hr != 0 (safe whether the signature stays DWORD or is
later changed to HRESULT).
- delete → delete[] at all six sites that free memory allocated via
new CHAR[] inside GlobalWideCharToMultiByte.
- GetConfig failure now includes a pConfig==NULL guard and a WriteEventViewerLog
instead of silently downgrading hr to S_OK.
- A previously-skipped branch (path[0]==0, empty config path on an
enabled module) now also fails closed instead of running with zero
rules.
New sticky flag: MODSECURITY_STORED_CONTEXT::configLoadingFailed. Once
set, every subsequent request returns 500 until the application pool
is recycled (IIS does not notify native modules when modsecurity.conf
changes, so no automatic recovery is possible).
Fixes owasp-modsecurity#729 (config-load side). Redirect-on-redirect is handled separately
by PR owasp-modsecurity#3689.
|
Hi @A13501350, thanks for adding the CR/LF guard, that resolves @fzipi's second point. A few more things I noticed while reading the current head (
And a reminder about the regression test from my earlier comment: I know the IIS harness is in #3613, so it is fine to add the test there or right after it lands, but please note it in the PR description so it does not get lost. Thanks! |
When a rule with a redirect action matches, the core engine stores the
target URI in r->headers_out ("Location") and perform_interception()
returns the redirect status. Both OnBeginRequest and OnSendResponse
only forwarded the status code to IIS, leaving clients with a 301/302
without a Location header.
Add a shared ApplyInterventionResponse() helper used by both request
(OnBeginRequest) and response (OnSendResponse, after Clear()) paths to
validate and forward the Location header. Targets that are empty, exceed
USHRT_MAX, or contain control characters (< 0x20 except HTAB, or 0x7f)
are rejected with HTTP 500, and SetStatus()/SetHeader() failures also
fall back to 500.
Validation and SetHeader() failures are recorded in the transaction's
debug and audit logs (with rule metadata and UNIQUE_ID) via
modsecLogInterventionError() rather than writing per-request Event
Viewer entries.
Fixes owasp-modsecurity#729
f83a0df to
ffe3530
Compare
|
@airween @fzipi Rebased onto
|
|
| if(location == nullptr) | ||
| return false; | ||
|
|
||
| size_t len = strlen(location); |



Summary
When a rule with a
redirectaction matches, the core engine stores the target URI in theLocationheader of therequest_rec'sheaders_outtable andperform_interception()returns the redirect status. Both the request (OnBeginRequest) and response (OnSendResponse) intervention paths on IIS only forwarded the status code, so clients received a 301/302 without aLocationheader.This fix introduces a shared
ApplyInterventionResponse()helper for bothOnBeginRequestandOnSendResponse(afterClear()), forwardingLocationand falling back to HTTP 500 if the target is empty, exceedsUSHRT_MAX, contains control characters (< 0x20except HTAB, or0x7f), or ifSetStatus()/SetHeader()fails. Errors are recorded in the transaction debug/audit logs with rule metadata andUNIQUE_IDrather than Windows Event Viewer.Fixes #729
Tests
Verified phases 1–4 redirects and invalid/oversized
Location500 fallbacks in CI on Windows IIS; permanent FTW regression tests will be added once #3613'siis/testsharness lands.