Using render.JSON with no http Request object throws an error
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 50/100
Research direction
Start in responder.go at line 103, where JSON accesses the request context, and reproduce the failure with the wrapper test shown in the issue. Check the nil-request path and verify that the response body is written without a panic while preserving the requested status behavior.
Written by the indexing model from the issue text.
Description
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?
- Dominant language
- Go
- Stars
- 340
- Forks
- 69
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from go-chi/render
-
Render and Bind panic on unexported nested interface fieldsPossibly taken @duongynhi000005-oss claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Channel response appends JSON null after context timeoutPossibly taken @awais-ur-rehman claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
Difficulty 5/5 Over a week Newbie friendliness 10/100
-
Any concern render.JSON() can leak internal errors to outside?Possibly taken @team-humaki claimed this 50 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 30/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
SAP/crossplane-provider-btp#1175 ·
Maintainers usually reply within 1 day
-
adk: stream concatenation errors bypass retry and failoverPossibly taken @OUAO-FRANK claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
google/osv-scalibr#2586 ·
Maintainers usually reply within 2 days
-
needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
stacklok/toolhive-catalog#1890 ·
Maintainers usually reply within 1 day
-
heap: pushing an item that is already queued corrupts the indexPossibly taken @lbarrosby claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
kubernetes-sigs/kwok#1800 ·
Maintainers usually reply within 1 day