Skip to content

Commit 143fdfd

Browse files
authored
Merge pull request #50 from aringo-bf/fix/httpbasic-segfault-and-quiet-flag
Fix HTTPBasic segfault and --quiet flag issues
2 parents 4c46611 + a33eef4 commit 143fdfd

File tree

4 files changed

+48
-22
lines changed

4 files changed

+48
-22
lines changed

cmd/auth.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,25 @@ func CheckSecuritySchemes(spec map[string]interface{}) {
4545
if schemeType := scheme["scheme"]; schemeType != nil {
4646
switch schemeType {
4747
case "basic":
48-
fmt.Println("Basic Authentication is accepted. Supply a username and password? (y/N)")
49-
fmt.Scanln(&autoApplyBasicAuth)
50-
autoApplyBasicAuth = strings.ToLower(autoApplyBasicAuth)
51-
if autoApplyBasicAuth == "y" {
52-
fmt.Printf("Enter a username.")
53-
fmt.Scanln(&basicAuthUser)
54-
fmt.Printf("Enter a password.")
55-
fmt.Scanln(&basicAuthPass)
56-
basicAuth = []byte(basicAuthUser + ":" + basicAuthPass)
57-
basicAuthString = base64.StdEncoding.EncodeToString(basicAuth)
58-
log.Infof("Using %s as the Basic Auth value.", basicAuthString)
59-
Headers = append(Headers, "Authorization: Basic "+basicAuthString)
60-
} else {
48+
if quiet {
49+
autoApplyBasicAuth = "n"
6150
log.Warn("A basic authentication header is accepted. Review the spec and craft a header manually using the -H flag.")
51+
} else {
52+
fmt.Println("Basic Authentication is accepted. Supply a username and password? (y/N)")
53+
fmt.Scanln(&autoApplyBasicAuth)
54+
autoApplyBasicAuth = strings.ToLower(autoApplyBasicAuth)
55+
if autoApplyBasicAuth == "y" {
56+
fmt.Printf("Enter a username.")
57+
fmt.Scanln(&basicAuthUser)
58+
fmt.Printf("Enter a password.")
59+
fmt.Scanln(&basicAuthPass)
60+
basicAuth = []byte(basicAuthUser + ":" + basicAuthPass)
61+
basicAuthString = base64.StdEncoding.EncodeToString(basicAuth)
62+
log.Infof("Using %s as the Basic Auth value.", basicAuthString)
63+
Headers = append(Headers, "Authorization: Basic "+basicAuthString)
64+
} else {
65+
log.Warn("A basic authentication header is accepted. Review the spec and craft a header manually using the -H flag.")
66+
}
6267
}
6368
case "bearer":
6469
log.Warn("A bearer token is accepted. Review the spec and craft a token manually using the -H flag.")

cmd/requests.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ func MakeRequest(client http.Client, method, target string, timeout int64, reqDa
5959
}
6060

6161
// Handling of dangerous keywords
62-
u, _ := url.Parse(target)
62+
u, err := url.Parse(target)
63+
if err != nil || u == nil {
64+
log.Printf("Error parsing URL '%s': %v - skipping request.", target, err)
65+
return nil, "", 0
66+
}
6367
endpoint := u.RawPath + "?" + u.RawQuery
6468
for _, v := range dangerousStrings {
6569
if os.Args[1] == "automate" && strings.Contains(endpoint, v) && !strings.Contains(strings.Join(safeWords, ","), v) {
@@ -87,8 +91,11 @@ func MakeRequest(client http.Client, method, target string, timeout int64, reqDa
8791
defer cancel()
8892

8993
req, err := http.NewRequest(method, target, reqData)
90-
if err != nil && err != context.Canceled && err != io.EOF {
91-
log.Fatal("Error: could not create HTTP request - ", err)
94+
if err != nil {
95+
if err != context.Canceled && err != io.EOF {
96+
log.Fatal("Error: could not create HTTP request - ", err)
97+
}
98+
return nil, "", 0
9299
}
93100

94101
for i := range Headers {
@@ -168,14 +175,21 @@ func MakeRequest(client http.Client, method, target string, timeout int64, reqDa
168175
}
169176

170177
func CheckContentType(client http.Client, target string) string {
171-
u, _ := url.Parse(target)
178+
u, err := url.Parse(target)
179+
if err != nil || u == nil {
180+
log.Printf("Error parsing URL '%s': %v - skipping request.", target, err)
181+
return ""
182+
}
172183

173184
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
174185
defer cancel()
175186

176187
req, err := http.NewRequest("GET", target, nil)
177-
if err != nil && err != context.Canceled && err != io.EOF {
178-
log.Fatal("Error: could not create HTTP request - ", err)
188+
if err != nil {
189+
if err != context.Canceled && err != io.EOF {
190+
log.Fatal("Error: could not create HTTP request - ", err)
191+
}
192+
return ""
179193
}
180194

181195
// User-Agent handling

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ $ sj convert -u https://petstore.swagger.io/v2/swagger.json -o openapi.json`,
5252
log.Error("Command not specified. See the --help flag for usage.")
5353
}
5454
},
55-
Version: "2.3.0",
55+
Version: "2.3.1",
5656
}
5757

5858
func Execute() {

cmd/utils.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ func BuildRequestsFromPaths(spec map[string]interface{}, client http.Client) {
198198
}
199199
}
200200

201-
logURL, _ := url.Parse(targetURL)
201+
logURL, parseErr := url.Parse(targetURL)
202+
if parseErr != nil || logURL == nil {
203+
log.Printf("Error parsing URL '%s': %v - skipping endpoint.", targetURL, parseErr)
204+
continue
205+
}
202206
switch os.Args[1] {
203207
case "automate":
204208
var postBodyData string
@@ -403,7 +407,10 @@ func GenerateRequests(bodyBytes []byte, client http.Client) {
403407
// Checks defined security schemes and prompts for authentication
404408
CheckSecuritySchemes(spec)
405409

406-
u, _ := url.Parse(swaggerURL)
410+
u, parseErr := url.Parse(swaggerURL)
411+
if parseErr != nil {
412+
u = &url.URL{}
413+
}
407414

408415
// Gets the target server and base path from the specification file
409416
if apiTarget == "" {

0 commit comments

Comments
 (0)