fix(terraform): reject empty-body 2xx responses in mcp server handler

This commit is contained in:
matthew-hull-bright 2026-09-11 15:37:07 +01:00
parent 9d02b4546f
commit 825b092015
2 changed files with 3 additions and 7 deletions

View file

@ -147,10 +147,6 @@ func handleMCPAPIResponse(resp *http.Response, result interface{}, client *Clien
resp.Status, client.redactSensitiveData(string(bodyBytes)))
}
if len(bodyBytes) == 0 || string(bodyBytes) == "null" {
return nil
}
if err := json.Unmarshal(bodyBytes, result); err != nil {
return fmt.Errorf("failed to parse response: %v", err)
}

View file

@ -113,7 +113,7 @@ func TestHandleMCPAPIResponseAcceptsFullSuccessRange(t *testing.T) {
}
}
func TestHandleMCPAPIResponseAcceptsEmptyBodyOn2xx(t *testing.T) {
func TestHandleMCPAPIResponseRejectsEmptyBodyOn2xx(t *testing.T) {
rec := httptest.NewRecorder()
rec.WriteHeader(http.StatusNoContent)
resp := rec.Result()
@ -122,7 +122,7 @@ func TestHandleMCPAPIResponseAcceptsEmptyBodyOn2xx(t *testing.T) {
var mcpResp MCPServerResponse
err := handleMCPAPIResponse(resp, &mcpResp, client)
if err != nil {
t.Fatalf("handleMCPAPIResponse returned unexpected error for empty-body 204: %v", err)
if err == nil {
t.Fatal("handleMCPAPIResponse returned no error for empty-body 204; every MCP caller (create/read/update) writes the parsed result straight into Terraform state with no fallback, so a silently-accepted empty body would blank out or empty-ID the resource")
}
}