Skip to content

Commit cca453b

Browse files
committed
fix the dropToSudo issue and update GoRelease accordingly
1 parent 674280c commit cca453b

File tree

5 files changed

+80
-44
lines changed

5 files changed

+80
-44
lines changed

.goreleaser.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
version: 2
12
project_name: reflex
23

34
builds:
@@ -18,12 +19,7 @@ builds:
1819

1920
archives:
2021
- id: archive
21-
builds:
22-
- reflex
2322
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
24-
format_overrides:
25-
- goos: windows
26-
format: zip
2723
files:
2824
- README.md
2925

internal/browser/browser.go

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ package browser
22

33
import (
44
"errors"
5-
"fmt"
65
"os"
76
"os/exec"
8-
"os/user"
97
"path/filepath"
108
"runtime"
11-
"strconv"
129
"strings"
13-
"syscall"
1410
)
1511

1612
// Open attempts to open the url in a browser cross-platform.
@@ -94,40 +90,7 @@ func appExists(path string) bool {
9490
// dropToSudoUser modifies cmd to run as the SUDO_USER if the current process
9591
// is running with uid 0. It also adjusts common environment variables so the
9692
// desktop session (DBus/XDG) can be discovered.
97-
func dropToSudoUser(cmd *exec.Cmd) {
98-
if runtime.GOOS == "windows" {
99-
return
100-
}
101-
if os.Geteuid() != 0 {
102-
return
103-
}
104-
sudoUser := os.Getenv("SUDO_USER")
105-
if sudoUser == "" {
106-
return
107-
}
108-
u, err := user.Lookup(sudoUser)
109-
if err != nil {
110-
return
111-
}
112-
uid, _ := strconv.Atoi(u.Uid)
113-
gid, _ := strconv.Atoi(u.Gid)
114-
cmd.SysProcAttr = &syscall.SysProcAttr{Credential: &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)}}
115-
116-
env := os.Environ()
117-
env = setEnv(env, "HOME", u.HomeDir)
118-
env = setEnv(env, "USER", sudoUser)
119-
env = setEnv(env, "LOGNAME", sudoUser)
120-
if runtime.GOOS == "linux" {
121-
xdg := fmt.Sprintf("/run/user/%d", uid)
122-
if st, err := os.Stat(xdg); err == nil && st.IsDir() {
123-
env = setEnv(env, "XDG_RUNTIME_DIR", xdg)
124-
if !hasEnv(env, "DBUS_SESSION_BUS_ADDRESS") {
125-
env = append(env, "DBUS_SESSION_BUS_ADDRESS=unix:path="+filepath.Join(xdg, "bus"))
126-
}
127-
}
128-
}
129-
cmd.Env = env
130-
}
93+
// dropToSudoUser is implemented per-OS in drop_*.go.
13194

13295
func setEnv(env []string, key, value string) []string {
13396
prefix := key + "="
@@ -149,4 +112,3 @@ func hasEnv(env []string, key string) bool {
149112
}
150113
return false
151114
}
152-

internal/browser/drop_darwin.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package browser
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"os/user"
7+
)
8+
9+
// On macOS, adjust env to the sudo-invoking user so GUI apps attach
10+
// to the right session. Avoid SysProcAttr credential fields (not portable).
11+
func dropToSudoUser(cmd *exec.Cmd) {
12+
if os.Geteuid() != 0 {
13+
return
14+
}
15+
sudoUser := os.Getenv("SUDO_USER")
16+
if sudoUser == "" {
17+
return
18+
}
19+
u, err := user.Lookup(sudoUser)
20+
if err != nil {
21+
return
22+
}
23+
env := os.Environ()
24+
env = setEnv(env, "HOME", u.HomeDir)
25+
env = setEnv(env, "USER", sudoUser)
26+
env = setEnv(env, "LOGNAME", sudoUser)
27+
cmd.Env = env
28+
}

internal/browser/drop_linux.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package browser
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"os/user"
8+
"path/filepath"
9+
"strconv"
10+
"syscall"
11+
)
12+
13+
// On Linux, drop privileges to the invoking sudo user so the browser opens
14+
// in the user's desktop session. Also set XDG/DBus envs when possible.
15+
func dropToSudoUser(cmd *exec.Cmd) {
16+
if os.Geteuid() != 0 {
17+
return
18+
}
19+
sudoUser := os.Getenv("SUDO_USER")
20+
if sudoUser == "" {
21+
return
22+
}
23+
u, err := user.Lookup(sudoUser)
24+
if err != nil {
25+
return
26+
}
27+
uid, _ := strconv.Atoi(u.Uid)
28+
gid, _ := strconv.Atoi(u.Gid)
29+
cmd.SysProcAttr = &syscall.SysProcAttr{Credential: &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)}}
30+
31+
env := os.Environ()
32+
env = setEnv(env, "HOME", u.HomeDir)
33+
env = setEnv(env, "USER", sudoUser)
34+
env = setEnv(env, "LOGNAME", sudoUser)
35+
36+
xdg := fmt.Sprintf("/run/user/%d", uid)
37+
if st, err := os.Stat(xdg); err == nil && st.IsDir() {
38+
env = setEnv(env, "XDG_RUNTIME_DIR", xdg)
39+
if !hasEnv(env, "DBUS_SESSION_BUS_ADDRESS") {
40+
env = append(env, "DBUS_SESSION_BUS_ADDRESS=unix:path="+filepath.Join(xdg, "bus"))
41+
}
42+
}
43+
cmd.Env = env
44+
}

internal/browser/drop_windows.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package browser
2+
3+
import "os/exec"
4+
5+
// On Windows, do nothing special; the browser will open under the current user.
6+
func dropToSudoUser(cmd *exec.Cmd) { /* no-op */ }

0 commit comments

Comments
 (0)