libopenapi icon indicating copy to clipboard operation
libopenapi copied to clipboard

Segmentation fault : When Extension value is "parameters" or has the prefix "x-"

Open MinhLA1410 opened this issue 11 months ago • 1 comments

Procedure:

Create extension in PathItems (x-user_extension) with value is: "parameters" or prefix "x-":

paths:
  '/test/users':
       x-user_extension: parameters
       get:
           description: test users 
           operationId: users
paths:
  '/test/users':
      x-user_extension: x-user
      get:
          description: test users 
          operationId: users

then call BuildV3Model() function. The segmentation fault will be happend with error "invalid memory address or nil pointer dereference" below

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xa18558]

goroutine 112 [running]:
github.com/pb33f/libopenapi/datamodel/low/v3.(*PathItem).Build(0xc000005c08, {0xd96c08, 0xc0002d2270}, 0xc0003e0a00, 0xc0003e0aa0, 0xc0003af608)
	/home/vagrant/go/pkg/mod/github.com/pb33f/[email protected]/datamodel/low/v3/path_item.go:207 +0x8d8
github.com/pb33f/libopenapi/datamodel/low/v3.extractPathItemsMap.func3({0xc0003e0a00, 0xc0003e0aa0})
	/home/vagrant/go/pkg/mod/github.com/pb33f/[email protected]/datamodel/low/v3/paths.go:219 +0x73a
github.com/pb33f/libopenapi/datamodel.TranslatePipeline[...].func1()
	/home/vagrant/go/pkg/mod/github.com/pb33f/[email protected]/datamodel/translate.go:237 +0x28b
created by github.com/pb33f/libopenapi/datamodel.TranslatePipeline[...] in goroutine 1
	/home/vagrant/go/pkg/mod/github.com/pb33f/[email protected]/datamodel/translate.go:229 +0x4dc

My Investigation

Crash happen in function Build() of path_item.go The purpose of the code block below is to ignore Extension to build information of PathNode get. However, when the value of extension is also "parameters" or HasPrefix("x-"), it will lead to the currentNode being assigned to incorrect pathNode (nil), resulting in currentNode.Value being invalid memory.

for i, pathNode := range root.Content {
		if strings.HasPrefix(strings.ToLower(pathNode.Value), "x-") {
			skip = true
			continue
		}
		if strings.HasPrefix(strings.ToLower(pathNode.Value), "parameters") {
			skip = true
			continue
		}
		if skip {
			skip = false
			continue
		}
		if i%2 == 0 {
			currentNode = pathNode
			continue
		}

		// the only thing we now care about is handling operations, filter out anything that's not a verb.
		switch currentNode.Value {              <------ crash 
		case GetLabel:

This error may happen in others object like paths.go, extraction_function.go, ... Could you please check and fix it?

MinhLA1410 avatar Mar 07 '25 03:03 MinhLA1410

Could you please check and fix it?

Thanks for the report. All PRs are welcome.

daveshanley avatar Mar 14 '25 13:03 daveshanley

fixed in v0.22.0

daveshanley avatar May 26 '25 22:05 daveshanley