Fix systemd WorkingDirectory syntax and verify with native validator
This commit is contained in:
@@ -37,3 +37,9 @@ An isolated installed-CLI check started the server in one directory and invoked
|
||||
runs completed and recorded/executed in the client invocation directory. The
|
||||
autostart preview required no repository configuration and passed `plutil -lint`.
|
||||
CLI and dashboard regression tests also check invocation-directory selection.
|
||||
|
||||
The installation tests were also executed as a Linux binary in Debian with
|
||||
systemd installed. The native `systemd-analyze verify` regression passed for the
|
||||
generated unit, including a directory containing spaces, quotes, and `%`/`$`.
|
||||
This reproduced and fixed the invalid quoted `WorkingDirectory` setting. A
|
||||
container syntax check does not prove live user-service startup or login restart.
|
||||
|
||||
@@ -43,7 +43,9 @@ func (d serviceDefinition) render(platform string) (string, error) {
|
||||
}
|
||||
if platform == "linux" {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "# %s\n[Unit]\nDescription=Pipelines server\n\n[Service]\nType=simple\nWorkingDirectory=%s\nExecStart=%s", serviceMarker, unitQuote(d.Directory), unitQuote(strings.ReplaceAll(d.Binary, "$", "$$")))
|
||||
// WorkingDirectory is a path setting, not a shell-style argument list.
|
||||
// Quotes become literal path characters and make an absolute path invalid.
|
||||
fmt.Fprintf(&b, "# %s\n[Unit]\nDescription=Pipelines server\n\n[Service]\nType=simple\nWorkingDirectory=%s\nExecStart=%s", serviceMarker, strings.ReplaceAll(d.Directory, "%", "%%"), unitQuote(strings.ReplaceAll(d.Binary, "$", "$$")))
|
||||
for _, arg := range d.Args {
|
||||
fmt.Fprintf(&b, " %s", unitQuote(strings.ReplaceAll(arg, "$", "$$")))
|
||||
}
|
||||
|
||||
@@ -6,12 +6,39 @@ import (
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSystemdDefinitionAcceptedByNativeValidator(t *testing.T) {
|
||||
if runtime.GOOS != "linux" {
|
||||
t.Skip("systemd validation requires Linux")
|
||||
}
|
||||
validator, err := exec.LookPath("systemd-analyze")
|
||||
if err != nil {
|
||||
t.Skip("systemd-analyze is not installed")
|
||||
}
|
||||
dir := filepath.Join(t.TempDir(), `directory with "quotes" 100% $USER`)
|
||||
if err := os.Mkdir(dir, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := serviceDefinition{Binary: "/bin/true", Directory: dir, Args: []string{"-db", filepath.Join(dir, "data.db")}, Environment: map[string]string{"PATH": "/usr/bin:/bin"}}
|
||||
text, err := d.render("linux")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(t.TempDir(), "pipelines.service")
|
||||
if err := os.WriteFile(path, []byte(text), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if output, err := exec.Command(validator, "verify", path).CombinedOutput(); err != nil {
|
||||
t.Fatalf("systemd rejected generated definition: %v\n%s\n%s", err, output, text)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceDefinitionsQuotePathsAndKeepEnvironmentLimited(t *testing.T) {
|
||||
d := serviceDefinition{Binary: `/some bin/server`, Directory: `/repo "quoted" 100% $USER`, Args: []string{"-db", `/repo "quoted" 100% $USER/data.db`}, Environment: map[string]string{"PATH": "/usr/bin:/custom bin", "PIPELINES_NTFY_TOKEN_FILE": "/secret & token", "PROVIDER_SECRET": "must-not-export"}}
|
||||
for _, platform := range []string{"linux", "darwin"} {
|
||||
@@ -23,6 +50,9 @@ func TestServiceDefinitionsQuotePathsAndKeepEnvironmentLimited(t *testing.T) {
|
||||
t.Fatal("unexpected credentials or missing marker", text)
|
||||
}
|
||||
if platform == "linux" {
|
||||
if !strings.Contains(text, "\nWorkingDirectory=/repo \"quoted\" 100%% $USER\n") {
|
||||
t.Fatal("WorkingDirectory must be an unquoted absolute path", text)
|
||||
}
|
||||
if !strings.Contains(text, `"/some bin/server"`) || !strings.Contains(text, `100%% $$USER`) || !strings.Contains(text, `Environment="PATH=/usr/bin:/custom bin"`) {
|
||||
t.Fatal("unsafe systemd escaping", text)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user