From 9ee2e003bf55453fa32bac8a1152f7e10034cce0 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Wed, 21 Oct 2026 13:05:10 +0100 Subject: [PATCH] fix(notify): open configured web run pages from ntfy --- compose.yaml | 1 + docs/notes/notification-web-links.md | 24 ++++++++++++ docs/ntfy.md | 32 +++++++++++++-- .../author/skill/references/notifications.md | 14 +++++-- internal/installation/service.go | 2 +- internal/installation/service_test.go | 15 +++++++ internal/server/notification_web_test.go | 25 ++++++++++++ internal/server/notify.go | 16 ++++++-- internal/server/notify_test.go | 9 ++++- scripts/setup-native.py | 12 ++++++ scripts/test_setup_native.py | 26 +++++++++++++ web/test/notification-web-link.mjs | 39 +++++++++++++++++++ 12 files changed, 201 insertions(+), 14 deletions(-) create mode 100644 docs/notes/notification-web-links.md create mode 100644 internal/server/notification_web_test.go create mode 100644 web/test/notification-web-link.mjs diff --git a/compose.yaml b/compose.yaml index cc02dc93..2f457cfe 100644 --- a/compose.yaml +++ b/compose.yaml @@ -12,6 +12,7 @@ services: TZ: ${TZ:?Set TZ to your host timezone; scripts/configure-compose.py detects it} PIPELINES_NTFY_URL: http://ntfy/pipelines PIPELINES_NTFY_PUBLIC_URL: ${NTFY_BASE_URL:?Set the address your Android phone can reach} + PIPELINES_WEB_PUBLIC_URL: ${PIPELINES_WEB_PUBLIC_URL:-} PIPELINES_NTFY_TOKEN_FILE: /run/secrets/ntfy_token PIPELINES_DEFAULT_MODEL: ${PIPELINES_DEFAULT_MODEL:-} PIPELINES_HOST_IP: ${PIPELINES_HOST_IP:-} diff --git a/docs/notes/notification-web-links.md b/docs/notes/notification-web-links.md new file mode 100644 index 00000000..8832be1f --- /dev/null +++ b/docs/notes/notification-web-links.md @@ -0,0 +1,24 @@ +# Notification web run links + +Run-attention/completion notifications and stage hooks now use +`PIPELINES_WEB_PUBLIC_URL` to build `/runs/` links to the authenticated +web app. This is an operator-controlled origin, not an inferred API/ntfy host. +Credentials, queries, fragments and subpaths are rejected; invalid configuration +omits the link with a diagnostic. Existing Android-only deployments without this +setting retain their original scheme. Already-sent notifications are unchanged. + +Native setup saves its advertised web address by default and retains explicit +origin overrides across reinstalls. Custom service generation preserves the +variable; Docker Compose forwards it. Authenticated proxy login already preserves +the target run and is covered by a new isolated HTTP regression. + +Verified: full Go race suite, build/vet, installer unit tests, stage-hook web links +(including attachments), waiting/failed run web payloads, URL validation and +service-environment retention on both renderers. The isolated real proxy test +checks unauthenticated gating, wrong-password rejection and return to the exact +run after login. No production notification was sent to test this fix. + +Code is not deployed/configured on the live server: the operator's phone-facing +web URL is still needed, and a backend update must wait for safe deployment. +Dummy run #22 completed with its old `pipelines://run/22` notification acknowledged +by ntfy; that verifies publishing, not reception or correct web navigation. diff --git a/docs/ntfy.md b/docs/ntfy.md index 9bbef3ba..ff87cb66 100644 --- a/docs/ntfy.md +++ b/docs/ntfy.md @@ -74,9 +74,35 @@ artifacts=markdown,files Independently of any `notify` block, the server publishes one run-level notification to the `system` destination when a run needs a person: when it enters `waiting` (a human stage asked for a decision) and when it fails. Nothing else -notifies — queued, running, succeeded, canceled and interrupted runs are silent. -The payload names the run and its pipeline and carries a `click` action -`pipelines://run/` that opens that run in the Android app. +notifies by default — queued, running, canceled and interrupted runs are silent. +Runs explicitly started with notifications **on** also notify on success. +The payload names the run and its pipeline. Run notifications and stage hooks +use the configured web run link described below. + +### Open notifications in the web app + +Set `PIPELINES_WEB_PUBLIC_URL` on the **Pipelines server** to the authenticated web +proxy's phone-reachable origin, for example `https://pipelines.example.com` or +`http://192.168.1.20:8082` on a trusted LAN. Notifications then carry +`https://pipelines.example.com/runs/`, opening that exact run in the browser +(or installed web app). The existing sign-in gate remains; after login the web +proxy returns to the requested run. + +Use the address you actually open on the phone, not localhost, the API port, or +the ntfy address. No credentials, query, fragment or subpath are allowed. This +does not expose a port or add authentication to the underlying control API. +An invalid configured origin omits the click action and logs a configuration +warning rather than leaking credentials. Without this setting, existing +Android-only installations retain `pipelines://run/` for compatibility. + +Native installs save a default from the web proxy's advertised LAN address/port. +Override it with `PIPELINES_WEB_PUBLIC_URL=https://pipelines.example.com ./install`; +reinstalls preserve that saved choice unless overridden. Custom services set the +environment variable in their service definition; `pipelines service enable` +preserves it. Docker Compose accepts the same variable, pointing to a separately +reachable authenticated web proxy. Apply service changes only after active work +is finished. Already-delivered notifications keep their original click target; +verify a new notification after configuration is installed. A genuine event notifies once: a run that stays `waiting` is not republished on a read or restart, and a stage `notify` policy that already covers the same event diff --git a/internal/author/skill/references/notifications.md b/internal/author/skill/references/notifications.md index c2078619..64b71a1a 100644 --- a/internal/author/skill/references/notifications.md +++ b/internal/author/skill/references/notifications.md @@ -98,9 +98,16 @@ fails. Nothing else notifies — a queued, running, succeeded, canceled or inter run sends nothing. You do not author these; they are the operator default, so a run you never wired still reaches the operator. -The payload names the run and its pipeline and carries a `click` action -`pipelines://run/` that opens that run in the Android app (the app declares the -scheme). A run that stays waiting is not republished on a read or restart, and a +The payload names the run and its pipeline. When the operator configures +`PIPELINES_WEB_PUBLIC_URL` on the server, run notifications and stage hooks carry +the authenticated web link `/runs/`. Native installs default this to +their advertised web address; custom/VPN domains require the operator's actual +phone-reachable URL. Never use the API listener or ntfy URL, nor put credentials +in the click link. Existing Android-only installations without that setting keep +`pipelines://run/`. Invalid origins omit the link and log a warning. Existing +delivered alerts are not rewritten. Do not change service settings or send a test +notification merely to author a workflow. A run that stays waiting is not +republished on a read or restart, and a stage `notify` policy that already covers the same event (`on=waiting`, or a failure hook) suppresses the run-level default, so one genuine event means one notification. A delivery failure is logged, never fails the run, and is not retried. @@ -110,4 +117,3 @@ a run waiting for a decision, then a failed run, then a run that finished since operator last looked. It is derived from the run events the server already pushes (no extra request), an entry clears when the run is resolved or the operator opens it, and it can be deliberately dismissed. It is not a new place to start a run. - diff --git a/internal/installation/service.go b/internal/installation/service.go index efb37311..01e75722 100644 --- a/internal/installation/service.go +++ b/internal/installation/service.go @@ -78,7 +78,7 @@ func (d serviceDefinition) render(platform string) (string, error) { // Only explicitly supported settings are captured, never the complete environment // or provider credentials. File-based notification credentials remain references. -var serviceEnvironmentKeys = []string{"PATH", "HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CACHE_HOME", "PIPELINES_OPENCODE", "PIPELINES_OPENCODE_ENDPOINT", "PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE", "PIPELINES_OPENCODE_ENDPOINT_USERNAME", "PIPELINES_NTFY_URL", "PIPELINES_NTFY_PUBLIC_URL", "PIPELINES_NTFY_TOKEN_FILE"} +var serviceEnvironmentKeys = []string{"PATH", "HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CACHE_HOME", "PIPELINES_OPENCODE", "PIPELINES_OPENCODE_ENDPOINT", "PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE", "PIPELINES_OPENCODE_ENDPOINT_USERNAME", "PIPELINES_NTFY_URL", "PIPELINES_NTFY_PUBLIC_URL", "PIPELINES_NTFY_TOKEN_FILE", "PIPELINES_WEB_PUBLIC_URL"} func envValues(env map[string]string) []string { var values []string diff --git a/internal/installation/service_test.go b/internal/installation/service_test.go index 2736914f..3f41b57b 100644 --- a/internal/installation/service_test.go +++ b/internal/installation/service_test.go @@ -111,6 +111,21 @@ func TestServiceWritesRefuseUnmanagedFilesAndSymlinks(t *testing.T) { } } +func TestServicePreservesNotificationWebOrigin(t *testing.T) { + d := serviceDefinition{Binary: "/bin/server", Directory: "/repo", Environment: map[string]string{ + "PIPELINES_WEB_PUBLIC_URL": "https://pipelines.example.com", + }} + for _, platform := range []string{"linux", "darwin"} { + text, err := d.render(platform) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(text, "PIPELINES_WEB_PUBLIC_URL") || !strings.Contains(text, "https://pipelines.example.com") { + t.Fatalf("%s lost notification web origin", platform) + } + } +} + func TestServiceConfigurationPreviewAndLifecycleWithMockOSManager(t *testing.T) { t.Setenv("PIPELINES_PREFIX", "") if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { diff --git a/internal/server/notification_web_test.go b/internal/server/notification_web_test.go new file mode 100644 index 00000000..c312b547 --- /dev/null +++ b/internal/server/notification_web_test.go @@ -0,0 +1,25 @@ +package server + +import "testing" + +func TestRunNotificationWebClick(t *testing.T) { + for _, tc := range []struct{ base, want string }{ + {"https://pipelines.example.com", "https://pipelines.example.com/runs/20"}, + {"http://10.0.0.75:8095/", "http://10.0.0.75:8095/runs/20"}, + {"", "pipelines://run/20"}, // Existing Android-only installations. + {"javascript:alert(1)", ""}, + {"https://user:password@example.com", ""}, + {"https://example.com?token=secret", ""}, + {"https://example.com/#fragment", ""}, + {"https://example.com/subpath", ""}, // Web app uses origin-root routes. + {"/runs", ""}, + {"https://example.com\r\nClick:bad", ""}, + } { + t.Run(tc.base, func(t *testing.T) { + t.Setenv("PIPELINES_WEB_PUBLIC_URL", tc.base) + if got := runAttentionClick(20); got != tc.want { + t.Fatalf("click = %q, want %q", got, tc.want) + } + }) + } +} diff --git a/internal/server/notify.go b/internal/server/notify.go index 51b0beb0..42233cee 100644 --- a/internal/server/notify.go +++ b/internal/server/notify.go @@ -73,7 +73,7 @@ func (r *runner) notifyStep(id int64, name, workspace string, policy *protocol.N return } receipt := &receipts[len(receipts)-1] - result, err := stage.PublishNtfy(ctx, "system", title, summary, filename, data, "") + result, err := stage.PublishNtfy(ctx, "system", title, summary, filename, data, runAttentionClick(id)) if err != nil { receipt.Status = "failed" receipt.Diagnostic = err.Error() @@ -294,10 +294,18 @@ func (r *runner) forgetAttention(id int64) { delete(r.attentionSent, id) } -// runAttentionClick is the deep link a run notification carries so tapping it -// opens the run in the Android app, whose intent filter registers this scheme -// and host. +// Prefer the operator's authenticated web origin. Never infer a public address +// from a request Host, the unauthenticated API listener or the ntfy server URL. +// Older Android-only installations retain their deep link until configured. func runAttentionClick(id int64) string { + if raw := os.Getenv("PIPELINES_WEB_PUBLIC_URL"); raw != "" { + base := publicNtfyBase(raw) + if base == "" { + log.Printf("notification web link unavailable: PIPELINES_WEB_PUBLIC_URL must be an HTTP(S) origin without credentials, query or fragment") + return "" + } + return fmt.Sprintf("%s/runs/%d", base, id) + } return fmt.Sprintf("pipelines://run/%d", id) } diff --git a/internal/server/notify_test.go b/internal/server/notify_test.go index 59ca9b5c..d17c5603 100644 --- a/internal/server/notify_test.go +++ b/internal/server/notify_test.go @@ -22,6 +22,7 @@ import ( ) func TestStageNotificationEventsAndArtifactAudit(t *testing.T) { + t.Setenv("PIPELINES_WEB_PUBLIC_URL", "https://pipelines.example.com/") ctx, cancel := context.WithCancel(context.Background()) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "notify.db")) @@ -36,6 +37,9 @@ func TestStageNotificationEventsAndArtifactAudit(t *testing.T) { var calls atomic.Int32 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { count := calls.Add(1) + if click := r.Header.Get("Click"); !strings.HasPrefix(click, "https://pipelines.example.com/runs/") { + t.Errorf("stage notification lacks web run link: %q", click) + } filename, _ := new(mime.WordDecoder).DecodeHeader(r.Header.Get("Filename")) body, _ := io.ReadAll(r.Body) if filename == "report.txt" && string(body) != "created file" { @@ -287,6 +291,7 @@ func TestRunAttentionEventMapping(t *testing.T) { // the run and its pipeline, the click action opens the run, only waiting/failed // publish, and a run that stays waiting is never republished. func TestRunAttentionNotificationDelivery(t *testing.T) { + t.Setenv("PIPELINES_WEB_PUBLIC_URL", "https://pipelines.example.com") ctx, cancel := context.WithCancel(context.Background()) defer cancel() store, err := pipeline.Open(filepath.Join(t.TempDir(), "attention.db")) @@ -336,7 +341,7 @@ func TestRunAttentionNotificationDelivery(t *testing.T) { if first.title != "Run needs your decision" || !strings.Contains(first.message, "Run #7") || !strings.Contains(first.message, "release") || !strings.Contains(first.message, "decision") || !strings.Contains(first.message, "approve") { t.Fatalf("waiting notification does not identify the run/pipeline/need: %+v", first) } - if first.click != "pipelines://run/7" { + if first.click != "https://pipelines.example.com/runs/7" { t.Fatalf("waiting notification click does not open the run: %q", first.click) } if first.authorization != "" { @@ -346,7 +351,7 @@ func TestRunAttentionNotificationDelivery(t *testing.T) { if second.title != "Run failed" || !strings.Contains(second.message, "Run #7") || !strings.Contains(second.message, "release") || !strings.Contains(second.message, "deploy") || !strings.Contains(second.message, "provider returned 500") { t.Fatalf("failed notification is not actionable: %+v", second) } - if second.click != "pipelines://run/7" { + if second.click != "https://pipelines.example.com/runs/7" { t.Fatalf("failed notification click does not open the run: %q", second.click) } select { diff --git a/scripts/setup-native.py b/scripts/setup-native.py index dbdac4a5..26beaf89 100644 --- a/scripts/setup-native.py +++ b/scripts/setup-native.py @@ -25,6 +25,7 @@ import subprocess import sys import tempfile from pathlib import Path +from urllib.parse import urlsplit from zoneinfo import ZoneInfoNotFoundError @@ -642,6 +643,16 @@ class Setup: raise ValueError("Invalid API/web bind address; use an IPv4 address such as 0.0.0.0 or 127.0.0.1") from None prefix = self.resolve_prefix() allow_authoring = self.authoring_role(prefix, args.allow_authoring) + web_public_url = self.environ.get("PIPELINES_WEB_PUBLIC_URL", "") + previous = prefix / MARKER + if not web_public_url and previous.is_file() and not previous.is_symlink(): + web_public_url = json.loads(previous.read_text()).get("opencode_env", {}).get("PIPELINES_WEB_PUBLIC_URL", "") + if web_public_url: + parsed = urlsplit(web_public_url) + if (parsed.scheme not in ("http", "https") or not parsed.hostname or parsed.username is not None + or parsed.password is not None or parsed.query or parsed.fragment or parsed.path not in ("", "/") + or any(ord(c) < 33 for c in web_public_url)): + raise ValueError("PIPELINES_WEB_PUBLIC_URL must be an HTTP(S) web origin without credentials, path, query or fragment") harness = self.harness() service_path = self.service_path() # Reuse an already built ntfy binary and skip the release lookup, so a @@ -684,6 +695,7 @@ class Setup: if self.environ.get(key): opencode_env[key] = self.environ[key] opencode_env["PIPELINES_DEFAULT_MODEL"] = args.model or pipeline_model + opencode_env["PIPELINES_WEB_PUBLIC_URL"] = web_public_url or f"http://{lan_ip if args.web_host == '0.0.0.0' else args.web_host}:{args.web_port}" path_value = str(prefix / "bin") + os.pathsep + self.environ.get("PATH", "") manifest = { "format": 1, diff --git a/scripts/test_setup_native.py b/scripts/test_setup_native.py index 7c11fdbf..a8b12e50 100644 --- a/scripts/test_setup_native.py +++ b/scripts/test_setup_native.py @@ -298,6 +298,31 @@ class SetupNativeTests(unittest.TestCase): installer.run(setup_native.arguments(arguments)) self.assertFalse(manage.load(prefix)["allow_authoring"]) + def test_notification_web_url_is_preserved_and_can_be_overridden(self): + for override in (None, "https://new-pipelines.example.com"): + with self.subTest(override=override), tempfile.TemporaryDirectory() as directory: + installer = self.installer(directory, Recorder()) + prefix = Path(directory) / "prefix" + prefix.mkdir() + (prefix / setup_native.MARKER).write_text(json.dumps({"opencode_env": { + "PIPELINES_WEB_PUBLIC_URL": "https://pipelines.example.com"}})) + if override: + installer.environ["PIPELINES_WEB_PUBLIC_URL"] = override + with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \ + patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \ + patch.object(setup_native.configure, "host_timezone", return_value="UTC"), \ + redirect_stdout(io.StringIO()): + installer.run(setup_native.arguments([])) + self.assertEqual(manage.load(prefix)["opencode_env"]["PIPELINES_WEB_PUBLIC_URL"], + override or "https://pipelines.example.com") + + def test_invalid_notification_web_url_refused_before_install(self): + with tempfile.TemporaryDirectory() as directory: + installer = self.installer(directory, Recorder()) + installer.environ["PIPELINES_WEB_PUBLIC_URL"] = "https://user:secret@example.com" + with self.assertRaisesRegex(ValueError, "PIPELINES_WEB_PUBLIC_URL"): + installer.run(setup_native.arguments([])) + def test_missing_opencode_fails_actionably(self): with tempfile.TemporaryDirectory() as directory: installer = self.installer(directory, Recorder()) @@ -325,6 +350,7 @@ class SetupNativeTests(unittest.TestCase): self.assertEqual(config["opencode"], "/usr/bin/opencode") self.assertEqual(config["pi"], "") self.assertEqual(config["opencode_env"]["PIPELINES_DEFAULT_MODEL"], "fixture/host-default") + self.assertEqual(config["opencode_env"]["PIPELINES_WEB_PUBLIC_URL"], "http://192.168.1.20:8082") host = json.loads(installer.harness_config_path().read_text())["profiles"]["opencode"] self.assertEqual(host["model"], "fixture/host-default") self.assertEqual(host["pipeline_model"], "fixture/host-default") diff --git a/web/test/notification-web-link.mjs b/web/test/notification-web-link.mjs new file mode 100644 index 00000000..50c69a4c --- /dev/null +++ b/web/test/notification-web-link.mjs @@ -0,0 +1,39 @@ +// Real authenticated proxy, private fixture credentials, no live server/ntfy. +import assert from "node:assert/strict"; +import { spawn } from "node:child_process"; +import { mkdtemp, writeFile, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import net from "node:net"; +import { eventually } from "./browser.mjs"; + +const scratch = await mkdtemp(join(process.argv[2] ?? tmpdir(), "notification-web-link-")); +const auth = join(scratch, "auth.json"); +await writeFile(auth, JSON.stringify({ password: "fixture-password-only", secret: "fixture-secret-not-real-credentials-123456789" }), { mode: 0o600 }); +const port = await new Promise(resolve => { const s = net.createServer(); s.listen(0, "127.0.0.1", () => { const p = s.address().port; s.close(() => resolve(p)); }); }); +const child = spawn(process.execPath, ["web/serve.mjs", "--host", "127.0.0.1", "--port", String(port), "--pipelines", "127.0.0.1:1"], { + cwd: resolve(import.meta.dirname, "../.."), env: { ...process.env, PIPELINES_WEB_AUTH: "required", PIPELINES_WEB_AUTH_FILE: auth }, stdio: "ignore", +}); +const origin = `http://127.0.0.1:${port}`; +try { + await eventually(async () => { try { return (await fetch(origin + "/login")).ok; } catch { return false; } }, { timeout: 8000 }); + const target = "/runs/20"; + const gated = await fetch(origin + target, { headers: { Accept: "text/html" }, redirect: "manual" }); + assert.equal(gated.status, 302); + const login = new URL(gated.headers.get("location"), origin); + assert.equal(login.pathname, "/login"); assert.equal(login.searchParams.get("next"), target); + assert.match(await (await fetch(login)).text(), /name="next" value="\/runs\/20"/); + const denied = await fetch(origin + "/login", { method: "POST", body: new URLSearchParams({ password: "wrong", next: target }), redirect: "manual" }); + assert.equal(denied.status, 401); + const signed = await fetch(origin + "/login", { method: "POST", body: new URLSearchParams({ password: "fixture-password-only", next: target }), redirect: "manual" }); + assert.equal(signed.status, 303); assert.equal(signed.headers.get("location"), target); + const cookie = signed.headers.get("set-cookie").split(";")[0]; + const run = await fetch(origin + target, { headers: { cookie }, redirect: "manual" }); + assert.equal(run.status, 200); assert.match(run.headers.get("content-type"), /text\/html/); + console.log("PASS: notification web run link remains authenticated and returns to the exact run after login; no notification or provider calls."); +} finally { + if (child.exitCode === null && child.signalCode === null) { + const exited = new Promise(resolve => child.once("exit", resolve)); child.kill("SIGTERM"); await exited; + } + await rm(scratch, { recursive: true, force: true }); +}