Skip to content

Commit

Permalink
Protect from recursion in openapi3.InternaliseRefs (#578)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitriy Lukiyanchuk <[email protected]>
  • Loading branch information
wtertius and Dmitriy Lukiyanchuk authored Aug 29, 2022
1 parent 8154829 commit 2470727
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
7 changes: 6 additions & 1 deletion openapi3/internalize_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (doc *T) addCallbackToSpec(c *CallbackRef, refNameResolver RefNameResolver)
}

func (doc *T) derefSchema(s *Schema, refNameResolver RefNameResolver) {
if s == nil {
if s == nil || doc.isVisitedSchema(s) {
return
}

Expand Down Expand Up @@ -229,6 +229,9 @@ func (doc *T) derefSchema(s *Schema, refNameResolver RefNameResolver) {
func (doc *T) derefHeaders(hs Headers, refNameResolver RefNameResolver) {
for _, h := range hs {
doc.addHeaderToSpec(h, refNameResolver)
if doc.isVisitedHeader(h.Value) {
continue
}
doc.derefParameter(h.Value.Parameter, refNameResolver)
}
}
Expand Down Expand Up @@ -324,6 +327,8 @@ func (doc *T) derefPaths(paths map[string]*PathItem, refNameResolver RefNameReso
//
// doc.InternalizeRefs(context.Background(), nil)
func (doc *T) InternalizeRefs(ctx context.Context, refNameResolver func(ref string) string) {
doc.resetVisited()

if refNameResolver == nil {
refNameResolver = DefaultRefNameResolver
}
Expand Down
2 changes: 2 additions & 0 deletions openapi3/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type T struct {
Servers Servers `json:"servers,omitempty" yaml:"servers,omitempty"`
Tags Tags `json:"tags,omitempty" yaml:"tags,omitempty"`
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`

visited visitedComponent
}

// MarshalJSON returns the JSON encoding of T.
Expand Down
4 changes: 4 additions & 0 deletions openapi3/testdata/recursiveRef/components/Cat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: object
properties:
cat:
$ref: ../openapi.yml#/components/schemas/Cat
2 changes: 2 additions & 0 deletions openapi3/testdata/recursiveRef/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ components:
$ref: ./components/Foo/Foo2.yml
Bar:
$ref: ./components/Bar.yml
Cat:
$ref: ./components/Cat.yml
8 changes: 8 additions & 0 deletions openapi3/testdata/recursiveRef/openapi.yml.internalized.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
}
},
"type": "object"
},
"Cat": {
"properties": {
"cat": {
"$ref": "#/components/schemas/Cat"
}
},
"type": "object"
}
}
},
Expand Down
41 changes: 41 additions & 0 deletions openapi3/visited.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package openapi3

func newVisited() visitedComponent {
return visitedComponent{
header: make(map[*Header]struct{}),
schema: make(map[*Schema]struct{}),
}
}

type visitedComponent struct {
header map[*Header]struct{}
schema map[*Schema]struct{}
}

// resetVisited clears visitedComponent map
// should be called before recursion over doc *T
func (doc *T) resetVisited() {
doc.visited = newVisited()
}

// isVisitedHeader returns `true` if the *Header pointer was already visited
// otherwise it returns `false`
func (doc *T) isVisitedHeader(h *Header) bool {
if _, ok := doc.visited.header[h]; ok {
return true
}

doc.visited.header[h] = struct{}{}
return false
}

// isVisitedHeader returns `true` if the *Schema pointer was already visited
// otherwise it returns `false`
func (doc *T) isVisitedSchema(s *Schema) bool {
if _, ok := doc.visited.schema[s]; ok {
return true
}

doc.visited.schema[s] = struct{}{}
return false
}

0 comments on commit 2470727

Please sign in to comment.