Skip to content

Commit a4d9f39

Browse files
authored
Add secure and httponly options for cookies
2 parents 7e60e88 + 25e05bb commit a4d9f39

5 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/auth/identification.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def _read_client_token(self, request_handler):
116116
def _write_client_token(self, client_id, request_handler):
117117
expiry_time = date_utils.get_current_millis() + days_to_ms(self.EXPIRES_DAYS)
118118
new_token = client_id + '&' + str(expiry_time)
119-
request_handler.set_secure_cookie(self.COOKIE_KEY, new_token, expires_days=self.EXPIRES_DAYS)
119+
server_config = request_handler.application.server_config
120+
request_handler.set_secure_cookie(self.COOKIE_KEY, new_token, expires_days=self.EXPIRES_DAYS, secure=server_config.cookie_secure, httponly=True)
120121

121122
def _can_write(self, request_handler):
122123
return can_write_secure_cookie(request_handler)

src/auth/oauth_token_manager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def update_tokens(self, token_response: OAuthTokenResponse, username, request_ha
2424
if not self._enabled:
2525
return
2626

27-
request_handler.set_secure_cookie('token', token_response.access_token)
27+
server_config = request_handler.application.server_config
28+
request_handler.set_secure_cookie('token', token_response.access_token, httponly=True, secure=server_config.cookie_secure)
2829

2930
if token_response.should_refresh():
3031
refresh_token = token_response.refresh_token
@@ -33,7 +34,7 @@ def update_tokens(self, token_response: OAuthTokenResponse, username, request_ha
3334
self._refresh_tokens[username] = refresh_token
3435
self._schedule_token_refresh(username, refresh_token, token_response.resolve_next_refresh_datetime())
3536

36-
request_handler.set_secure_cookie('token_details', token_response.serialize_details())
37+
request_handler.set_secure_cookie('token_details', token_response.serialize_details(), httponly=True, secure=server_config.cookie_secure)
3738

3839
def can_restore_state(self, request_handler):
3940
if not self._enabled:

src/auth/tornado_auth.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ def authenticate(self, request_handler):
8888

8989
LOGGER.info('Authenticated user ' + username)
9090

91-
request_handler.set_secure_cookie('username', username, expires_days=self.authenticator.auth_expiration_days)
91+
server_config = request_handler.application.server_config
92+
request_handler.set_secure_cookie('username', username, expires_days=self.authenticator.auth_expiration_days, httponly=True, secure=server_config.cookie_secure)
9293

9394
path = tornado.escape.url_unescape(request_handler.get_argument('next', '/'))
9495

src/model/server_conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def __init__(self) -> None:
4545
self.xsrf_protection = None
4646
# noinspection PyTypeChecker
4747
self.env_vars: EnvVariables = None
48+
self.cookie_secure = True
4849

4950
def get_port(self):
5051
return self.port
@@ -201,6 +202,7 @@ def from_json(conf_path, temp_folder):
201202

202203
security = model_helper.read_dict(json_object, 'security')
203204

205+
config.cookie_secure = model_helper.read_bool_from_config('cookie_secure', security, default=True)
204206
config.allowed_users = _prepare_allowed_users(allowed_users, admin_users, user_groups)
205207
config.alerts_config = json_object.get('alerts')
206208
config.callbacks_config = json_object.get('callbacks')

src/web/server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,11 @@ def init(server_config: ServerConfig,
864864
'websocket_ping_timeout': 300,
865865
'compress_response': True,
866866
'xsrf_cookies': server_config.xsrf_protection != XSRF_PROTECTION_DISABLED,
867+
'xsrf_cookie_kwargs': {
868+
'httponly': True,
869+
'secure': server_config.cookie_secure,
870+
'samesite': 'Lax'
871+
},
867872
}
868873

869874
application = tornado.web.Application(handlers, **settings)

0 commit comments

Comments
 (0)