From a39028b463839dffb1df6bd91b5ac1859cefe884 Mon Sep 17 00:00:00 2001 From: Tony West <125916820+twest-bf@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:58:51 -0400 Subject: [PATCH] Fixed bug in handling variations of schema references --- .goreleaser.yaml | 5 ++ cmd/automate.go | 1 + cmd/root.go | 3 +- cmd/utils.go | 188 ++++++++++++++++++++++++----------------------- 4 files changed, 102 insertions(+), 95 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index f54c5bd..6fb0297 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -11,6 +11,11 @@ builds: - linux - windows - darwin + flags: + - -trimpath + ldflags: + - -s -w -X main.build={{.Version}} + archives: - format: tar.gz diff --git a/cmd/automate.go b/cmd/automate.go index 3e482b4..4ed321e 100644 --- a/cmd/automate.go +++ b/cmd/automate.go @@ -13,6 +13,7 @@ import ( var getAccessibleEndpoints bool var outputFormat string +var responsePreviewLength int var verbose bool var automateCmd = &cobra.Command{ diff --git a/cmd/root.go b/cmd/root.go index 3c58254..24e75d8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 @@ -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() { diff --git a/cmd/utils.go b/cmd/utils.go index 7f6e719..11eb7c2 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -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"` - } - - 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 { - 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 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"` } - 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"` + } - 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.") + } + 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 - xmlData, err := xml.Marshal(root) - if err != nil { - log.Warn("Error marshalling XML data.") - } - s.BodyData = xmlData + 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 { - s.Body["test"] = "test" - s.BodyData = []byte("test=test") + 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, + }) + } + + 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") + } } } }