From c7b2d079a3323765e2c56e24309c89728c1b8b6a Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E8=83=A1=E7=8E=AE=E6=96=87?= Date: Mon, 5 Jul 2021 21:27:57 +0800 Subject: [PATCH] mgr/dashboard: fix Accept-Language header parsing MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Fix 400 error caused by multi-subtag language in Accept-Language header. Reproduced by Firefox for Android in Chinese. The regex expression is fixed according to https://datatracker.ietf.org/doc/html/rfc4647#section-2.1 Fixes: https://tracker.ceph.com/issues/51528 Signed-off-by: 胡玮文 --- src/pybind/mgr/dashboard/controllers/home.py | 2 +- src/pybind/mgr/dashboard/tests/test_home.py | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/pybind/mgr/dashboard/controllers/home.py b/src/pybind/mgr/dashboard/controllers/home.py index 5f9454c85cf..809b50617b1 100644 --- a/src/pybind/mgr/dashboard/controllers/home.py +++ b/src/pybind/mgr/dashboard/controllers/home.py @@ -58,7 +58,7 @@ class LanguageMixin(object): class HomeController(BaseController, LanguageMixin): LANG_TAG_SEQ_RE = re.compile(r'\s*([^,]+)\s*,?\s*') LANG_TAG_RE = re.compile( - r'^(?P[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})?)(;q=(?P[01]\.\d{0,3}))?$') + r'^(?P[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*|\*)(;q=(?P[01]\.\d{0,3}))?$') MAX_ACCEPTED_LANGS = 10 @lru_cache() diff --git a/src/pybind/mgr/dashboard/tests/test_home.py b/src/pybind/mgr/dashboard/tests/test_home.py index dc73a6d2ead..b0641f068f3 100644 --- a/src/pybind/mgr/dashboard/tests/test_home.py +++ b/src/pybind/mgr/dashboard/tests/test_home.py @@ -64,3 +64,10 @@ class HomeTest(ControllerTestCase, FakeFsMixin): self.assertStatus(200) logger.info(self.body) self.assertIn('', self.body.decode('utf-8')) + + @mock.patch(FakeFsMixin.builtins_open, new=FakeFsMixin.f_open) + @mock.patch('os.stat', new=FakeFsMixin.f_os.stat) + @mock.patch('os.listdir', new=FakeFsMixin.f_os.listdir) + def test_home_multiple_subtags_lang(self): + self._get('/', headers=[('Accept-Language', 'zh-Hans-CN')]) + self.assertStatus(200) -- 2.39.5