Skip to content

Commit 7c116a1

Browse files
fix(desktop): limit conditional Exec quoting to URL handlers
1 parent 56e4be8 commit 7c116a1

2 files changed

Lines changed: 37 additions & 31 deletions

File tree

‎apps/desktop/src/app/DesktopLinuxUrlHandler.test.ts‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -179,26 +179,23 @@ describe("DesktopLinuxUrlHandler", () => {
179179
assert.include(entry, "Icon=/home/al ice/icons/T3\\\\x.png");
180180
});
181181

182-
it("quotes the Exec argument only when it contains a reserved character", () => {
183-
// xdg-open's generic fallback runs `command -v` on the first Exec word
184-
// without unquoting it, so a plain path must be written bare.
185-
assert.equal(
186-
DesktopLinuxUrlHandler.escapeDesktopEntryExecArgument("/home/alice/bin/t3code.AppImage"),
187-
"/home/alice/bin/t3code.AppImage",
188-
);
189-
assert.equal(
190-
DesktopLinuxUrlHandler.escapeDesktopEntryExecArgument("/opt/T3(beta)/t3code.AppImage"),
191-
'"/opt/T3(beta)/t3code.AppImage"',
192-
);
193-
assert.equal(
194-
DesktopLinuxUrlHandler.escapeDesktopEntryExecArgument("/opt/t3~old/t3code.AppImage"),
195-
'"/opt/t3~old/t3code.AppImage"',
196-
);
197-
// A percent sign is not reserved, but must still be doubled (field codes).
198-
assert.equal(
199-
DesktopLinuxUrlHandler.escapeDesktopEntryExecArgument("/opt/t3%20code/t3code.AppImage"),
200-
"/opt/t3%%20code/t3code.AppImage",
201-
);
182+
it.each([
183+
{
184+
execTarget: "/opt/T3(beta)/t3code.AppImage",
185+
execLine: 'Exec="/opt/T3(beta)/t3code.AppImage" %U',
186+
},
187+
{
188+
execTarget: "/opt/t3%20code/t3code.AppImage",
189+
execLine: "Exec=/opt/t3%%20code/t3code.AppImage %U",
190+
},
191+
])("renders the handler Exec line for $execTarget", ({ execTarget, execLine }) => {
192+
const entry = DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
193+
displayName: "T3 Code",
194+
execTarget,
195+
scheme: "t3code",
196+
});
197+
198+
assert.include(entry.split("\n"), execLine);
202199
});
203200

204201
it("carries structured context on registration errors", () => {
@@ -269,7 +266,12 @@ describe("DesktopLinuxUrlHandler", () => {
269266

270267
assert.include(
271268
recorded.files[0]?.content,
272-
`Exec=${DesktopLinuxUrlHandler.escapeDesktopEntryExecArgument(process.execPath)} %U`,
269+
DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
270+
displayName: "T3 Code (Alpha)",
271+
execTarget: process.execPath,
272+
scheme: "t3code",
273+
iconPath: "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png",
274+
}),
273275
);
274276
});
275277
});

‎apps/desktop/src/app/DesktopLinuxUrlHandler.ts‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,25 @@ const escapeDesktopEntryString = (value: string): string =>
6262
.replaceAll("\r", "\\r")
6363
.replaceAll("\t", "\\t");
6464

65-
// Characters the Desktop Entry spec reserves in Exec arguments ("The Exec key").
66-
const EXEC_RESERVED_CHARACTER = /[ \t\n"'\\><~|&;$*?#()`]/;
67-
6865
// Exec values are unescaped twice by implementations: first the general
6966
// string-value rules, then the Exec quoting rules — so writing composes the
70-
// layers in reverse. Literal percent signs are always doubled (field codes).
71-
// The argument is quoted only when it contains a reserved character: xdg-open's
72-
// generic fallback does not unquote Exec, so a quoted plain path never resolves.
67+
// layers in reverse.
7368
export function escapeDesktopEntryExecArgument(value: string): string {
74-
const literal = value.replaceAll("%", () => "%%");
75-
if (!EXEC_RESERVED_CHARACTER.test(literal)) return escapeDesktopEntryString(literal);
76-
const quoted = literal.replace(/["`$\\]/g, (character) => `\\${character}`);
69+
const quoted = value
70+
.replaceAll("\\", () => "\\\\")
71+
.replaceAll("`", () => "\\`")
72+
.replaceAll("$", () => "\\$")
73+
.replaceAll('"', () => '\\"')
74+
.replaceAll("%", () => "%%");
7775
return escapeDesktopEntryString(`"${quoted}"`);
7876
}
7977

78+
// Legacy xdg-open looks up the first Exec word without unquoting it.
79+
const escapeUrlHandlerExecTarget = (value: string): string =>
80+
/[ \t\n"'\\><~|&;$*?#()`]/.test(value)
81+
? escapeDesktopEntryExecArgument(value)
82+
: escapeDesktopEntryString(value.replaceAll("%", "%%"));
83+
8084
// The AppImage integration entry owns the window identity. This
8185
// hidden URL-only entry must not compete with it for StartupWMClass matching.
8286
export function renderUrlHandlerDesktopEntry(input: {
@@ -89,7 +93,7 @@ export function renderUrlHandlerDesktopEntry(input: {
8993
"[Desktop Entry]",
9094
"Type=Application",
9195
`Name=${escapeDesktopEntryString(input.displayName)}`,
92-
`Exec=${escapeDesktopEntryExecArgument(input.execTarget)} %U`,
96+
`Exec=${escapeUrlHandlerExecTarget(input.execTarget)} %U`,
9397
...(input.iconPath === undefined ? [] : [`Icon=${escapeDesktopEntryString(input.iconPath)}`]),
9498
"Terminal=false",
9599
"NoDisplay=true",

0 commit comments

Comments
 (0)