Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: response body (based on #326) #334

Merged
merged 2 commits into from
Jan 10, 2025

Conversation

airween
Copy link
Member

@airween airween commented Nov 19, 2024

This patch is based on #326 but there was a problem: if Nginx uses any custom error page, then because of the the internal redirect a new transaction started and it generated an audit.log entry with empty messages field. See this commit.

I found PR #273 by @liudongmiao but that pull request was very old, and I wasn't able to build the module with that. I picked up the modifications and added to the base PR.

Credits to: @liudongmiao, @g00g1.

@Dr-Lazarus-V2, @g00g1, @liudongmiao, @jeremyjpj0916 - guys please could you test this branch?

Suggested test cases:

  • Nginx as proxy
  • Nginx as standalone HTTP server
  • use custom error pages
  • without custom error pages
  • circumstances above with different phases (use CRS/custom rules to check REQUEST_URI (rules with phase:1 but Nginx calls msc_process_uri(), REQUEST_HEADERS (phase:1), eg ARGS (phase2), etc...) (with response 403))

Feel free to ask here or on Slack.

g00g1 and others added 2 commits June 18, 2024 20:52

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.

Verified

This commit was signed with the committer’s verified signature.
airween Ervin Hegedus
Copy link

@jeremyjpj0916
Copy link

jeremyjpj0916 commented Nov 19, 2024

Does this fix losing the context of the tx due to nginx internal redirects(POSTS all recorded as GETs etc.)? If so thats a good first step in the right direction.

@airween
Copy link
Member Author

airween commented Nov 19, 2024

Does this fix losing the context of the tx due to nginx internal redirects(POSTS all recorded as GETs etc.)? If so thats a good first step in the right direction.

Yes, it does. The base PR (#326) didn't do that but I could add #273 (see my initial commit) which does. Both two PR's are necessary. I also faced the duplicated tx in case of internal redirect, this is why I started to research.

@Dr-Lazarus-V2
Copy link

Will have a look and test this out!

@Dr-Lazarus-V2
Copy link

Hey I have done some initial testing, it appears to work as intended.

@liudongmiao
Copy link

@airween For ngx_http_modsecurity_get_module_ctx part, I'd suggest to use with the original realip part, which checks r->internal || r->filter_finalize too.

When I use it with the old modsecurity, I don't know why I removed the internal and flter_finalize part.

https://github.com/nginx/nginx/blob/476d6526b2e8297025c608425f4cad07b4f65990/src/http/modules/ngx_http_realip_module.c#L544-L568

@airween
Copy link
Member Author

airween commented Nov 25, 2024

Hi @liudongmiao,

@airween For ngx_http_modsecurity_get_module_ctx part, I'd suggest to use with the original realip part, which checks r->internal || r->filter_finalize too.

When I use it with the old modsecurity, I don't know why I removed the internal and flter_finalize part.

https://github.com/nginx/nginx/blob/476d6526b2e8297025c608425f4cad07b4f65990/src/http/modules/ngx_http_realip_module.c#L544-L568

thanks for suggestion - you mean here I should replace that function with your suggestion?

Could you send a review with your solution?

@airween
Copy link
Member Author

airween commented Dec 22, 2024

@liudongmiao ping.

@liudongmiao
Copy link

@airween Yes, I suggest you use original version from nginx, and check whether use nginx version or my version.

@liudongmiao
Copy link

@airween However, my patched version works well since the pr I maked 2 years ago.

@airween
Copy link
Member Author

airween commented Dec 28, 2024

@airween Yes, I suggest you use original version from nginx, and check whether use nginx version or my version.

Thanks - just to clarify: you mean we should add the extra condition to check if the ctx is null or not, right? So now I copied from your solution this:

    ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
    if (ctx == NULL) {

but we should change to this:

    ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
    if (ctx == NULL && (r->internal || r->filter_finalize)) {

It would be nice to know what the r->filter_finalize does, because Nginx's documentation does not explains that (r->internal is covered in the docs).

@liudongmiao
Copy link

@airween In my old cases, I just want to get context, ignore any nginx's internal status.

You should check it carefully.

@airween
Copy link
Member Author

airween commented Jan 10, 2025

@airween In my old cases, I just want to get context, ignore any nginx's internal status.

You should check it carefully.

Thanks - then I keep it as it is. We can add other conditions if everyone has an issue.

Thanks for all the help guys, merging now.

@airween airween merged commit fb678c5 into owasp-modsecurity:master Jan 10, 2025
4 checks passed
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.

None yet

5 participants