-
Notifications
You must be signed in to change notification settings - Fork 3k
fix: improve URL parsing error handling in ListInputProvider #6448
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,7 +5,6 @@ package list | |||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||
| "bufio" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "regexp" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -39,6 +38,7 @@ const DefaultMaxDedupeItemsCount = 10000 | |||||||||||||||||||||||||||||||||||||||||||||||
| // it supports list type of input ex: urls,file,stdin,uncover,etc. (i.e just url not complete request/response) | ||||||||||||||||||||||||||||||||||||||||||||||||
| type ListInputProvider struct { | ||||||||||||||||||||||||||||||||||||||||||||||||
| ipOptions *ipOptions | ||||||||||||||||||||||||||||||||||||||||||||||||
| options *types.Options | ||||||||||||||||||||||||||||||||||||||||||||||||
| inputCount int64 | ||||||||||||||||||||||||||||||||||||||||||||||||
| excludedCount int64 | ||||||||||||||||||||||||||||||||||||||||||||||||
| dupeCount int64 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -71,6 +71,7 @@ func New(opts *Options) (*ListInputProvider, error) { | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| input := &ListInputProvider{ | ||||||||||||||||||||||||||||||||||||||||||||||||
| hostMap: hm, | ||||||||||||||||||||||||||||||||||||||||||||||||
| options: options, | ||||||||||||||||||||||||||||||||||||||||||||||||
| ipOptions: &ipOptions{ | ||||||||||||||||||||||||||||||||||||||||||||||||
| ScanAllIPs: options.ScanAllIPs, | ||||||||||||||||||||||||||||||||||||||||||||||||
| IPV4: sliceutil.Contains(options.IPVersion, "4"), | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -147,16 +148,22 @@ func (i *ListInputProvider) Set(executionId string, value string) { | |||||||||||||||||||||||||||||||||||||||||||||||
| // parse hostname if url is given | ||||||||||||||||||||||||||||||||||||||||||||||||
| urlx, err := urlutil.Parse(URL) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil || (urlx != nil && urlx.Host == "") { | ||||||||||||||||||||||||||||||||||||||||||||||||
| gologger.Debug().Label("url").MsgFunc(func() string { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Sprintf("failed to parse url %v got %v skipping ip selection", URL, err) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Sprintf("got empty hostname for %v skipping ip selection", URL) | ||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||
| metaInput := contextargs.NewMetaInput() | ||||||||||||||||||||||||||||||||||||||||||||||||
| metaInput.Input = URL | ||||||||||||||||||||||||||||||||||||||||||||||||
| i.setItem(metaInput) | ||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
| gologger.Error().Msgf("Failed to parse URL %v: %v", URL, err) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| gologger.Error().Msgf("Invalid hostname for URL %v", URL) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if this is single target mode (-u) or batch mode (-l) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if i.options.TargetsFilePath == "" && len(i.options.Targets) <= 1 { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Single target mode: exit immediately | ||||||||||||||||||||||||||||||||||||||||||||||||
| gologger.Info().Msg("Stopping scan due to invalid target") | ||||||||||||||||||||||||||||||||||||||||||||||||
| os.Exit(1) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Batch mode: log error and skip this target | ||||||||||||||||||||||||||||||||||||||||||||||||
| gologger.Warning().Msgf("Skipping invalid target: %v", URL) | ||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if input is ip or hostname | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -391,16 +398,22 @@ func (i *ListInputProvider) Del(executionId string, value string) { | |||||||||||||||||||||||||||||||||||||||||||||||
| // parse hostname if url is given | ||||||||||||||||||||||||||||||||||||||||||||||||
| urlx, err := urlutil.Parse(URL) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil || (urlx != nil && urlx.Host == "") { | ||||||||||||||||||||||||||||||||||||||||||||||||
| gologger.Debug().Label("url").MsgFunc(func() string { | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Sprintf("failed to parse url %v got %v skipping ip selection", URL, err) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Sprintf("got empty hostname for %v skipping ip selection", URL) | ||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||
| metaInput := contextargs.NewMetaInput() | ||||||||||||||||||||||||||||||||||||||||||||||||
| metaInput.Input = URL | ||||||||||||||||||||||||||||||||||||||||||||||||
| i.delItem(metaInput) | ||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||
| gologger.Error().Msgf("Failed to parse URL %v: %v", URL, err) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| gologger.Error().Msgf("Invalid hostname for URL %v", URL) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if this is single target mode (-u) or batch mode (-l) | ||||||||||||||||||||||||||||||||||||||||||||||||
| if i.options.TargetsFilePath == "" && len(i.options.Targets) <= 1 { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Single target mode: exit immediately | ||||||||||||||||||||||||||||||||||||||||||||||||
| gologger.Info().Msg("Stopping scan due to invalid target") | ||||||||||||||||||||||||||||||||||||||||||||||||
| os.Exit(1) | ||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Batch mode: log error and skip this target | ||||||||||||||||||||||||||||||||||||||||||||||||
| gologger.Warning().Msgf("Skipping invalid target: %v", URL) | ||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+401
to
+416
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Don’t os.Exit from Del; treat invalid excludes as non-fatal and skip. Exiting from Del can kill a scan due to an invalid exclude pattern even in -l/stdin runs. Keep it a single warning and continue. - if err != nil {
- gologger.Error().Msgf("Failed to parse URL %v: %v", URL, err)
- } else {
- gologger.Error().Msgf("Invalid hostname for URL %v", URL)
- }
-
- // Check if this is single target mode (-u) or batch mode (-l)
- if i.options.TargetsFilePath == "" && len(i.options.Targets) <= 1 {
- // Single target mode: exit immediately
- gologger.Info().Msg("Stopping scan due to invalid target")
- os.Exit(1)
- } else {
- // Batch mode: log error and skip this target
- gologger.Warning().Msgf("Skipping invalid target: %v", URL)
- return
- }
+ reason := "empty hostname"
+ if err != nil {
+ reason = err.Error()
+ }
+ gologger.Warning().Msgf("Skipping invalid exclude target %v: %v", URL, reason)
+ return📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // Check if input is ip or hostname | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Invalid-target handling: wrong single-target detection, duplicate logs, and os.Exit in library code.
Minimum fix: correct detection and emit a single message; keep exit only if you must. Suggested patch:
Add this helper once in the file:
If feasible, prefer bubbling an error to the caller instead of os.Exit to keep package-level code side-effect free. I can draft that refactor if you want.
🤖 Prompt for AI Agents