mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-20 07:21:26 +02:00
fix(middleware): skip body rewriters when buffering fails
Prevent response modifiers that require body rewriting from running when the body rewrite gate blocks buffering (for example, chunked transfer encoding). Add an explicit `requiresBodyRewrite` capability and implement it for HTML/theme/error-page modifiers, including bypass delegation. Also add a regression test to ensure the original response body remains readable and is not closed prematurely when rewrite is blocked. This commit fixeds the "http: read on closed response body" with empty page error happens when body-rewriting middleware (like themed) runs on responses where body rewrite is blocked (e.g. chunked), then the gate restores an already-closed original body.
This commit is contained in:
@@ -122,6 +122,10 @@ func (c *checkBypass) modifyResponse(resp *http.Response) error {
|
|||||||
return c.modRes.modifyResponse(resp)
|
return c.modRes.modifyResponse(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *checkBypass) requiresBodyRewrite() bool {
|
||||||
|
return requiresBodyRewrite(c.modRes)
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Middleware) withCheckBypass() any {
|
func (m *Middleware) withCheckBypass() any {
|
||||||
if len(m.Bypass) > 0 {
|
if len(m.Bypass) > 0 {
|
||||||
modReq, _ := m.impl.(RequestModifier)
|
modReq, _ := m.impl.(RequestModifier)
|
||||||
|
|||||||
@@ -20,6 +20,10 @@ var CustomErrorPage = NewMiddleware[customErrorPage]()
|
|||||||
|
|
||||||
const StaticFilePathPrefix = "/$gperrorpage/"
|
const StaticFilePathPrefix = "/$gperrorpage/"
|
||||||
|
|
||||||
|
func (customErrorPage) requiresBodyRewrite() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// before implements RequestModifier.
|
// before implements RequestModifier.
|
||||||
func (customErrorPage) before(w http.ResponseWriter, r *http.Request) (proceed bool) {
|
func (customErrorPage) before(w http.ResponseWriter, r *http.Request) (proceed bool) {
|
||||||
return !ServeStaticErrorPageFile(w, r)
|
return !ServeStaticErrorPageFile(w, r)
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ type middlewareChain struct {
|
|||||||
modResps []ResponseModifier
|
modResps []ResponseModifier
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type bodyRewriteRequired interface {
|
||||||
|
requiresBodyRewrite() bool
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: check conflict or duplicates.
|
// TODO: check conflict or duplicates.
|
||||||
func NewMiddlewareChain(name string, chain []*Middleware) *Middleware {
|
func NewMiddlewareChain(name string, chain []*Middleware) *Middleware {
|
||||||
chainMid := &middlewareChain{}
|
chainMid := &middlewareChain{}
|
||||||
@@ -59,6 +63,9 @@ func modifyResponseWithBodyRewriteGate(mr ResponseModifier, resp *http.Response)
|
|||||||
originalBody := resp.Body
|
originalBody := resp.Body
|
||||||
originalContentLength := resp.ContentLength
|
originalContentLength := resp.ContentLength
|
||||||
allowBodyRewrite := canBufferAndModifyResponseBody(responseHeaderForBodyRewriteGate(resp))
|
allowBodyRewrite := canBufferAndModifyResponseBody(responseHeaderForBodyRewriteGate(resp))
|
||||||
|
if !allowBodyRewrite && requiresBodyRewrite(mr) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
if err := mr.modifyResponse(resp); err != nil {
|
if err := mr.modifyResponse(resp); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -87,6 +94,11 @@ func modifyResponseWithBodyRewriteGate(mr ResponseModifier, resp *http.Response)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func requiresBodyRewrite(mr ResponseModifier) bool {
|
||||||
|
required, ok := mr.(bodyRewriteRequired)
|
||||||
|
return ok && required.requiresBodyRewrite()
|
||||||
|
}
|
||||||
|
|
||||||
func responseHeaderForBodyRewriteGate(resp *http.Response) http.Header {
|
func responseHeaderForBodyRewriteGate(resp *http.Response) http.Header {
|
||||||
h := resp.Header.Clone()
|
h := resp.Header.Clone()
|
||||||
if len(resp.TransferEncoding) > 0 && len(h.Values("Transfer-Encoding")) == 0 {
|
if len(resp.TransferEncoding) > 0 && len(h.Values("Transfer-Encoding")) == 0 {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
@@ -30,6 +31,29 @@ type testResponseRewrite struct {
|
|||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type closeSensitiveBody struct {
|
||||||
|
data []byte
|
||||||
|
offset int
|
||||||
|
closed bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *closeSensitiveBody) Read(p []byte) (int, error) {
|
||||||
|
if b.closed {
|
||||||
|
return 0, errors.New("http: read on closed response body")
|
||||||
|
}
|
||||||
|
if b.offset >= len(b.data) {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
n := copy(p, b.data[b.offset:])
|
||||||
|
b.offset += n
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *closeSensitiveBody) Close() error {
|
||||||
|
b.closed = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (t testResponseRewrite) modifyResponse(resp *http.Response) error {
|
func (t testResponseRewrite) modifyResponse(resp *http.Response) error {
|
||||||
resp.StatusCode = t.StatusCode
|
resp.StatusCode = t.StatusCode
|
||||||
resp.Header.Set(t.HeaderKey, t.HeaderVal)
|
resp.Header.Set(t.HeaderKey, t.HeaderVal)
|
||||||
@@ -226,3 +250,34 @@ func TestMiddlewareResponseRewriteGateServeHTTP(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMiddlewareResponseRewriteGateSkipsBodyRewriterWhenRewriteBlocked(t *testing.T) {
|
||||||
|
originalBody := &closeSensitiveBody{
|
||||||
|
data: []byte("<html><body>original</body></html>"),
|
||||||
|
}
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
|
||||||
|
resp := &http.Response{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
Header: http.Header{
|
||||||
|
"Content-Type": []string{"text/html; charset=utf-8"},
|
||||||
|
"Transfer-Encoding": []string{"chunked"},
|
||||||
|
},
|
||||||
|
Body: originalBody,
|
||||||
|
ContentLength: -1,
|
||||||
|
TransferEncoding: []string{"chunked"},
|
||||||
|
Request: req,
|
||||||
|
}
|
||||||
|
|
||||||
|
themedMid, err := Themed.New(OptionsRaw{
|
||||||
|
"theme": DarkTheme,
|
||||||
|
})
|
||||||
|
expect.NoError(t, err)
|
||||||
|
|
||||||
|
respMod, ok := themedMid.impl.(ResponseModifier)
|
||||||
|
expect.True(t, ok)
|
||||||
|
expect.NoError(t, modifyResponseWithBodyRewriteGate(respMod, resp))
|
||||||
|
|
||||||
|
data, err := io.ReadAll(resp.Body)
|
||||||
|
expect.NoError(t, err)
|
||||||
|
expect.Equal(t, string(data), "<html><body>original</body></html>")
|
||||||
|
}
|
||||||
|
|||||||
@@ -22,6 +22,10 @@ type modifyHTML struct {
|
|||||||
|
|
||||||
var ModifyHTML = NewMiddleware[modifyHTML]()
|
var ModifyHTML = NewMiddleware[modifyHTML]()
|
||||||
|
|
||||||
|
func (*modifyHTML) requiresBodyRewrite() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (m *modifyHTML) before(_ http.ResponseWriter, req *http.Request) bool {
|
func (m *modifyHTML) before(_ http.ResponseWriter, req *http.Request) bool {
|
||||||
req.Header.Set("Accept-Encoding", "identity")
|
req.Header.Set("Accept-Encoding", "identity")
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -54,6 +54,10 @@ func (m *themed) modifyResponse(resp *http.Response) error {
|
|||||||
return m.m.modifyResponse(resp)
|
return m.m.modifyResponse(resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*themed) requiresBodyRewrite() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (m *themed) finalize() error {
|
func (m *themed) finalize() error {
|
||||||
m.m.Target = "body"
|
m.m.Target = "body"
|
||||||
if m.FontURL != "" && m.FontFamily != "" {
|
if m.FontURL != "" && m.FontFamily != "" {
|
||||||
|
|||||||
Reference in New Issue
Block a user