diff --git a/bin/omarchy-notification-send b/bin/omarchy-notification-send index 9e7fd0499674..a3a440f2284f 100755 --- a/bin/omarchy-notification-send +++ b/bin/omarchy-notification-send @@ -173,10 +173,26 @@ if ((exec_present)); then echo "Write: --exec ${exec_args[0]}" >&2 exit 1 fi + # The notifications service mints this under XDG_RUNTIME_DIR. Sandboxed + # same-uid clients cannot read it, so a forged omarchy-exec-argv alone is + # not enough for click-to-exec. + runtime_dir=${XDG_RUNTIME_DIR:-/run/user/$(id -u)} + exec_token_file=$runtime_dir/omarchy/notification-exec-token + if [[ ! -r $exec_token_file ]]; then + echo "omarchy-notification-send: no exec token at $exec_token_file" >&2 + echo "Is omarchy-shell running? --exec needs the live notification service." >&2 + exit 1 + fi + exec_token=$(tr -d '\n' <"$exec_token_file") + if [[ -z $exec_token ]]; then + echo "omarchy-notification-send: exec token file is empty" >&2 + exit 1 + fi # NUL-delimit into jq so every byte survives as data: jq's own --args would eat # a bare "--", and a newline in an arg must not split the vector. exec_argv_json=$(printf '%s\0' "${exec_args[@]}" | jq -Rsc 'split("\u0000")[:-1]') hints+=(omarchy-exec-argv s "$exec_argv_json") + hints+=(omarchy-exec-token s "$exec_token") fi hint_count=$((${#hints[@]} / 3)) diff --git a/shell/plugins/notifications/NotificationLogic.js b/shell/plugins/notifications/NotificationLogic.js index b5a6f3ea9910..9ee55daa014c 100644 --- a/shell/plugins/notifications/NotificationLogic.js +++ b/shell/plugins/notifications/NotificationLogic.js @@ -146,17 +146,25 @@ function glyphFromHints(hints) { // clickable (a libnotify action can't — its sender is gone). Run via // Util.execArgv as bash positional parameters, never a shell string, so // attacker-controlled values (a title, a filename) can't become commands. -function execArgvFromHints(hints) { - return stringHint(hints, "omarchy-exec-argv") +// +// expectedToken is the per-session value written under XDG_RUNTIME_DIR. A +// sandboxed same-uid sender can forge omarchy-exec-argv on the bus, but cannot +// read the host runtime dir to attach a matching omarchy-exec-token. +function execArgvFromHints(hints, expectedToken) { + var argv = stringHint(hints, "omarchy-exec-argv") + if (!argv) return "" + var expected = String(expectedToken || "") + if (!expected) return "" + if (stringHint(hints, "omarchy-exec-token") !== expected) return "" + return argv } // Validate a persisted omarchy-exec-argv into a runnable argv, or null. This is // a STRUCTURAL check only: it fails closed on a malformed hint (non-array, a // non-string or empty program, or a leading-dash program that argv would read as // an option). It does not judge intent — a well-formed ["bash","-c",…] is -// accepted. WHICH senders may set this hint is a separate boundary: any -// session-bus process can, by the freedesktop protocol's design (see -// docs/notifications.md), which is equivalent to same-uid code execution. +// accepted once the token gate in execArgvFromHints has already passed (or the +// value was restored from a previously validated popup snapshot). function parseExecArgv(value) { var text = String(value || "") if (!text) return null @@ -180,7 +188,7 @@ function shouldRenderCompactGlyph(glyph, iconSource, singleLineToast) { return String(glyph || "").length > 0 && String(iconSource || "").length === 0 && !!singleLineToast } -function snapshotOf(notification, timestamp) { +function snapshotOf(notification, timestamp, expectedToken) { var n = notification || {} var id = n.id || 0 var expireTimeout = Number(n.expireTimeout || 0) @@ -194,7 +202,7 @@ function snapshotOf(notification, timestamp) { body: n.body || "", image: n.image || "", glyph: glyphFromHints(n.hints), - execArgv: execArgvFromHints(n.hints), + execArgv: execArgvFromHints(n.hints, expectedToken), urgency: n.urgency, expireTimeout: expireTimeout, timestamp: timestamp === undefined ? Date.now() : timestamp @@ -227,8 +235,8 @@ function popupRowChanged(row, updated) { // the popup it took over: the file name is the timestamp and id the popup was // first persisted under, and the restore, replace and archive paths all key // off that name. Only what the card draws comes from the updated object. -function replacementSnapshot(notification, originalId, timestamp) { - var updated = snapshotOf(notification, timestamp) +function replacementSnapshot(notification, originalId, timestamp, expectedToken) { + var updated = snapshotOf(notification, timestamp, expectedToken) updated.id = originalId updated.originalId = originalId return updated diff --git a/shell/plugins/notifications/Service.qml b/shell/plugins/notifications/Service.qml index 0122310ee7d6..f03bd93a9ba1 100644 --- a/shell/plugins/notifications/Service.qml +++ b/shell/plugins/notifications/Service.qml @@ -127,12 +127,17 @@ Item { // Trusted because it's almost always omarchy or system shell scripts — // chat apps set app_name to their brand (Discord/Slack/Vesktop), which // falls outside this rule. + // Per-session capability for honoring omarchy-exec-argv. Written under + // XDG_RUNTIME_DIR (mode 0600) so sandboxed same-uid bus clients cannot read + // it; omarchy-notification-send attaches it as omarchy-exec-token. + property string execToken: "" + function shouldBypassDnd(notification) { return NotificationLogic.shouldBypassDnd(notification, NotificationUrgency.Critical) } function snapshotOf(notification) { - return NotificationLogic.snapshotOf(notification, Date.now()) + return NotificationLogic.snapshotOf(notification, Date.now(), service.execToken) } // A notification nobody looks back at: @@ -206,7 +211,7 @@ Item { writeHistoryFile(written, function() { var updated = null try { - updated = NotificationLogic.replacementSnapshot(notification, written.originalId, written.timestamp) + updated = NotificationLogic.replacementSnapshot(notification, written.originalId, written.timestamp, service.execToken) } catch (e) { // Torn down by the server while the write was queued. } @@ -260,7 +265,7 @@ Item { var updated try { - updated = NotificationLogic.replacementSnapshot(notification, originalId, timestamp) + updated = NotificationLogic.replacementSnapshot(notification, originalId, timestamp, service.execToken) } catch (e) { // Object torn down by the server while the signal was in flight. return @@ -415,6 +420,31 @@ Item { running: false } + // Mint a fresh click-exec token for this shell session and park it where + // only host processes can read it. Sandboxed notify callers share the bus + // but not $XDG_RUNTIME_DIR/omarchy. + Process { + id: mintExecTokenProc + running: false + command: ["bash", "-c", + "set -euo pipefail\n" + + "runtime=\"${XDG_RUNTIME_DIR:-/run/user/$(id -u)}\"\n" + + "dir=\"$runtime/omarchy\"\n" + + "mkdir -p -m 700 -- \"$dir\"\n" + + "token=$(openssl rand -hex 32)\n" + + "umask 077\n" + + "printf '%s' \"$token\" > \"$dir/notification-exec-token\"\n" + + "printf '%s' \"$token\"\n"] + stdout: StdioCollector { + waitForEnd: true + onStreamFinished: { + var token = String(text || "").trim() + if (token) service.execToken = token + else console.warn("omarchy notifications: failed to mint exec token") + } + } + } + // ---------------------------------------------------- popup persistence // // Mirror every on-screen popup to its own file under popupStateDir so @@ -834,6 +864,7 @@ Item { Component.onCompleted: { ensureDirsProc.running = true + mintExecTokenProc.running = true // Once mkdir has had a tick, load the existing settings file. FileView // surfaces an empty string when the file doesn't exist; loadSettings // handles that path. diff --git a/test/shell.d/notification-send-test.sh b/test/shell.d/notification-send-test.sh index 6a8f8a217b74..4eb9e32fb255 100644 --- a/test/shell.d/notification-send-test.sh +++ b/test/shell.d/notification-send-test.sh @@ -24,8 +24,15 @@ printf '%s\n' '#!/bin/bash' 'echo "notify-send was invoked" >"$OMARCHY_TEST_NOTI chmod +x "$tmpdir/notify-send" tripwire="$tmpdir/notify-send-was-used" +# --exec needs the live session token that omarchy-shell writes under +# XDG_RUNTIME_DIR. Mint one here the same way the service does. +mkdir -p -m 700 "$tmpdir/run/omarchy" +printf 'test-exec-token' >"$tmpdir/run/omarchy/notification-exec-token" +chmod 600 "$tmpdir/run/omarchy/notification-exec-token" + send() { OMARCHY_TEST_BUSCTL_ARGS="$args_file" OMARCHY_TEST_NOTIFY_TRIPWIRE="$tripwire" \ + XDG_RUNTIME_DIR="$tmpdir/run" \ PATH="$tmpdir:$ROOT/bin:$PATH" omarchy-notification-send "$@" } @@ -63,7 +70,8 @@ load [[ $(hint_value urgency) == "2" ]] || fail "notification wrapper maps critical urgency to 2" [[ $(hint_value omarchy-glyph) == "K" ]] || fail "notification wrapper sets the glyph hint" [[ $(hint_value omarchy-exec-argv) == '["mpv","--","/tmp/a b.mp4"]' ]] || fail "notification wrapper builds the click argv hint" "$(hint_value omarchy-exec-argv)" -pass "notification wrapper issues a Notify call with app, icon, urgency, glyph, and click argv" +[[ $(hint_value omarchy-exec-token) == "test-exec-token" ]] || fail "notification wrapper attaches the session exec token" "$(hint_value omarchy-exec-token)" +pass "notification wrapper issues a Notify call with app, icon, urgency, glyph, click argv, and token" [[ -f $tripwire ]] && fail "notification wrapper must never invoke notify-send" pass "notification wrapper never invokes notify-send" @@ -183,6 +191,15 @@ if send "Head" --exec "" 2>/dev/null; then fi pass "notification wrapper rejects --exec with an empty program" +# Without a readable session token, --exec fails closed at send time. +rm -f "$tmpdir/run/omarchy/notification-exec-token" +if send "Head" --exec true 2>/dev/null; then + fail "notification wrapper rejects --exec without a session token" +fi +printf 'test-exec-token' >"$tmpdir/run/omarchy/notification-exec-token" +chmod 600 "$tmpdir/run/omarchy/notification-exec-token" +pass "notification wrapper rejects --exec without a session token" + # A description that begins with a dash is content, not options: a price, a # negative number, a diff line. It must reach the body, not error out. : >"$args_file" diff --git a/test/shell.d/notifications-test.sh b/test/shell.d/notifications-test.sh index f06e0481f165..46ab8c1d0395 100644 --- a/test/shell.d/notifications-test.sh +++ b/test/shell.d/notifications-test.sh @@ -211,20 +211,63 @@ assertEqual(notifications.parseExecArgv('["mpv",5]'), null, 'notifications rejec assertEqual(notifications.parseExecArgv('["--include=x","y"]'), null, 'notifications reject a leading-dash program in the exec argv') assertEqual(notifications.parseExecArgv('["",""]'), null, 'notifications reject an empty program in the exec argv') +// Click-to-exec requires the matching per-session token. A sandboxed sender +// can forge omarchy-exec-argv, but without the token the snapshot stays inert. +assertEqual( + notifications.execArgvFromHints({ 'omarchy-exec-argv': '["bash","-c","touch /tmp/pwn"]' }, 'session-token'), + '', + 'notifications drop exec argv without a matching token' +) +assertEqual( + notifications.execArgvFromHints({ + 'omarchy-exec-argv': '["bash","-c","touch /tmp/pwn"]', + 'omarchy-exec-token': 'wrong' + }, 'session-token'), + '', + 'notifications drop exec argv with a mismatched token' +) +assertEqual( + notifications.execArgvFromHints({ + 'omarchy-exec-argv': '["mpv","--","/tmp/clip.mp4"]', + 'omarchy-exec-token': 'session-token' + }, 'session-token'), + '["mpv","--","/tmp/clip.mp4"]', + 'notifications keep exec argv when the session token matches' +) +assertEqual( + notifications.execArgvFromHints({ + 'omarchy-exec-argv': '["mpv","--","/tmp/clip.mp4"]', + 'omarchy-exec-token': 'session-token' + }, ''), + '', + 'notifications drop exec argv before the session token is minted' +) + // The argv vector rides on the snapshot as the raw JSON string, so the model's // value comparison stays a plain string compare and the file round-trip is -// lossless. +// lossless. Token is checked at snapshot time and never persisted. const execSnapshot = notifications.snapshotOf({ id: 3, appName: 'omarchy-action', summary: 'Download complete', - hints: { 'omarchy-exec-argv': '["mpv","--","/tmp/clip.mp4"]' } -}, 1) + hints: { + 'omarchy-exec-argv': '["mpv","--","/tmp/clip.mp4"]', + 'omarchy-exec-token': 'session-token' + } +}, 1, 'session-token') assertEqual( execSnapshot.execArgv, '["mpv","--","/tmp/clip.mp4"]', 'notifications carry the exec argv hint onto the snapshot' ) +assertEqual( + notifications.snapshotOf({ + id: 4, + hints: { 'omarchy-exec-argv': '["bash","-c","x"]' } + }, 1, 'session-token').execArgv, + '', + 'notifications do not carry an untoked exec argv onto the snapshot' +) assertDeepEqual( notifications.popupPlacement('top', 32, 6),