Skip to content

Commit

Permalink
Handle Daphne-Root-Path for websockets, adding root_path to scope.
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro R. Sedeño <[email protected]>
Signed-off-by: Alejandro R Sedeño <[email protected]>
  • Loading branch information
asedeno committed Nov 19, 2022
1 parent ba9bc82 commit cecd476
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions daphne/ws_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@ def onConnect(self, request):
self.server.protocol_connected(self)
self.request = request
self.protocol_to_accept = None
self.root_path = self.server.root_path
self.socket_opened = time.time()
self.last_ping = time.time()
try:
# Sanitize and decode headers
# Sanitize and decode headers, potentially extracting root path
self.clean_headers = []
for name, value in request.headers.items():
name = name.encode("ascii")
# Prevent CVE-2015-0219
if b"_" in name:
continue
self.clean_headers.append((name.lower(), value.encode("latin1")))
if name.lower() == b"daphne-root-path":
self.root_path = unquote(value)
else:
self.clean_headers.append((name.lower(), value.encode("latin1")))
# Get client address if possible
peer = self.transport.getPeer()
host = self.transport.getHost()
Expand Down Expand Up @@ -76,6 +80,7 @@ def onConnect(self, request):
"type": "websocket",
"path": unquote(self.path.decode("ascii")),
"raw_path": self.path,
"root_path": self.root_path,
"headers": self.clean_headers,
"query_string": self._raw_query_string, # Passed by HTTP protocol
"client": self.client_addr,
Expand Down

0 comments on commit cecd476

Please sign in to comment.