From 383637ec05ddadd6290497c2d8a41ea845da3a66 Mon Sep 17 00:00:00 2001 From: JustAnotherArchivist Date: Fri, 27 Dec 2019 03:30:00 +0000 Subject: [PATCH] Validate maps --- http2irc.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/http2irc.py b/http2irc.py index 86d0491..1238f0f 100644 --- a/http2irc.py +++ b/http2irc.py @@ -110,6 +110,7 @@ class Config(dict): if 'port' in obj['web'] and (not isinstance(obj['web']['port'], int) or not 1 <= obj['web']['port'] <= 65535): raise InvalidConfig('Invalid web port') if 'maps' in obj: + seenWebPaths = {} for key, map_ in obj['maps'].items(): if not isinstance(key, str) or not key: raise InvalidConfig(f'Invalid map key {key!r}') @@ -117,7 +118,30 @@ class Config(dict): raise InvalidConfig(f'Invalid map for {key!r}') if any(x not in ('webpath', 'ircchannel', 'auth', 'module', 'moduleargs') for x in map_): raise InvalidConfig(f'Unknown key(s) found in map {key!r}') - #TODO: Check values + + if 'webpath' not in map_: + map_['webpath'] = f'/{key}' + if not isinstance(map_['webpath'], str): + raise InvalidConfig(f'Invalid map {key!r} web path: not a string') + if not map_['webpath'].startswith('/'): + raise InvalidConfig(f'Invalid map {key!r} web path: does not start at the root') + if map_['webpath'] in seenWebPaths: + raise InvalidConfig(f'Invalid map {key!r} web path: collides with map {seenWebPaths[map_["webpath"]]!r}') + seenWebPaths[map_['webpath']] = key + + if 'ircchannel' in map_: + if not isinstance(map_['ircchannel'], str): + raise InvalidConfig(f'Invalid map {key!r} IRC channel: not a string') + if not map_['ircchannel'].startswith('#') and not map_['ircchannel'].startswith('&'): + raise InvalidConfig(f'Invalid map {key!r} IRC channel: does not start with # or &') + #TODO Check if it's a valid name per IRC spec + + if 'auth' in map_: + if map_['auth'] is not False and not isinstance(map_['auth'], str): + raise InvalidConfig(f'Invalid map {key!r} auth: must be false or a string') + if isinstance(map_['auth'], str) and ':' not in map_['auth']: + raise InvalidConfig(f'Invalid map {key!r} auth: must contain a colon') + if 'module' in map_ and not os.path.isfile(map_['module']): raise InvalidConfig(f'Module {map_["module"]!r} in map {key!r} is not a file') if 'moduleargs' in map_: @@ -131,8 +155,7 @@ class Config(dict): # Fill in default values for the maps for key, map_ in obj['maps'].items(): - if 'webpath' not in map_: - map_['webpath'] = f'/{key}' + # webpath is already set above for duplicate checking if 'ircchannel' not in map_: map_['ircchannel'] = f'#{key}' if 'auth' not in map_: