Skip to content
Myr

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

2 files+76 added−0 removed

modified:response_writer_test.go+70 added
3 unmodified lines
45package gin67import (8 "bufio"9 "net"10 "net/http"11 "net/http/httptest"12 "testing"13
111 unmodified lines
125126 w.Flush()127}128129type mockHijacker struct {130 *httptest.ResponseRecorder131 hijacked bool132}133134// Hijack implements the http.Hijacker interface. It just records that it was called.135func (m *mockHijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) {136 m.hijacked = true137 return nil, nil, nil138}139140func 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 }169170 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)176177 // Check initial state178 assert.False(t, w.Written(), "should not be written initially")179180 // Perform pre-hijack action181 require.NoError(t, tc.action(w), "unexpected error during pre-hijack action")182183 // Check state before hijacking184 assert.Equal(t, tc.expectWrittenBeforeHijack, w.Written(), "unexpected w.Written() state before hijack")185186 // Attempt to hijack187 _, _, hijackErr := w.Hijack()188189 // 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}196197func 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 lines
modified:response_writer.go+6 added
4 unmodified lines
5package gin67import (8 "bufio"9 "errors"10 "io"11 "net"12 "net/http"13)
2 unmodified lines
16 noWritten = -117 defaultStatus = http.StatusOK18)1920var errHijackAlreadyWritten = errors.New("gin: response already written")2122// ResponseWriter ...23type ResponseWriter interface {24 http.ResponseWriter25 http.Hijacker
82 unmodified lines
108}109110// Hijack implements the http.Hijacker interface.111func (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