#!/usr/bin/env python3 """Local Compose check: stored credentials are hashes and are absent from logs. The check creates one synthetic user and one session, then reads only the password hash and token hash from PostgreSQL. The synthetic password and token stay in process memory; on failure the script never prints them. """ import hashlib import pathlib import subprocess from client import Client, LAB, arguments, check, run def read_env(path): values = {} env_path = pathlib.Path(path) if not env_path.is_file(): return values for raw in env_path.read_text(encoding='utf-8-sig').splitlines(): line = raw.strip() if not line or line.startswith('#') or '=' not in line: continue name, value = line.split('=', 1) values[name.strip()] = value.strip() return values def configure(parser): parser.add_argument('--env-file', default='.env') def compose(args, *command, input_text=None): return subprocess.run([ 'docker', 'compose', '--env-file', args.env_file, *command, ], cwd=pathlib.Path(__file__).resolve().parents[1], input=input_text, capture_output=True, text=True, timeout=60) def psql(args, database_user, database_name, sql, variables=None): command = [ 'exec', '-T', 'postgres', 'psql', '--username', database_user, '--dbname', database_name, '--set', 'ON_ERROR_STOP=1', '--tuples-only', '--no-align', ] for name, value in (variables or {}).items(): command.extend(['--set', name + '=' + value]) result = compose(args, *command, input_text=sql) check(result.returncode == 0, 'operator database query failed') return result.stdout.strip() def main(): args = arguments('Local Compose credential storage and log leak check', configure) check(LAB >= 2, 'credential storage requires lab2 or later') client = Client(args.base_url, args.ca_file) client.call('GET', '/health/ready', schema='Health') actor = client.user('storage') username = actor['registration']['username'] password = actor['registration']['password'] token = actor['token'] env = read_env(args.env_file) database_user = env.get('DATABASE_USER', 'messenger') database_name = env.get('DATABASE_NAME', 'messenger') password_hash = psql(args, database_user, database_name, """ SELECT password_hash FROM users WHERE username = :'target_username'; """, {'target_username': username}) check(password_hash.startswith('$argon2id$v=19$m=32768,t=2,p=1$'), 'stored password value is not the measured Argon2id PHC string') check(password not in password_hash, 'stored password hash contains the plaintext password') check(len(password_hash) > 50, 'stored password hash is unexpectedly short') token_hash = psql(args, database_user, database_name, """ SELECT encode(token_hash, 'hex') FROM sessions WHERE user_id = (SELECT id FROM users WHERE username = :'target_username') ORDER BY created_at DESC LIMIT 1; """, {'target_username': username}) expected = hashlib.sha256(token.encode('ascii')).hexdigest() check(token_hash == expected, 'stored session value is not the SHA-256 of the issued token') check(len(token_hash) == 64, 'stored session hash is not 32 bytes') check(token_hash != token, 'session token itself was stored') log_text = '' for service in ('api', 'nginx'): result = compose(args, 'logs', '--no-color', service) check(result.returncode == 0, 'could not read ' + service + ' logs') log_text += result.stdout + result.stderr check(len(log_text) > 0, 'no API/LB logs were available for the leak check') check(password not in log_text, 'API/LB logs contain the synthetic password') check(token not in log_text, 'API/LB logs contain the synthetic session token') check(password_hash not in log_text, 'API/LB logs contain the stored password hash') check(token_hash not in log_text, 'API/LB logs contain the stored token hash') print('PASS: password and session token are stored only as hashes and are absent from API/LB logs.') if __name__ == '__main__': run(main)