Replace ReverseProxied with werkzeug ProxyFix #126
No reviewers
Labels
No labels
Closed As
Duplicate
Closed As
Fixed
Closed As
Invalid
easyfix
enhancement
ai-review-please
Backlog Status
Needs Review
Backlog Status
Ready
chore
documentation
points
01
points
02
points
03
points
05
points
08
points
13
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Sprint Status
Blocked
Sprint Status
Done
Sprint Status
In Progress
Sprint Status
Review
Sprint Status
To Do
Technical Debt
Work Item
Bug
Work Item
Epic
Work Item
Spike
Work Item
Task
Work Item
User Story
No milestone
No project
No assignees
4 participants
Notifications
Due date
No due date set.
Blocks
#112 Replace
ReverseProxied with werkzeug/middleware/proxy_fix.py
quality/testdays-web
Reference
quality/testdays-web!126
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "feature/112-replace-reverseproxied"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
WIP: Replace ReverseProxied with werkzeug ProxyFixto Replace ReverseProxied with werkzeug ProxyFixAI Code Review
📋 MR Summary
Replaces the custom ReverseProxied middleware with Werkzeug's ProxyFix and adds a custom middleware to deduplicate HAProxy headers.
ReverseProxiedmiddleware.ProxyFixmiddleware configurable viaPROXYFIX_X_*app configurations._ForwardedHeaderFixmiddleware to normalize duplicateX-Forwarded-*headers before ProxyFix execution.Detailed Code Review
The migration to Werkzeug's ProxyFix and the logic for deduplicating HAProxy headers are generally well-implemented. However, there is a critical logical flaw in how the WSGI middleware stack is constructed. WSGI middlewares execute outside-in (the last applied wrapper executes first on incoming requests). The current implementation wraps
_ForwardedHeaderFixwithProxyFix, meaningProxyFixprocesses the raw, duplicated headers before the deduplication logic runs, which contradicts the stated intent.📂 File Reviews
📄 `testdays/app.py` - Adds configuration for Werkzeug ProxyFix and a custom header deduplication middleware.
ProxyFixis applied last, it becomes the outermost middleware and executes first on the request. This meansProxyFixprocesses the duplicated headers before_ForwardedHeaderFixcan normalize them._ForwardedHeaderFixis applied last and becomes the outermost wrapper.📄 `tests/unit/test_proxy_fix.py` - Adds tests verifying the middleware application and header deduplication behavior.
ProxyFixis outermost._ForwardedHeaderFixas the outermost middleware, wrappingProxyFix.✅ Summary
testdays/app.pyso_ForwardedHeaderFixis evaluated beforeProxyFix.test_forwarded_header_fix_in_middleware_stackto assert the corrected middleware execution order._ForwardedHeaderFixto handle edge cases if HAProxy modifies header casing, though not strictly necessary if strictly handling identical duplicates.🤖 AI Code Review | Generated with ai-code-review | Model:
gemini-3.1-pro-preview⚠️ AI-generated suggestions may be incorrect. Verify before applying. Not a replacement for human review.
Replace ReverseProxied with werkzeug ProxyFixto WIP: Replace ReverseProxied with werkzeug ProxyFixWIP: Replace ReverseProxied with werkzeug ProxyFixto Replace ReverseProxied with werkzeug ProxyFixAI Code Review
📋 MR Summary
Replaces a custom
ReverseProxiedmiddleware with Werkzeug's officialProxyFixmiddleware and adds a custom wrapper to handle duplicate header bugs specific to OpenShift HAProxy.ReverseProxiedwithwerkzeug.middleware.proxy_fix.ProxyFix._ForwardedHeaderFixmiddleware to normalize identically duplicatedX-Forwarded-*headers before they reachProxyFix.PROXYFIX_X_*config variables.ProxyFix) for proxy parsing. The custom middleware handles a very specific edge case safely without interfering with standard proxy chains. Security defaults are set strictly.Detailed Code Review
The implementation is solid and addresses the duplicate header issue elegantly. Replacing custom middleware with Werkzeug's
ProxyFiximproves security and standardizes the application's reverse-proxy handling. The execution order of WSGI middleware is correct, ensuring headers are normalized before parsing. Test coverage is thorough and accurately validates both normal operation and edge cases.📂 File Reviews
📄 `testdays/app.py` - Implements the new header-fixing middleware and configures ProxyFix in the app factory.
int()to parse config variables during application startup will result in aValueErrorif environment variables are injected with non-numeric values. While failing fast on startup is acceptable, you might consider using a safe fallback or explicit parsing.✅ Summary
PROXYFIX_X_*environment variables to prevent ugly tracebacks if misconfigured.🤖 AI Code Review | Generated with ai-code-review | Model:
gemini-3.1-pro-preview⚠️ AI-generated suggestions may be incorrect. Verify before applying. Not a replacement for human review.
Replace ReverseProxied with werkzeug ProxyFixto WIP: Replace ReverseProxied with werkzeug ProxyFixI'm not convinced that
_ForwardedHeaderFixis correct/necessary/a good idea. I think usingProxyFixwith correct configuration should avoid the duplicated header issue; the trick is configuring it with exactly the correct number of expected proxies. We should look at other cases where this has been done for a reference.ReverseProxiedwith werkzeug/middleware/proxy_fix.pySetting trusted proxies count to 2 enables correct originating IP values in app access log.
WIP: Replace ReverseProxied with werkzeug ProxyFixto Replace ReverseProxied with werkzeug ProxyFixAI Code Review
📋 MR Summary
This MR replaces the custom
ReverseProxiedmiddleware with Werkzeug's standardProxyFixmiddleware to better handle reverse proxy headers in the OpenShift deployment. It also introduces configuration variables to control the trust levels for variousX-Forwarded-*headers.ReverseProxiedclass and its ad-hoc duplicate header fixing logic.ProxyFixmiddleware wrapped around the WSGI application whenIS_OPENSHIFTis enabled.PROXYFIX_X_*configuration values with default trust levels (e.g.,2for FOR and PROTO).ProxyFixinitialization, middleware ordering, and configuration parsing.Detailed Code Review
The implementation cleanly migrates away from custom middleware to Werkzeug's standard
ProxyFix, which is the recommended approach for Flask/Werkzeug applications behind a reverse proxy.One minor observation is that while the PR description and context notes mention a
DuplicateHeaderFixmiddleware, the actual diff relies onProxyFixnatively handling the HAProxy append mode (as noted in the updated inline comments). This is generally acceptable asProxyFixparses comma-separated lists correctly according to RFCs.Ensure that environment variables mapping to
PROXYFIX_X_*configurations are always valid integer strings, as the currentint(app.config[...])casting will raise aValueErrororTypeErrorif they are empty strings orNonein overridden settings.📂 File Reviews
📄 `testdays/app.py` - Replaces the custom proxy middleware with Werkzeug's ProxyFix and wires up the configuration.
intwithout fallback or validation can crash the app on startup if a user overrides these with invalid strings (e.g., empty strings from env vars) orNone.app.config.get(..., default_value)and safely casting tointto ensure application resilience against misconfiguration.✅ Summary
ProxyFixis solidly implemented with proper testing. No critical issues were identified.PROXYFIX_X_*configuration values to integers to prevent startup crashes on misconfiguration.DuplicateHeaderFixmiddleware was dropped in favor of nativeProxyFixlist parsing.🤖 AI Code Review | Generated with ai-code-review | Model:
gemini-3.1-pro-preview⚠️ AI-generated suggestions may be incorrect. Verify before applying. Not a replacement for human review.
Re AI Review: defaults are set in Config, no need to duplicate elsewhere. Startup crash on misconfiguration with wildly incorrect values is actually preferred here instead of silent fallback to defaults.
Ready for review
OK. Putting it in config is good because this is deployment-dependent. In theory if someone wanted to run testdays-web in some other context, they could/should change it. Thanks.
(I don't intend to review this)
c88196b355d807212543d8072125435797f0ed5e