Cleanup previous PR
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run

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.
This commit is contained in:
Kovid Goyal 2026-10-31 09:28:45 +05:30
parent 8072376d27
commit c93279d856
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 44 additions and 71 deletions

View file

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

View file

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

View file

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