Skip to content

Commit

Permalink
Fixed bug in handling variations of schema references
Browse files Browse the repository at this point in the history
  • Loading branch information
twest-bf committed Aug 19, 2024
1 parent e22c11c commit a39028b
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 92 deletions.
5 changes: 5 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ builds:
- linux
- windows
- darwin
flags:
- -trimpath
ldflags:
- -s -w -X main.build={{.Version}}


archives:
- format: tar.gz
Expand Down
1 change: 1 addition & 0 deletions cmd/automate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

var getAccessibleEndpoints bool
var outputFormat string
var responsePreviewLength int
var verbose bool

var automateCmd = &cobra.Command{
Expand Down
3 changes: 1 addition & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var outfile string
var proxy string
var quiet bool
var randomUserAgent bool
var responsePreviewLength int
var safeWords []string
var swaggerURL string
var timeout int64
Expand Down Expand Up @@ -47,7 +46,7 @@ $ sj brute -u https://petstore.swagger.io`,
log.Error("Command not specified. See the --help flag for usage.")
}
},
Version: "1.7.0",
Version: "1.7.1",
}

func Execute() {
Expand Down
182 changes: 92 additions & 90 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,106 +323,108 @@ func (s SwaggerRequest) AddParametersToRequest(op *openapi3.Operation) SwaggerRe
} else {
EnforceSingleContentType(contentType)
}
if op.RequestBody.Value.Content.Get(i).Schema.Value == nil {
s = s.SetParametersFromSchema(nil, "body", op.RequestBody.Value.Content.Get(i).Schema.Ref, op.RequestBody, 0)
if strings.Contains(i, "json") {
s.BodyData, _ = json.Marshal(s.Body)
} else if strings.Contains(i, "x-www-form-urlencoded") {
var formData []string
for j := range s.Body {
formData = append(formData, fmt.Sprintf("%s=%s", j, fmt.Sprint(s.Body[j])))
}
s.BodyData = []byte(strings.Join(formData, "&"))
} else if strings.Contains(i, "xml") {
type Element struct {
XMLName xml.Name
Content any `xml:",chardata"`
}
if op.RequestBody.Value.Content.Get(i).Schema != nil {
if op.RequestBody.Value.Content.Get(i).Schema.Value == nil {
s = s.SetParametersFromSchema(nil, "body", op.RequestBody.Value.Content.Get(i).Schema.Ref, op.RequestBody, 0)
if strings.Contains(i, "json") {
s.BodyData, _ = json.Marshal(s.Body)
} else if strings.Contains(i, "x-www-form-urlencoded") {
var formData []string
for j := range s.Body {
formData = append(formData, fmt.Sprintf("%s=%s", j, fmt.Sprint(s.Body[j])))
}
s.BodyData = []byte(strings.Join(formData, "&"))
} else if strings.Contains(i, "xml") {
type Element struct {
XMLName xml.Name
Content any `xml:",chardata"`
}

type Root struct {
XMLName xml.Name `xml:"root"`
Elements []Element `xml:",any"`
}
type Root struct {
XMLName xml.Name `xml:"root"`
Elements []Element `xml:",any"`
}

var elements []Element
for key, value := range s.Body {
elements = append(elements, Element{
XMLName: xml.Name{Local: key},
Content: value,
})
}
var elements []Element
for key, value := range s.Body {
elements = append(elements, Element{
XMLName: xml.Name{Local: key},
Content: value,
})
}

root := Root{
Elements: elements,
}
root := Root{
Elements: elements,
}

xmlData, err := xml.Marshal(root)
if err != nil {
log.Warn("Error marshalling XML data.")
xmlData, err := xml.Marshal(root)
if err != nil {
log.Warn("Error marshalling XML data.")
}
s.BodyData = xmlData
} else {
log.Warnf("Content type not supported. Test this path manually: %s (Content type: %s)\n", s.URL.Path, i)
}
s.BodyData = xmlData
} else {
log.Warnf("Content type not supported. Test this path manually: %s (Content type: %s)\n", s.URL.Path, i)
}
} else {
var formData []string

for j := range op.RequestBody.Value.Content.Get(i).Schema.Value.Properties {
if op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Ref != "" {
s = s.SetParametersFromSchema(nil, "body", op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Ref, op.RequestBody, 0)
} else {
var valueType string = op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Value.Type
if op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Value != nil {
if valueType == "string" {
s.Body[j] = "test"
} else if valueType == "boolean" {
s.Body[j] = false
} else if valueType == "integer" || valueType == "number" {
s.Body[j] = 1
} else {
s.Body[j] = "unknown_type_populate_manually"
}
if i == "application/x-www-form-urlencoded" {
formData = append(formData, fmt.Sprintf("%s=%s", j, fmt.Sprint(s.Body[j])))
}
}

if i == "application/x-www-form-urlencoded" {
s.BodyData = []byte(strings.Join(formData, "&"))
} else if strings.Contains(i, "json") || i == "*/*" {
s.BodyData, _ = json.Marshal(s.Body)
} else if strings.Contains(i, "xml") {
//
type Element struct {
XMLName xml.Name
Content any `xml:",chardata"`
}

type Root struct {
XMLName xml.Name `xml:"root"`
Elements []Element `xml:",any"`
}

var elements []Element
for key, value := range s.Body {
elements = append(elements, Element{
XMLName: xml.Name{Local: key},
Content: value,
})
}
var formData []string

root := Root{
Elements: elements,
for j := range op.RequestBody.Value.Content.Get(i).Schema.Value.Properties {
if op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Ref != "" {
s = s.SetParametersFromSchema(nil, "body", op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Ref, op.RequestBody, 0)
} else {
var valueType string = op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Value.Type
if op.RequestBody.Value.Content.Get(i).Schema.Value.Properties[j].Value != nil {
if valueType == "string" {
s.Body[j] = "test"
} else if valueType == "boolean" {
s.Body[j] = false
} else if valueType == "integer" || valueType == "number" {
s.Body[j] = 1
} else {
s.Body[j] = "unknown_type_populate_manually"
}
if i == "application/x-www-form-urlencoded" {
formData = append(formData, fmt.Sprintf("%s=%s", j, fmt.Sprint(s.Body[j])))
}
}

xmlData, err := xml.Marshal(root)
if err != nil {
log.Warn("Error marshalling XML data.")
if i == "application/x-www-form-urlencoded" {
s.BodyData = []byte(strings.Join(formData, "&"))
} else if strings.Contains(i, "json") || i == "*/*" {
s.BodyData, _ = json.Marshal(s.Body)
} else if strings.Contains(i, "xml") {
//
type Element struct {
XMLName xml.Name
Content any `xml:",chardata"`
}

type Root struct {
XMLName xml.Name `xml:"root"`
Elements []Element `xml:",any"`
}

var elements []Element
for key, value := range s.Body {
elements = append(elements, Element{
XMLName: xml.Name{Local: key},
Content: value,
})
}

root := Root{
Elements: elements,
}

xmlData, err := xml.Marshal(root)
if err != nil {
log.Warn("Error marshalling XML data.")
}
s.BodyData = xmlData
} else {
s.Body["test"] = "test"
s.BodyData = []byte("test=test")
}
s.BodyData = xmlData
} else {
s.Body["test"] = "test"
s.BodyData = []byte("test=test")
}
}
}
Expand Down

0 comments on commit a39028b

Please sign in to comment.