fix: detect HTTPS via CloudFront-injected header instead of ProxyFix#7
Merged
Conversation
The previous ProxyFix(x_proto=2) attempt was based on the assumption that ALB *appends* to X-Forwarded-Proto (so the value at the app would be "https,http"). It doesn't — ALB with an HTTP listener *replaces* X-Forwarded-Proto with "http", clobbering whatever CloudFront sent. The header at the app is just "http" (single value), which is why ProxyFix's _get_real_value returned None and the WSGI scheme stayed http even after the previous "fix" deployed. Approach: have CloudFront inject an immutable `X-Forwarded-Scheme: https` custom origin header. Custom name (not X-Forwarded-Proto) sidesteps ALB's rewrite — ALB has no special handling for that name and passes it through verbatim. Hard-coding "https" is safe because viewer_protocol_policy="redirect-to-https" guarantees every request reaching origin came from an HTTPS viewer. App side replaces ProxyFix with a small WSGI middleware that flips wsgi.url_scheme when the header is present, so url_for(_external=True) emits https URLs (the OIDC redirect_uri Keycloak validates). Deploy order: terraform apply first (or the app waits for the header that isn't there yet); both must land for login to succeed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
✅ Terraform PlanShow plan |
There was a problem hiding this comment.
Pull request overview
Updates the production deployment to reliably generate https:// external URLs (notably OIDC redirect_uri) when running behind CloudFront → ALB(HTTP) → ECS, by using a CloudFront-injected header rather than relying on ProxyFix and X-Forwarded-Proto.
Changes:
- Add a CloudFront origin custom header
X-Forwarded-Scheme: httpsto preserve viewer scheme past the ALB. - Replace
ProxyFixwith a small WSGI middleware that flipswsgi.url_schemetohttpswhen the CloudFront header is present.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| terraform/main/modules/cloudfront/main.tf | Injects X-Forwarded-Scheme: https as an origin custom header so the app can detect HTTPS deterministically. |
| app/init.py | Swaps ProxyFix for a targeted middleware that sets wsgi.url_scheme based on the CloudFront header. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The docstring pointed at modules/cloudfront/main.tf, but the file lives at terraform/main/modules/cloudfront/main.tf. Updated so the cross-reference resolves from the repo root. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.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.
Why
PR #6's ProxyFix-based fix was based on a wrong assumption about ALB behaviour and didn't actually flip the scheme. The OIDC redirect_uri the app generates is still `http://librarian.services.keyholding.com/auth/callback\` (the path part is fixed, the scheme isn't), and Keycloak rejects it with `Invalid parameter: redirect_uri`.
What was wrong
I assumed ALB would append to `X-Forwarded-Proto` like it does to `X-Forwarded-For`, producing `https,http` at the app, with `ProxyFix(x_proto=2)` picking the leftmost (`https`) value.
Actually: ALB with an HTTP listener replaces `X-Forwarded-Proto` with `http`, clobbering whatever CloudFront sent. The header at the app is just `http` (single value). `_get_real_value` returns `None` when `len(values) < trusted`, so ProxyFix did nothing — the WSGI scheme stayed `http`. Silent no-op, which is why the previous deploy looked like it worked but didn't.
Fix
Two parts that must both ship:
Why a custom header instead of fixing ProxyFix
There's no value of `x_proto` that makes ProxyFix work in this stack. ALB strips/replaces `X-Forwarded-Proto` regardless of upstream value, so the app never sees CloudFront's `https`. Reading from a non-standard header CloudFront injects directly is the deterministic fix; tuning ProxyFix can't get there.
Deploy sequence
```bash
1. Apply the CloudFront change so the header gets injected
cd terraform/main/envs/services
terraform apply
CloudFront edge propagation: 5–15 min
2. Force ECS to roll new tasks with the updated image
aws ecs update-service \
--region eu-west-1 \
--cluster librarian-services \
--service librarian-services-api \
--force-new-deployment
```
Either order is non-breaking; both must land before login goes green.
Test plan
🤖 Generated with Claude Code