Skip to content

Commit 8476cf4

Browse files
committed
Fix related to the #6171 (WebSocket Host port)
1 parent 3130623 commit 8476cf4

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎lib/core/common.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5140,7 +5140,7 @@ def getHostHeader(url):
51405140
if url:
51415141
parsed = _urllib.parse.urlparse(url)
51425142
retVal = parsed.netloc
5143-
defaultPort = {"http": 80, "https": 443}.get(parsed.scheme.lower())
5143+
defaultPort = {"http": 80, "https": 443, "ws": 80, "wss": 443}.get(parsed.scheme.lower())
51445144

51455145
# Keep non-default ports, including 80 on HTTPS and 443 on HTTP.
51465146
if defaultPort is not None and retVal.endswith(':%d' % defaultPort):

‎lib/core/settings.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from thirdparty import six
2020

2121
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
22-
VERSION = "1.10.11.2"
22+
VERSION = "1.10.11.3"
2323
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
2424
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
2525
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)

‎lib/request/websocket.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def connect(self, url, header=None, cookie=None):
7373
self.sock.settimeout(self._timeout)
7474

7575
hostport = "[%s]" % host if ":" in host else host # bracket IPv6 literals
76-
if port not in (80, 443):
76+
if port != (443 if secure else 80): # Note: only the scheme's own default port gets omitted
7777
hostport = "%s:%d" % (hostport, port)
7878

7979
key = getText(base64.b64encode(os.urandom(16)))

‎tests/test_websocket.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,35 @@ def recv(self, n):
133133
ws = _client(); ws.sock = _RaisingSock(_ssl.SSLError("decryption failed"))
134134
self.assertRaises(_ssl.SSLError, ws.recv)
135135

136+
def test_handshake_host_port(self):
137+
# Note: only the scheme's own default port is omitted (port 443 used to be dropped for 'ws://' too)
138+
import lib.request.websocket as W
139+
140+
class _Sent(Exception):
141+
pass
142+
143+
class _Capture(_FakeSock):
144+
def settimeout(self, value):
145+
pass
146+
147+
def sendall(self, data):
148+
raise _Sent(data)
149+
150+
original = W.socket.create_connection
151+
try:
152+
W.socket.create_connection = lambda *args, **kwargs: _Capture()
153+
for url, host in (("ws://example.com:443/x", "example.com:443"), ("ws://example.com:80/x", "example.com"), ("ws://example.com:8080/x", "example.com:8080")):
154+
ws = WebSocket.__new__(WebSocket)
155+
ws._timeout = None
156+
try:
157+
ws.connect(url)
158+
except _Sent as ex:
159+
self.assertIn(("Host: %s\r\n" % host).encode(), ex.args[0])
160+
else:
161+
self.fail("handshake was not sent")
162+
finally:
163+
W.socket.create_connection = original
164+
136165

137166
if __name__ == "__main__":
138167
unittest.main()

0 commit comments

Comments
 (0)