I had things working, it was very exciting, I was chatting with friends and running send.py to inject messages. then something happened and I can’t figure out what.
This is post 1. I’m loosing track of what is relevant. before I could post this, someone walked me though strace and looking at the redis server logs where I found:
7731:M 17 Feb 2024 15:45:02.894 # Possible SECURITY ATTACK detected. It looks like somebody is sending POST or Host: commands to Redis. This is likely due to an attacker attempting to use Cross Protocol Scripting to compromise your Redis instance. Connection aborted.
I will hit post here because maybe what is here is helpful, but I would skip it and read my reply, coming soon…
back to post #1:
it seems to be related to redis as I can make the error go away by using the InMemory backend.
# nginx conf:
location /ws/ {
proxy_pass http://localhost:8085;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
# unknown "connection_upgrade" variable
# proxy_set_header Connection $connection_upgrade;
}
# settings.py
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer"
}
daphne in a shell so I can see what is going on:
(venv) videoteam@val2:/srv/www/pib$ daphne --port 8085 pib.asgi:application -v 3
2024-02-17 06:29:46,890 INFO Starting server at tcp:port=8085:interface=127.0.0.1
2024-02-17 06:29:46,890 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2024-02-17 06:29:46,890 INFO Configuring endpoint tcp:port=8085:interface=127.0.0.1
2024-02-17 06:29:46,891 INFO HTTPFactory starting on 8085
2024-02-17 06:29:46,891 INFO Starting factory <daphne.http_protocol.HTTPFactory object at 0x7f47d4e9cf50>
2024-02-17 06:29:46,892 INFO Listening on TCP address 127.0.0.1:8085
this js in a browser:
const roomName = "pi2"
console.log('roomName: %s', roomName);
const chatSocket = new WebSocket(
'wss://'
+ window.location.host
+ '/ws/pistat/'
+ roomName
+ '/'
);
console.log('connected: %s', chatSocket);
console shows:
roomName: pi2
tut.html:26 connected: [object WebSocket]
daphne:
76.227.131.147:48606 - - [17/Feb/2024:06:32:36] "WSCONNECTING /ws/pistat/pi2/" - -
2024-02-17 06:32:36,846 DEBUG Upgraded connection ['76.227.131.147', 48606] to WebSocket
2024-02-17 06:32:36,855 DEBUG WebSocket ['76.227.131.147', 48606] open and established
76.227.131.147:48606 - - [17/Feb/2024:06:32:36] "WSCONNECT /ws/pistat/pi2/" - -
2024-02-17 06:32:36,855 DEBUG WebSocket ['76.227.131.147', 48606] accepted by application
side question that I hope isn’t relevant to this problem but I would like to understand:
why Access denied?
> (venv) videoteam@val2:/srv/www/pib$ node_modules/wscat/bin/wscat --connect wss://ps1.fpgas.mithis.com/ws/pistat/pi2/
error: Unexpected server response: 403
daphne:
76.227.131.147:35638 - - [17/Feb/2024:06:34:40] "WSCONNECTING /ws/pistat/pi2/" - -
2024-02-17 06:34:40,284 DEBUG Upgraded connection ['76.227.131.147', 35638] to WebSocket
2024-02-17 06:34:40,286 INFO failing WebSocket opening handshake ('Access denied')
2024-02-17 06:34:40,286 WARNING dropping connection to peer tcp4:76.227.131.147:35638 with abort=False: Access denied
2024-02-17 06:34:40,286 DEBUG WebSocket ['76.227.131.147', 35638] rejected by application
76.227.131.147:35638 - - [17/Feb/2024:06:34:40] "WSREJECT /ws/pistat/pi2/" - -
2024-02-17 06:34:40,288 DEBUG WebSocket closed for ['76.227.131.147', 35638]
76.227.131.147:35638 - - [17/Feb/2024:06:34:40] "WSDISCONNECT /ws/pistat/pi2/" - -
Back to the error I care about. If I use redis:
settings.py
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("127.0.0.1", 6379)],
},
},
}
reload the js,
tut.html:18 WebSocket connection to 'wss://ps1.fpgas.mithis.com/ws/pistat/pi2/' failed:
(anonymous) @ tut.html:18
tut.html:34 Chat socket closed unexpectedly
daphne:
76.227.131.147:42132 - - [17/Feb/2024:06:43:26] "WSCONNECTING /ws/pistat/pi2/" - -
2024-02-17 06:43:26,273 DEBUG Upgraded connection ['76.227.131.147', 42132] to WebSocket
2024-02-17 06:43:26,365 DEBUG <asyncio.TransportSocket fd=13, family=2, type=1, proto=6, laddr=('127.0.0.1', 38608), raddr=('127.0.0.1', 6379)> connected to 127.0.0.1:6379: (<_SelectorSocketTransport fd=13 read=polling write=<idle, bufsize=0>>, <asyncio.streams.StreamReaderProtocol object at 0x7f8945a90ad0>)
2024-02-17 06:43:26,366 DEBUG <_SelectorSocketTransport fd=13 read=polling write=<idle, bufsize=0>>: Fatal write error on socket transport
Traceback (most recent call last):
File "/usr/lib/python3.11/asyncio/selector_events.py", line 1057, in write
n = self._sock.send(data)
^^^^^^^^^^^^^^^^^^^^^
BrokenPipeError: [Errno 32] Broken pipe
2024-02-17 06:43:27,017 ERROR Exception inside application: Error UNKNOWN while writing to socket. Connection lost.
Traceback (most recent call last):
File "/srv/www/pib/venv/lib/python3.11/site-packages/redis/asyncio/connection.py", line 442, in send_packed_command
await self._writer.drain()
File "/usr/lib/python3.11/asyncio/streams.py", line 378, in drain
await self._protocol._drain_helper()
File "/usr/lib/python3.11/asyncio/streams.py", line 167, in _drain_helper
raise ConnectionResetError('Connection lost')
ConnectionResetError: Connection lost
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/routing.py", line 62, in __call__
return await application(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/security/websocket.py", line 37, in __call__
return await self.application(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/sessions.py", line 47, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/sessions.py", line 263, in __call__
return await self.inner(wrapper.scope, receive, wrapper.send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/auth.py", line 185, in __call__
return await super().__call__(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/middleware.py", line 24, in __call__
return await self.inner(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/routing.py", line 116, in __call__
return await application(
^^^^^^^^^^^^^^^^^^
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/consumer.py", line 94, in app
return await consumer(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/consumer.py", line 58, in __call__
await await_many_dispatch(
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/utils.py", line 50, in await_many_dispatch
await dispatch(result)
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/consumer.py", line 73, in dispatch
await handler(message)
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels/generic/websocket.py", line 173, in websocket_connect
await self.connect()
File "/srv/www/pib/venv/lib/python3.11/site-packages/pistat/consumers.py", line 14, in connect
await self.channel_layer.group_add(self.group_name, self.channel_name)
File "/srv/www/pib/venv/lib/python3.11/site-packages/channels_redis/core.py", line 504, in group_add
await connection.zadd(group_key, {channel: time.time()})
File "/srv/www/pib/venv/lib/python3.11/site-packages/redis/asyncio/client.py", line 601, in execute_command
conn = self.connection or await pool.get_connection(command_name, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/srv/www/pib/venv/lib/python3.11/site-packages/redis/asyncio/connection.py", line 1040, in get_connection
await self.ensure_connection(connection)
File "/srv/www/pib/venv/lib/python3.11/site-packages/redis/asyncio/connection.py", line 1062, in ensure_connection
await connection.connect()
File "/srv/www/pib/venv/lib/python3.11/site-packages/redis/asyncio/connection.py", line 258, in connect
await self.on_connect()
File "/srv/www/pib/venv/lib/python3.11/site-packages/redis/asyncio/connection.py", line 362, in on_connect
await self.send_command("CLIENT", "SETINFO", "LIB-VER", self.lib_version)
File "/srv/www/pib/venv/lib/python3.11/site-packages/redis/asyncio/connection.py", line 466, in send_command
await self.send_packed_command(
File "/srv/www/pib/venv/lib/python3.11/site-packages/redis/asyncio/connection.py", line 453, in send_packed_command
raise ConnectionError(
redis.exceptions.ConnectionError: Error UNKNOWN while writing to socket. Connection lost.
2024-02-17 06:43:27,020 INFO failing WebSocket opening handshake ('Internal server error')
2024-02-17 06:43:27,020 WARNING dropping connection to peer tcp4:76.227.131.147:42132 with abort=False: Internal server error
2024-02-17 06:43:27,022 DEBUG WebSocket closed for ['76.227.131.147', 42132]
76.227.131.147:42132 - - [17/Feb/2024:06:43:27] "WSDISCONNECT /ws/pistat/pi2/" - -
The node wcat gives the same
failing WebSocket opening handshake (‘Access denied’)