From 2752d6cb2609e5f51c86c78f9caf5975331ec32c Mon Sep 17 00:00:00 2001 From: kreedom <60944649+kreedom@users.noreply.github.com> Date: Wed, 20 May 2020 22:20:31 +0300 Subject: [PATCH] vmalert add quotes escape function (#510) * vmalert add quotes escape function Co-authored-by: kreedom --- app/vmalert/notifier/alert_test.go | 10 +++++----- app/vmalert/notifier/template_func.go | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/vmalert/notifier/alert_test.go b/app/vmalert/notifier/alert_test.go index 6612891b21..3cd032d6a8 100644 --- a/app/vmalert/notifier/alert_test.go +++ b/app/vmalert/notifier/alert_test.go @@ -52,15 +52,15 @@ func TestAlert_ExecTemplate(t *testing.T) { { name: "expression-template", alert: &Alert{ - Expr: "vm_rows>0", + Expr: `vm_rows{"label"="bar"}>0`, }, annotations: map[string]string{ - "exprEscapedQuery": "{{ $expr|queryEscape }}", - "exprEscapedPath": "{{ $expr|pathEscape }}", + "exprEscapedQuery": "{{ $expr|quotesEscape|queryEscape }}", + "exprEscapedPath": "{{ $expr|quotesEscape|pathEscape }}", }, expTpl: map[string]string{ - "exprEscapedQuery": "vm_rows%3E0", - "exprEscapedPath": "vm_rows%3E0", + "exprEscapedQuery": "vm_rows%7B%5C%22label%5C%22%3D%5C%22bar%5C%22%7D%3E0", + "exprEscapedPath": "vm_rows%7B%5C%22label%5C%22=%5C%22bar%5C%22%7D%3E0", }, }, } diff --git a/app/vmalert/notifier/template_func.go b/app/vmalert/notifier/template_func.go index 88547d1a5b..abfd8fc918 100644 --- a/app/vmalert/notifier/template_func.go +++ b/app/vmalert/notifier/template_func.go @@ -148,6 +148,9 @@ func InitTemplateFunc(externalURL *url.URL) { "queryEscape": func(q string) string { return url.QueryEscape(q) }, + "quotesEscape": func(q string) string { + return strings.Replace(q, `"`, `\"`, -1) + }, } }