Merge pull request #5073 from FinnStutzenstein/ws404

Closing websocket connections to unknown urls
This commit is contained in:
Finn Stutzenstein 2019-10-16 12:04:59 +03:00 committed by GitHub
commit 2f01e46415
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -1,13 +1,15 @@
from channels.routing import ProtocolTypeRouter, URLRouter
from django.conf.urls import url
from openslides.utils.consumers import SiteConsumer
from openslides.utils.consumers import CloseConsumer, SiteConsumer
from openslides.utils.middleware import AuthMiddlewareStack
application = ProtocolTypeRouter(
{
# WebSocket chat handler
"websocket": AuthMiddlewareStack(URLRouter([url(r"^ws/$", SiteConsumer)]))
"websocket": AuthMiddlewareStack(
URLRouter([url(r"^ws/$", SiteConsumer), url(".*", CloseConsumer)])
)
}
)

View File

@ -3,6 +3,8 @@ from collections import defaultdict
from typing import Any, Dict, List, Optional
from urllib.parse import parse_qs
from channels.generic.websocket import AsyncWebsocketConsumer
from ..utils.websocket import WEBSOCKET_CHANGE_ID_TOO_HIGH
from . import logging
from .auth import async_anonymous_is_enabled
@ -217,3 +219,16 @@ class SiteConsumer(ProtocollAsyncJsonWebsocketConsumer):
content = {"change_id": change_id, "data": data}
await self.send_json(type="projector", content=content, in_response=in_response)
class CloseConsumer(AsyncWebsocketConsumer):
""" Auto-closes the connection """
groups: List[str] = []
def __init__(self, args: Dict[str, Any], **kwargs: Any) -> None:
logger.info(f'Closing connection to unknown websocket url {args["path"]}')
async def connect(self) -> None:
await self.accept()
await self.close()