fix(proxy): use Header.Set instead of Header.Add to overwrite backend request headers#240
Open
wthrajat wants to merge 1 commit into
Open
fix(proxy): use Header.Set instead of Header.Add to overwrite backend request headers#240wthrajat wants to merge 1 commit into
Header.Set instead of Header.Add to overwrite backend request headers#240wthrajat wants to merge 1 commit into
Conversation
Author
|
From the official docs: // Add adds the key, value pair to the header.
// It appends to any existing values associated with key.
// The key is case insensitive; it is canonicalized by
// [CanonicalHeaderKey].
func (h Header) Add(key, value string) {
textproto.MIMEHeader(h).Add(key, value)
}// Set sets the header entries associated with key to the
// single element value. It replaces any existing values
// associated with key. The key is case insensitive; it is
// canonicalized by [textproto.CanonicalMIMEHeaderKey].
// To use non-canonical keys, assign to the map directly.
func (h Header) Set(key, value string) {
textproto.MIMEHeader(h).Set(key, value)
}The comment here https://github.com/lightninglabs/aperture/blob/master/proxy/proxy.go#L540-L541 says // Now overwrite header fields of the client request
// with the fields from the configuration file.to overwrite the header field, so we should use |
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.
description
Aperture allows configuring custom backend headers under the services section of
aperture.yaml. For example, setting the upstream authentication credential:Currently, in
proxy/proxy.go, when Aperture forwards a client request to the target backend, it appends these configured headers usingreq.Header.Add(name, value).If a client sends a request containing credentials (such as an
Authorization: L402 ...orAuthorization: LSAT ...header), the outgoing request forwarded to the target backend will accumulate both values. It contains:issue
Many strict API gateways and WAFs (such as Cloudflare protecting
api.openai.comfor example) will reject HTTP requests containing multiple conflicting Authorization header values, immediately dropping the request with a 400 Bad Request.steps to reproduce
api.openai.com) with a configuredAuthorization: Bearer <key>header.Authorization: Bearer <key>header. The upstream server returns 200 OK.fix
We can use
req.Header.Set(name, value)instead.This correctly replaces/overwrites any client side credentials with the configured upstream credentials before the request leaves the gateway, matching the comment's stated intent: "Now overwrite header fields of the client request with the fields from the configuration file."
I was working on a small project to understand L402 better when I came across this issue. As a workaround, I'm using this script as of now https://github.com/wthrajat/autocommit-l402/blob/main/aperture_sanitizer.go which does the job.
If not this workaround, then re-compiling
apertureusing this branchwthrajat:fix-headersfixes the issue.