Files
Pulse/internal/notifications/notifications_security_cli_test.go
rcourtman 7d60209fac Harden notification destinations, webhook headers, and Apprise CLI
Refuse blank or whitespace-only SMTP servers in EnhancedEmailManager
before dialing so an empty host cannot reach loopback under tenant
outbound policy.

Bind saved SMTP passwords, Apprise API keys, and masked webhook headers
and custom fields to their saved destination and transport security so
updates or test sends to another host require re-entering the secret.

Percent-encode control bytes and cap length on alert-derived webhook
headers (X-Pulse-Event-ID and ntfy Title) so agent-supplied IDs cannot
fail grouped webhook batches. Pass the Apprise CLI body on standard
input with NUL bytes removed and bound the title argument so
unrepresentable alert text cannot block grouped Apprise CLI deliveries.

Contract-Neutral: Security hardening for notification destination binding, SMTP blank-host rejection, webhook header encoding, and Apprise CLI delivery without changing public API contracts
2026-11-24 11:59:33 +00:00

100 lines
2.5 KiB
Go

package notifications
import (
"context"
"errors"
"slices"
"testing"
)
func TestSendAppriseViaCLINoTargets(t *testing.T) {
nm := NewNotificationManager("")
defer nm.Stop()
err := nm.sendAppriseViaCLI(AppriseConfig{
CLIPath: "apprise",
TimeoutSeconds: 1,
}, "title", "body")
if err == nil {
t.Fatalf("expected error when no targets configured")
}
}
func TestSendAppriseViaCLIExecError(t *testing.T) {
nm := NewNotificationManager("")
defer nm.Stop()
nm.appriseExec = func(ctx context.Context, args []string, _ string) (int64, error) {
return int64(len("boom")), errors.New("exec failed")
}
err := nm.sendAppriseViaCLI(AppriseConfig{
CLIPath: "apprise",
TimeoutSeconds: 1,
Targets: []string{"discord://token"},
}, "title", "body")
if err == nil {
t.Fatalf("expected exec error")
}
}
func TestSendAppriseViaCLISuccess(t *testing.T) {
nm := NewNotificationManager("")
defer nm.Stop()
nm.appriseExec = func(ctx context.Context, args []string, _ string) (int64, error) {
return int64(len("ok")), nil
}
err := nm.sendAppriseViaCLI(AppriseConfig{
CLIPath: "apprise",
TimeoutSeconds: 1,
Targets: []string{"discord://token"},
}, "title", "body")
if err != nil {
t.Fatalf("expected success, got %v", err)
}
}
func TestSendAppriseViaCLISuccessNoOutput(t *testing.T) {
nm := NewNotificationManager("")
defer nm.Stop()
nm.appriseExec = func(ctx context.Context, args []string, _ string) (int64, error) {
return 0, nil
}
err := nm.sendAppriseViaCLI(AppriseConfig{
CLIPath: "apprise",
TimeoutSeconds: 1,
Targets: []string{"discord://token"},
}, "title", "body")
if err != nil {
t.Fatalf("expected success, got %v", err)
}
}
func TestSendAppriseViaCLIPreservesTelegramTopicTarget(t *testing.T) {
nm := NewNotificationManager("")
defer nm.Stop()
const topicTarget = "tgram://bot-token/-1001234567890:42"
var receivedArgs []string
nm.appriseExec = func(ctx context.Context, args []string, _ string) (int64, error) {
receivedArgs = slices.Clone(args)
return 0, nil
}
err := nm.sendAppriseViaCLI(AppriseConfig{
CLIPath: "apprise",
TimeoutSeconds: 1,
Targets: []string{topicTarget},
}, "title", "body")
if err != nil {
t.Fatalf("expected Telegram topic target delivery to succeed, got %v", err)
}
if len(receivedArgs) == 0 || receivedArgs[len(receivedArgs)-1] != topicTarget {
t.Fatalf("expected Telegram topic target to be preserved as one CLI argument, got %#v", receivedArgs)
}
}