From 5832429540bb940c44c3b0d8c2bcf7783ff2ecaf Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Sat, 11 Jan 2020 13:52:37 +0100 Subject: [PATCH] Fix unrecognized user agents seen in the wild --- src/pretix/helpers/cookies.py | 7 +++++-- src/tests/multidomain/test_middlewares.py | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/pretix/helpers/cookies.py b/src/pretix/helpers/cookies.py index da9cb44e7f..76d5dab756 100644 --- a/src/pretix/helpers/cookies.py +++ b/src/pretix/helpers/cookies.py @@ -52,7 +52,7 @@ def drops_unrecognized_same_site_cookies(useragent): # Regex parsing of User-Agent string. (See note above!) RE_CHROMIUM = re.compile(r"Chrom(e|ium)") -RE_CHROMIUM_VERSION = re.compile(r"Chrom[^ /]+/([0-9]+)[.0-9]*") +RE_CHROMIUM_VERSION = re.compile(r"Chrom[^ /]+[ /]([0-9]+)[.0-9]*") RE_UC_VERSION = re.compile(r"UCBrowser/([0-9]+)\.([0-9]+)\.([0-9]+)[.0-9]* ") RE_IOS_VERSION = re.compile(r"\(iP.+; CPU .*OS ([0-9]+)[_0-9]*.*\) AppleWebKit/") RE_MAC_VERSION = re.compile(r"\(Macintosh;.*Mac OS X ([0-9]+)_([0-9]+)[_0-9]*.*\) AppleWebKit/") @@ -90,7 +90,10 @@ def is_chromium_based(useragent): def is_chromium_version_at_least(major, useragent): # Extract digits from first capturing group. - version = int(RE_CHROMIUM_VERSION.search(useragent).group(1)) + match = RE_CHROMIUM_VERSION.search(useragent) + if not match: + return False + version = int(match.group(1)) return version >= major diff --git a/src/tests/multidomain/test_middlewares.py b/src/tests/multidomain/test_middlewares.py index 9dbdbba01d..6764ff55e1 100644 --- a/src/tests/multidomain/test_middlewares.py +++ b/src/tests/multidomain/test_middlewares.py @@ -119,6 +119,9 @@ def test_with_forwarded_host(env, client): 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.59.10 (KHTML, like Gecko) Version/5.1.9 ' 'Safari/534.59.10', + 'Mozilla 5.0 (Windows NT 10.0; Win64; x64) AppleWebKit 537.36 (KHTML, like Gecko) Chrome 78.0.3904.97 Safari 537.36 OPR 65.0.3467.48', + 'Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20150101 Firefox/47.0 (Chrome)', + 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.0.0', ]) def test_cookie_samesite_none(env, client, agent): client.post('/mrmcd/2015/cart/add', HTTP_HOST='example.com', HTTP_USER_AGENT=agent,