forked from mtusi2026/mtusi2026lab2
fix(httpx): reject request bodies with trailing data after JSON
This commit is contained in:
@@ -2,6 +2,8 @@ package httpx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
|
||||
@@ -25,5 +27,10 @@ func DecodeJSONBody(w http.ResponseWriter, r *http.Request, requestID string, ds
|
||||
return false
|
||||
}
|
||||
|
||||
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
|
||||
WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid request body", requestID)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user