server/config: use safer YAML loader

Fixes #254
This commit is contained in:
Shyam Sunder 2019-04-27 17:50:49 -04:00
parent 4117f63375
commit 26127eaaf5

View file

@ -4,11 +4,11 @@ import yaml
from szurubooru import errors
def merge(left: Dict, right: Dict) -> Dict:
def _merge(left: Dict, right: Dict) -> Dict:
for key in right:
if key in left:
if isinstance(left[key], dict) and isinstance(right[key], dict):
merge(left[key], right[key])
_merge(left[key], right[key])
elif left[key] != right[key]:
left[key] = right[key]
else:
@ -16,7 +16,7 @@ def merge(left: Dict, right: Dict) -> Dict:
return left
def docker_config() -> Dict:
def _docker_config() -> Dict:
for key in [
'POSTGRES_USER',
'POSTGRES_PASSWORD',
@ -45,15 +45,18 @@ def docker_config() -> Dict:
}
def read_config() -> Dict:
with open('config.yaml.dist') as handle:
ret = yaml.load(handle.read())
def _file_config(filename: str) -> Dict:
with open(filename) as handle:
return yaml.load(handle.read(), Loader=yaml.SafeLoader)
def _read_config() -> Dict:
ret = _file_config('config.yaml.dist')
if os.path.exists('config.yaml'):
with open('config.yaml') as handle:
ret = merge(ret, yaml.load(handle.read()))
ret = _merge(ret, _file_config('config.yaml'))
if os.path.exists('/.dockerenv'):
ret = merge(ret, docker_config())
ret = _merge(ret, _docker_config())
return ret
config = read_config() # pylint: disable=invalid-name
config = _read_config() # pylint: disable=invalid-name