From b6abbf76552e4eff3265b52f75bfff5e26539575 Mon Sep 17 00:00:00 2001 From: Zhizhen He Date: Sat, 31 Oct 2026 11:43:54 +0800 Subject: [PATCH] desktop-ui: Quote portal activation executable paths --- docs/changelog.rst | 3 ++ kittens/desktop_ui/portal.go | 5 +++ kittens/desktop_ui/portal_test.go | 68 +++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index e783ba0dc..d7c7ae04f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -425,6 +425,9 @@ Detailed list of changes silently died and fix colors 16-255 being set to white with themes that do 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 + - 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 7229a6c8c..84d015d29 100644 --- a/kittens/desktop_ui/portal.go +++ b/kittens/desktop_ui/portal.go @@ -22,6 +22,7 @@ import ( "github.com/kovidgoyal/dbus/introspect" "github.com/kovidgoyal/dbus/prop" "github.com/kovidgoyal/kitty/tools/utils" + "github.com/kovidgoyal/kitty/tools/utils/shlex" "github.com/kovidgoyal/kitty/tools/utils/style" "golang.org/x/sys/unix" ) @@ -441,6 +442,10 @@ 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)) if err = os.WriteFile(dbus_service_defn, utils.UnsafeStringToBytes(fmt.Sprintf( `[D-BUS Service] Name=%s diff --git a/kittens/desktop_ui/portal_test.go b/kittens/desktop_ui/portal_test.go index e1689c817..8d2f67179 100644 --- a/kittens/desktop_ui/portal_test.go +++ b/kittens/desktop_ui/portal_test.go @@ -3,7 +3,14 @@ package desktop_ui import ( + "os" + "os/exec" + "path/filepath" + "reflect" + "strings" "testing" + + "github.com/kovidgoyal/kitty/tools/utils/shlex" ) func TestInvalidAccentColor(t *testing.T) { @@ -21,3 +28,64 @@ func TestInvalidAccentColor(t *testing.T) { t.Fatalf("Invalid accent_color in config changed the default to: %#v", c.Accent_color) } } + +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"}, + } { + 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) + } + }) + } +}