Skip to content

Open-source AI podcast clipper. Generate vertical clips with face tracking and burned-in captions. CLI, MCP server, web app.

Refuse to run under sudo instead of taking the user's install (#161)

Nika Siradze54444e91149d
diff --git a/cli/VERSION b/cli/VERSION
index 24ba9a3..860487c 100644
--- a/cli/VERSION
+++ b/cli/VERSION
@@ -1 +1 @@
-2.7.0
+2.7.1
diff --git a/cli/main.go b/cli/main.go
index 304311e..c81ab87 100644
--- a/cli/main.go
+++ b/cli/main.go
@@ -22,8 +22,9 @@ import (
func main() {
os.Setenv("PODCLI_VERSION", Version)
- update.CleanupOldBinary()
args := os.Args[1:]
+ refuseSudo(args)
+ update.CleanupOldBinary()
if len(args) == 0 {
os.Exit(runEngine(args)) // backend's branded interactive menu
}
diff --git a/cli/sudo.go b/cli/sudo.go
new file mode 100644
index 0000000..8188285
--- /dev/null
+++ b/cli/sudo.go
@@ -0,0 +1,64 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "strings"
+
+ "podcli/internal/paths"
+)
+
+// sudoRefusalMessage returns the text to print, or "" when the invocation is fine.
+//
+// macOS sudo keeps HOME, so `sudo podcli` resolves the same managed dir as the
+// real user and provisions the whole runtime as root. The user is then locked
+// out of their own install, and the first symptom is an ensurepip traceback
+// several thousand files too late to read as a permissions problem.
+//
+// Keyed on SUDO_USER rather than euid: a container that legitimately runs as
+// root has its own home and must keep working.
+func sudoRefusalMessage(sudoUser, home string, rootOwned bool) string {
+ if sudoUser == "" {
+ return ""
+ }
+ var b strings.Builder
+ b.WriteString("podcli: refusing to run under sudo.\n\n")
+ b.WriteString(" Sudo keeps your HOME, so everything installed lands in\n")
+ b.WriteString(" " + home + "\n")
+ b.WriteString(" owned by root, and you lose access to your own install.\n")
+ b.WriteString(" podcli needs no elevated privileges. Run it as " + sudoUser + ".\n")
+ if rootOwned {
+ b.WriteString("\n An earlier sudo run already left root-owned files there. Repair with:\n")
+ b.WriteString(fmt.Sprintf(" sudo chown -R %s %s\n", shellQuote(sudoUser), shellQuote(home)))
+ }
+ return b.String()
+}
+
+func shellQuote(s string) string {
+ if !strings.ContainsAny(s, " \t\n\"'$`\\") {
+ return s
+ }
+ return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
+}
+
+// refuseSudo prints and exits when podcli was invoked through sudo.
+//
+// uninstall is exempt: once a previous sudo run has taken the directory, root
+// is the only thing that can remove it, and refusing there would trap the user
+// this guard exists for.
+func refuseSudo(args []string) {
+ if len(args) > 0 && args[0] == "uninstall" {
+ return
+ }
+ sudoUser := os.Getenv("SUDO_USER")
+ if sudoUser == "" {
+ return
+ }
+ home := paths.Home()
+ msg := sudoRefusalMessage(sudoUser, home, homeHasRootOwnedFiles(home))
+ if msg == "" {
+ return
+ }
+ fmt.Fprint(os.Stderr, msg)
+ os.Exit(1)
+}
diff --git a/cli/sudo_test.go b/cli/sudo_test.go
new file mode 100644
index 0000000..d0f8d27
--- /dev/null
+++ b/cli/sudo_test.go
@@ -0,0 +1,55 @@
+package main
+
+import (
+ "strings"
+ "testing"
+)
+
+func TestNoRefusalWithoutSudo(t *testing.T) {
+ if msg := sudoRefusalMessage("", "/home/x/podcli", false); msg != "" {
+ t.Fatalf("expected no refusal, got %q", msg)
+ }
+}
+
+func TestRefusalNamesTheRealUserAndHome(t *testing.T) {
+ msg := sudoRefusalMessage("nika", "/Users/nika/Library/Application Support/podcli", false)
+ if msg == "" {
+ t.Fatal("expected a refusal")
+ }
+ for _, want := range []string{"refusing to run under sudo", "nika", "/Users/nika/Library/Application Support/podcli"} {
+ if !strings.Contains(msg, want) {
+ t.Errorf("message missing %q:\n%s", want, msg)
+ }
+ }
+ if strings.Contains(msg, "chown") {
+ t.Errorf("clean home should not suggest a repair:\n%s", msg)
+ }
+}
+
+func TestRefusalSuggestsRepairWhenAlreadyRootOwned(t *testing.T) {
+ msg := sudoRefusalMessage("nika", "/Users/nika/Library/Application Support/podcli", true)
+ want := `sudo chown -R nika '/Users/nika/Library/Application Support/podcli'`
+ if !strings.Contains(msg, want) {
+ t.Errorf("expected repair command %q in:\n%s", want, msg)
+ }
+}
+
+func TestRepairCommandQuotesTheUserName(t *testing.T) {
+ msg := sudoRefusalMessage("od d; rm -rf /", "/home/x/podcli", true)
+ want := `sudo chown -R 'od d; rm -rf /' /home/x/podcli`
+ if !strings.Contains(msg, want) {
+ t.Errorf("expected the name quoted, got:\n%s", msg)
+ }
+}
+
+func TestShellQuoteOnlyWhenNeeded(t *testing.T) {
+ if got := shellQuote("/home/x/podcli"); got != "/home/x/podcli" {
+ t.Errorf("plain path should not be quoted, got %q", got)
+ }
+ if got := shellQuote("/a b/c"); got != "'/a b/c'" {
+ t.Errorf("spaced path: got %q", got)
+ }
+ if got := shellQuote("/a'b"); got != `'/a'\''b'` {
+ t.Errorf("single quote: got %q", got)
+ }
+}
diff --git a/cli/sudo_unix.go b/cli/sudo_unix.go
new file mode 100644
index 0000000..fb9e925
--- /dev/null
+++ b/cli/sudo_unix.go
@@ -0,0 +1,25 @@
+//go:build !windows
+
+package main
+
+import (
+ "os"
+ "path/filepath"
+ "syscall"
+)
+
+// homeHasRootOwnedFiles reports whether a previous sudo run already took the
+// directory. Checks only the entries a run creates first, so the refusal is not
+// delayed by walking a multi-gigabyte install.
+func homeHasRootOwnedFiles(home string) bool {
+ for _, p := range []string{home, filepath.Join(home, "runtime"), filepath.Join(home, "models")} {
+ info, err := os.Stat(p)
+ if err != nil {
+ continue
+ }
+ if st, ok := info.Sys().(*syscall.Stat_t); ok && st.Uid == 0 {
+ return true
+ }
+ }
+ return false
+}
diff --git a/cli/sudo_windows.go b/cli/sudo_windows.go
new file mode 100644
index 0000000..7f91bb2
--- /dev/null
+++ b/cli/sudo_windows.go
@@ -0,0 +1,6 @@
+//go:build windows
+
+package main
+
+// Windows has no sudo, and SUDO_USER is never set by its shells.
+func homeHasRootOwnedFiles(string) bool { return false }
diff --git a/package.json b/package.json
index ddf8fc0..3a75084 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "podcli",
- "version": "2.7.0",
+ "version": "2.7.1",
"private": true,
"description": "AI-powered podcast clip generator for TikTok/YouTube Shorts. Transcribe, find viral moments, export vertical clips with burned captions.",
"type": "module",