Coverage for execution/secret_scan.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 12:55 +0000

1"""Detect secrets that must never be committed (Phase V2-22 security guard). 

2 

3Backs `aq secrets-check` and the `.githooks/pre-commit` hook. Two independent 

4checks, both pure over their inputs so they are trivially testable: 

5 

61. `find_populated_secret_fields()` - flags any field in a parsed `lean.json` 

7 whose NAME looks like a credential (`*-api-key`, `*-password`, `ib-account`, 

8 …) and whose VALUE is non-empty. The tracked `lean.json` is meant to ship 

9 with every such field empty (see execution/lean_config_render.py); a 

10 populated one means real keys are about to be committed. 

112. `is_tracked_env_secret()` - flags a `.env`-style filename that is NOT one of 

12 the committed `*.example` templates. 

13 

14Nothing here touches disk or git - the IO/glue (reading files, asking git what 

15is tracked) lives in aq_cli.py so this module stays pure and unit-testable. 

16Only field NAMES are ever returned, never the secret values. 

17""" 

18 

19from __future__ import annotations 

20 

21from typing import Any 

22 

23# A lean.json key is treated as a secret if it ends with any of these. Covers 

24# the whole stock template's credential surface (api keys/secrets, passwords, 

25# access/refresh tokens, private keys) plus the IB identity fields, which are 

26# not "*-password" suffixed but still must not be published. 

27SECRET_FIELD_SUFFIXES: tuple[str, ...] = ( 

28 "-api-key", 

29 "-api-secret", 

30 "-secret", 

31 "-password", 

32 "-access-token", 

33 "-refresh-token", 

34 "-auth-token", 

35 "-shared-key", 

36 "-private-key-hex", 

37 "-session-password", 

38 "-rest-app-key", 

39 "-rest-app-secret", 

40 "-app-key", 

41) 

42 

43# Exact-match secret fields that don't follow the suffix pattern above. 

44SECRET_FIELD_EXACT: frozenset[str] = frozenset( 

45 { 

46 "ib-account", 

47 "ib-user-name", 

48 "ib-password", 

49 } 

50) 

51 

52 

53def is_secret_field(field: str) -> bool: 

54 """Whether a config field name holds a secret (an exact name or a secret-like suffix).""" 

55 return field in SECRET_FIELD_EXACT or field.endswith(SECRET_FIELD_SUFFIXES) 

56 

57 

58def find_populated_secret_fields(lean_config: dict[str, Any]) -> list[str]: 

59 """Return the sorted names of every secret-looking lean.json field that has 

60 a non-empty string value. Empty template -> empty list.""" 

61 populated = [ 

62 field 

63 for field, value in lean_config.items() 

64 if is_secret_field(field) and isinstance(value, str) and value.strip() 

65 ] 

66 return sorted(populated) 

67 

68 

69def is_tracked_env_secret(filename: str) -> bool: 

70 """True if `filename` is a real `.env` secret file (must not be committed) 

71 rather than one of the committed `*.example` templates. Accepts a bare 

72 filename or a path - only the final component matters.""" 

73 name = filename.replace("\\", "/").rsplit("/", 1)[-1] 

74 if name.endswith(".example"): 

75 return False 

76 return name == ".env" or name.startswith(".env.")