diff --git a/runner/runner.go b/runner/runner.go index 43c536f6d..70724a9e8 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -3246,28 +3246,17 @@ func normalizeHeaders(headers map[string][]string) map[string]interface{} { return normalized } +var ( + // Connection: Upgrade alone is not enough: servers send it for h2c too. + // Upgrade may list several protocols, optionally versioned (RFC 9110 7.8). + webSocketHeaders = regexp.MustCompile(`(?mi)^(?:Sec-WebSocket-Accept:[ \t]*\S|Upgrade:(?:[^\r\n]*,)?[ \t]*websocket(?:/[^,\r\n]*)?[ \t]*(?:,|\r?$))`) + webSocketBody = regexp.MustCompile(`{"socket":true,"socketUrl":"wss?://.*"}|{"sid":"[^"]*","upgrades":\["websocket"\].*}`) +) + func isWebSocket(resp *httpx.Response) bool { - if resp.StatusCode == 101 { - return true - } - // TODO: improve this checks - // Check for specific headers that indicate WebSocket support - keyHeaders := []string{`^Sec-WebSocket-Accept:\s+.+`, `^Upgrade:\s+websocket`, `^Connection:\s+upgrade`} - for _, header := range keyHeaders { - re := regexp.MustCompile(header) - if re.MatchString(resp.RawHeaders) { - return true - } - } - // Check for specific data that indicates WebSocket support - keyData := []string{`{"socket":true,"socketUrl":"(?:wss?|ws)://.*"}`, `{"sid":"[^"]*","upgrades":\["websocket"\].*}`} - for _, data := range keyData { - re := regexp.MustCompile(data) - if re.Match(resp.RawData) { - return true - } - } - return false + return resp.StatusCode == 101 || + webSocketHeaders.MatchString(resp.RawHeaders) || + webSocketBody.Match(resp.RawData) } // stripANSI removes ANSI color codes from a string using pre-compiled regex diff --git a/runner/websocket_test.go b/runner/websocket_test.go new file mode 100644 index 000000000..f0321906a --- /dev/null +++ b/runner/websocket_test.go @@ -0,0 +1,31 @@ +package runner + +import ( + "testing" + + "github.com/projectdiscovery/httpx/common/httpx" + "github.com/stretchr/testify/require" +) + +func TestIsWebSocket(t *testing.T) { + for _, tc := range []struct { + name string + resp httpx.Response + want bool + }{ + {"switching protocols", httpx.Response{StatusCode: 101}, true}, + {"upgrade required", httpx.Response{StatusCode: 426, RawHeaders: "HTTP/1.1 426 Upgrade Required\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n"}, true}, + {"lowercase header", httpx.Response{StatusCode: 400, RawHeaders: "HTTP/1.1 400 Bad Request\r\nupgrade: WebSocket\r\n"}, true}, + {"protocol list", httpx.Response{StatusCode: 426, RawHeaders: "HTTP/1.1 426 Upgrade Required\r\nUpgrade: h2c, websocket\r\n"}, true}, + {"accept header", httpx.Response{StatusCode: 200, RawHeaders: "HTTP/1.1 200 OK\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n"}, true}, + {"h2c upgrade offer", httpx.Response{StatusCode: 200, RawHeaders: "HTTP/1.1 200 OK\r\nConnection: Upgrade\r\nUpgrade: h2c\r\n"}, false}, + {"other protocol name", httpx.Response{StatusCode: 426, RawHeaders: "HTTP/1.1 426 Upgrade Required\r\nUpgrade: websocket-other\r\n"}, false}, + {"empty accept header", httpx.Response{StatusCode: 200, RawHeaders: "HTTP/1.1 200 OK\r\nSec-WebSocket-Accept:\r\nServer: x\r\n"}, false}, + {"socket.io handshake", httpx.Response{StatusCode: 200, RawHeaders: "HTTP/1.1 200 OK\r\n", RawData: []byte(`{"sid":"abc","upgrades":["websocket"],"pingInterval":25000}`)}, true}, + {"plain page", httpx.Response{StatusCode: 200, RawHeaders: "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n", RawData: []byte("")}, false}, + } { + t.Run(tc.name, func(t *testing.T) { + require.Equal(t, tc.want, isWebSocket(&tc.resp)) + }) + } +}