0c75ff1b0d
- ntfy uses the link only beside a web run view; otherwise it keeps the run click and the receipt says why (app click stays the link). - The outbox retry of a hook's failed publication re-resolves the stage's link=, so the ntfy and app tap targets keep it. - A valid link file is never the attached artifact. - Notification artifacts are read through os.OpenRoot with O_NOFOLLOW. - The link note is recorded once, on the first receipt, and never becomes the run-level failure reason. - Android: a recents relaunch does not reopen the link, and a run thread of several entries opens the run rather than the latest external page. - Docs and the protocol comment say "first non-empty line".
139 lines
4.7 KiB
Go
139 lines
4.7 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
"git.a-wan.net/alan/pipelines/internal/stage"
|
|
"io"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"syscall"
|
|
)
|
|
|
|
// 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 an absolute http(s) URL ntfy accepts as a click.
|
|
// 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 that
|
|
// also passes the ntfy click rules (no credentials, no fragment, bounded).
|
|
func validNotificationLink(link string) bool {
|
|
u, err := url.Parse(link)
|
|
return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Hostname() != "" && stage.ValidNtfyClick(link)
|
|
}
|
|
|
|
// ntfyLinkClick is the ntfy tap target: the link only while the run stays
|
|
// reachable through a web run view, else the run itself with a note saying so.
|
|
func ntfyLinkClick(runID int64, link string, views []stage.NtfyView) (string, string) {
|
|
if link == "" {
|
|
return runAttentionClick(runID), ""
|
|
}
|
|
if len(views) == 0 {
|
|
return runAttentionClick(runID), "notify link: ntfy keeps the run tap target without a web run view (PIPELINES_WEB_PUBLIC_URL)"
|
|
}
|
|
return link, ""
|
|
}
|
|
|
|
// 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 failed publication 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 "", ""
|
|
}
|