From bba94a62d0dc2861833e81224b6e0395d27b51c7 Mon Sep 17 00:00:00 2001 From: Mike Sawka Date: Fri, 24 Jan 2025 14:24:15 -0800 Subject: [PATCH] add jwt token back to wsl connections (#1841) Co-authored-by: Evan Simkowitz --- cmd/wsh/cmd/wshcmd-setbg.go | 2 +- frontend/app/app-bg.tsx | 2 +- pkg/remote/connparse/connparse.go | 16 +++++++++++----- pkg/shellexec/shellexec.go | 4 ++++ pkg/util/shellutil/tokenswap.go | 1 - pkg/wshrpc/wshserver/wshserver.go | 14 ++++++++++++++ 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/cmd/wsh/cmd/wshcmd-setbg.go b/cmd/wsh/cmd/wshcmd-setbg.go index bbd207135..be57b9abd 100644 --- a/cmd/wsh/cmd/wshcmd-setbg.go +++ b/cmd/wsh/cmd/wshcmd-setbg.go @@ -140,7 +140,7 @@ func setBgRun(cmd *cobra.Command, args []string) (rtnErr error) { } // Create URL-safe path - escapedPath := strings.ReplaceAll(absPath, "\\", "\\\\") + escapedPath := filepath.ToSlash(absPath) escapedPath = strings.ReplaceAll(escapedPath, "'", "\\'") bgStyle = fmt.Sprintf("url('%s')", escapedPath) diff --git a/frontend/app/app-bg.tsx b/frontend/app/app-bg.tsx index 8b1006de2..2a06f6d22 100644 --- a/frontend/app/app-bg.tsx +++ b/frontend/app/app-bg.tsx @@ -54,7 +54,7 @@ function processBackgroundUrls(cssText: string): string { return; } // allow absolute paths - if (originalUrl.startsWith("/") || originalUrl.startsWith("~/")) { + if (originalUrl.startsWith("/") || originalUrl.startsWith("~/") || /^[a-zA-Z]:(\/|\\)/.test(originalUrl)) { const newUrl = encodeFileURL(originalUrl); node.value = newUrl; return; diff --git a/pkg/remote/connparse/connparse.go b/pkg/remote/connparse/connparse.go index 4f045e637..1114c3dfd 100644 --- a/pkg/remote/connparse/connparse.go +++ b/pkg/remote/connparse/connparse.go @@ -23,6 +23,7 @@ const ( ) var windowsDriveRegex = regexp.MustCompile(`^[a-zA-Z]:`) +var wslConnRegex = regexp.MustCompile(`^wsl://[^/]+`) type Connection struct { Scheme string @@ -117,12 +118,17 @@ func ParseURI(uri string) (*Connection, error) { remotePath = rest } } else { - split = strings.SplitN(rest, "/", 2) - host = split[0] - if len(split) > 1 { - remotePath = split[1] + if strings.HasPrefix(rest, "wsl://") { + host = wslConnRegex.FindString(rest) + remotePath = strings.TrimPrefix(rest, host) } else { - remotePath = "/" + split = strings.SplitN(rest, "/", 2) + host = split[0] + if len(split) > 1 { + remotePath = split[1] + } else { + remotePath = "/" + } } } diff --git a/pkg/shellexec/shellexec.go b/pkg/shellexec/shellexec.go index 1f9324116..93c866126 100644 --- a/pkg/shellexec/shellexec.go +++ b/pkg/shellexec/shellexec.go @@ -262,6 +262,10 @@ func StartWslShellProc(ctx context.Context, termSize waveobj.TermSize, cmdStr st conn.Debugf(ctx, "packed swaptoken %s\n", packedToken) cmdCombined = fmt.Sprintf(`%s=%s %s`, wavebase.WaveSwapTokenVarName, packedToken, cmdCombined) } + jwtToken := cmdOpts.SwapToken.Env[wavebase.WaveJwtTokenVarName] + if jwtToken != "" { + cmdCombined = fmt.Sprintf(`%s=%s %s`, wavebase.WaveJwtTokenVarName, jwtToken, cmdCombined) + } log.Printf("full combined command: %s", cmdCombined) ecmd := exec.Command("wsl.exe", "~", "-d", client.Name(), "--", "sh", "-c", cmdCombined) if termSize.Rows == 0 || termSize.Cols == 0 { diff --git a/pkg/util/shellutil/tokenswap.go b/pkg/util/shellutil/tokenswap.go index 89a16c618..339ec6445 100644 --- a/pkg/util/shellutil/tokenswap.go +++ b/pkg/util/shellutil/tokenswap.go @@ -20,7 +20,6 @@ type TokenSwapEntry struct { Token string `json:"token"` SockName string `json:"sockname,omitempty"` RpcContext *wshrpc.RpcContext `json:"rpccontext,omitempty"` - JwtToken string `json:"jwttoken,omitempty"` Env map[string]string `json:"env,omitempty"` ScriptText string `json:"scripttext,omitempty"` Exp time.Time `json:"-"` diff --git a/pkg/wshrpc/wshserver/wshserver.go b/pkg/wshrpc/wshserver/wshserver.go index edc7f7629..fd575872b 100644 --- a/pkg/wshrpc/wshserver/wshserver.go +++ b/pkg/wshrpc/wshserver/wshserver.go @@ -28,6 +28,7 @@ import ( "github.com/wavetermdev/waveterm/pkg/remote/fileshare" "github.com/wavetermdev/waveterm/pkg/telemetry" "github.com/wavetermdev/waveterm/pkg/util/envutil" + "github.com/wavetermdev/waveterm/pkg/util/shellutil" "github.com/wavetermdev/waveterm/pkg/util/utilfn" "github.com/wavetermdev/waveterm/pkg/util/wavefileutil" "github.com/wavetermdev/waveterm/pkg/waveai" @@ -51,6 +52,19 @@ func (*WshServer) WshServerImpl() {} var WshServerImpl = WshServer{} +// TODO remove this after implementing in multiproxy, just for wsl +func (ws *WshServer) AuthenticateTokenCommand(ctx context.Context, data wshrpc.CommandAuthenticateTokenData) (wshrpc.CommandAuthenticateRtnData, error) { + entry := shellutil.GetAndRemoveTokenSwapEntry(data.Token) + if entry == nil { + return wshrpc.CommandAuthenticateRtnData{}, fmt.Errorf("invalid token") + } + rtn := wshrpc.CommandAuthenticateRtnData{ + Env: entry.Env, + InitScriptText: entry.ScriptText, + } + return rtn, nil +} + func (ws *WshServer) TestCommand(ctx context.Context, data string) error { defer func() { panichandler.PanicHandler("TestCommand", recover())