Skip to content

fix(security): harden credential handling, concurrency, validation, and retry mechanisms - #8

Merged
lv10 merged 1 commit into
mainfrom
security/harden-api-and-fix-badges
Aug 24, 2026
Merged

fix(security): harden credential handling, concurrency, validation, and retry mechanisms#8
lv10 merged 1 commit into
mainfrom
security/harden-api-and-fix-badges

Conversation

@lv10

@lv10 lv10 commented Aug 24, 2026

Copy link
Copy Markdown
Owner

Summary of Changes

1. Credential & Secret Protection (CWE-532)

  • Added repr=False to OAuthToken.access_token and implemented masked __repr__ methods across OAuthToken, OAuthTokenManager, SigV4Signer, AmazonCreatorsAPI, AsyncAmazonCreatorsAPI, AmazonPAAPI5, AsyncAmazonPAAPI5, AmazonAPI, and AsyncAmazonAPI.
  • Prevents accidental leak of sensitive credentials (credential_secret, secret_key, access_token) in interactive sessions, logging, and error tracebacks.

2. Concurrency & Race Condition Elimination (CWE-362 / CWE-662)

  • Added thread-safe lazy initialization for asyncio.Lock in OAuthTokenManager.
  • Synchronized _cached_token reading and updating across synchronous and asynchronous execution paths and cache clearing.

3. HTTP Client Lifecycle & Resource Safety (CWE-400 / CWE-775)

  • Wrapped lazy .client property initialization with mutex locks to prevent race conditions and duplicate orphaned client allocations.
  • Maintained clean closing in context managers.

4. Thundering Herd Defense & Retry Hardening (CWE-399 / CWE-799)

  • Implemented calculate_backoff() with full jitter (random.uniform) to prevent synchronized retry storms.
  • Added parse_retry_after() to extract and respect HTTP 429 and transient 502/503/504 Retry-After headers (integer seconds and RFC 7231 HTTP-date).

5. Operation Allowlisting & SSRF Protection (CWE-20 / CWE-88)

  • Added strict operation and endpoint allowlists (ALLOWED_CREATORS_ENDPOINTS and ALLOWED_PAAPI5_OPERATIONS) in _execute_request to reject path traversal and header/operation injection attempts.

6. Client-Side Input Validation (CWE-20 / CWE-1284)

  • Added client-side validation to reject empty/whitespace ASINs, invalid bounds on item_count (1–10), item_page (1–10), min_reviews_rating (1–5), min_saving_percent (1–100), and negative prices before making remote requests.

7. Bounded Error Response Memory (CWE-400)

  • Truncated raw HTTP error response text capture in map_http_error() to 4096 characters to prevent memory exhaustion and log bloat from massive error responses.

8. CI/CD & Documentation Hardening (CWE-250)

  • Added permissions: contents: read to .github/workflows/ci.yml.
  • Updated the Shields.io badge in README.md to accurately reflect Python 3.9 through 3.13 support.

Test & Validation Results

  • Pytest: 73 passed (89% test coverage), including 15 new security-specific unit tests.
  • Ruff: All checks passed.
  • Mypy: Success, no issues found across 16 source files.

…nd retry backoff

- Mask credentials and secrets in __repr__ across client, auth, and token models (CWE-532)
- Eliminate race conditions in OAuthTokenManager lock and cache synchronization (CWE-362/662)
- Protect HTTP client initialization with mutexes to prevent socket descriptor leaks (CWE-400/775)
- Implement jittered exponential backoff and Retry-After header parsing for 429/5xx (CWE-399/799)
- Enforce strict operation allowlists and parameter boundary validation (CWE-20/88/1284)
- Bound error response memory capture in exception handlers (CWE-400)
- Add least-privilege permissions to CI workflow (CWE-250)
- Update README Python version badge to show Python 3.9-3.13+ support
- Add 15 new security-focused unit tests
@lv10
lv10 merged commit 76fee91 into main Aug 24, 2026
15 checks passed
@lv10
lv10 deleted the security/harden-api-and-fix-badges branch August 24, 2026 00:05
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.

1 participant