desktop-ui: Quote portal activation executable paths

This commit is contained in:
Zhizhen He 2026-10-31 11:43:54 +08:00
parent f2f7927ec5
commit b6abbf7655
3 changed files with 76 additions and 0 deletions

View file

@ -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`)

View file

@ -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

View file

@ -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)
}
})
}
}