Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: unescaped paths make the launchd plist and systemd unit malformed

Luigi Agosti4197367d4ac9
diff --git a/internal/daemon/launchd.go b/internal/daemon/launchd.go
index 23d89311..549c4024 100644
--- a/internal/daemon/launchd.go
+++ b/internal/daemon/launchd.go
@@ -1,6 +1,7 @@
package daemon
import (
+ "encoding/xml"
"errors"
"fmt"
"os"
@@ -177,7 +178,24 @@ func isLaunchdIgnorableError(err error) bool {
strings.Contains(msg, launchdAlreadyBootstrapped)
}
+// plistString escapes a value for inclusion in a plist <string> element. The
+// paths reaching here are user-influenced (--config / PINCHTAB_CONFIG, and the
+// home directory), and a bare "&" — ordinary in a macOS directory name like
+// "R&D" — otherwise makes launchctl reject the file as malformed XML.
+func plistString(value string) string {
+ var b strings.Builder
+ if err := xml.EscapeText(&b, []byte(value)); err != nil {
+ return ""
+ }
+ return b.String()
+}
+
func renderLaunchdPlist(execPath, configPath, homeDir, stdoutPath, stderrPath string) string {
+ execPath = plistString(execPath)
+ configPath = plistString(configPath)
+ homeDir = plistString(homeDir)
+ stdoutPath = plistString(stdoutPath)
+ stderrPath = plistString(stderrPath)
return fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
diff --git a/internal/daemon/render_escaping_test.go b/internal/daemon/render_escaping_test.go
new file mode 100644
index 00000000..6c7a282c
--- /dev/null
+++ b/internal/daemon/render_escaping_test.go
@@ -0,0 +1,78 @@
+package daemon
+
+import (
+ "encoding/xml"
+ "strings"
+ "testing"
+)
+
+// The plist is XML, and the paths interpolated into it are user-influenced —
+// --config / PINCHTAB_CONFIG in particular. Directory names containing "&" are
+// ordinary on macOS ("R&D"), and an unescaped one makes launchctl reject the
+// file with an XML parse error rather than anything actionable.
+func TestRenderLaunchdPlistStaysWellFormedXML(t *testing.T) {
+ paths := []struct {
+ name string
+ path string
+ }{
+ {"ampersand", "/Users/tester/R&D/pinchtab.json"},
+ {"angle brackets", "/Users/tester/a<b>c/pinchtab.json"},
+ {"quotes", `/Users/tester/say "hi"/pinchtab.json`},
+ }
+
+ for _, tt := range paths {
+ t.Run(tt.name, func(t *testing.T) {
+ plist := renderLaunchdPlist("/usr/local/bin/pinchtab", tt.path, "/Users/tester",
+ "/Users/tester/.pinchtab/logs/out.log", "/Users/tester/.pinchtab/logs/err.log")
+
+ decoder := xml.NewDecoder(strings.NewReader(plist))
+ for {
+ _, err := decoder.Token()
+ if err != nil {
+ if err.Error() == "EOF" {
+ break
+ }
+ t.Fatalf("plist is not well-formed XML with config path %q: %v", tt.path, err)
+ }
+ }
+ })
+ }
+}
+
+// The escaped value must still round-trip to the original path, or the daemon
+// would launch with a corrupted config location.
+func TestRenderLaunchdPlistPreservesPathValue(t *testing.T) {
+ const configPath = "/Users/tester/R&D/pinchtab.json"
+ plist := renderLaunchdPlist("/usr/local/bin/pinchtab", configPath, "/Users/tester", "/o.log", "/e.log")
+
+ var doc struct {
+ Strings []string `xml:"dict>dict>string"`
+ }
+ if err := xml.Unmarshal([]byte(plist), &doc); err != nil {
+ t.Fatalf("unmarshal plist: %v", err)
+ }
+ var found bool
+ for _, s := range doc.Strings {
+ if s == configPath {
+ found = true
+ }
+ }
+ if !found {
+ t.Errorf("config path did not round-trip through the plist, got %q", doc.Strings)
+ }
+}
+
+// systemd expands "%" specifiers in unit files (%h, %i, ...), so a literal
+// percent in a path must be doubled or the daemon starts with a mangled
+// config location.
+func TestRenderSystemdUnitEscapesPercent(t *testing.T) {
+ const configPath = "/home/tester/100%backup/pinchtab.json"
+ unit := renderSystemdUnit("/usr/local/bin/pinchtab", configPath, "/o.log", "/e.log")
+
+ if strings.Contains(unit, "100%backup") {
+ t.Errorf("literal %% reached the unit file unescaped: %s", unit)
+ }
+ if !strings.Contains(unit, "100%%backup") {
+ t.Errorf("expected the percent to be doubled, got: %s", unit)
+ }
+}
diff --git a/internal/daemon/systemd.go b/internal/daemon/systemd.go
index 57d7d473..3d8985d4 100644
--- a/internal/daemon/systemd.go
+++ b/internal/daemon/systemd.go
@@ -138,7 +138,18 @@ func (m *systemdUserManager) ManualInstructions() string {
return b.String()
}
+// unitValue escapes a value for a systemd unit file. systemd expands "%" as a
+// specifier prefix (%h, %i, ...), so a literal percent in a path must be
+// doubled or the daemon starts against a mangled location.
+func unitValue(value string) string {
+ return strings.ReplaceAll(value, "%", "%%")
+}
+
func renderSystemdUnit(execPath, configPath, stdoutPath, stderrPath string) string {
+ execPath = unitValue(execPath)
+ configPath = unitValue(configPath)
+ stdoutPath = unitValue(stdoutPath)
+ stderrPath = unitValue(stderrPath)
return fmt.Sprintf(`[Unit]
Description=Pinchtab Browser Service
After=network.target