Skip to content

Commit 7e6f963

Browse files
authored
fix for waveterm environment variables leaking from prod to dev (#1153)
1 parent cbb8259 commit 7e6f963

File tree

8 files changed

+114
-76
lines changed

8 files changed

+114
-76
lines changed

Taskfile.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ tasks:
185185
generate:
186186
desc: Generate Typescript bindings for the Go backend.
187187
cmds:
188-
- NO_PANIC=1 go run cmd/generatets/main-generatets.go
189-
- NO_PANIC=1 go run cmd/generatego/main-generatego.go
188+
- go run cmd/generatets/main-generatets.go
189+
- go run cmd/generatego/main-generatego.go
190190
sources:
191191
- "cmd/generatego/*.go"
192192
- "cmd/generatets/*.go"

cmd/server/main-server.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"os"
1111
"os/signal"
1212
"runtime/debug"
13-
"strconv"
1413

1514
"runtime"
1615
"sync"
@@ -46,8 +45,6 @@ const InitialTelemetryWait = 10 * time.Second
4645
const TelemetryTick = 2 * time.Minute
4746
const TelemetryInterval = 4 * time.Hour
4847

49-
const ReadySignalPidVarName = "WAVETERM_READY_SIGNAL_PID"
50-
5148
var shutdownOnce sync.Once
5249

5350
func doShutdown(reason string) {
@@ -166,15 +163,31 @@ func createMainWshClient() {
166163
wshutil.DefaultRouter.RegisterRoute(wshutil.MakeConnectionRouteId(wshrpc.LocalConnName), localConnWsh, true)
167164
}
168165

166+
func grabAndRemoveEnvVars() error {
167+
err := authkey.SetAuthKeyFromEnv()
168+
if err != nil {
169+
return fmt.Errorf("setting auth key: %v", err)
170+
}
171+
err = wavebase.CacheAndRemoveEnvVars()
172+
if err != nil {
173+
return err
174+
}
175+
err = wcloud.CacheAndRemoveEnvVars()
176+
if err != nil {
177+
return err
178+
}
179+
return nil
180+
}
181+
169182
func main() {
170183
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
171184
log.SetPrefix("[wavesrv] ")
172185
wavebase.WaveVersion = WaveVersion
173186
wavebase.BuildTime = BuildTime
174187

175-
err := authkey.SetAuthKeyFromEnv()
188+
err := grabAndRemoveEnvVars()
176189
if err != nil {
177-
log.Printf("error setting auth key: %v\n", err)
190+
log.Printf("[error] %v\n", err)
178191
return
179192
}
180193
err = service.ValidateServiceMap()
@@ -279,17 +292,11 @@ func main() {
279292
return
280293
}
281294
go func() {
282-
pidStr := os.Getenv(ReadySignalPidVarName)
283-
if pidStr != "" {
284-
_, err := strconv.Atoi(pidStr)
285-
if err == nil {
286-
if BuildTime == "" {
287-
BuildTime = "0"
288-
}
289-
// use fmt instead of log here to make sure it goes directly to stderr
290-
fmt.Fprintf(os.Stderr, "WAVESRV-ESTART ws:%s web:%s version:%s buildtime:%s\n", wsListener.Addr(), webListener.Addr(), WaveVersion, BuildTime)
291-
}
295+
if BuildTime == "" {
296+
BuildTime = "0"
292297
}
298+
// use fmt instead of log here to make sure it goes directly to stderr
299+
fmt.Fprintf(os.Stderr, "WAVESRV-ESTART ws:%s web:%s version:%s buildtime:%s\n", wsListener.Addr(), webListener.Addr(), WaveVersion, BuildTime)
293300
}()
294301
go wshutil.RunWshRpcOverListener(unixListener)
295302
web.RunWebServer(webListener) // blocking

emain/authkey.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ipcMain } from "electron";
55
import { getWebServerEndpoint, getWSServerEndpoint } from "../frontend/util/endpoints";
66

77
const AuthKeyHeader = "X-AuthKey";
8-
export const AuthKeyEnv = "AUTH_KEY";
8+
export const WaveAuthKeyEnv = "WAVETERM_AUTH_KEY";
99
export const AuthKey = crypto.randomUUID();
1010

1111
ipcMain.on("get-auth-key", (event) => {

emain/emain-wavesrv.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as electron from "electron";
55
import * as child_process from "node:child_process";
66
import * as readline from "readline";
77
import { WebServerEndpointVarName, WSServerEndpointVarName } from "../frontend/util/endpoints";
8-
import { AuthKey, AuthKeyEnv } from "./authkey";
8+
import { AuthKey, WaveAuthKeyEnv } from "./authkey";
99
import { setForceQuit } from "./emain-activity";
1010
import { WaveAppPathVarName } from "./emain-util";
1111
import {
@@ -19,8 +19,6 @@ import {
1919
} from "./platform";
2020
import { updater } from "./updater";
2121

22-
export const WaveSrvReadySignalPidVarName = "WAVETERM_READY_SIGNAL_PID";
23-
2422
let isWaveSrvDead = false;
2523
let waveSrvProc: child_process.ChildProcessWithoutNullStreams | null = null;
2624
let WaveVersion = "unknown"; // set by WAVESRV-ESTART
@@ -56,8 +54,7 @@ export function runWaveSrv(handleWSEvent: (evtMsg: WSEventType) => void): Promis
5654
});
5755
const envCopy = { ...process.env };
5856
envCopy[WaveAppPathVarName] = getElectronAppUnpackedBasePath();
59-
envCopy[WaveSrvReadySignalPidVarName] = process.pid.toString();
60-
envCopy[AuthKeyEnv] = AuthKey;
57+
envCopy[WaveAuthKeyEnv] = AuthKey;
6158
envCopy[WaveDataHomeVarName] = getWaveDataDir();
6259
envCopy[WaveConfigHomeVarName] = getWaveConfigDir();
6360
const waveSrvCmd = getWaveSrvPath();

pkg/authkey/authkey.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
var authkey string
1313

14-
const AuthKeyEnv = "AUTH_KEY"
14+
const WaveAuthKeyEnv = "WAVETERM_AUTH_KEY"
1515
const AuthKeyHeader = "X-AuthKey"
1616

1717
func ValidateIncomingRequest(r *http.Request) error {
@@ -26,11 +26,11 @@ func ValidateIncomingRequest(r *http.Request) error {
2626
}
2727

2828
func SetAuthKeyFromEnv() error {
29-
authkey = os.Getenv(AuthKeyEnv)
29+
authkey = os.Getenv(WaveAuthKeyEnv)
3030
if authkey == "" {
3131
return fmt.Errorf("no auth key found in environment variables")
3232
}
33-
os.Setenv(AuthKeyEnv, "")
33+
os.Unsetenv(WaveAuthKeyEnv)
3434
return nil
3535
}
3636

pkg/util/panic/panic.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

pkg/wavebase/wavebase.go

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,25 @@ import (
1717
"strings"
1818
"sync"
1919
"time"
20-
21-
"github.com/wavetermdev/waveterm/pkg/util/panic"
2220
)
2321

2422
// set by main-server.go
2523
var WaveVersion = "0.0.0"
2624
var BuildTime = "0"
2725

28-
const WaveConfigHomeEnvVar = "WAVETERM_CONFIG_HOME"
29-
const WaveDataHomeEnvVar = "WAVETERM_DATA_HOME"
30-
const WaveDevVarName = "WAVETERM_DEV"
26+
const (
27+
WaveConfigHomeEnvVar = "WAVETERM_CONFIG_HOME"
28+
WaveDataHomeEnvVar = "WAVETERM_DATA_HOME"
29+
WaveAppPathVarName = "WAVETERM_APP_PATH"
30+
WaveDevVarName = "WAVETERM_DEV"
31+
WaveDevViteVarName = "WAVETERM_DEV_VITE"
32+
)
33+
34+
var ConfigHome_VarCache string // caches WAVETERM_CONFIG_HOME
35+
var DataHome_VarCache string // caches WAVETERM_DATA_HOME
36+
var AppPath_VarCache string // caches WAVETERM_APP_PATH
37+
var Dev_VarCache string // caches WAVETERM_DEV
38+
3139
const WaveLockFile = "wave.lock"
3240
const DomainSocketBaseName = "wave.sock"
3341
const RemoteDomainSocketBaseName = "wave-remote.sock"
@@ -37,7 +45,6 @@ const ConfigDir = "config"
3745

3846
var RemoteWaveHome = ExpandHomeDirSafe("~/.waveterm")
3947

40-
const WaveAppPathVarName = "WAVETERM_APP_PATH"
4148
const AppPathBinDir = "bin"
4249

4350
var baseLock = &sync.Mutex{}
@@ -47,13 +54,39 @@ type FDLock interface {
4754
Close() error
4855
}
4956

57+
func CacheAndRemoveEnvVars() error {
58+
ConfigHome_VarCache = os.Getenv(WaveConfigHomeEnvVar)
59+
if ConfigHome_VarCache == "" {
60+
return fmt.Errorf(WaveConfigHomeEnvVar + " not set")
61+
}
62+
os.Unsetenv(WaveConfigHomeEnvVar)
63+
DataHome_VarCache = os.Getenv(WaveDataHomeEnvVar)
64+
if DataHome_VarCache == "" {
65+
return fmt.Errorf("%s not set", WaveDataHomeEnvVar)
66+
}
67+
os.Unsetenv(WaveDataHomeEnvVar)
68+
AppPath_VarCache = os.Getenv(WaveAppPathVarName)
69+
os.Unsetenv(WaveAppPathVarName)
70+
Dev_VarCache = os.Getenv(WaveDevVarName)
71+
os.Unsetenv(WaveDevVarName)
72+
os.Unsetenv(WaveDevViteVarName)
73+
return nil
74+
}
75+
5076
func IsDevMode() bool {
51-
pdev := os.Getenv(WaveDevVarName)
52-
return pdev != ""
77+
return Dev_VarCache != ""
5378
}
5479

5580
func GetWaveAppPath() string {
56-
return os.Getenv(WaveAppPathVarName)
81+
return AppPath_VarCache
82+
}
83+
84+
func GetWaveDataDir() string {
85+
return DataHome_VarCache
86+
}
87+
88+
func GetWaveConfigDir() string {
89+
return ConfigHome_VarCache
5790
}
5891

5992
func GetWaveAppBinPath() string {
@@ -108,22 +141,6 @@ func GetRemoteDomainSocketName() string {
108141
return filepath.Join(RemoteWaveHome, RemoteDomainSocketBaseName)
109142
}
110143

111-
func GetWaveDataDir() string {
112-
retVal, found := os.LookupEnv(WaveDataHomeEnvVar)
113-
if !found {
114-
panic.Panic(WaveDataHomeEnvVar + " not set")
115-
}
116-
return retVal
117-
}
118-
119-
func GetWaveConfigDir() string {
120-
retVal, found := os.LookupEnv(WaveConfigHomeEnvVar)
121-
if !found {
122-
panic.Panic(WaveConfigHomeEnvVar + " not set")
123-
}
124-
return retVal
125-
}
126-
127144
func EnsureWaveDataDir() error {
128145
return CacheEnsureDir(GetWaveDataDir(), "wavehome", 0700, "wave home directory")
129146
}

pkg/wcloud/wcloud.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const WCloudEndpointVarName = "WCLOUD_ENDPOINT"
2727
const WCloudWSEndpoint = "wss://wsapi.waveterm.dev/"
2828
const WCloudWSEndpointVarName = "WCLOUD_WS_ENDPOINT"
2929

30+
var WCloudWSEndpoint_VarCache string
31+
var WCloudEndpoint_VarCache string
32+
3033
const APIVersion = 1
3134
const MaxPtyUpdateSize = (128 * 1024)
3235
const MaxUpdatesPerReq = 10
@@ -43,27 +46,56 @@ const TelemetryUrl = "/telemetry"
4346
const NoTelemetryUrl = "/no-telemetry"
4447
const WebShareUpdateUrl = "/auth/web-share-update"
4548

46-
func GetEndpoint() string {
49+
func CacheAndRemoveEnvVars() error {
50+
WCloudEndpoint_VarCache = os.Getenv(WCloudEndpointVarName)
51+
err := checkEndpointVar(WCloudEndpoint_VarCache, "wcloud endpoint", WCloudEndpointVarName)
52+
if err != nil {
53+
return err
54+
}
55+
os.Unsetenv(WCloudEndpointVarName)
56+
WCloudWSEndpoint_VarCache = os.Getenv(WCloudWSEndpointVarName)
57+
err = checkWSEndpointVar(WCloudWSEndpoint_VarCache, "wcloud ws endpoint", WCloudWSEndpointVarName)
58+
if err != nil {
59+
return err
60+
}
61+
os.Unsetenv(WCloudWSEndpointVarName)
62+
return nil
63+
}
64+
65+
func checkEndpointVar(endpoint string, debugName string, varName string) error {
4766
if !wavebase.IsDevMode() {
48-
return WCloudEndpoint
67+
return nil
4968
}
50-
endpoint := os.Getenv(WCloudEndpointVarName)
5169
if endpoint == "" || !strings.HasPrefix(endpoint, "https://") {
52-
log.Printf("Invalid wcloud dev endpoint, WCLOUD_ENDPOINT not set or invalid\n")
53-
return ""
70+
return fmt.Errorf("invalid %s, %s not set or invalid", debugName, varName)
71+
}
72+
return nil
73+
}
74+
75+
func checkWSEndpointVar(endpoint string, debugName string, varName string) error {
76+
if !wavebase.IsDevMode() {
77+
return nil
5478
}
79+
log.Printf("checking endpoint %q\n", endpoint)
80+
if endpoint == "" || !strings.HasPrefix(endpoint, "wss://") {
81+
return fmt.Errorf("invalid %s, %s not set or invalid", debugName, varName)
82+
}
83+
return nil
84+
}
85+
86+
func GetEndpoint() string {
87+
if !wavebase.IsDevMode() {
88+
return WCloudEndpoint
89+
}
90+
endpoint := WCloudEndpoint_VarCache
5591
return endpoint
5692
}
5793

5894
func GetWSEndpoint() string {
5995
if !wavebase.IsDevMode() {
6096
return WCloudWSEndpoint
6197
}
62-
endpoint := os.Getenv(WCloudWSEndpointVarName)
63-
if endpoint == "" || !strings.HasPrefix(endpoint, "wss://") {
64-
log.Printf("Invalid wcloud ws dev endpoint, WCLOUD_WS_ENDPOINT not set or invalid\n")
65-
return ""
66-
}
98+
endpoint := WCloudWSEndpoint_VarCache
6799
return endpoint
68100
}
69101

0 commit comments

Comments
 (0)