Files
Pulse/internal/notifications/webhook_url_redaction.go
rcourtman 7904e5fcc9 Keep plain notification errors readable in the delivery log
The delivery log runs every error through the webhook URL redactor. When
the text held no http(s) URL, the redactor parsed the whole message as a
single URL. Queue errors start with prose like `process queued apprise
notification "...": ...`, and a colon before the first slash is not a
valid relative URL, so every Apprise CLI, SMTP and other URL-free failure
reached the UI as "[invalid webhook URL]".

A Pro user whose image lacked the apprise binary saw exactly that: the
delivery warning fired, but the log row pointed at a URL problem instead
of "executable file not found in PATH".

Text without an http(s) URL now has only its URL-shaped words (those
containing "/" or "?") parsed and redacted. A malformed one still fails
closed, and path and query credentials in schemeless words are still
masked.

Contract-Neutral: bug fix; URL-free delivery-log error prose is no longer parsed as one URL, and the documented redaction and fail-closed rules are unchanged
2026-11-02 22:10:43 +00:00

183 lines
5.8 KiB
Go

package notifications
import (
"errors"
"net/url"
"strings"
)
const invalidWebhookURLDiagnostic = "[invalid webhook URL]"
// RedactWebhookURLSecrets masks credentials commonly embedded in webhook URLs
// while preserving the URL shape needed for operator diagnostics.
func RedactWebhookURLSecrets(urlString string) string {
// Userinfo can contain a password or a credential used as the username.
// Do not use URL.Redacted: it preserves usernames. Fail closed on invalid
// URLs rather than returning unparsed credentials to diagnostic callers.
parsed, err := url.Parse(urlString)
if err != nil {
return invalidWebhookURLDiagnostic
}
if parsed.User != nil {
parsed.User = url.User("REDACTED")
urlString = parsed.String()
}
// Slack incoming webhook paths are credentials, including legacy paths.
// Match the parsed host, not a substring, and discard RawPath so escaped
// credentials cannot survive URL.String(). Do not change the destination.
switch strings.ToLower(parsed.Hostname()) {
case "hooks.slack.com", "hooks.slack-gov.com":
if strings.HasPrefix(parsed.Path, "/services/") {
parsed.Path = "/services/REDACTED"
} else {
parsed.Path = "/REDACTED"
}
parsed.RawPath = ""
urlString = parsed.String()
case "discord.com", "discordapp.com":
// Discord webhook IDs and tokens follow /webhooks/ in both
// unversioned and versioned API paths. Mask the entire suffix,
// including escaped credentials and compatibility endpoint paths.
if idx := strings.Index(parsed.Path, "/webhooks/"); idx != -1 {
parsed.Path = parsed.Path[:idx] + "/webhooks/REDACTED"
parsed.RawPath = ""
urlString = parsed.String()
}
}
// Telegram also supports local API servers, so retain host-independent
// masking, but inspect only the decoded path. Searching the whole URL
// misses escaped prefixes and can mistake hostnames or query URLs for
// bot credentials. Clear RawPath to prevent escaped secrets resurfacing.
if idx := strings.Index(parsed.Path, "/bot"); idx != -1 {
end := len(parsed.Path)
if suffix := strings.Index(parsed.Path[idx+4:], "/"); suffix != -1 {
end = idx + 4 + suffix
}
parsed.Path = parsed.Path[:idx+4] + "REDACTED" + parsed.Path[end:]
parsed.RawPath = ""
urlString = parsed.String()
}
// Decode names exactly once, as net/url does, but retain the original
// spelling, order and unrelated values in diagnostic URLs. Inspect every
// occurrence rather than Query().Get(), which would miss repeated keys.
// Mask recognised credential names regardless of case for diagnostics only;
// the destination retains the original, potentially case-sensitive query.
parts := strings.Split(parsed.RawQuery, "&")
changed := false
for i, part := range parts {
name, _, hasValue := strings.Cut(part, "=")
decoded, err := url.QueryUnescape(name)
if err != nil {
return invalidWebhookURLDiagnostic
}
switch strings.ToLower(decoded) {
case "token", "apikey", "api_key", "key", "secret", "password":
if hasValue {
parts[i] = name + "=REDACTED"
changed = true
}
}
}
if changed {
parsed.RawQuery = strings.Join(parts, "&")
return parsed.String()
}
return urlString
}
// RedactWebhookDiagnosticSecrets masks webhook URLs embedded in diagnostic
// text while retaining the non-secret context around them. A malformed URL
// still fails closed rather than returning potentially sensitive text.
func RedactWebhookDiagnosticSecrets(message string) string {
lowerMessage := strings.ToLower(message)
cursor := 0
foundURL := false
var redacted strings.Builder
for cursor < len(message) {
httpOffset := strings.Index(lowerMessage[cursor:], "http://")
httpsOffset := strings.Index(lowerMessage[cursor:], "https://")
offset := httpOffset
if offset == -1 || (httpsOffset != -1 && httpsOffset < offset) {
offset = httpsOffset
}
if offset == -1 {
break
}
start := cursor + offset
end := len(message)
if whitespace := strings.IndexAny(message[start:], " \t\r\n"); whitespace != -1 {
end = start + whitespace
}
redactedURL := RedactWebhookURLSecrets(message[start:end])
if redactedURL == invalidWebhookURLDiagnostic {
return invalidWebhookURLDiagnostic
}
redacted.WriteString(message[cursor:start])
redacted.WriteString(redactedURL)
cursor = end
foundURL = true
}
if !foundURL {
return redactSchemelessDiagnosticSecrets(message)
}
redacted.WriteString(message[cursor:])
return redacted.String()
}
// redactSchemelessDiagnosticSecrets handles text with no http(s) URL. Only
// words that could hold a path or query credential are parsed, and a
// malformed one still fails closed. Parsing the whole message as one URL
// replaced ordinary error prose such as "execute apprise CLI: executable file
// not found in PATH" with the invalid-URL marker, because a colon before the
// first slash is not a valid relative URL.
func redactSchemelessDiagnosticSecrets(message string) string {
var redacted strings.Builder
rest := message
for {
end := strings.IndexAny(rest, " \t\r\n")
if end == -1 {
end = len(rest)
}
word := rest[:end]
if strings.ContainsAny(word, "/?") {
word = RedactWebhookURLSecrets(word)
if word == invalidWebhookURLDiagnostic {
return invalidWebhookURLDiagnostic
}
}
redacted.WriteString(word)
if end == len(rest) {
return redacted.String()
}
redacted.WriteByte(rest[end])
rest = rest[end+1:]
}
}
func redactWebhookTransportError(err error) error {
if err == nil {
return nil
}
var urlError *url.Error
if !errors.As(err, &urlError) {
return err
}
redacted := *urlError
redacted.URL = RedactWebhookURLSecrets(urlError.URL)
if errors.Is(err, errNotificationRedirectOrigin) {
// A receiver controls Location and can echo private payloads or keys
// into arbitrary paths/query names. None of that URL is diagnostic data.
redacted.URL = "[redirect destination withheld]"
}
return &redacted
}