From: 胡玮文 Date: Mon, 5 Jul 2021 13:27:57 +0000 (+0800) Subject: mgr/dashboard: fix Accept-Language header parsing X-Git-Tag: v17.1.0~1460^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F42183%2Fhead;p=ceph.git mgr/dashboard: fix Accept-Language header parsing 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: 胡玮文 --- diff --git a/src/pybind/mgr/dashboard/controllers/home.py b/src/pybind/mgr/dashboard/controllers/home.py index 5f9454c85cf8..809b50617b10 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 dc73a6d2ead6..b0641f068f30 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)