diff --git a/src/hub/settings/base.py b/src/hub/settings/base.py index f02dad6872f002fd71c728335521641b5c91991f..191702cba6f7ed8ce3e188a560c937fdd8f98723 100644 --- a/src/hub/settings/base.py +++ b/src/hub/settings/base.py @@ -102,6 +102,26 @@ def gen_secret_key(): return ''.join([random.SystemRandom().choice(SECRET_CHARSET) for i in range(50)]) +# regex to match arbitrary hostnames +REGEX_HOST = re.compile(r'([\w\d-]+\.)+\w+(:\d+)?') + + +# helper function for parsing DEREFERER_*LIST (allow hostname only, regex and https:// prefixed strings) +def _handle_hostpattern_list(target: list, items: list): + for item in items: + if item.startswith('/') and item.endswith('/'): + try: + target.append(re.compile('https://' + item[1:-1])) + except re.error as err: + print('WARNING: failed to parse regex pattern "', item, "', ignoring it: ", err, sep='') + elif item.startswith('https://') or item.startswith('http://'): + target.append(item) + elif REGEX_HOST.fullmatch(item): + target.append('https://' + item) + else: + print('WARNING: failed to parse pattern "', item, "' (expected hostname, url or slash-enclosed regex), skipping it", sep='') + + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -528,22 +548,6 @@ PLAINUI_CONFERENCE = env('PLAINUI_CONFERENCE') # If you modify this on a non-empty database you should run `manage.py rerender_markdown` to update cashed markdown that contains old urls. PLAINUI_DEREFERER_URL = env('PLAINUI_DEREFERER_URL') -# helper function for parsing DEREFERER_*LIST (allow hostname only, regex and https:// prefixed strings) -re_host = re.compile('([\w\d-]+\.)+\w+(:\d+)?$') -def _handle_hostpattern_list(target: list, items: list): - for item in items: - if item.startswith('/') and item.endswith('/'): - try: - target.append(re.compile('https://' + item[1:-1])) - except re.error as err: - print('WARNING: failed to parse regex pattern "', item, "', ignoring it: ", err, sep='') - elif item.startswith('https://') or item.startswith('http://'): - target.append(item) - elif re_host.match(item): - target.append('https://' + item) - else: - print('WARNING: failed to parse pattern "', item, "' (expected hostname, url or slash-enclosed regex), skipping it", sep='') - # URI patterns that are auto-allowed (never get dereferrer'd) # Only looks at schema and host/port, so for example for the url https://rc3.world:4321/some/where/ the entry should be `'https://rc3.world'`. # Can also be a regex, for example re.compile(r'https://.*\.rc3\.world')