Skip to content

Commit 7aaa989

Browse files
committed
fix: reject malformed external validation flags
1 parent 2d73374 commit 7aaa989

3 files changed

Lines changed: 70 additions & 9 deletions

File tree

‎internal/handlers/validate.go‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ func (vh *PubliccodeymlValidatorHandler) Query(ctx fiber.Ctx) error {
3232
var normalized *string
3333

3434
valid := true
35-
parser := vh.parser
3635

37-
if checks := fiber.Query[bool](ctx, "external-checks", false); checks {
38-
parser = vh.parserExternalChecks
36+
parser, invalidExternalChecks := vh.parserForExternalChecks(ctx)
37+
if invalidExternalChecks {
38+
return common.Error(
39+
fiber.StatusBadRequest,
40+
"invalid external-checks parameter",
41+
"external-checks must be true or false",
42+
)
3943
}
4044

4145
if len(ctx.Body()) == 0 {
@@ -72,3 +76,19 @@ func (vh *PubliccodeymlValidatorHandler) Query(ctx fiber.Ctx) error {
7276
//nolint:wrapcheck
7377
return ctx.JSON(fiber.Map{"valid": valid, "results": results, "normalized": normalized})
7478
}
79+
80+
func (vh *PubliccodeymlValidatorHandler) parserForExternalChecks(ctx fiber.Ctx) (*publiccodeParser.Parser, bool) {
81+
args := ctx.Request().URI().QueryArgs()
82+
if !args.Has("external-checks") {
83+
return vh.parser, false
84+
}
85+
86+
switch string(args.Peek("external-checks")) {
87+
case "true":
88+
return vh.parserExternalChecks, false
89+
case "false":
90+
return vh.parser, false
91+
default:
92+
return nil, true
93+
}
94+
}

‎main_test.go‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func runTestCases(t *testing.T, tests []TestCase) {
8484
if test.headers != nil {
8585
req.Header = test.headers
8686
}
87-
req.URL.RawQuery = u.Query().Encode()
87+
req.URL.RawQuery = u.RawQuery
8888

8989
res, err := app.Test(req, fiber.TestConfig{Timeout: 0, FailOnTimeout: false})
9090
assert.Nil(t, err)
@@ -185,6 +185,17 @@ func TestValidateEndpoint(t *testing.T) {
185185
assert.Empty(t, response["results"])
186186
},
187187
},
188+
{
189+
description: "validate: external checks explicitly disabled",
190+
query: "QUERY /v1/validate?external-checks=false",
191+
body: externalChecksYml,
192+
expectedCode: 200,
193+
expectedContentType: "application/json; charset=utf-8",
194+
validateFunc: func(t *testing.T, response map[string]any) {
195+
assert.Equal(t, true, response["valid"])
196+
assert.Empty(t, response["results"])
197+
},
198+
},
188199
{
189200
description: "validate: external checks enabled",
190201
query: "QUERY /v1/validate?external-checks=true",
@@ -200,6 +211,29 @@ func TestValidateEndpoint(t *testing.T) {
200211
assert.Nil(t, response["normalized"])
201212
},
202213
},
214+
{
215+
description: "validate: repeated external checks use first value",
216+
query: "QUERY /v1/validate?external-checks=true&external-checks=false",
217+
body: externalChecksYml,
218+
expectedCode: 200,
219+
expectedContentType: "application/json; charset=utf-8",
220+
validateFunc: func(t *testing.T, response map[string]any) {
221+
assert.Equal(t, false, response["valid"])
222+
results, ok := response["results"].([]any)
223+
assert.True(t, ok)
224+
assert.NotEmpty(t, results)
225+
assert.Contains(t, fmt.Sprint(results), "non-public")
226+
assert.Nil(t, response["normalized"])
227+
},
228+
},
229+
{
230+
description: "validate: malformed external checks flag",
231+
query: "QUERY /v1/validate?external-checks=invalid",
232+
body: validYml,
233+
expectedCode: 400,
234+
expectedBody: `{"title":"invalid external-checks parameter","detail":"external-checks must be true or false","status":400}`,
235+
expectedContentType: "application/problem+json",
236+
},
203237
{
204238
description: "validate: invalid file",
205239
query: "QUERY /v1/validate",

‎publiccode-validator-api.oas.yaml‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,22 @@ paths:
9696
description: "missing required field"
9797
normalized: null
9898
"400":
99-
description: Bad request (e.g. empty body)
99+
description: Bad request (e.g. empty body or invalid external-checks value)
100100
content:
101101
application/problem+json:
102102
schema:
103103
$ref: "#/components/schemas/ProblemJSON"
104-
example:
105-
title: empty body
106-
detail: need a body to validate
107-
status: 400
104+
examples:
105+
emptyBody:
106+
value:
107+
title: empty body
108+
detail: need a body to validate
109+
status: 400
110+
invalidExternalChecks:
111+
value:
112+
title: invalid external-checks parameter
113+
detail: external-checks must be true or false
114+
status: 400
108115
"405":
109116
description: Method not allowed
110117
headers:

0 commit comments

Comments
 (0)