← Back to Pull requests

Alerting: Only render external runbook URLs in notification emails #838

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 22, 2026 00:28 UTC
Author

<!-- CURSOR_AGENT_PR_BODY_BEGIN -->
What is this feature?

The default alert notification email (public/emails/ng_alert_notification.html / .txt) renders the runbook_url annotation as a View runbook button, as a row in the annotations table, and as a Runbook: line in the plain-text body. This PR changes emailSender.SendEmail (pkg/services/ngalert/notifier/sender.go) so that it removes runbook_url from Alerts[].Annotations and CommonAnnotations before the notifications service renders the template, unless the value is an absolute http/https URL whose host is not this Grafana server.

Here, "this Grafana server" means the host of the email data's ExternalURL, which production sets from cfg.AppURL (root_url). The check works like this:

  • The value must parse as http(s)://host/.... Values with userinfo, an opaque or missing authority, backslashes, whitespace or control characters are refused.

  • Hosts are compared in browser-canonical form: lowercased, trailing dots trimmed, IPv6 literals normalized (IPv4-mapped addresses unmapped), and IPv4 shorthand such as 2130706433, 0x7f.1, 0177.0.0.1 or 127.1 parsed following the WHATWG URL rules. Hosts that aren't plain ASCII labels are refused, because IDNA mapping could turn them into this server's name.

  • The port is ignored, because cookies are shared across ports.

  • If the server host can't be determined, no runbook is rendered.
  • The title and a custom contact-point message are rendered by the alerting library before they reach the sender. If such a template printed a removed runbook, that exact value is also stripped from those strings. This only applies to values containing . or :, so placeholders such as - are left alone.

    Why do we need this feature?

    Anyone who can edit an alert rule controls runbook_url. Setting it to a URL on the Grafana server itself, for example /logout, made the email link back to Grafana. A recipient who clicked it opened Grafana with their session cookie, and GET /logout ended that session.

    Who is this feature for?

    Recipients of alert notification emails.

    Tests that prove the fix

    The failing tests are in https://github.com/shaw-andy/grafana/pull/836 (pkg/services/ngalert/notifier/runbook_url_test.go). That PR is intentionally not included here.

    Tests run:

    | Test | Without fix | With fix |
    | --- | --- | --- |
    | TestAlertEmailDoesNotLinkRunbookToThisGrafanaServer (from #836, on its branch) | 10 same-server cases FAIL, 2 external cases pass | all 12 PASS |
    | Local-only variant test (not committed): percent-encoded / fullwidth / ideographic-dot hosts, http:/, http:, http:///, backslash forms, embedded tab, leading/trailing whitespace, other port on same host, protocol-relative, templated {{ externalURL }}logout, javascript:, decimal/hex/octal/short IPv4, IPv4-mapped and expanded IPv6, each with the default message, default.message, and a custom message printing .CommonAnnotations.runbook_url | 23 of 24 FAIL (the 24th is a bare relative path, which cannot navigate from an email) | all PASS; external runbooks still linked |
    | go test ./pkg/services/ngalert/notifier/... (this branch) | — | PASS |
    | TestEmailNotifierIntegration (this branch) | — | PASS |
    | TestIntegrationNotificationChannels in pkg/tests/api/alerting (this branch, SQLite) | — | PASS |
    | go vet ./pkg/services/ngalert/notifier/, gofmt | — | clean |

    Special notes for your reviewer:

    Behaviour change: a runbook that points at a page on the same Grafana host (for example a dashboard on the same host) is no longer shown in notification emails. The rule's dashboard and panel links are still rendered by their own buttons.

    Other places that render runbook_url (or annotation text) as a link, not changed here and still open:

  • Frontend rule viewer (public/app/features/alerting/unified/components/rule-viewer/RuleViewer.tsx, isValidRunbookURL): this renders the runbook as a link and explicitly accepts relative and same-origin URLs. It needs a separate frontend PR.

  • Other receivers (Slack, Teams, PagerDuty, webhook, etc.): these are built in the external github.com/grafana/alerting module, whose default text templates print all annotations, including runbook_url. The receiving apps may linkify them. They don't pass through this email sender.

  • Free-text annotations (summary, description, custom keys): the email prints these as text. They are not URL fields and are not checked here.

  • Underlying issue: GET /logout (pkg/api/login.go, Logout) changes state on a GET, so any link to it ends the session.
  • Please check that:

  • It works as expected from a user's perspective.

  • If this is a pre-GA feature, it is behind a feature toggle.

  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.
  • <!-- CURSOR_AGENT_PR_BODY_END -->

    <div><a href="https://cursor.com/agents/bc-114cc60f-0015-4165-a596-ee65a348fe34?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-web-light.png"><img alt="Open in Web" width="114" height="28" src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a>&nbsp;<a href="https://cursor.com/background-agent?bcId=bc-114cc60f-0015-4165-a596-ee65a348fe34&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img alt="Open in Cursor" width="131" height="28" src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a>&nbsp;</div>

    <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    Medium Risk

    Security-focused change to email notification content with strict URL parsing; behavior change for same-host runbooks but limited to the email sender path.

    > <sup>Reviewed by Cursor Bugbot for commit d1d020d7380321483f061ab38f8b1c89d29de84b. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: cursor[bot] Review duration: 43m
    All Vouch review confidence checks passed
    Residual risk 0.33 = Change Risk (0.57) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 650 lines diff across 8 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #838"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 43.0% · Lower = rushed or shallow review
    43.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 32.5% · Safe to merge
    32.5%