Skip to content

Commit e1b2092

Browse files
Stripped hop-by-hop headers from responses.
Co-authored-by: danmoz <danmoz@users.noreply.github.com>
1 parent 577d42e commit e1b2092

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

‎middleware.go‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,40 @@ func (m *Lambda) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.H
197197
bodyBytes = []byte(reply.Body)
198198
}
199199

200+
// Hop-by-hop headers that must not be forwarded from the Lambda reply to
201+
// the downstream client. Content-Length is included because it is managed
202+
// by Go's HTTP server rather than copied through from the reply.
203+
strip := map[string]struct{}{
204+
"connection": {},
205+
"proxy-connection": {},
206+
"keep-alive": {},
207+
"proxy-authenticate": {},
208+
"proxy-authorization": {},
209+
"te": {},
210+
"trailer": {},
211+
"transfer-encoding": {},
212+
"upgrade": {},
213+
"content-length": {},
214+
}
215+
216+
// Headers named by the Connection header are likewise hop-by-hop.
217+
for k, vals := range reply.Meta.Headers {
218+
if !strings.EqualFold(k, "Connection") {
219+
continue
220+
}
221+
for _, v := range vals {
222+
for _, name := range strings.Split(v, ",") {
223+
name = strings.TrimSpace(name)
224+
if name != "" {
225+
strip[strings.ToLower(name)] = struct{}{}
226+
}
227+
}
228+
}
229+
}
230+
200231
// Write the response HTTP headers
201232
for k, vals := range reply.Meta.Headers {
202-
if strings.EqualFold(k, "Content-Length") || strings.EqualFold(k, "Transfer-Encoding") || strings.EqualFold(k, "Connection") {
233+
if _, ok := strip[strings.ToLower(k)]; ok {
203234
continue
204235
}
205236
for _, v := range vals {

‎middleware_test.go‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,58 @@ func TestServeHTTPSkipsBodyForNoBodyStatusesAndFramingHeaders(t *testing.T) {
326326
}
327327
}
328328

329+
func TestServeHTTPStripsHopByHopHeaders(t *testing.T) {
330+
fake := &fakeLambdaInvoker{output: &lambda.InvokeOutput{Payload: []byte(`{
331+
"type":"HTTPJSON-REP",
332+
"meta":{"status":200,"headers":{
333+
"Connection":["keep-alive, X-Nominated"],
334+
"Proxy-Connection":["keep-alive"],
335+
"Keep-Alive":["timeout=5"],
336+
"Proxy-Authenticate":["Basic"],
337+
"Proxy-Authorization":["Basic dXNlcjpwYXNz"],
338+
"TE":["trailers"],
339+
"Trailer":["X-Trailer"],
340+
"Transfer-Encoding":["chunked"],
341+
"Upgrade":["websocket"],
342+
"X-Nominated":["strip-me"],
343+
"Content-Type":["text/plain"],
344+
"X-Custom":["keep"]
345+
}},
346+
"body":"ok"
347+
}`)}}
348+
m := &Lambda{FunctionName: "test-function", EventFormat: eventFormatHTTPJSON, Timeout: caddy.Duration(time.Second), log: zap.NewNop(), svc: fake}
349+
w := httptest.NewRecorder()
350+
351+
if err := m.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/", nil), nil); err != nil {
352+
t.Fatalf("ServeHTTP() error = %v", err)
353+
}
354+
355+
for _, header := range []string{
356+
"Connection",
357+
"Proxy-Connection",
358+
"Keep-Alive",
359+
"Proxy-Authenticate",
360+
"Proxy-Authorization",
361+
"TE",
362+
"Trailer",
363+
"Transfer-Encoding",
364+
"Upgrade",
365+
"Content-Length",
366+
"X-Nominated",
367+
} {
368+
if got := w.Header().Get(header); got != "" {
369+
t.Errorf("%s = %q, want empty", header, got)
370+
}
371+
}
372+
373+
if got := w.Header().Get("Content-Type"); got != "text/plain" {
374+
t.Errorf("Content-Type = %q, want text/plain", got)
375+
}
376+
if got := w.Header().Get("X-Custom"); got != "keep" {
377+
t.Errorf("X-Custom = %q, want keep", got)
378+
}
379+
}
380+
329381
func TestServeHTTPReturnsApplicationErrorStatus(t *testing.T) {
330382
fake := &fakeLambdaInvoker{output: &lambda.InvokeOutput{Payload: []byte(`{
331383
"type":"HTTPJSON-REP",

0 commit comments

Comments
 (0)