backend: headlamp: filter sensitive headers in external proxy - #5680
backend: headlamp: filter sensitive headers in external proxy#5680ayushmaan-16 wants to merge 10 commits into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: ayushmaan-16 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
9a228dc to
d1d5908
Compare
illume
left a comment
There was a problem hiding this comment.
Thanks for working on this.
The commit messages could use some tidying up to match our contribution guidelines. We use Linux kernel style — the contributing guide has the details, and git log shows good examples.
Commits that need attention
backend: headlamp: filter sensitive headers in external proxy— Description must start with a capital letter — e.g.frontend: HomeButton: Fix the buttonnotfrontend: HomeButton: fix the button.
Commit guidelines
- Use atomic commits focused on a single change.
- Use the title format
<area>: <Description of changes>— description must start with a capital letter. - Keep the title under 72 characters (soft requirement).
- Explain the intention and why the change is needed.
- Make commit titles meaningful and describe what changed.
- Do not add code that a later commit rewrites; squash or reorder commits instead.
- Do not include
Fixes #NNin commit messages.
Good examples:
frontend: HomeButton: Fix so it navigates to homebackend: config: Add enable-dynamic-clusters flag
There was a problem hiding this comment.
Pull request overview
This PR hardens the backend /externalproxy endpoint to reduce credential/header leakage when proxying browser requests to allowed external services (e.g., ArtifactHub).
Changes:
- Added header filtering in the external proxy request path to avoid forwarding sensitive headers.
- Added a regression test intended to ensure sensitive headers are stripped while normal headers are preserved.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| backend/cmd/headlamp.go | Filters selected request headers before forwarding to the external proxy target. |
| backend/cmd/headlamp_test.go | Adds a regression test covering external proxy header filtering behavior. |
Comments suppressed due to low confidence (1)
backend/cmd/headlamp_test.go:2678
- The regression test uses "X-Headlamp-Backend-Token"/"X-Headlamp-Custom" (hyphenated) and only asserts for the "X-HEADLAMP-" prefix, but the backend’s sensitive header is actually "X-HEADLAMP_BACKEND-TOKEN" (underscore). Update the test inputs/assertions so it fails if underscore-form internal headers are forwarded, and consider asserting that internal control headers like "proxy-to"/"Forward-to" are not forwarded either.
// Set sensitive headers that should be filtered
req.Header.Set("Authorization", "Bearer sensitive-token")
req.Header.Set("Cookie", "session=sensitive-cookie")
req.Header.Set("X-Headlamp-Backend-Token", "sensitive-backend-token")
req.Header.Set("X-Headlamp-Custom", "sensitive-custom-header")
illume
left a comment
There was a problem hiding this comment.
Thanks for the contribution.
Could you take a look at the commit messages in this PR? We follow a Linux kernel style for git commits — see the contributing guide and git log for examples.
Commits that need attention
backend: headlamp: filter sensitive headers in external proxy— Description must start with a capital letter — e.g.frontend: HomeButton: Fix the buttonnotfrontend: HomeButton: fix the button.
Commit guidelines
- Use atomic commits focused on a single change.
- Use the title format
<area>: <Description of changes>— description must start with a capital letter. - Keep the title under 72 characters (soft requirement).
- Explain the intention and why the change is needed.
- Make commit titles meaningful and describe what changed.
- Do not add code that a later commit rewrites; squash or reorder commits instead.
- Do not include
Fixes #NNin commit messages.
Good examples:
frontend: HomeButton: Fix so it navigates to homebackend: config: Add enable-dynamic-clusters flag
The backend test job in CI is failing. Run cd backend && go test ./... to reproduce the errors locally.
How to run the backend tests
Run cd backend && go test ./... to see all failures. Fix the failing tests and commit the result.
0a4bc5a to
860056e
Compare
illume
left a comment
There was a problem hiding this comment.
Thanks for working on this.
Looks like there are git conflicts in this PR. Can you fix them up?
How to resolve conflicts
Rebase or merge the latest main into your branch, resolve the conflicts, and push the updated branch.
Would you mind addressing the open Copilot review comments? Please mark each comment as resolved after addressing it.
6d1dbd0 to
eb76287
Compare
Prevent external proxy requests from forwarding sensitive auth. Filter internal routing headers before proxying upstream. Add a regression test for Headlamp headers and preserved unrelated headers.
Signed-off-by: ayushmaan-16 <ayushmaan.sharma911@gmail.com>
Signed-off-by: ayushmaan-16 <ayushmaan.sharma911@gmail.com>
…m URLs Signed-off-by: ayushmaan-16 <ayushmaan.sharma911@gmail.com>
Signed-off-by: ayushmaan-16 <ayushmaan.sharma911@gmail.com>
Signed-off-by: ayxsh_shxrma <ayushmaan.sharma911@gmail.com>
Signed-off-by: ayxsh_shxrma <ayushmaan.sharma911@gmail.com>
f37af33 to
bcdab72
Compare
skoeva
left a comment
There was a problem hiding this comment.
the commits can be squashed using git rebase -i
illume
left a comment
There was a problem hiding this comment.
Thanks again for this.
Can you please add to the PR description:
- How a user can use this? How can we test this manually?
- What are the backwards compatibility implications of this?
I asked for a few things to be documented so this is easier to understand and test.
However, this existing headlamp.go code that was there already is quite messy, and it seems this feature is also not documented well (or almost at all really). So I think it might be worth extracting this now into a separate module first before modifying it. Are you up for that? If not that's ok, we can create an issue and maybe someone else can take on that first.
What do you think?
cheers!
| return ip != nil && ip.IsLoopback() | ||
| } | ||
|
|
||
| func shouldFilterExternalProxyRequestHeader(headerName string) bool { |
There was a problem hiding this comment.
We're trying to move everything out of headlamp.go into separate packages/files.
Can you please move these new code into a separate package?
| } | ||
| } | ||
|
|
||
| func externalProxyConnectionHeaderTokens(headers http.Header) map[string]struct{} { |
There was a problem hiding this comment.
Can you please add documentation to the new functions?
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: ayushmaan-16 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
@ayushmaan-16 — happy to take this: extract externalproxy into a module and carry forward the header/redirect fixes + docs in one PR (or extract first). OK if I open that? |
Description
Headlamp's external proxy previously forwarded all incoming headers from the user's browser directly to external services (like ArtifactHub) and mishandled HTTP redirects by dropping their
Locationheaders. This posed a security risk (leaking sensitive credentials) and broke functionality for targets utilizing HTTP→HTTPS redirects.I have fixed these issues by implementing a comprehensive header filtering mechanism and an allowlist-enforced redirect follower.
Changes Made
backend/cmd/headlamp.go:/externalproxyhandler to explicitly sanitize incoming headers before forwarding the request. It now strictly removes:Authorization,Proxy-Authorization, andCookie.X-HEADLAMP-,proxy-to, andForward-to.Connection,Upgrade,Transfer-Encoding,Keep-Alive,Proxy-Connection,TE,Trailer,Accept-Encoding, as well as any dynamic header explicitly listed in the incomingConnectionheader.3xxredirects only if the new redirect target matches the configuredProxyURLsallowlist. Un-followable3xxresponses are explicitly rejected (returning a 502 Bad Gateway) to prevent returning broken redirects to the frontend.backend/cmd/headlamp_test.go:TestExternalProxyHeaderFilteringto confirm that sensitive and hop-by-hop headers are removed while normal headers are successfully passed through.TestExternalProxyDoesNotFollowRedirectsto verify that disallowed redirects are explicitly blocked.TestExternalProxyFollowsAllowedRedirectsto ensure valid allowlist redirects are transparently followed.How to Test
Run the following commands to verify the comprehensive header filtering and the new redirect handling logic: