fc7dbb0ac0
Server: drop ntfy publishing (stage hooks, run outbox, chat attention, chat tool notify, auto-switch and schedule notices), the chat push outbox (its table is dropped at startup), ntfy status/endpoints and PIPELINES_NTFY_*. The app_notifications stream, subscribe/clear and kinds are unchanged. Notification settings keep run_finished/chat_needs_you/chat_reply_finished; ntfy_enabled is accepted and ignored and its stored row is never read. Stage type=ntfy is rejected: "ntfy stages were removed; notifications go to the Pipelines apps — use the stage notify hook". The notify hook remains. Installer: no ntfy build, accounts, config or supervisor child; manifest gets top-level "mode" (legacy ntfy.mode still read). Reinstall retires an old ntfy (safe PID-file stop, deletes binary, config, data dir, token, log, with a note in logs/install.log); uninstall cleans leftovers. App: ntfy settings section replaced by "Notification events"; texts updated. Docs: docs/ntfy.md folded into docs/notifications.md; install, docker, cli, author skill references updated; security review M1 marked resolved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
133 lines
4.4 KiB
Go
133 lines
4.4 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"io"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"syscall"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// notificationLinkLimit bounds a `link=` file: it holds one URL, not a document.
|
|
const notificationLinkLimit = 4 << 10
|
|
|
|
// resolveNotificationLink reads the tap target a stage's `link=` pattern names
|
|
// in the run's shared public/: the first non-empty line of the most recently
|
|
// modified match, when it is a valid absolute http(s) URL.
|
|
// It returns the link and its public/ file, or no link and the reason for the
|
|
// notification receipt.
|
|
func (r *runner) resolveNotificationLink(runID int64, pattern string) (string, string, string) {
|
|
if pattern == "" {
|
|
return "", "", ""
|
|
}
|
|
rel := r.publicNotificationFile(runID, pattern, "")
|
|
if rel == "" {
|
|
return "", "", fmt.Sprintf("notify link: no public file matches %s", pattern)
|
|
}
|
|
data, err := readNotificationLink(filepath.Join(r.artifactsRoot, fmt.Sprint(runID), "public"), rel)
|
|
if err != nil {
|
|
return "", "", fmt.Sprintf("notify link: public/%s %v", rel, err)
|
|
}
|
|
for _, line := range strings.Split(string(data), "\n") {
|
|
if line = strings.TrimSpace(line); line == "" {
|
|
continue
|
|
}
|
|
if !validNotificationLink(line) {
|
|
return "", "", fmt.Sprintf("notify link: public/%s does not start with an absolute http(s) URL", rel)
|
|
}
|
|
return line, rel, ""
|
|
}
|
|
return "", "", fmt.Sprintf("notify link: public/%s is empty", rel)
|
|
}
|
|
|
|
// openPublicFile opens a regular file below root without following a symlink
|
|
// out of it or blocking on a special file.
|
|
func openPublicFile(root, rel string) (*os.File, os.FileInfo, error) {
|
|
scoped, err := os.OpenRoot(root)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
defer scoped.Close()
|
|
file, err := scoped.OpenFile(filepath.FromSlash(rel), os.O_RDONLY|syscall.O_NONBLOCK|syscall.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
info, err := file.Stat()
|
|
if err != nil || !info.Mode().IsRegular() {
|
|
file.Close()
|
|
return nil, nil, fmt.Errorf("not a regular file")
|
|
}
|
|
return file, info, nil
|
|
}
|
|
|
|
// readNotificationLink reads one small regular file below root.
|
|
func readNotificationLink(root, rel string) ([]byte, error) {
|
|
file, info, err := openPublicFile(root, rel)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("is unreadable")
|
|
}
|
|
defer file.Close()
|
|
if info.Size() > notificationLinkLimit {
|
|
return nil, fmt.Errorf("must be a regular file of at most 4 KiB")
|
|
}
|
|
data, err := io.ReadAll(io.LimitReader(file, notificationLinkLimit+1))
|
|
if err != nil || len(data) > notificationLinkLimit {
|
|
return nil, fmt.Errorf("must be a regular file of at most 4 KiB")
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// validNotificationLink accepts only an absolute http(s) URL with a host, no
|
|
// credentials, no fragment, no whitespace or control characters and a bounded
|
|
// length. The value is an operator-visible link, never a place to smuggle a
|
|
// token.
|
|
func validNotificationLink(link string) bool {
|
|
if len(link) > 2048 || !utf8.ValidString(link) || strings.ContainsAny(link, "\r\n\t\x00 ") {
|
|
return false
|
|
}
|
|
u, err := url.Parse(link)
|
|
return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Hostname() != "" && u.User == nil && u.Fragment == ""
|
|
}
|
|
|
|
// withNotificationLink makes link the app notification's tap target, keeping
|
|
// the run as its run action.
|
|
func withNotificationLink(record pipeline.AppNotificationRecord, link string) pipeline.AppNotificationRecord {
|
|
if link != "" {
|
|
record.Click, record.RunClick = link, runAttentionClick(record.RunID)
|
|
}
|
|
return record
|
|
}
|
|
|
|
// stageHookLink re-resolves the `link=` of the stage hook behind a queued run
|
|
// event, so the durable retry of the hook's unrecorded notification keeps its
|
|
// link.
|
|
// It returns the link and its public/ file, or nothing.
|
|
func (r *runner) stageHookLink(n pipeline.RunNotification) (string, string) {
|
|
event := map[string]string{protocol.NotifyWaiting: protocol.NotifyWaiting, "failed": "failure", "succeeded": "success"}[n.Event]
|
|
if event == "" || r.store == nil {
|
|
return "", ""
|
|
}
|
|
detail, err := r.store.GetRun(r.ctx, n.RunID)
|
|
if err != nil {
|
|
return "", ""
|
|
}
|
|
for _, step := range detail.Steps {
|
|
if step.Sequence != n.Sequence {
|
|
continue
|
|
}
|
|
for _, definition := range detail.Graph.Stages {
|
|
if definition.Key == step.Stage && definition.Notify != nil && slices.Contains(definition.Notify.On, event) {
|
|
link, file, _ := r.resolveNotificationLink(n.RunID, definition.Notify.Link)
|
|
return link, file
|
|
}
|
|
}
|
|
}
|
|
return "", ""
|
|
}
|