regex: make sure we don't have more than one group in the regex

This commit is contained in:
lilydjwg 2021-11-06 10:44:10 +08:00
parent f8be3e7418
commit f3be1c585f
3 changed files with 11 additions and 1 deletions

View file

@ -1,4 +1,4 @@
# MIT licensed
# Copyright (c) 2013-2021 lilydjwg <lilydjwg@gmail.com>, et al.
__version__ = '2.5'
__version__ = '2.6dev'

View file

@ -17,6 +17,8 @@ async def get_version_impl(info):
regex = re.compile(conf['regex'])
except sre_constants.error as e:
raise GetVersionError('bad regex', exc_info=e)
if regex.groups > 1:
raise GetVersionError('multi-group regex')
encoding = conf.get('encoding', 'latin1')

View file

@ -51,6 +51,14 @@ async def test_missing(get_version, httpbin):
"regex": "foobar",
})
async def test_multi_group(get_version, httpbin):
with pytest.raises(RuntimeError):
await get_version("example", {
"source": "regex",
"url": httpbin.url + "/base64/" + base64_encode("1.2"),
"regex": r"(\d+)\.(\d+)",
})
async def test_regex_with_tokenBasic(get_version, httpbin):
assert await get_version("example", {
"source": "regex",