Mod with Nginx - websocket request doesn't work
Hello, I'm using Nginx and configure MOD. We have application which has http as well as websocket request. We are seeing issues with MOD enabled for websocket request, as below "WebSocket is closed before the connection is established" i.e websocket request are getting closed immediately.
If we remove MOD and try again then it works, so I would like to understand can we use MOD for application which serves http as well as Websocket request.
FYI,
- I know MOD doesn't support websocket so we are using MOD only for monitoring http request and its able to monitor and filter out http request.
- At nginx we have below configuration to support http and websocket which works without MOD
map $http_upgrade $connection_upgrade { default upgrade; '' close; }
server { . . . proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; }
Thanks, Chetan
Currently, ModSecurity does not support WebSockets requests.
I don't know if the Apache/2.9.5 ModSecurity still doesn't support this. We have the same issue. What do I need to configure to get this working.
{
"transaction": {
"time": "09/Aug/2022:10:57:35.451928 +0000",
"transaction_id": "YvI9n_vsy6nS8IMMhtQ_wQAAAMI",
"remote_address": "172.23.0.1",
"remote_port": 43900,
"local_address": "172.23.0.2",
"local_port": 443
},
"request": {
"request_line": "GET /dataChanges/793/epiwujlx/websocket HTTP/1.1",
"headers": {
"Host": "localhost.test.nl",
"Connection": "Upgrade",
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
"Upgrade": "websocket",
"Origin": "https://localhost.test.nl",
"Sec-WebSocket-Version": "13",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "nl,nl-NL;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6",
"Cookie": "branch=municipality; theme=\"\"; JSESSIONID=a6zQ7um4DJt3N4Hez9ctXngVezcJ9BOxdDIG-QwS",
"Sec-WebSocket-Key": "71FkR6GQY7WNRYZVQWkJUQ==",
"Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits"
}
},
"response": {
"protocol": "HTTP/1.1",
"status": 400,
"headers": {
"Expires": "0",
"Cross-Origin-Opener-Policy": "same-origin",
"Cache-Control": "no-cache, no-store, max-age=0, must-revalidate",
"X-XSS-Protection": "1; mode=block",
"Pragma": "no-cache",
"X-Frame-Options": "SAMEORIGIN",
"Referrer-Policy": "no-referrer",
"Content-Security-Policy": "upgrade-insecure-requests;base-uri 'self';default-src 'self';frame-ancestors 'self';manifest-src 'self';media-src 'self';prefetch-src 'self';style-src 'self' 'unsafe-inline' https://fonts.googleapis.com ;font-src 'self' https://fonts.gstatic.com;script-src 'self' 'unsafe-inline' 'unsafe-eval' data: https://maps.googleapis.com ;worker-src 'none';object-src 'none';script-src-elem 'self' 'unsafe-inline' https://maps.googleapis.com data: ;img-src 'self' data: https://maps.googleapis.com https://maps.gstatic.com ;form-action 'self';connect-src 'self' https://maps.googleapis.com ;frame-src 'self' https://www.test.com;",
"Cross-Origin-Embedder-Policy": "unsafe-none",
"Vary": "Origin,Access-Control-Request-Method,Access-Control-Request-Headers",
"X-Content-Type-Options": "nosniff",
"Strict-Transport-Security": "max-age=31536000 ; includeSubDomains",
"Permissions-Policy": "accelerometer=(), ambient-light-sensor=(), autoplay=(), battery=(), camera=(), cross-origin-isolated=(), display-capture=(), document-domain=(), encrypted-media=(), execution-while-not-rendered=(), execution-while-out-of-viewport=(), fullscreen=(), geolocation=(self), gyroscope=(), keyboard-map=(), magnetometer=(), microphone=(), midi=(), navigation-override=(), payment=(), picture-in-picture=(), publickey-credentials-get=(), screen-wake-lock=(), sync-xhr=(), usb=(), web-share=(), xr-spatial-tracking=(), clipboard-read=(), clipboard-write=(self), gamepad=(), speaker-selection=(), conversion-measurement=(), focus-without-user-activation=(), hid=(), idle-detection=(), interest-cohort=(), serial=(), sync-script=(), trust-token-redemption=(), window-placement=(), vertical-scroll=()",
"Content-Length": "34",
"Connection": "close"
}
},
"audit_data": {
"handler": "proxy-server",
"stopwatch": {
"p1": 1081,
"p2": 0,
"p3": 0,
"p4": 0,
"p5": 11,
"sr": 209,
"sw": 1,
"l": 0,
"gc": 0
},
"producer": [
"ModSecurity for Apache/2.9.5 (http://www.modsecurity.org/)",
"OWASP_CRS/3.3.2"
],
"server": "Apache"
}
}
Hello @RALGIE ,
The mention in this item that ModSecurity does not inspect WebSocket requests is true for both ModSecurity v2.9.x and v3.0.x.
However, that is referring to actual WebSocket content (i.e. that is not going over HTTP). What you have shown here is an HTTP transaction where the request is inviting the server to Upgrade to WebSockets. I'm not aware of any reason why this individual HTTP transaction cannot be done successfully while ModSecurity is active.
Note that just because you are seeing ModSecurity audit log output for that transaction does not mean that ModSecurity is causing the 400 status code. If you are using SecAuditEngine RelevantOnly with a pertinent setting for SecAuditLogRelevantStatus, then the audit log output will occur because the web application itself generated the 400 status (i.e. ModSecurity is only an observer and reporter of what was otherwise generated as an HTTP response). I suspect that is what happened in your case (particularly since there is no rule referenced in the audit log).