From 08af129b4da1bf61efd8051b0e6490c92b769b91 Mon Sep 17 00:00:00 2001 From: Paul Gier Date: Wed, 4 Oct 2017 10:01:04 -0500 Subject: [PATCH] cmd/prometheus: don't allow quotes at beginning or end of url This prevents accidental copy/paste error where a the web.external-url or alertmanager.url params could have an extra set of quotes. See also: https://github.com/prometheus/prometheus/issues/1229 --- cmd/prometheus/config.go | 9 +++++++++ cmd/prometheus/config_test.go | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/cmd/prometheus/config.go b/cmd/prometheus/config.go index 73f17986d2..2460e96f7a 100644 --- a/cmd/prometheus/config.go +++ b/cmd/prometheus/config.go @@ -319,6 +319,11 @@ func parse(args []string) error { return nil } +func startsOrEndsWithQuote(s string) bool { + return strings.HasPrefix(s, "\"") || strings.HasPrefix(s, "'") || + strings.HasSuffix(s, "\"") || strings.HasSuffix(s, "'") +} + func parsePrometheusURL() error { if cfg.prometheusURL == "" { hostname, err := os.Hostname() @@ -330,6 +335,8 @@ func parsePrometheusURL() error { return err } cfg.prometheusURL = fmt.Sprintf("http://%s:%s/", hostname, port) + } else if startsOrEndsWithQuote(cfg.prometheusURL) { + return fmt.Errorf("web.external-url must not begin or end with quotes") } promURL, err := url.Parse(cfg.prometheusURL) @@ -349,6 +356,8 @@ func parsePrometheusURL() error { func validateAlertmanagerURL(u string) error { if u == "" { return nil + } else if startsOrEndsWithQuote(u) { + return fmt.Errorf("alertmanager.url must not begin or end with quotes: %s", u) } url, err := url.Parse(u) if err != nil { diff --git a/cmd/prometheus/config_test.go b/cmd/prometheus/config_test.go index f26f8cbe1d..3248820d07 100644 --- a/cmd/prometheus/config_test.go +++ b/cmd/prometheus/config_test.go @@ -40,6 +40,10 @@ func TestParse(t *testing.T) { input: []string{"-web.external-url", "'https://url/prometheus'"}, valid: false, }, + { + input: []string{"-web.external-url", "'relative/path/with/quotes'"}, + valid: false, + }, { input: []string{"-alertmanager.url", ""}, valid: true, @@ -56,6 +60,14 @@ func TestParse(t *testing.T) { input: []string{"-alertmanager.url", "https://double--dash.de"}, valid: true, }, + { + input: []string{"-alertmanager.url", "'http://starts/with/quote"}, + valid: false, + }, + { + input: []string{"-alertmanager.url", "ends/with/quote\""}, + valid: false, + }, } for i, test := range tests {