Skip to content
Snippets Groups Projects
Commit 864bf6bc authored by HeJ's avatar HeJ
Browse files

codestyle fixes (dereferer env parsing)

parent 7cd53d94
No related branches found
No related tags found
No related merge requests found
......@@ -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')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment