Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions bin/omarchy-notification-send
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions docs/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ flags map onto that call:
| Flag | Becomes | Meaning |
|---|---|---|
| `-g` / `--glyph` | hint `omarchy-glyph` | Nerd Font glyph for the icon slot when no image icon resolves |
| `--exec <program> [args…]` | hint `omarchy-exec-argv` | the click command; consumes the rest of the line, so it comes last. Each word is a discrete argument the shell runs without re-parsing (see below) |
| `--exec <program> [args…]` | hints `omarchy-exec-argv` + `omarchy-exec-token` | the click command; consumes the rest of the line, so it comes last. Each word is a discrete argument the shell runs without re-parsing (see below). The token is the per-session capability written under `$XDG_RUNTIME_DIR/omarchy/` — without a match the shell ignores the argv |
| `--image` | hint `image-path` | the standard freedesktop image hint |
| `-i` / `--icon` | `app_icon` | themed icon name for the toast |
| `--app-name` | `app_name` | defaults to `omarchy-action` |
Expand All @@ -92,8 +92,8 @@ sender blocked waiting for `ActionInvoked`, and dies unanswered whenever the
shell restarts underneath it — the installer toasts restart the shell as their
first act. Carrying the command as a hint means the shell executes the click
itself (detached, so the command outlives the shell process) from the copy it
keeps with the popup, which the persistence files preserve: a restored toast
clicks through exactly like a live one, and oneshot senders can exit
keeps with the popup for the live toast. Restore after a shell restart clears
the click command (the token is session-scoped); oneshot senders can exit
immediately. For third-party clients the click falls back to the libnotify
`default` action while the sender is alive, then to focusing the sender's
window by class via `omarchy-hyprland-focus-app` — chat apps rarely register
Expand Down
26 changes: 17 additions & 9 deletions shell/plugins/notifications/NotificationLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
// Restore clears execArgv so disk snapshots cannot keep click-to-exec.
function parseExecArgv(value) {
var text = String(value || "")
if (!text) return null
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
77 changes: 62 additions & 15 deletions shell/plugins/notifications/Service.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -353,8 +358,9 @@ Item {
}

// Run the popup's click action, then dismiss. Omarchy's own toasts carry the
// action as an argv vector in the `execArgv` role (see execArgvFromHints),
// which the persistence files preserve, so restored toasts stay clickable.
// action as an argv vector in the `execArgv` role (see execArgvFromHints).
// Restore clears execArgv so pre-token or planted popup files cannot keep
// a click-to-exec after restart.
// Third-party clients register a libnotify action under the canonical
// identifier "default" instead; that one only works while the sender is live.
function invokePopupDefault(index) {
Expand Down Expand Up @@ -415,6 +421,48 @@ 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. Start immediately (not from
// Component.onCompleted) so the token file exists before early --exec
// senders race the bus claim.
property bool restoreAfterTokenStarted: false
Process {
id: mintExecTokenProc
running: true
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")
service.startRestoreAfterToken()
}
}
}

function startRestoreAfterToken() {
if (service.restoreAfterTokenStarted) return
service.restoreAfterTokenStarted = true
// Re-show popups that were on screen when the previous shell died.
// The glob-through-bash tolerates a missing/empty dir (first run).
// awk 1 (not cat) so a torn file missing its trailing newline can't
// glue itself onto the next file and take a valid popup down with it.
restorePopupsProc.command = ["bash", "-c",
"awk 1 \"$1\"/*.json 2>/dev/null || true", "--", service.popupStateDir]
restorePopupsProc.running = true
service.sweepOrphanImages()
}

// ---------------------------------------------------- popup persistence
//
// Mirror every on-screen popup to its own file under popupStateDir so
Expand Down Expand Up @@ -743,6 +791,11 @@ Item {
// never carry it, and ListModel roles must stay consistent.
delete entry.deadline
}
// Drop click-exec on restore. Pre-token snapshots (and any file an
// attacker could plant under the popup dir) must not keep working
// after a shell restart; a fresh Notify with a matching token can
// re-attach the action.
entry.execArgv = ""
live.push(entry)
}
if (live.length === 0) return
Expand Down Expand Up @@ -834,21 +887,15 @@ Item {

Component.onCompleted: {
ensureDirsProc.running = true
// Token mint starts with the Process itself (running: true). Restore
// waits for that so a missing token cannot leave restored click-exec
// armed before the gate is live. If mint already finished, restore now.
if (service.execToken) service.startRestoreAfterToken()
// 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.
Qt.callLater(function() {
settingsFile.reload()
// Re-show popups that were on screen when the previous shell died.
// The glob-through-bash tolerates a missing/empty dir (first run).
// awk 1 (not cat) so a torn file missing its trailing newline can't
// glue itself onto the next file and take a valid popup down with it.
restorePopupsProc.command = ["bash", "-c",
"awk 1 \"$1\"/*.json 2>/dev/null || true", "--", service.popupStateDir]
restorePopupsProc.running = true
// Safe beside the restore read: it only re-persists entries whose
// JSON exists, exactly the images the sweep keeps.
service.sweepOrphanImages()
})
}

Expand Down
19 changes: 18 additions & 1 deletion test/shell.d/notification-send-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"
}

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
75 changes: 72 additions & 3 deletions test/shell.d/notifications-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,73 @@ 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'
)
assertEqual(
notifications.serializePopup(execSnapshot, 1).indexOf('omarchy-exec-token'),
-1,
'notifications never persist the session exec token'
)
assertEqual(
'execToken' in JSON.parse(notifications.serializePopup(execSnapshot, 1)),
false,
'notifications persist validated execArgv without a token field'
)

assertDeepEqual(
notifications.popupPlacement('top', 32, 6),
Expand Down Expand Up @@ -715,6 +768,22 @@ assert(
/parseExecArgv\(entry \? entry\.execArgv : ""\)[\s\S]{0,200}?Util\.execArgv\(argv\)/.test(serviceQml),
'notifications service runs the popup click argv itself instead of a libnotify action'
)
assert(
/property string execToken: ""/.test(serviceQml),
'notifications service keeps the per-session exec token in memory'
)
assert(
/id: mintExecTokenProc[\s\S]*?running: true/.test(serviceQml),
'notifications service mints the exec token on startup'
)
assert(
/startRestoreAfterToken/.test(serviceQml) && /entry\.execArgv = ""/.test(serviceQml),
'notifications service restores popups only after the token and clears restored click-exec'
)
assert(
/notification-exec-token/.test(serviceQml),
'notifications service writes the exec token under XDG_RUNTIME_DIR'
)
assert(
/function clear\(\): string \{\s*service\.clearHistory\(\)/.test(serviceQml),
'notifications clear IPC forgets the recorded history'
Expand Down