Open ntfy notifications in the app and notify chats that need the operator
Run notifications now always click through to pipelines://run/<id>; a valid PIPELINES_WEB_PUBLIC_URL adds an "Open in browser" ntfy view action instead of replacing the click. Queued rows derive links from the run id at delivery, so legacy rows holding a web URL still open the app. Unresolved chat questions/forms and permission requests publish one async, bounded notification to the system destination (pipelines://chat/<id> plus the browser action), deduplicated per form/request id across reconnects and replays. stage.PublishNtfy gains optional, strictly validated view actions. Server tests now clear the operator's real ntfy environment.
This commit is contained in:
+50
-2
@@ -92,14 +92,19 @@ func (NtfyStage) Run(ctx context.Context, def Definition, input Values) (Result,
|
||||
// An empty filename publishes an event without an attachment. A non-empty click
|
||||
// is an optional ntfy Click action (an absolute URL) so a tap on the notification
|
||||
// opens the destination the operator should act on; it is validated here and
|
||||
// never carries credentials.
|
||||
func PublishNtfy(ctx context.Context, destination, title, summary, filename string, data []byte, click string) (Values, error) {
|
||||
// never carries credentials. Optional views add ntfy "view" action buttons (for
|
||||
// example "Open in browser"); each is validated as strictly as the click.
|
||||
func PublishNtfy(ctx context.Context, destination, title, summary, filename string, data []byte, click string, views ...NtfyView) (Values, error) {
|
||||
if !destinationName.MatchString(destination) || strings.TrimSpace(title) == "" || strings.TrimSpace(summary) == "" || len(title) > 256 || len(summary) > 2048 || !utf8.ValidString(title) || !utf8.ValidString(summary) || strings.ContainsAny(title+summary, "\r\n\x00") {
|
||||
return nil, fmt.Errorf("invalid ntfy notification metadata")
|
||||
}
|
||||
if !validNtfyClick(click) {
|
||||
return nil, fmt.Errorf("invalid ntfy click action")
|
||||
}
|
||||
actions, err := ntfyViewActions(views)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(data) > 1<<20 || (filename != "" && (filename != filepath.Base(filename) || filename == "." || strings.ContainsAny(filename, "/\\\r\n\x00") || len(filename) > 200)) {
|
||||
return nil, fmt.Errorf("invalid ntfy attachment")
|
||||
}
|
||||
@@ -154,6 +159,9 @@ func PublishNtfy(ctx context.Context, destination, title, summary, filename stri
|
||||
if click != "" {
|
||||
request.Header.Set("Click", click)
|
||||
}
|
||||
if actions != "" {
|
||||
request.Header.Set("Actions", actions)
|
||||
}
|
||||
if filename != "" {
|
||||
request.Header.Set("Filename", mime.BEncoding.Encode("utf-8", filename))
|
||||
}
|
||||
@@ -213,3 +221,43 @@ func validNtfyClick(click string) bool {
|
||||
u, err := url.Parse(click)
|
||||
return err == nil && u.Scheme != "" && u.User == nil && u.Fragment == ""
|
||||
}
|
||||
|
||||
// NtfyView is one ntfy "view" action button: a tap opens URL. Label is short
|
||||
// printable ASCII and URL is an absolute HTTP(S) address without credentials.
|
||||
type NtfyView struct {
|
||||
Label string
|
||||
URL string
|
||||
}
|
||||
|
||||
// ntfyViewActions renders views in ntfy's simple Actions header format
|
||||
// ("view, <label>, <url>", separated by "; "). Commas, semicolons and quotes are
|
||||
// rejected rather than escaped, so a value can never split into another action.
|
||||
func ntfyViewActions(views []NtfyView) (string, error) {
|
||||
if len(views) > 3 { // ntfy allows at most three actions.
|
||||
return "", fmt.Errorf("at most 3 ntfy actions")
|
||||
}
|
||||
parts := make([]string, 0, len(views))
|
||||
for _, view := range views {
|
||||
label := strings.TrimSpace(view.Label)
|
||||
if label == "" || len(label) > 64 || strings.ContainsAny(label, ",;\"'\\=") || !printableASCII(label) {
|
||||
return "", fmt.Errorf("invalid ntfy action label")
|
||||
}
|
||||
if view.URL == "" || !validNtfyClick(view.URL) || strings.ContainsAny(view.URL, ",;\"'\\") || !printableASCII(view.URL) {
|
||||
return "", fmt.Errorf("invalid ntfy action URL")
|
||||
}
|
||||
if u, err := url.Parse(view.URL); err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Hostname() == "" {
|
||||
return "", fmt.Errorf("invalid ntfy action URL")
|
||||
}
|
||||
parts = append(parts, "view, "+label+", "+view.URL)
|
||||
}
|
||||
return strings.Join(parts, "; "), nil
|
||||
}
|
||||
|
||||
func printableASCII(text string) bool {
|
||||
for i := 0; i < len(text); i++ {
|
||||
if text[i] < 0x20 || text[i] > 0x7e {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -168,3 +168,49 @@ func TestNtfyDoesNotRedirectOrRetryAndRedactsErrors(t *testing.T) {
|
||||
t.Fatal("cancellation ignored")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNtfyClickAndViewActions(t *testing.T) {
|
||||
var click, actions []string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
click = append(click, r.Header.Get("Click"))
|
||||
actions = append(actions, r.Header.Get("Actions"))
|
||||
_, _ = w.Write([]byte(`{"id":"msg","event":"message"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
t.Setenv("PIPELINES_NTFY_ANIME_URL", srv.URL+"/anime")
|
||||
t.Setenv("PIPELINES_NTFY_ANIME_TOKEN", "")
|
||||
t.Setenv("PIPELINES_NTFY_ANIME_TOKEN_FILE", "")
|
||||
view := NtfyView{Label: "Open in browser", URL: "https://pipelines.example.com/runs/7"}
|
||||
if _, err := PublishNtfy(context.Background(), "anime", "Title", "Summary", "", nil, "pipelines://run/7", view); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := PublishNtfy(context.Background(), "anime", "Title", "Summary", "", nil, "pipelines://chat/ses_1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(click) != 2 || click[0] != "pipelines://run/7" || actions[0] != "view, Open in browser, https://pipelines.example.com/runs/7" || click[1] != "pipelines://chat/ses_1" || actions[1] != "" {
|
||||
t.Fatalf("click=%q actions=%q", click, actions)
|
||||
}
|
||||
for _, bad := range []NtfyView{
|
||||
{Label: "", URL: "https://example.com/runs/1"},
|
||||
{Label: "Open, now", URL: "https://example.com/runs/1"},
|
||||
{Label: "Open\r\nX: y", URL: "https://example.com/runs/1"},
|
||||
{Label: "Open", URL: ""},
|
||||
{Label: "Open", URL: "pipelines://run/1"},
|
||||
{Label: "Open", URL: "javascript:alert(1)"},
|
||||
{Label: "Open", URL: "https://user:pass@example.com/runs/1"},
|
||||
{Label: "Open", URL: "https://example.com/runs/1\r\nAuthorization: x"},
|
||||
{Label: "Open", URL: "https://example.com/a,b"},
|
||||
{Label: "Open", URL: "https://example.com/a;view, x, https://evil.example"},
|
||||
{Label: "Open", URL: "/runs/1"},
|
||||
} {
|
||||
if _, err := PublishNtfy(context.Background(), "anime", "Title", "Summary", "", nil, "", bad); err == nil {
|
||||
t.Fatalf("unsafe action accepted: %+v", bad)
|
||||
}
|
||||
}
|
||||
if _, err := PublishNtfy(context.Background(), "anime", "Title", "Summary", "", nil, "", view, view, view, view); err == nil {
|
||||
t.Fatal("more than three actions accepted")
|
||||
}
|
||||
if len(click) != 2 {
|
||||
t.Fatalf("invalid actions still published: %d", len(click))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user