I have this function in cookiecutter-toolforge and all tools generated with it:
@decorator.decorator def read_private(func, *args, **kwargs): try: f = args[0] fd = f.fileno() except AttributeError: pass except IndexError: pass else: mode = os.stat(fd).st_mode if (stat.S_IRGRP | stat.S_IROTH) & mode: name = getattr(f, "name", "config file") raise ValueError(f'{name} is readable to others, ' 'must be exclusively user-readable!') return func(*args, **kwargs)
It’s used to load the tool’s config:
has_config = app.config.from_file('config.yaml', load=read_private(yaml.safe_load), silent=True)
And ensures that, if the config is world-readable, the tool will immediately refuse to start up. (Ideally, the user would then realize that they need to rotate the secret key and, if there was an OAuth consumer in the config file, request a new one; but some will probably just chmod 600 the file and hope nobody saw it.)
It would be nice to have this function in a shared place instead of copy+pasting it into every tool, and the toolforge library might be a good place for it. What do you think?