🐛 Allow disabling CORS headers on SockJS endpoints

Fixes 2) of #3024
This commit is contained in:
Gina Häußge committed 2019-02-01 10:40:37 +01:00
1 parent 1acf69cb6d
commit 100fe5f290
3 files changed
+49 -5

No files matched your search

+5 -2
View File
@@ -509,8 +509,11 @@ class Server(object):
ioloop = IOLoop()
ioloop.install()
enable_cors = settings().getBoolean(["api", "allowCrossOrigin"])
self._router = SockJSRouter(self._create_socket_connection, "/sockjs",
session_kls=util.sockjs.ThreadSafeSession)
session_kls=util.sockjs.ThreadSafeSession,
user_settings=dict(websocket_allow_origin="*" if enable_cors else ""))
upload_suffixes = dict(name=self._settings.get(["server", "uploads", "nameSuffix"]), path=self._settings.get(["server", "uploads", "pathSuffix"]))
@@ -559,7 +562,7 @@ class Server(object):
return joined
util.tornado.RequestlessExceptionLoggingMixin.LOG_REQUEST = debug
util.tornado.CorsSupportMixin.ENABLE_CORS = settings().getBoolean(["api", "allowCrossOrigin"])
util.tornado.CorsSupportMixin.ENABLE_CORS = enable_cors
server_routes = self._router.urls + [
# various downloads
+41 -2
View File
@@ -12,6 +12,15 @@ import logging
from tornado.web import asynchronous, RequestHandler
try:
# python 3
# noinspection PyCompatibility
from urllib.parse import urlparse
except ImportError:
# python 2
# noinspection PyCompatibility
from urlparse import urlparse
CACHE_TIME = 31536000
LOG = logging.getLogger("tornado.general")
@@ -126,5 +135,35 @@ class PreflightHandler(BaseHandler):
def verify_origin(self):
"""Verify if request can be served"""
# TODO: Verify origin
return True
# adapted from sockjs.tornado.websocket.SockJSWebSocketHandler
origin = self.request.headers.get('Origin', '*')
# first check if connection from the same domain
same_domain = self.check_origin(origin)
if same_domain:
return True
# this is cross-origin connection - check using SockJS server settings
allow_origin = self.server.settings.get("websocket_allow_origin", "*")
if allow_origin == "":
return False
elif allow_origin == "*":
return True
else:
parsed_origin = urlparse(origin)
origin = parsed_origin.netloc
origin = origin.lower()
return origin in allow_origin
def check_origin(self, origin):
# adapted from tornado.websocket.WebSocketHandler
parsed_origin = urlparse(origin)
origin = parsed_origin.netloc
origin = origin.lower()
host = self.request.headers.get("Host")
# Check to see that origin matches host directly, including ports
return origin == host
+3 -1
View File
@@ -23,7 +23,9 @@ class SockJSWebSocketHandler(websocket.WebSocketHandler):
# this is cross-origin connection - check using SockJS server settings
allow_origin = self.server.settings.get("websocket_allow_origin", "*")
if allow_origin == "*":
if allow_origin == "":
return False
elif allow_origin == "*":
return True
else:
parsed_origin = urlparse(origin)