fix(api): reject malformed workflow requests

This commit is contained in:
Virgil 2026-04-01 21:22:30 +00:00
parent 892901dee2
commit 6551e6a91d
2 changed files with 29 additions and 1 deletions

View file

@ -6,6 +6,8 @@
package api
import (
"errors"
stdio "io"
"io/fs"
"net/http"
@ -545,7 +547,11 @@ func (p *BuildProvider) generateReleaseWorkflow(c *gin.Context) {
var req releaseWorkflowRequest
if err := c.ShouldBindJSON(&req); err != nil {
req.Path = ""
// Empty bodies are valid; malformed JSON is not.
if !errors.Is(err, stdio.EOF) {
c.JSON(http.StatusBadRequest, api.Fail("invalid_request", err.Error()))
return
}
}
path := req.Path

View file

@ -257,6 +257,28 @@ func TestProvider_GenerateReleaseWorkflow_OutputAlias_Good(t *testing.T) {
assert.Contains(t, content, "workflow_dispatch:")
}
func TestProvider_GenerateReleaseWorkflow_InvalidJSON_Bad(t *testing.T) {
gin.SetMode(gin.TestMode)
projectDir := t.TempDir()
p := NewProvider(projectDir, nil)
recorder := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodPost, "/release/workflow", bytes.NewBufferString(`{"path":`))
request.Header.Set("Content-Type", "application/json")
ctx, _ := gin.CreateTestContext(recorder)
ctx.Request = request
p.generateReleaseWorkflow(ctx)
assert.Equal(t, http.StatusBadRequest, recorder.Code)
path := build.ReleaseWorkflowPath(projectDir)
_, err := io.Local.Read(path)
assert.Error(t, err)
}
func TestProvider_DiscoverProject_Good(t *testing.T) {
gin.SetMode(gin.TestMode)