Files
pipelines/internal/pipeline/notify.go
T
alan f5f31b7bcd Notify link=: a stage's public/ file sets the phone tap target
A stage's notify block may name `link=<pattern>` (validated like `open=`).
When the hook fires, the newest public/ match is read (at most 4 KiB); its
first non-empty line, if an absolute http(s) URL ntfy accepts as a click,
becomes the ntfy Click and the app notification click, with the run kept
as the app's run action and ntfy's browser view. A missing file or invalid
URL keeps today's tap target and is noted on the receipt diagnostic.

Android opens such an http(s) click in the browser (after clearing the
notification) instead of handing it to Flutter as a route.
2026-12-02 12:20:22 +00:00

91 lines
2.8 KiB
Go

package pipeline
import (
"fmt"
"git.a-wan.net/alan/pipelines/internal/protocol"
"path"
"regexp"
"strings"
)
func parseNotify(text string) (*protocol.NotificationPolicy, error) {
values := map[string]string{}
for _, line := range strings.Split(text, "\n") {
if strings.TrimSpace(line) == "" {
continue
}
key, value, ok := strings.Cut(line, "=")
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
if !ok || value == "" {
return nil, fmt.Errorf("invalid notify setting %q", line)
}
if _, exists := values[key]; exists {
return nil, fmt.Errorf("duplicate notify setting %s", key)
}
switch key {
case "on", "destination", "artifacts", "open", "link":
default:
return nil, fmt.Errorf("unknown notify setting %s", key)
}
values[key] = value
}
// Accept legacy destination syntax without rewriting immutable sources.
// All stage notification hooks now use the server's global connection.
if destination := values["destination"]; destination != "" && !regexp.MustCompile(`^[a-z][a-z0-9_]{0,63}$`).MatchString(destination) {
return nil, fmt.Errorf("invalid legacy notify destination")
}
p := &protocol.NotificationPolicy{Destination: "system"}
parse := func(text string, allowed map[string]bool) ([]string, error) {
var result []string
seen := map[string]bool{}
for _, item := range strings.Split(text, ",") {
item = strings.TrimSpace(item)
if !allowed[item] || seen[item] {
return nil, fmt.Errorf("invalid or duplicate notify value %q", item)
}
seen[item] = true
result = append(result, item)
}
return result, nil
}
var err error
p.On, err = parse(values["on"], map[string]bool{"success": true, "failure": true, protocol.NotifyWaiting: true})
if err != nil {
return nil, err
}
if values["artifacts"] != "" {
p.Artifacts, err = parse(values["artifacts"], map[string]bool{"markdown": true, "files": true})
if err != nil {
return nil, err
}
}
if p.Open, err = notifyPattern("open", values["open"]); err != nil {
return nil, err
}
if p.Link, err = notifyPattern("link", values["link"]); err != nil {
return nil, err
}
return p, nil
}
// notifyPattern validates an optional glob over the run's shared public/
// directory: relative, clean, no hidden or traversal segment.
func notifyPattern(key, pattern string) (string, error) {
if pattern == "" {
return "", nil
}
if strings.ContainsAny(pattern, "\\\x00\r\n") || strings.HasPrefix(pattern, "/") || path.IsAbs(pattern) || path.Clean(pattern) != pattern {
return "", fmt.Errorf("invalid notify %s pattern", key)
}
for _, part := range strings.Split(pattern, "/") {
if part == ".." || strings.HasPrefix(part, ".") || part == "" {
return "", fmt.Errorf("invalid notify %s pattern", key)
}
}
if _, err := path.Match(pattern, ""); err != nil {
return "", fmt.Errorf("invalid notify %s pattern: %v", key, err)
}
return pattern, nil
}