diff --git a/modules/caddyhttp/templates/templates.go b/modules/caddyhttp/templates/templates.go index de847f667..48ae0dc7c 100644 --- a/modules/caddyhttp/templates/templates.go +++ b/modules/caddyhttp/templates/templates.go @@ -497,6 +497,16 @@ func (t *Templates) executeTemplate(rr caddyhttp.ResponseRecorder, r *http.Reque err := ctx.executeTemplateInBuffer(r.URL.Path, rr.Buffer()) if err != nil { + // Delete some headers that may have been set by the underlying + // handler (such as file_server) which may break the error response. + rr.Header().Del("Content-Length") + rr.Header().Del("Content-Type") + rr.Header().Del("Etag") + rr.Header().Del("Last-Modified") + rr.Header().Del("Accept-Ranges") + rr.Header().Del("Content-Encoding") + rr.Header().Del("Content-Range") + // templates may return a custom HTTP error to be propagated to the client, // otherwise for any other error we assume the template is broken if handlerErr, ok := errors.AsType[caddyhttp.HandlerError](err); ok { diff --git a/modules/caddyhttp/templates/templates_test.go b/modules/caddyhttp/templates/templates_test.go new file mode 100644 index 000000000..7cea7f120 --- /dev/null +++ b/modules/caddyhttp/templates/templates_test.go @@ -0,0 +1,65 @@ +// Copyright 2015 Matthew Holt and The Caddy Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package templates + +import ( + "errors" + "net/http" + "net/http/httptest" + "testing" + + "github.com/caddyserver/caddy/v2/modules/caddyhttp" +) + +func TestServeHTTPErrorDeletesFileHeaders(t *testing.T) { + for i, tc := range []struct { + body string + wantStatus int + }{ + {body: `{{httpError 404}}`, wantStatus: http.StatusNotFound}, + {body: `{{nosuchfunc}}`, wantStatus: http.StatusInternalServerError}, + {body: `{{include "missing.html"}}`, wantStatus: http.StatusInternalServerError}, + } { + tmpl := &Templates{MIMETypes: defaultMIMETypes} + next := caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.Header().Set("Content-Length", "123") + w.Header().Set("Etag", `"abc"`) + w.Header().Set("Last-Modified", "Mon, 02 Jan 2006 15:04:05 GMT") + w.Header().Set("Accept-Ranges", "bytes") + w.Header().Set("Content-Encoding", "gzip") + w.Header().Set("Content-Range", "bytes 0-122/456") + _, err := w.Write([]byte(tc.body)) + return err + }) + + w := httptest.NewRecorder() + r := httptest.NewRequest(http.MethodGet, "/index.html", nil) + + err := tmpl.ServeHTTP(w, r, next) + handlerErr, ok := errors.AsType[caddyhttp.HandlerError](err) + if !ok { + t.Fatalf("Test %d: expected a handler error, got %v", i, err) + } + if handlerErr.StatusCode != tc.wantStatus { + t.Errorf("Test %d: expected status %d, got %d", i, tc.wantStatus, handlerErr.StatusCode) + } + for _, name := range []string{"Content-Length", "Content-Type", "Etag", "Last-Modified", "Accept-Ranges", "Content-Encoding", "Content-Range"} { + if got := w.Header().Get(name); got != "" { + t.Errorf("Test %d: expected %s to be deleted, got %q", i, name, got) + } + } + } +} diff --git a/modules/caddyhttp/templates/tplcontext.go b/modules/caddyhttp/templates/tplcontext.go index e7222183f..8f4c3b63a 100644 --- a/modules/caddyhttp/templates/tplcontext.go +++ b/modules/caddyhttp/templates/tplcontext.go @@ -473,6 +473,8 @@ func (c TemplateContext) funcHTTPError(statusCode int) (bool, error) { c.RespHeader.Header.Del("Etag") c.RespHeader.Header.Del("Last-Modified") c.RespHeader.Header.Del("Accept-Ranges") + c.RespHeader.Header.Del("Content-Encoding") + c.RespHeader.Header.Del("Content-Range") return false, caddyhttp.Error(statusCode, nil) }