Refactor SDK architecture and harden security guardrails#44
Open
skupriienko-mailgun wants to merge 36 commits into
Open
Refactor SDK architecture and harden security guardrails#44skupriienko-mailgun wants to merge 36 commits into
skupriienko-mailgun wants to merge 36 commits into
Conversation
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Links:
Jira
Actions:
Architecture & Developer Experience (DX):
Completely decoupled the monolithic
client.pyinto highly cohesive, single-responsibility modules (builders.py,config.py,endpoints.py,security.py,types.py, etc.).Introduced
MailgunMessageBuilderandMailgunTemplateBuilderto provide a fluent, autocomplete-friendly way to construct complex payloads (handlingv:,h:, ando:prefixes automatically).Fixed Context Managers (
__enter__/__exit__and__aenter__/__aexit__) forClientandAsyncClientto guarantee underlying TCP connection pools are gracefully closed.Added a
.stream()generator toEndpointandAsyncEndpointfor automatic, memory-safe traversal of cursor-based pagination URLs.Added
dry_run=TrueZero-Leak Sandbox Mode to safely intercept and mock network requests during local development and CI pipelines.Added
TypedDictcontracts (e.g.,SendMessagePayload) to enable IDE autocomplete and compile-time validation viamypy.Completely refactored the
mailgun/examples/directory to cleanly group synchronous and asynchronous equivalents side-by-side using strict context manager patterns.Deleted legacy
mailgun/handlers/utils.py, strictly centralizing path sanitization in the newSecurityGuardclass.Security & Guardrails:
CWE-319 (Protocol Downgrade): Enforced a strict minimum TLS 1.2+ protocol context via
SecureHTTPAdapterandssl_contextconfigurations to prevent MITM downgrade attacks.CWE-316 (Cleartext Storage): Integrated a centralized
RedactingFilterto automatically scrub Mailgun API credentials and webhook secrets from all standard log outputs.CWE-316 (Memory Purging): Explicitly zeroed-out the
authtuples and HTTP headers in memory uponClient.close()andAsyncClient.aclose()teardown.CWE-22 & CWE-400 (Path Traversal & Resource Exhaustion): Implemented strict local attachment guardrails (
SecurityGuard.validate_attachment_pathandcheck_file_size) to block LFI attempts and fail-fast on files exceeding Mailgun's 25MB boundary.CWE-400 (Uncontrolled Resource Consumption): Hardened
sanitize_timeoutto strictly enforce finite, positive integer/float boundaries against malicious timeout injection.Enterprise Security Audit Hooks: Implemented native Python PEP 578
sys.auditevents (e.g.,mailgun.api.request) for Zero-Trust enterprise observability.Path Segment Sanitization: Hardened
SecurityGuard.sanitize_path_segment()to strictly canonicalize and URL-encode inputs, neutralizing edge-case Path Traversal and Injection attacks during dynamic route building.Bug Fixes:
Fixed internal request data formatting and dynamic routing resolution across various API handlers.
Resolved outstanding strict-mode MyPy errors and Ruff linting violations (e.g.,
F401,PLC0415) across the core package and test suites.Testing, CI/CD & Supply Chain:
Vastly expanded SDK test coverage by introducing rigorous unit, integration, regression, Hypothesis (property-based), and Atheris fuzzing tests.
Integrated
osv-scanner, CodeQL,pip-audit, Bandit, and Semgrep into GitHub Actions to automatically block insecure transitive dependencies and validate code security.Verification & Testing:
To verify these changes locally, ensure your environment variables (
APIKEYandDOMAIN, and others) are set, then run the following commands:1. Run the Unit Test Suite (Fast):
Validates the core routing logic, data structures, and mock handlers.
2. Run the Live Routing Meta-Test (No state mutation):
Proves the SDK correctly constructs URLs for all supported endpoints by hitting live Mailgun servers (expects 200, 400, 401, or 403 responses; tests fail if the Python SDK crashes or generates a 404 bad route).
3. Run the Full Integration Suite (State mutation):
Executes end-to-end flows against your Sandbox domain (creates/deletes real resources).
4. Execute the Interactive Smoke Test:
Runs the executable documentation script demonstrating cross-version routing and payload serialization.
5. Run the Performance & Cold-Boot Benchmarks:
Validates the new O(1) routing dispatch and
__slots__memory optimizations using our unified DX script.