From c93279d8569018262e51c1da87fd1114de92ef6b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 31 Oct 2026 09:28:45 +0530 Subject: [PATCH] Cleanup previous PR Move the D-Bus service Exec line generation into a helper with a fast unit test instead of copying the test binary and re-executing it. Refuse paths containing a carriage return, since D-Bus decodes \r as a newline and rejects .service files containing a raw carriage return. --- docs/changelog.rst | 2 +- kittens/desktop_ui/portal.go | 29 ++++++++--- kittens/desktop_ui/portal_test.go | 84 ++++++++----------------------- 3 files changed, 44 insertions(+), 71 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index d7c7ae04f..93bd3d1c8 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -426,7 +426,7 @@ Detailed list of changes not define them (:iss:`10691`, :iss:`10661`, :pull:`10727`, :pull:`10737`) - Desktop UI kitten: Fix D-Bus activation when kitten is installed in a path - containing spaces or quoting characters + containing spaces or quoting characters (:pull:`10784`) - Themes kitten: Fix overwriting the Unicode input kitten's recent entries and input mode, and vice versa, by using separate caches (:pull:`10780`) diff --git a/kittens/desktop_ui/portal.go b/kittens/desktop_ui/portal.go index 84d015d29..634c4f2aa 100644 --- a/kittens/desktop_ui/portal.go +++ b/kittens/desktop_ui/portal.go @@ -396,6 +396,23 @@ func patch_portals_conf(text []byte) []byte { return utils.UnsafeStringToBytes(strings.Join(lines, "\n")) } +// dbus_service_exec_line returns the value for the Exec key of a D-Bus +// .service file. D-Bus first unescapes the value (\\, \n, \t) and then +// parses it as a shell-style command line. +func dbus_service_exec_line(argv ...string) (string, error) { + quoted := make([]string, len(argv)) + for i, arg := range argv { + // D-Bus decodes \r as a newline and rejects files with a raw CR + if strings.ContainsRune(arg, '\r') { + return "", fmt.Errorf("cannot use %#v in a D-Bus service file as it contains a carriage return", arg) + } + quoted[i] = dbus_service_value_escaper.Replace(shlex.Quote(arg)) + } + return strings.Join(quoted, " "), nil +} + +var dbus_service_value_escaper = strings.NewReplacer(`\`, `\\`, "\n", `\n`, "\t", `\t`) + func enable_portal() (err error) { if len(WritableDataDirs()) == 0 { return fmt.Errorf("Could not find any writable data directories. Make sure XDG_DATA_DIRS is set and contains at least one directory for which you have write permission") @@ -442,15 +459,15 @@ Interfaces=%s; if exe_path, err = filepath.Abs(exe_path); err != nil { return fmt.Errorf("failed to get path to kitten executable with error: %w", err) } - // D-Bus unescapes .service values before parsing Exec as a command line. - exe_path = strings.NewReplacer( - `\`, `\\`, "\n", `\n`, "\r", `\r`, "\t", `\t`, - ).Replace(shlex.Quote(exe_path)) + exec_line, err := dbus_service_exec_line(exe_path, "desktop-ui", "run-server") + if err != nil { + return err + } if err = os.WriteFile(dbus_service_defn, utils.UnsafeStringToBytes(fmt.Sprintf( `[D-BUS Service] Name=%s -Exec=%s desktop-ui run-server -`, PORTAL_BUS_NAME, exe_path)), 0o644); err != nil { +Exec=%s +`, PORTAL_BUS_NAME, exec_line)), 0o644); err != nil { return err } fmt.Println("Wrote kitty DBUS activation service file to:", dbus_service_defn) diff --git a/kittens/desktop_ui/portal_test.go b/kittens/desktop_ui/portal_test.go index 8d2f67179..6c4a5a170 100644 --- a/kittens/desktop_ui/portal_test.go +++ b/kittens/desktop_ui/portal_test.go @@ -3,14 +3,7 @@ package desktop_ui import ( - "os" - "os/exec" - "path/filepath" - "reflect" - "strings" "testing" - - "github.com/kovidgoyal/kitty/tools/utils/shlex" ) func TestInvalidAccentColor(t *testing.T) { @@ -29,63 +22,26 @@ func TestInvalidAccentColor(t *testing.T) { } } -func TestPortalExecPath(t *testing.T) { - if os.Getenv("KITTY_TEST_PORTAL_EXEC") == "1" { - if err := enable_portal(); err != nil { - t.Fatal(err) - } - return - } - - exe, err := os.Executable() - if err != nil { - t.Fatal(err) - } - raw, err := os.ReadFile(exe) - if err != nil { - t.Fatal(err) - } - for _, tc := range []struct{ name, directory string }{ - {"plain", "kitty"}, {"space", "kitty app"}, - {"single_quote", "kitty'app"}, {"double_quote", "kitty\"app"}, - {"backslash", `kitty\app`}, {"dollar", "kitty$app"}, - {"tab", "kitty\tapp"}, {"newline", "kitty\napp"}, {"carriage_return", "kitty\rapp"}, +func TestDBusServiceExecLine(t *testing.T) { + for _, tc := range []struct{ exe, want string }{ + {"/opt/kitty/kitten", `/opt/kitty/kitten a`}, + {"/opt/kitty app/kitten", `'/opt/kitty app/kitten' a`}, + {"/opt/kitty'app/kitten", `'/opt/kitty'"'"'app/kitten' a`}, + {`/opt/kitty"app/kitten`, `'/opt/kitty"app/kitten' a`}, + {`/opt/kitty\app/kitten`, `'/opt/kitty\\app/kitten' a`}, + {"/opt/kitty$app/kitten", `'/opt/kitty$app/kitten' a`}, + {"/opt/kitty\tapp/kitten", `'/opt/kitty\tapp/kitten' a`}, + {"/opt/kitty\napp/kitten", `'/opt/kitty\napp/kitten' a`}, } { - t.Run(tc.name, func(t *testing.T) { - dir := t.TempDir() - copied_exe := filepath.Join(dir, tc.directory, "kitten") - data_dir := filepath.Join(dir, "data") - for _, p := range []string{filepath.Dir(copied_exe), data_dir} { - if err := os.MkdirAll(p, 0o700); err != nil { - t.Fatal(err) - } - } - if err := os.WriteFile(copied_exe, raw, 0o700); err != nil { - t.Fatal(err) - } - cmd := exec.Command(copied_exe, "-test.run=^TestPortalExecPath$") - cmd.Env = append(os.Environ(), "KITTY_TEST_PORTAL_EXEC=1", - "XDG_DATA_HOME="+data_dir, "XDG_DATA_DIRS="+data_dir, - "XDG_CONFIG_HOME="+filepath.Join(dir, "config"), "XDG_CURRENT_DESKTOP=probe") - if output, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("enable-portal failed: %s\n%s", err, output) - } - service, err := os.ReadFile(filepath.Join(data_dir, "dbus-1", "services", PORTAL_BUS_NAME+".service")) - if err != nil { - t.Fatal(err) - } - var command_line string - for _, line := range strings.Split(string(service), "\n") { - if value, ok := strings.CutPrefix(line, "Exec="); ok { - command_line = value - } - } - command_line = strings.NewReplacer(`\\`, `\`, `\s`, " ", `\n`, "\n", `\r`, "\r", `\t`, "\t").Replace(command_line) - got, err := shlex.Split(command_line) - want := []string{copied_exe, "desktop-ui", "run-server"} - if err != nil || !reflect.DeepEqual(got, want) { - t.Fatalf("service argv: got %#v (%v), want %#v", got, err, want) - } - }) + got, err := dbus_service_exec_line(tc.exe, "a") + if err != nil { + t.Fatalf("Unexpected error for %#v: %s", tc.exe, err) + } + if got != tc.want { + t.Fatalf("Exec line for %#v: got %#v want %#v", tc.exe, got, tc.want) + } + } + if _, err := dbus_service_exec_line("/opt/kitty\rapp/kitten"); err == nil { + t.Fatalf("No error for path containing a carriage return") } }