Using render.JSON with no http Request object throws an error
評価
調査の方向性
JSON がリクエストコンテキストにアクセスする responder.go の 103 行目から始め、issue に示されている wrapper test で失敗を再現します。nil-request パスを確認し、要求されたステータスの動作を維持したまま、panic なしでレスポンスボディが書き込まれることを検証します。
索引モデルが issue の本文から書いたものです。
説明
In the responder, render.JSON() function takes in an "http.Request" object which will be used to get the http response status code
I'm customizing the behavior of the function, by wrapping it with a wrapper function in my project. So I can call one function to pass in the response body as well as the status code, like so
func (r *responder) JSON(w http.ResponseWriter, v interface{}, s int) {
// Write http response status
w.WriteHeader(s)
render.JSON(w, nil, v)
}
When "nil" is passed is shown above instead of an http.Request object, the function throws an error.
--- FAIL: TestJSON (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0xf0 pc=0x12a9ca0]
Here is the test I wrote to test the wrapper:
func TestJSON(t *testing.T) {
type mockResponse struct {
MockField string `json:"field"`
}
responder := NewResponder()
recorder := httptest.NewRecorder()
responder.JSON(recorder, mockResponse{MockField: "test"}, http.StatusOK)
assert.Equal(t, recorder.Code, 200)
assert.JSONEq(t, `{"field": "test"}`, recorder.Body.String())
}
I did some debugging, it seems like the error is caused by line 103 in JSON function in responder.go, which is the following:
if status, ok := r.Context().Value(StatusCtxKey).(int); ok {
w.WriteHeader(status)
}
How about adding a check for when the request object is nil, it skips this whole check and only writes the body to the buffer?
- 主要言語
- Go
- スター
- 340
- フォーク
- 69
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
go-chi/render のほかの issue
-
Render and Bind panic on unexported nested interface fields対応中かも @duongynhi000005-oss が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
Channel response appends JSON null after context timeout対応中かも @awais-ur-rehman が 1 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 25/100
-
難易度 5/5 1週間以上 初心者へのやさしさ 10/100
-
Any concern render.JSON() can leak internal errors to outside?対応中かも @team-humaki が 50 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 30/100
似ている issue
-
needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
stacklok/toolhive-catalog#1890 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
integrations/terraform-provider-github#3719 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Wayfinder sample agents don't reach the Data Plane on the quickstart pack対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンType/Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
thunder-id/thunderid#6402 ·
メンテナーはふだん 1 日以内に返信
-
GoStratumEngineオープンaddition
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
awesome-selfhosted/awesome-selfhosted-data#3290 ·
メンテナーはふだん 1 日以内に返信
-
log4j2.xml from orchestration.extraConfiguration is rendered twice, and the install fails when orchestration.log4j2 is also set対応中かも @irisfield が今日担当しました。 オープンplatform/local
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
camunda/camunda-platform-helm#7854 ·
メンテナーはふだん 1 日以内に返信