Skip to content

Fix EncodeJSONResponse superfluous WriteHeader call#540

Open
Kubudak90 wants to merge 2 commits into
coinbase:masterfrom
Kubudak90:fix-encode-json-response
Open

Fix EncodeJSONResponse superfluous WriteHeader call#540
Kubudak90 wants to merge 2 commits into
coinbase:masterfrom
Kubudak90:fix-encode-json-response

Conversation

@Kubudak90

Copy link
Copy Markdown

Fixes #407

Problem

The EncodeJSONResponse function was calling w.WriteHeader(status) before json.Encode(), which caused a superfluous WriteHeader call when encoding failed and http.Error() tried to write another header.

This resulted in the error: http: superfluous response.WriteHeader call

Solution

  • Encode JSON to a buffer first before writing headers
  • Only write status header after successful encoding
  • Return 500 status on encoding error without calling http.Error

Changes

  • Added bytes import
  • Encode to buffer first to detect encoding errors
  • Write headers only after successful encoding
  • On encoding error, write 500 status without body (avoiding double WriteHeader)

This prevents the panic/error that occurred when JSON encoding failed after headers were already written.

Kubudak90 added 2 commits April 8, 2026 02:14
The SaveAccountWorker function validates AccountIdentifier but does not
validate KeyPair before passing it to StoreKey. This could cause nil
pointer dereference panics if KeyPair is nil or has nil/empty fields.

Add validation for:
- KeyPair is not nil
- KeyPair.PublicKey is not nil
- KeyPair.PrivateKey is not empty

This follows the same validation pattern used elsewhere in the codebase.
Fixes coinbase#407

The EncodeJSONResponse function was calling w.WriteHeader(status) before
json.Encode(), which caused a superfluous WriteHeader call when encoding
failed and http.Error() tried to write another header.

Changes:
- Encode JSON to a buffer first before writing headers
- Only write status header after successful encoding
- Return 500 status on encoding error without calling http.Error

This prevents the 'http: superfluous response.WriteHeader call' error
that occurred when JSON encoding failed.
@cb-heimdall

Copy link
Copy Markdown

🟡 Heimdall Review Status

Requirement Status More Info
Reviews 🟡 0/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 1
Sum 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

EncodeJSONResponse - http: superfluous response.WriteHeader call

2 participants