Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements a max recursive depth check #2022

Merged
merged 3 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
review comments
  • Loading branch information
drewwells committed Mar 9, 2021
commit a20f75107054a2683202ff82f23779ee771e00ee
2 changes: 0 additions & 2 deletions protoc-gen-openapiv2/internal/genopenapi/cycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
)

func TestCycle(t *testing.T) {

for _, tt := range []struct {
max int
attempt int
Expand Down Expand Up @@ -39,5 +38,4 @@ func TestCycle(t *testing.T) {
t.Errorf("got: %t wanted: %t", final, tt.e)
}
}

}
11 changes: 1 addition & 10 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ func queryParams(message *descriptor.Message, field *descriptor.Field, prefix st
}

type cycleChecker struct {
mu sync.RWMutex
m map[string]int
count int
}
Expand All @@ -150,27 +149,21 @@ func newCycleChecker(recursive int) *cycleChecker {
// Check returns whether name is still within recursion
// toleration
func (c *cycleChecker) Check(name string) bool {
c.mu.RLock()
count, ok := c.m[name]
count = count + 1
isCycle := count > c.count
c.mu.RUnlock()

if isCycle {
return false
}

// provision map entry if not available
if !ok {
c.mu.Lock()
c.m[name] = 1
c.mu.Unlock()
return true
}

c.mu.Lock()
c.m[name] = count
c.mu.Unlock()

return true
}
Expand All @@ -181,11 +174,9 @@ func (c *cycleChecker) Branch() *cycleChecker {
m: map[string]int{},
}

c.mu.RLock()
for k, v := range c.m {
copy.m[k] = v
}
c.mu.RUnlock()

return copy
}
Expand Down Expand Up @@ -306,7 +297,7 @@ func nestedQueryParams(message *descriptor.Message, field *descriptor.Field, pre
// Check for cyclical message reference:
isOK := cycle.Check(*msg.Name)
if !isOK {
return nil, fmt.Errorf("recursive types are not allowed for query parameters, cycle found on %q", fieldType)
return nil, fmt.Errorf("exceeded recursive count (%d) for query parameter %q", cycle.count, fieldType)
}

// Construct a new map with the message name so a cycle further down the recursive path can be detected.
Expand Down