fix(usage-export): mask acronym-cased and unseparated credential names in exported metadata

This commit is contained in:
jesus 2026-11-06 03:43:06 +00:00
parent 4c4fe6e296
commit b3bab0065f
4 changed files with 49 additions and 4 deletions

View file

@ -31,7 +31,7 @@ _DEFAULT_SENSITIVE_PATTERNS: Final = frozenset(
)
_CAMEL_BOUNDARY: Final = re.compile(r"(?<=[a-z0-9])(?=[A-Z])")
_CAMEL_BOUNDARY: Final = re.compile(r"(?<=[a-z0-9])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])")
def _object_items(data: Mapping[str, object]) -> ItemsView[str, object]:
@ -92,7 +92,7 @@ class SensitiveDataMasker:
# Split on underscores and hyphens (and, when opted in, camelCase boundaries too) and check whether any
# segment matches the pattern. This avoids false positives like "max_tokens" matching "token"
# but still catches "api_key", "access_token" and, with split_camel_case, "apiKey" and "clientSecret".
# but still catches "api_key", "access_token" and, with split_camel_case, "apiKey", "APIKey" and "AWSSecret".
key_lower: Final = str(key).lower().replace("-", "_")
camel_lower: Final = (
_CAMEL_BOUNDARY.sub("_", str(key)).lower().replace("-", "_") if self.split_camel_case else key_lower

View file

@ -143,7 +143,31 @@ def _daily_rows_table(
raise AssertionError("unreachable")
_EXPORT_METADATA_MASKER: Final = SensitiveDataMasker(non_sensitive_overrides=frozenset(), split_camel_case=True)
_EXPORT_UNSEPARATED_CREDENTIAL_NAMES: Final[frozenset[str]] = frozenset(
{
"apikey",
"apitoken",
"accesskey",
"accesstoken",
"authkey",
"authtoken",
"bearertoken",
"clientsecret",
"passwd",
"privatekey",
"pwd",
"refreshtoken",
"secretkey",
"sessiontoken",
"signingkey",
"webhooksecret",
}
)
_EXPORT_METADATA_MASKER: Final = SensitiveDataMasker(
non_sensitive_overrides=frozenset(),
extra_sensitive_patterns=_EXPORT_UNSEPARATED_CREDENTIAL_NAMES,
split_camel_case=True,
)
def _redacted_metadata(metadata: Mapping[str, object]) -> Mapping[str, object]:

View file

@ -614,7 +614,20 @@ def test_redact_credentials_in_payload_leaves_a_realistic_tool_schema_intact():
assert result["api_key"] == "REDACTED"
@pytest.mark.parametrize("key", ["apiKey", "clientSecret", "accessToken", "secretAccessKey", "x-apiKey"])
@pytest.mark.parametrize(
"key",
[
"apiKey",
"clientSecret",
"accessToken",
"secretAccessKey",
"x-apiKey",
"APIKey",
"OpenAIAPIKey",
"AWSSecret",
"XAPIKey",
],
)
def test_split_camel_case_opt_in_catches_camel_case_credential_names(key: str):
assert SensitiveDataMasker(split_camel_case=True).is_sensitive_key(key) is True

View file

@ -693,6 +693,10 @@ async def test_export_redacts_credential_named_values_inside_metadata_objects()
"owners": [{"name": "ops", "password": "pw"}],
"scim_roles": ["admin"],
"langfuse": {"apiKey": "lf", "logging": [{"callback_vars": {"langfuse_secret_key": "s"}}]},
"OpenAIAPIKey": "sk-acronym",
"apikey": "sk-unseparated",
"APIKEY": "sk-upper",
"passwd": "pw-short",
},
"user_metadata": {
"department": "research",
@ -716,6 +720,10 @@ async def test_export_redacts_credential_named_values_inside_metadata_objects()
"webhook": {"url": "https://hooks.example", "auth_token": "REDACTED", "clientSecret": "REDACTED"},
"owners": [{"name": "ops", "password": "REDACTED"}],
"langfuse": {"apiKey": "REDACTED"},
"OpenAIAPIKey": "REDACTED",
"apikey": "REDACTED",
"APIKEY": "REDACTED",
"passwd": "REDACTED",
}
assert rows[0].user_metadata == {
"department": "research",