fix(continuous-learning-v2): redact numeric DSN passwords cut at the truncation boundary

This commit is contained in:
Wen committed 2026-10-27 16:52:44 -03:00
1 parent 64f28b6096
commit 7d44734328
2 files changed
+22 -6

No files matched your search

@@ -283,11 +283,12 @@ _SECRET_RE = re.compile(
# on a literal prefix and uses bounded or non-overlapping classes (#2278).
# Tokens may follow a JSON escape (\n, \t, \uXXXX) since dict I/O is encoded
# before scrubbing; a DSN cut by truncation before its "@" ends at \Z,
# except a bare trailing host:port such as http://localhost:3000 (#3568).
# except a bare trailing host:port after http(s):// such as http://localhost:3000;
# any other scheme (postgres://app:1234) is redacted even when the password is numeric (#3568).
_SECRET_VALUE_RE = re.compile(
r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)"
r"|(?:(?<![A-Za-z0-9_])|(?<=\\[bfnrt])|(?<=\\u[0-9a-fA-F]{4}))(?:gh[pousr]_[A-Za-z0-9]{20,255}|github_pat_[A-Za-z0-9_]{20,255}|(?:AKIA|ASIA)[A-Z0-9]{16})\b"
r"""|(?<=://)([^\s:/@"\\]{1,256}:)(?!\d{1,5}\Z)[^\s@/"\\]{1,256}(?=@|\Z)"""
r"""|(?<=://)(?:(?<=https://)|(?<=http://)|(?<!https://)(?<!http://)())([^\s:/@"\\]{1,256}:)(?(1)|(?!\d{1,5}\Z))[^\s@/"\\]{1,256}(?=@|\Z)"""
)
import signal
@@ -302,7 +303,7 @@ except Exception:
raw = sys.stdin.read()[:2000]
raw = _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", raw)
raw = _SECRET_VALUE_RE.sub(lambda m: (m.group(1) or "") + "[REDACTED]", raw)
raw = _SECRET_VALUE_RE.sub(lambda m: (m.group(2) or "") + "[REDACTED]", raw)
print(json.dumps({"timestamp": os.environ["TIMESTAMP"], "event": "parse_error", "raw": raw}))
' >> "$OBSERVATIONS_FILE"
exit 0
@@ -367,18 +368,19 @@ _SECRET_RE = re.compile(
# on a literal prefix and uses bounded or non-overlapping classes (#2278).
# Tokens may follow a JSON escape (\n, \t, \uXXXX) since dict I/O is encoded
# before scrubbing; a DSN cut by truncation before its "@" ends at \Z,
# except a bare trailing host:port such as http://localhost:3000 (#3568).
# except a bare trailing host:port after http(s):// such as http://localhost:3000;
# any other scheme (postgres://app:1234) is redacted even when the password is numeric (#3568).
_SECRET_VALUE_RE = re.compile(
r"-----BEGIN[A-Z ]{0,40}PRIVATE KEY-----[\s\S]*?(?:-----END[A-Z ]{0,40}PRIVATE KEY-----|\Z)"
r"|(?:(?<![A-Za-z0-9_])|(?<=\\[bfnrt])|(?<=\\u[0-9a-fA-F]{4}))(?:gh[pousr]_[A-Za-z0-9]{20,255}|github_pat_[A-Za-z0-9_]{20,255}|(?:AKIA|ASIA)[A-Z0-9]{16})\b"
r"""|(?<=://)([^\s:/@"\\]{1,256}:)(?!\d{1,5}\Z)[^\s@/"\\]{1,256}(?=@|\Z)"""
r"""|(?<=://)(?:(?<=https://)|(?<=http://)|(?<!https://)(?<!http://)())([^\s:/@"\\]{1,256}:)(?(1)|(?!\d{1,5}\Z))[^\s@/"\\]{1,256}(?=@|\Z)"""
)
def scrub(val):
if val is None:
return None
val = _SECRET_RE.sub(lambda m: m.group(1) + m.group(2) + (m.group(3) or "") + "[REDACTED]", str(val))
return _SECRET_VALUE_RE.sub(lambda m: (m.group(1) or "") + "[REDACTED]", val)
return _SECRET_VALUE_RE.sub(lambda m: (m.group(2) or "") + "[REDACTED]", val)
if parsed["input"]:
observation["input"] = scrub(parsed["input"])
@@ -126,6 +126,20 @@ if (process.platform === 'win32') {
});
}
// Numeric password cut right before '@' (or at end of text) must not be mistaken for host:port.
const numPad = (end) => 'a'.repeat(end - ' postgres://app:1234'.length) + ' postgres://app:1234';
for (const [label, phase, input, expectedEvent] of [
['string tool output', 'post', { tool_name: 'Bash', tool_input: { command: 'env' },
tool_response: numPad(5000) }],
['parse-error fallback', 'post', JSON.stringify([numPad(2000 - 2)]), 'parse_error']
]) {
test(`redacts numeric DSN password cut at the truncation boundary in ${label}`, () => {
const records = capture(phase, input, expectedEvent);
assert.ok(!records.includes('app:1234'), 'numeric DSN password persisted');
assert.ok(records.includes('postgres://app:[REDACTED]'));
});
}
// Ordinary code, URLs without credentials and SSH remotes stay readable.
const benign = [
'const tokenizer = new Tokenizer(config);',