From: 胡玮文 Date: Mon, 5 Jul 2021 13:27:57 +0000 (+0800) Subject: mgr/dashboard: fix Accept-Language header parsing X-Git-Tag: v16.2.6~133^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2a2c504ac0ee50d12ab0f61741436f6aedcf9f45;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: 胡玮文 (cherry picked from commit c7b2d079a3323765e2c56e24309c89728c1b8b6a) --- diff --git a/src/pybind/mgr/dashboard/controllers/home.py b/src/pybind/mgr/dashboard/controllers/home.py index 793b241dd755..cb4a4a5a1853 100644 --- a/src/pybind/mgr/dashboard/controllers/home.py +++ b/src/pybind/mgr/dashboard/controllers/home.py @@ -59,7 +59,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 37e09b4b2af9..1b1b6b706390 100644 --- a/src/pybind/mgr/dashboard/tests/test_home.py +++ b/src/pybind/mgr/dashboard/tests/test_home.py @@ -65,3 +65,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)