Skip to content

fix: sanitize API error responses to prevent credential leakage#34

Open
prabhakarpujeri wants to merge 1 commit intohuggingface:mainfrom
prabhakarpujeri:fix/sanitize-error-responses
Open

fix: sanitize API error responses to prevent credential leakage#34
prabhakarpujeri wants to merge 1 commit intohuggingface:mainfrom
prabhakarpujeri:fix/sanitize-error-responses

Conversation

@prabhakarpujeri
Copy link
Copy Markdown

Summary

  • Add _sanitize_response() function to prevent credential leakage in error messages
  • Truncate long API error responses to 500 characters
  • Redact sensitive patterns (bearer tokens, API keys, secrets, passwords)

Problem

API error responses were included directly in exception messages without sanitization. If the API returned tokens, credentials, or user data in error responses, this information could be logged or displayed to users.

Solution

Added a sanitization function in client.py that:

  1. Truncates responses longer than 500 characters
  2. Redacts patterns matching:
    • Bearer tokens
    • Token fields in JSON
    • API keys
    • Secrets
    • Passwords

Applied to all three locations where response.text was used:

  • Line 139: JSONDecodeError handling
  • Line 152: Fallback error message from response text
  • Line 170: APIError response parameter

Changes

  • dell_ai/client.py - Added _sanitize_response() function and applied it to error handling
  • tests/unit/test_client.py - Added 14 new tests for sanitization logic

Test plan

  • All 93 tests pass (11 new tests added)
  • Test coverage increased to 80%
  • Verified truncation works correctly
  • Verified sensitive patterns are redacted
  • Verified normal error messages are unchanged

Fixes #30

Added _sanitize_response() function that:
- Truncates long responses to 500 characters max
- Redacts sensitive patterns (bearer tokens, API keys, secrets, passwords)
- Applied to all error handling paths in _make_request()

This prevents potential credential exposure in logs, CLI output,
and monitoring tools when API error responses contain sensitive data.

Fixes huggingface#30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Security] API error responses could leak sensitive data

1 participant