From ea8ca7c6fc6179329006e1ea80cdc3b5c32717d5 Mon Sep 17 00:00:00 2001 From: Stas Dmytryshyn Date: Thu, 2 Jan 2025 23:12:56 +0100 Subject: [PATCH] feat: escape support (#3) --- cmd/proxy/main.go | 5 +++++ cmd/proxy/win.go | 11 ++++++++++- cmd/switcher/main.go | 6 +++++- pkg/escaper/escape.go | 20 ++++++++++++++++++++ pkg/escaper/unescape.go | 16 ++++++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 pkg/escaper/escape.go create mode 100644 pkg/escaper/unescape.go diff --git a/cmd/proxy/main.go b/cmd/proxy/main.go index a98a8fc..2afe42c 100644 --- a/cmd/proxy/main.go +++ b/cmd/proxy/main.go @@ -2,6 +2,9 @@ package main import ( "context" + "os" + + "github.com/rs/zerolog" logger2 "github.com/ft-t/browser-switcher/pkg/logger" ) @@ -10,6 +13,8 @@ func main() { logger := logger2.GetLogger() ctx := logger.WithContext(context.Background()) + zerolog.Ctx(ctx).Debug().Msgf("starting proxy with arguments: %v", os.Args) + if err := run(ctx); err != nil { logger.Panic().Err(err).Msg("failed to run") } diff --git a/cmd/proxy/win.go b/cmd/proxy/win.go index 5577587..c3cfbd0 100644 --- a/cmd/proxy/win.go +++ b/cmd/proxy/win.go @@ -11,6 +11,8 @@ import ( "github.com/rs/zerolog" "golang.org/x/sys/windows/registry" + + "github.com/ft-t/browser-switcher/pkg/escaper" ) func findAppRegistrationPath(ctx context.Context) (string, error) { @@ -39,11 +41,18 @@ func run(ctx context.Context) error { return err } + var escapedArgs []string + for _, arg := range os.Args[1:] { + escapedArgs = append(escapedArgs, escaper.Escape(arg)) + } + args := slices.Concat([]string{ "/C", "start", appPath, - }, os.Args[1:]) + }, escapedArgs) + + zerolog.Ctx(ctx).Debug().Msgf("running command: cmd.exe %v", escapedArgs) cmd := exec.Command( "cmd.exe", diff --git a/cmd/switcher/main.go b/cmd/switcher/main.go index aefbc6e..11926e8 100644 --- a/cmd/switcher/main.go +++ b/cmd/switcher/main.go @@ -10,6 +10,7 @@ import ( "gopkg.in/natefinch/lumberjack.v2" config2 "github.com/ft-t/browser-switcher/pkg/config" + "github.com/ft-t/browser-switcher/pkg/escaper" "github.com/ft-t/browser-switcher/pkg/launcher" "github.com/ft-t/browser-switcher/pkg/selector" "github.com/ft-t/browser-switcher/pkg/ui" @@ -37,8 +38,11 @@ func main() { } targetURL := os.Args[1] + targetURL = escaper.Unescape(targetURL) - ctx = lg.With().Str("targetURL", targetURL).Logger().WithContext(ctx) + ctx = lg.With().Str("rawTargetURL", os.Args[1]). + Str("targetURL", targetURL). + Logger().WithContext(ctx) browserConfig, err := config2.ReadConfig(ctx) diff --git a/pkg/escaper/escape.go b/pkg/escaper/escape.go new file mode 100644 index 0000000..363987a --- /dev/null +++ b/pkg/escaper/escape.go @@ -0,0 +1,20 @@ +package escaper + +import ( + "fmt" + "strings" +) + +var charsToEscape = []string{ + "&", +} + +func Escape( + input string, +) string { + for _, char := range charsToEscape { + input = strings.ReplaceAll(input, char, fmt.Sprintf(`"%s"`, char)) + } + + return input +} diff --git a/pkg/escaper/unescape.go b/pkg/escaper/unescape.go new file mode 100644 index 0000000..4622e09 --- /dev/null +++ b/pkg/escaper/unescape.go @@ -0,0 +1,16 @@ +package escaper + +import ( + "fmt" + "strings" +) + +func Unescape( + input string, +) string { + for _, char := range charsToEscape { + input = strings.ReplaceAll(input, fmt.Sprintf(`"%s"`, char), char) + } + + return input +}