Alerting: Only render external runbook URLs in notification emails #838
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
cursor[bot]
<!-- CURSOR_AGENT_PR_BODY_BEGIN -->
What is this feature?
The default alert notification email (
public/emails/ng_alert_notification.html/.txt) renders therunbook_urlannotation as a View runbook button, as a row in the annotations table, and as aRunbook:line in the plain-text body. This PR changesemailSender.SendEmail(pkg/services/ngalert/notifier/sender.go) so that it removesrunbook_urlfromAlerts[].AnnotationsandCommonAnnotationsbefore the notifications service renders the template, unless the value is an absolutehttp/httpsURL whose host is not this Grafana server.Here, "this Grafana server" means the host of the email data's
ExternalURL, which production sets fromcfg.AppURL(root_url). The check works like this:http(s)://host/.... Values with userinfo, an opaque or missing authority, backslashes, whitespace or control characters are refused.2130706433,0x7f.1,0177.0.0.1or127.1parsed 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 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, andGET /logoutended 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 ||
TestIntegrationNotificationChannelsinpkg/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: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.github.com/grafana/alertingmodule, whose default text templates print all annotations, includingrunbook_url. The receiving apps may linkify them. They don't pass through this email sender.summary,description, custom keys): the email prints these as text. They are not URL fields and are not checked here.GET /logout(pkg/api/login.go,Logout) changes state on a GET, so any link to it ends the session.Please check that:
<!-- 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> <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> </div>
<!-- CURSOR_SUMMARY -->
---
<!-- /CURSOR_SUMMARY -->