chore(response): prevention of Hijack() runtime panics (#4295)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Prevention of Hijack() runtime panics * added test of Hijack() * fix review * fix lint error * added check assertion of Wrrten() condition before calling Hijack()
authored by 諏訪原慶斗 · committed by GitHub · parent b987b62 · Browse files · 2 files·+76 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+76 added−0 removed
- other:+76 added−0 removed
Provenance
- Commit
32065bb- Tree
0d23789- Author
諏訪原慶斗 <131525647+suwakei@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
b987b62
2 files changed+76 added−0 removed
modified:response_writer_test.go+70 added
3 unmodified lines4 5 package gin6 7 import (8+ "bufio"9+ "net"10 "net/http"11 "net/http/httptest"12 "testing"13 111 unmodified lines125 126 w.Flush()127 }128 129+type mockHijacker struct {130+ *httptest.ResponseRecorder131+ hijacked bool132+}133+134+// Hijack implements the http.Hijacker interface. It just records that it was called.135+func (m *mockHijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) {136+ m.hijacked = true137+ return nil, nil, nil138+}139+140+func TestResponseWriterHijackAfterWrite(t *testing.T) {141+ tests := []struct {142+ name string143+ action func(w ResponseWriter) error // Action to perform before hijacking144+ expectWrittenBeforeHijack bool145+ expectHijackSuccess bool146+ expectWrittenAfterHijack bool147+ expectError error148+ }{149+ {150+ name: "hijack before write should succeed",151+ action: func(w ResponseWriter) error { return nil },152+ expectWrittenBeforeHijack: false,153+ expectHijackSuccess: true,154+ expectWrittenAfterHijack: true, // Hijack itself marks the writer as written155+ expectError: nil,156+ },157+ {158+ name: "hijack after write should fail",159+ action: func(w ResponseWriter) error {160+ _, err := w.Write([]byte("test"))161+ return err162+ },163+ expectWrittenBeforeHijack: true,164+ expectHijackSuccess: false,165+ expectWrittenAfterHijack: true,166+ expectError: errHijackAlreadyWritten,167+ },168+ }169+170+ for _, tc := range tests {171+ t.Run(tc.name, func(t *testing.T) {172+ hijacker := &mockHijacker{ResponseRecorder: httptest.NewRecorder()}173+ writer := &responseWriter{}174+ writer.reset(hijacker)175+ w := ResponseWriter(writer)176+177+ // Check initial state178+ assert.False(t, w.Written(), "should not be written initially")179+180+ // Perform pre-hijack action181+ require.NoError(t, tc.action(w), "unexpected error during pre-hijack action")182+183+ // Check state before hijacking184+ assert.Equal(t, tc.expectWrittenBeforeHijack, w.Written(), "unexpected w.Written() state before hijack")185+186+ // Attempt to hijack187+ _, _, hijackErr := w.Hijack()188+189+ // Check results190+ require.ErrorIs(t, hijackErr, tc.expectError, "unexpected error from Hijack()")191+ assert.Equal(t, tc.expectHijackSuccess, hijacker.hijacked, "unexpected hijacker.hijacked state")192+ assert.Equal(t, tc.expectWrittenAfterHijack, w.Written(), "unexpected w.Written() state after hijack")193+ })194+ }195+}196+197 func TestResponseWriterFlush(t *testing.T) {198 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {199 writer := &responseWriter{}200 writer.reset(w)59 unmodified linesmodified:response_writer.go+6 added
4 unmodified lines5 package gin6 7 import (8 "bufio"9+ "errors"10 "io"11 "net"12 "net/http"13 )2 unmodified lines16 noWritten = -117 defaultStatus = http.StatusOK18 )19 20+var errHijackAlreadyWritten = errors.New("gin: response already written")21+22 // ResponseWriter ...23 type ResponseWriter interface {24 http.ResponseWriter25 http.Hijacker82 unmodified lines108 }109 110 // Hijack implements the http.Hijacker interface.111 func (w *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {112+ if w.Written() {113+ return nil, nil, errHijackAlreadyWritten114+ }115 if w.size < 0 {116 w.size = 0117 }118 return w.ResponseWriter.(http.Hijacker).Hijack()19 unmodified lines