Skip to content

Commit

Permalink
fix: this should do the trick and user inputs shouldn't have any \n left
Browse files Browse the repository at this point in the history
  • Loading branch information
equals215 committed May 24, 2024
1 parent 24afe15 commit a1a2f86
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
8 changes: 4 additions & 4 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package main
import (
"fmt"
"os"
"strings"

"github.com/equals215/deepsentinel/agent"
"github.com/equals215/deepsentinel/config"
"github.com/equals215/deepsentinel/daemonize"
"github.com/equals215/deepsentinel/utils"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -66,21 +66,21 @@ func installCmd(rootCmd *cobra.Command) {

fmt.Print("Input the server address (ex.: http://localhost:8080): ")
fmt.Scanln(&serverAddress)
err = config.AgentSetServerAddress(strings.ReplaceAll(serverAddress, "\n", ""))
err = config.AgentSetServerAddress(utils.CleanString(serverAddress))
if err != nil {
return err
}

fmt.Print("Input the auth token: ")
fmt.Scanln(&authToken)
err = config.AgentSetAuthToken(strings.ReplaceAll(authToken, "\n", ""))
err = config.AgentSetAuthToken(utils.CleanString(authToken))
if err != nil {
return err
}

fmt.Print("Input the machine name: ")
fmt.Scanln(&machineName)
err = config.AgentSetMachineName(strings.ReplaceAll(machineName, "\n", ""))
err = config.AgentSetMachineName(utils.CleanString(machineName))
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions config/agent_live_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"net/url"

"github.com/equals215/deepsentinel/utils"
"github.com/mrz1836/go-sanitize"
"github.com/spf13/viper"
)

Expand All @@ -20,7 +22,7 @@ func AgentSetServerAddress(args ...any) error {
return fmt.Errorf("too many arguments")
}

address := args[0].(string)
address := sanitize.URL(args[0].(string))
url, err := url.Parse(address)
if err != nil {
return fmt.Errorf("Invalid URL: %s", err)
Expand Down Expand Up @@ -49,7 +51,7 @@ func AgentSetAuthToken(args ...any) error {
return fmt.Errorf("too many arguments")
}

token := args[0].(string)
token := utils.CleanString(args[0].(string))

Agent.Lock()
viper.Set("auth-token", token)
Expand All @@ -71,7 +73,7 @@ func AgentSetMachineName(args ...any) error {
return fmt.Errorf("too many arguments")
}

name := args[0].(string)
name := utils.CleanString(args[0].(string))

Agent.Lock()
viper.Set("machine-name", name)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/grongor/panicwatch v1.2.0
github.com/jxsl13/osfacts v0.4.0
github.com/kristinjeanna/redact v1.0.0
github.com/mrz1836/go-sanitize v1.3.2
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mrz1836/go-sanitize v1.3.2 h1:sGhusPxP4L+7NAUVAUl/WrrBazNSNECsvYh3yumuJXQ=
github.com/mrz1836/go-sanitize v1.3.2/go.mod h1:wvRS2ALFDxOCK3ORQPwKUxl7HTIBUV8S3U34Hwn96r4=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
16 changes: 16 additions & 0 deletions utils/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package utils

import (
"strings"
"unicode"
)

// CleanString removes all non-graphic characters from a string
func CleanString(dirty string) string {
return strings.Map(func(r rune) rune {
if unicode.IsGraphic(r) {
return r
}
return -1
}, dirty)
}

0 comments on commit a1a2f86

Please sign in to comment.