Skip to content

Commit 5f8f8e5

Browse files
authored
fix(cookies): allow more characters in cookie names, as defined by more recent RFCs (#490)
Python's http.cookies library is very strict, and throws an exception when a cookie-name violates older RFC definitions of the name. This is reflected back as a 500. But the user has no indication that he was the cause of it. More recent RFCs seem to allow for more characters, which is also reflected in the http.cookies library, but not used. So switch to that more relaxed definition of valid cookie name. Still, illegal names should not return 500 in aiohttp, but that is a problem for another time.
1 parent 5943a8f commit 5f8f8e5

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

truewiki/__main__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import click
33
import logging
44
import os
5+
import re
56

67
from aiohttp import web
78
from aiohttp.web_log import AccessLogger
9+
from http import cookies
810
from openttd_helpers import click_helper
911
from openttd_helpers.logging_helper import click_logging
1012
from openttd_helpers.sentry_helper import click_sentry
@@ -58,6 +60,19 @@
5860
REMOTE_IP_HEADER = None
5961

6062

63+
# Monkey-patch the http.cookies library, as it is considering certain
64+
# cookie-names invalid, which happen in the real world. It throws an exception
65+
# in those cases, causing a 500. But the user can do absolutely nothing about
66+
# having a cookie the browser considers valid. aiohttp should honestly handle
67+
# that more gracefully, but it doesn't.
68+
# There is a bit of discussion what cookie-names are actually allowed, but
69+
# more recent RFCs allow for more characters. So we monkey-patch that in.
70+
#
71+
# See https://github.com/aio-libs/aiohttp/issues/2683 for more details.
72+
#
73+
cookies._is_legal_key = re.compile("[%s]+" % re.escape(cookies._UnescapedChars)).fullmatch
74+
75+
6176
class ErrorOnlyAccessLogger(AccessLogger):
6277
def log(self, request, response, time):
6378
# Only log if the status was not successful

0 commit comments

Comments
 (0)