mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-20 14:59:36 +00:00
templates: Delete file headers on any template error (#8149)
* templates: Delete file headers on any template error
A template that fails to parse or execute (not only {{httpError}}) left
the Content-Length, Etag and related headers set by file_server on the
error response, so clients received a 500 that declared the file's size
but had no body.
* templates: Also delete Content-Encoding and Content-Range on errors
Precompressed and ranged file responses could leave these headers on the
replacement error response.
(cherry picked from commit 6727b2f3a9)
This commit is contained in:
parent
5f780a34b5
commit
4e6ad67427
3 changed files with 77 additions and 0 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
65
modules/caddyhttp/templates/templates_test.go
Normal file
65
modules/caddyhttp/templates/templates_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue