Repository navigation
fix(context): report short writes from Context response helpers #3259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -562,6 +562,14 @@ func isValidJSONPCallback(callback string) bool { | |
| } | ||
|
|
||
| // writeJSONPHeader validates the callback and writes the JSONP response headers. | ||
| func (c *Context) writeAll(b []byte) error { | ||
| n, err := c.response.Write(b) | ||
| if err == nil && n < len(b) { | ||
| err = io.ErrShortWrite | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func (c *Context) writeJSONPHeader(code int, callback string) error { | ||
| if !isValidJSONPCallback(callback) { | ||
| return ErrBadRequest.Wrap(ErrInvalidJSONPCallback) | ||
|
|
@@ -576,16 +584,16 @@ func (c *Context) jsonPBlob(code int, callback string, i any) (err error) { | |
| if err = c.writeJSONPHeader(code, callback); err != nil { | ||
| return | ||
| } | ||
| if _, err = c.response.Write(stringToBytes(callback)); err != nil { | ||
| if err = c.writeAll(stringToBytes(callback)); err != nil { | ||
| return | ||
| } | ||
| if _, err = c.response.Write(jsonpOpen); err != nil { | ||
| if err = c.writeAll(jsonpOpen); err != nil { | ||
| return | ||
| } | ||
| if err = c.echo.JSONSerializer.Serialize(c, i, ""); err != nil { | ||
| return | ||
| } | ||
| if _, err = c.response.Write(jsonpClose); err != nil { | ||
| if err = c.writeAll(jsonpClose); err != nil { | ||
| return | ||
| } | ||
| return | ||
|
|
@@ -653,11 +661,10 @@ func (c *Context) JSONPBlob(code int, callback string, b []byte) (err error) { | |
| if _, err = c.response.Write(jsonpOpen); err != nil { | ||
| return | ||
| } | ||
| if _, err = c.response.Write(b); err != nil { | ||
| if err = c.writeAll(b); err != nil { | ||
| return | ||
| } | ||
| _, err = c.response.Write(jsonpClose) | ||
| return | ||
| return c.writeAll(jsonpClose) | ||
| } | ||
|
|
||
| func (c *Context) xml(code int, i any, indent string) (err error) { | ||
|
|
@@ -667,7 +674,7 @@ func (c *Context) xml(code int, i any, indent string) (err error) { | |
| if indent != "" { | ||
| enc.Indent("", indent) | ||
| } | ||
| if _, err = c.response.Write([]byte(xml.Header)); err != nil { | ||
| if err = c.writeAll([]byte(xml.Header)); err != nil { | ||
| return | ||
| } | ||
| return enc.Encode(i) | ||
|
|
@@ -690,16 +697,14 @@ func (c *Context) XMLBlob(code int, b []byte) (err error) { | |
| if _, err = c.response.Write([]byte(xml.Header)); err != nil { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check the XMLBlob declaration write. If the writer shortens only Use
|
||
| return | ||
| } | ||
| _, err = c.response.Write(b) | ||
| return | ||
| return c.writeAll(b) | ||
| } | ||
|
|
||
| // Blob sends a blob response with status code and content type. | ||
| func (c *Context) Blob(code int, contentType string, b []byte) (err error) { | ||
| c.writeContentType(contentType) | ||
| c.response.WriteHeader(code) | ||
| _, err = c.response.Write(b) | ||
| return | ||
| return c.writeAll(b) | ||
| } | ||
|
|
||
| // Stream sends a streaming response with status code and content type. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package echo | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| ) | ||
|
|
||
| type shortResponseWriter struct { | ||
| header http.Header | ||
| } | ||
|
|
||
| func (w *shortResponseWriter) Header() http.Header { | ||
| if w.header == nil { | ||
| w.header = make(http.Header) | ||
| } | ||
| return w.header | ||
| } | ||
| func (w *shortResponseWriter) WriteHeader(int) {} | ||
| func (w *shortResponseWriter) Write(p []byte) (int, error) { | ||
| if len(p) == 0 { | ||
| return 0, nil | ||
| } | ||
| return len(p) - 1, nil | ||
| } | ||
|
|
||
| func TestContextBlobShortWrite(t *testing.T) { | ||
| e := New() | ||
| c := e.NewContext(httptest.NewRequest(http.MethodGet, "/", nil), httptest.NewRecorder()) | ||
| c.SetResponse(&shortResponseWriter{}) | ||
| if err := c.Blob(http.StatusOK, MIMEApplicationJSON, []byte("payload")); !errors.Is(err, io.ErrShortWrite) { | ||
| t.Errorf("Blob: got %v, want io.ErrShortWrite", err) | ||
| } | ||
| } | ||
|
|
||
| func TestContextXMLBlobShortWrite(t *testing.T) { | ||
| e := New() | ||
| c := e.NewContext(httptest.NewRequest(http.MethodGet, "/", nil), httptest.NewRecorder()) | ||
| c.SetResponse(&shortResponseWriter{}) | ||
| if err := c.XMLBlob(http.StatusOK, []byte("<a/>")); !errors.Is(err, io.ErrShortWrite) { | ||
| t.Errorf("XMLBlob: got %v, want io.ErrShortWrite", err) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is about lines 658-661, outside the diff; it is anchored on the nearest change, line 664.
Check both JSONPBlob prefix writes.
If the writer shortens only the callback or opening wrapper and returns nil, these writes discard the short count. Complete payload and closing writes then make
JSONPBlobreturn nil for a truncated response. The new payload check does not cover this case.Use
writeAllfor both prefix writes. Add a writer case that shortens only a prefix write and accepts later writes.