Skip to content

Commit ff1378e

Browse files
committed
fix(parameters): avoid panic on double-slash request paths
Fixes #274
1 parent f95f219 commit ff1378e

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

parameters/path_parameters.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ func (v *paramValidator) ValidatePathParamsWithPathItem(request *http.Request, p
6060
continue
6161
}
6262

63+
// guard against length mismatch (e.g. request path containing
64+
// a double slash producing extra empty segments).
65+
if x >= len(submittedSegments) {
66+
continue
67+
}
68+
6369
var rgx *regexp.Regexp
6470

6571
if v.options.RegexCache != nil {
@@ -83,6 +89,9 @@ func (v *paramValidator) ValidatePathParamsWithPathItem(request *http.Request, p
8389
}
8490

8591
matches := rgx.FindStringSubmatch(submittedSegments[x])
92+
if matches == nil {
93+
continue
94+
}
8695
matches = matches[1:]
8796

8897
// Check if it is well-formed.

parameters/path_parameters_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,3 +2348,35 @@ paths:
23482348
assert.EqualValues(t, 1, cache.storeCount, "No new stores on cache hit")
23492349
assert.EqualValues(t, 1, cache.hitCount, "Second OData lookup should hit cache")
23502350
}
2351+
2352+
func TestValidatePathParamsWithPathItem_DoubleSlashDoesNotPanic(t *testing.T) {
2353+
// Regression test for #274: ValidatePathParamsWithPathItem panics for
2354+
// request paths containing a leading double slash (e.g. //test/path/x),
2355+
// because path segments and submitted segments differ in length.
2356+
spec := `openapi: 3.1.0
2357+
paths:
2358+
/test/path/{param}:
2359+
get:
2360+
operationId: testParam
2361+
parameters:
2362+
- in: path
2363+
name: param
2364+
required: true
2365+
schema:
2366+
type: string
2367+
responses:
2368+
"200":
2369+
description: ok`
2370+
2371+
doc, _ := libopenapi.NewDocument([]byte(spec))
2372+
m, _ := doc.BuildV3Model()
2373+
v := NewParameterValidator(&m.Model)
2374+
2375+
req, _ := http.NewRequest(http.MethodGet, "https://example.com//test/path/fubar", nil)
2376+
pathItem := m.Model.Paths.PathItems.GetOrZero("/test/path/{param}")
2377+
require.NotNil(t, pathItem)
2378+
2379+
assert.NotPanics(t, func() {
2380+
_, _ = v.ValidatePathParamsWithPathItem(req, pathItem, "/test/path/{param}")
2381+
})
2382+
}

0 commit comments

Comments
 (0)