redact_connstr.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """Keyword/value connection-string password masking for agent.redact.
  2. ADO.NET / ODBC / JDBC ``Host=db;Password=…`` and libpq ``host=db password=…`` conninfo; the
  3. URL form (``scheme://user:pass@host``) stays in agent/redact.py.
  4. """
  5. import re
  6. # Keyword/value connection strings (ADO.NET, Npgsql, ODBC, JDBC SQL Server):
  7. # ``Host=db;Username=app;Password=s3cret;Database=x``, and libpq's space-delimited conninfo
  8. # (``psql "host=db user=app password=s3cret dbname=x"``). _DB_CONNSTR_RE only covers the
  9. # ``scheme://user:pass@`` URL form, so a .NET connection string printed by ``kubectl get
  10. # configmap``, ``cat appsettings.json`` or ``env`` leaked its password verbatim.
  11. #
  12. # Every key pattern starts AT the key name behind a fixed-width lookbehind. A leading
  13. # ``(?:^|[;\s])\s*`` rescans runs of whitespace from every start position: quadratic, and
  14. # a 16K-space line took ~30s. Value extents are found by _kv_value_end, not by regex, so
  15. # each grammar (ODBC braces, ADO.NET doubled quotes, libpq backslash escapes) is exact.
  16. _KV_KEY_BOUNDARY = r"(?<![^\s;\"'])"
  17. _KV_CONNSTR_PW_RE = re.compile(
  18. _KV_KEY_BOUNDARY + r"(?:password|pwd)[ \t]*=[ \t]*", re.IGNORECASE)
  19. _KV_CONNSTR_GATE_RE = re.compile(r"(?:password|pwd)\s*=", re.IGNORECASE)
  20. # A ``password=``/``pwd=`` key is only treated as a connection-string field when its line
  21. # also carries a companion connection key (``host=``, ``user=``, ``dbname=``...). Keeps prose
  22. # (``the password= field``), form bodies (handled by _redact_form_body) and lone CLI flags out.
  23. _KV_CONNSTR_KEYS = (
  24. r"host|server|data\s+source|address|addr|network\s+address|user\s*id|uid|"
  25. r"user(?:name)?|database|dbname|hostaddr|initial\s+catalog|port")
  26. _KV_CONNSTR_COMPANION_RE = re.compile(
  27. _KV_KEY_BOUNDARY + r"(?:" + _KV_CONNSTR_KEYS + r")[ \t]*=", re.IGNORECASE)
  28. # ``;``-delimited grammar (ADO.NET/ODBC/JDBC) when a ``;`` is followed by a known field.
  29. # libpq values may contain a bare ``;`` (``password=a;b dbname=x``), so a ``;`` alone
  30. # does not decide the grammar.
  31. _KV_SEMICOLON_FIELD_RE = re.compile(
  32. r";[ \t]*(?:" + _KV_CONNSTR_KEYS + r"|password|pwd|driver|encrypt|pooling|"
  33. r"integrated\s+security|trusted_connection|trustservercertificate|sslmode|ssl\s+mode|"
  34. r"timeout|connect(?:ion)?\s+timeout|application\s+name|search\s+path)[ \t]*=",
  35. re.IGNORECASE)
  36. # ``{0}`` / ``{pw}`` / ``{cfg['pw']}`` / ``{pw!r}`` / ``{ get_pw() }``: a str.format, f-string or C#
  37. # interpolation slot. An ODBC-braced literal is the rest: ``;`` inside, a ``}}`` escape, or content
  38. # that does not open like an expression (``{Zq7x;LmP2}``).
  39. # ``@ $ & #`` and a ``!`` that is not a ``!r``/``!s``/``!a`` conversion never appear in such a slot but
  40. # do in passwords (``{P@ssw0rd!}``, ``{Tr0ub4dor&3}``).
  41. _FORMAT_SLOT_RE = re.compile(r"\{\s*+(?:[A-Za-z0-9_](?:[^;{}@$&#!]|![rsa](?=\s*+[:}]))*+)?\}")
  42. def _kv_value_end(line: str, i: int, semicolon: bool) -> int:
  43. """End index of the value starting at ``line[i]``; the scan never backtracks."""
  44. n = len(line)
  45. if i >= n:
  46. return i
  47. c = line[i]
  48. if semicolon:
  49. if c == "{": # ODBC braced value, ``}}`` escapes a brace
  50. j = i + 1
  51. while j < n:
  52. if line[j] == "}":
  53. if j + 1 < n and line[j + 1] == "}":
  54. j += 2
  55. continue
  56. return j + 1
  57. j += 1
  58. return n
  59. if c in "\"'": # ADO.NET quoting, doubled quote escapes
  60. j = i + 1
  61. while j < n:
  62. if line[j] == c:
  63. if j + 1 < n and line[j + 1] == c:
  64. j += 2
  65. continue
  66. return j + 1
  67. j += 1
  68. return n
  69. j = i
  70. while j < n and line[j] not in ";\"'\r\n":
  71. j += 1
  72. while j > i and line[j - 1] in " \t": # trailing padding stays visible
  73. j -= 1
  74. return j
  75. # libpq conninfo: quoted or unquoted, backslash escapes any character.
  76. if c in "\"'":
  77. j = i + 1
  78. while j < n:
  79. if line[j] == "\\":
  80. j += 2
  81. continue
  82. if line[j] == c:
  83. return j + 1
  84. j += 1
  85. return n
  86. j = i
  87. while j < n:
  88. if line[j] == "\\":
  89. j += 2
  90. continue
  91. if line[j] in " \t\r\n\"'": # quotes: the shell quoting that wraps the conninfo
  92. break
  93. j += 1
  94. return min(j, n)
  95. def _redact_kv_connection_line(line: str, code_file: bool) -> str:
  96. if not _KV_CONNSTR_GATE_RE.search(line) or not _KV_CONNSTR_COMPANION_RE.search(line):
  97. return line
  98. semicolon = _KV_SEMICOLON_FIELD_RE.search(line) is not None
  99. out, pos = [], 0
  100. for m in _KV_CONNSTR_PW_RE.finditer(line):
  101. if m.start() < pos: # inside a value already masked
  102. continue
  103. vstart = m.end()
  104. vend = _kv_value_end(line, vstart, semicolon)
  105. value = line[vstart:vend]
  106. # ``the pwd= flag``: space after ``=`` but not before it is prose, not an empty value followed
  107. # by the next word. Padding on both sides (``password = x``) is still a conninfo field.
  108. eq = line.rindex("=", m.start(), m.end())
  109. if not value or (not semicolon and line[m.end() - 1] in " \t" and line[eq - 1] not in " \t"):
  110. continue
  111. bare = value.strip("\"'")
  112. # Templates, not credentials: ``Password={0}``, ``Password=${DB_PASS}``, ``{pw}``. ODBC braces a
  113. # literal value (``PWD={Zq7x;LmP2}``), so on a ``;`` line only a format slot or an identifier
  114. # counts as a template; ``cat appsettings.json`` output is read as code_file.
  115. if code_file and bare.startswith(("{", "${")) and bare.endswith("}") and (
  116. not semicolon or bare.startswith("${") or _FORMAT_SLOT_RE.fullmatch(bare)):
  117. continue
  118. if value[0] == "{" and semicolon:
  119. masked = "{***}"
  120. elif value[0] in "\"'" and len(value) > 1 and value[-1] == value[0]:
  121. masked = f"{value[0]}***{value[0]}"
  122. else:
  123. masked = "***"
  124. out.append(line[pos:vstart])
  125. out.append(masked)
  126. pos = vend
  127. if not out:
  128. return line
  129. out.append(line[pos:])
  130. return "".join(out)
  131. def _redact_kv_connection_strings(text: str, code_file: bool) -> str:
  132. """Mask ``Password=``/``Pwd=`` values inside keyword/value connection strings, one
  133. companion-key scan per line."""
  134. return "".join(_redact_kv_connection_line(line, code_file)
  135. for line in text.splitlines(keepends=True))