Fixed issue with host string and added internal url package

This commit is contained in:
David Allen 2024-08-14 10:57:30 -06:00
parent 4444a1d299
commit 4597f63d12
No known key found for this signature in database
GPG key ID: 717C593FF60A2ACC
7 changed files with 131 additions and 115 deletions

View file

@ -3,10 +3,10 @@ package cmd
import (
"fmt"
"os/user"
"strings"
magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/internal/cache/sqlite"
urlx "github.com/OpenCHAMI/magellan/internal/url"
"github.com/OpenCHAMI/magellan/pkg/auth"
"github.com/cznic/mathutil"
"github.com/rs/zerolog/log"
@ -33,8 +33,10 @@ var collectCmd = &cobra.Command{
}
// URL sanitanization for host argument
host = strings.TrimSuffix(host, "/")
host = strings.ReplaceAll(host, "//", "/")
host, err = urlx.Sanitize(host)
if err != nil {
log.Error().Err(err).Msg("failed to sanitize host")
}
// try to load access token either from env var, file, or config if var not set
if accessToken == "" {

View file

@ -4,9 +4,8 @@ import (
"encoding/json"
"fmt"
"log"
"net/url"
"strings"
urlx "github.com/OpenCHAMI/magellan/internal/url"
"github.com/OpenCHAMI/magellan/pkg/crawler"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -25,19 +24,14 @@ var crawlCmd = &cobra.Command{
" magellan crawl https://bmc.example.com -i -u username -p password",
Args: func(cmd *cobra.Command, args []string) error {
// Validate that the only argument is a valid URI
var err error
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
parsedURI, err := url.ParseRequestURI(args[0])
args[0], err = urlx.Sanitize(args[0])
if err != nil {
return fmt.Errorf("invalid URI specified: %s", args[0])
return fmt.Errorf("failed to sanitize URI: %w", err)
}
// Remove any trailing slashes
parsedURI.Path = strings.TrimSuffix(parsedURI.Path, "/")
// Collapse any doubled slashes
parsedURI.Path = strings.ReplaceAll(parsedURI.Path, "//", "/")
// Update the URI in the args slice
args[0] = parsedURI.String()
return nil
},
Run: func(cmd *cobra.Command, args []string) {

View file

@ -9,9 +9,9 @@ import (
magellan "github.com/OpenCHAMI/magellan/internal"
"github.com/OpenCHAMI/magellan/internal/cache/sqlite"
"github.com/OpenCHAMI/magellan/pkg/client"
"github.com/rs/zerolog/log"
urlx "github.com/OpenCHAMI/magellan/internal/url"
"github.com/cznic/mathutil"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -72,8 +72,8 @@ var scanCmd = &cobra.Command{
}
// format and combine flag and positional args
targetHosts = append(targetHosts, client.FormatHostUrls(args, ports, scheme, verbose)...)
targetHosts = append(targetHosts, client.FormatHostUrls(hosts, ports, scheme, verbose)...)
targetHosts = append(targetHosts, urlx.FormatHosts(args, ports, scheme, verbose)...)
targetHosts = append(targetHosts, urlx.FormatHosts(hosts, ports, scheme, verbose)...)
// add more hosts specified with `--subnet` flag
if debug {