From c5a4705e73014a6ce6c77f9f165e1bc908205049 Mon Sep 17 00:00:00 2001 From: theluckystrike <51033404+theluckystrike@users.noreply.github.com> Date: Sat, 21 Nov 2026 15:49:49 +0700 Subject: [PATCH] Encode telemetry ping query values The custom query_string_normalizer joined keys and values without encoding them. A fail message such as an exception text with & or # was cut at that character, the rest became a bogus parameter, and a + turned into a space. Cronitor.job sends e.message on failure, so the message shown in Cronitor could lose most of the error. Form-encode every value and every metric in the normalizer and add a spec that decodes the built query string and compares the message, host and metric. --- lib/cronitor/monitor.rb | 5 +++-- spec/cronitor_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/cronitor/monitor.rb b/lib/cronitor/monitor.rb index 4833c1a..c57ec02 100644 --- a/lib/cronitor/monitor.rb +++ b/lib/cronitor/monitor.rb @@ -159,11 +159,12 @@ def ping(params = {}) query.compact! metrics = query[:metric] query.delete(:metric) - out = query.map { |k, v| "#{k}=#{v}" } - out += metrics.map { |m| "metric=#{m}" } unless metrics.nil? + out = query.map { |k, v| "#{k}=#{URI.encode_www_form_component(v.to_s)}" } + out += metrics.map { |m| "metric=#{URI.encode_www_form_component(m)}" } unless metrics.nil? out.join('&') end # query_string_normalizer for metrics. instead of metric[]=foo:1 we want metric=foo:1 + # values are form-encoded so a message with &, # or + reaches the API unchanged ) if response.code != 200 diff --git a/spec/cronitor_spec.rb b/spec/cronitor_spec.rb index 8e2c81b..27e20a8 100644 --- a/spec/cronitor_spec.rb +++ b/spec/cronitor_spec.rb @@ -196,6 +196,33 @@ series: 'world', } } + context 'with special characters in the message' do + before do + Cronitor.api_key = FAKE_API_KEY + end + + after do + Cronitor.api_key = nil + end + + it 'encodes every query value so the message is sent unchanged' do + message = "undefined method 'x' for nil & co #42 a+b=c" + query_string = nil + expect(HTTParty).to receive(:get) do |_url, opts| + query_string = opts[:query_string_normalizer].call(opts[:query]) + instance_double(HTTParty::Response, code: 200) + end + + monitor.ping(state: 'fail', message: message, host: 'web 1', metrics: { duration: 1.5 }) + + params = URI.decode_www_form(query_string) + expect(params.assoc('message')).to eq(['message', message]) + expect(params.assoc('host')).to eq(['host', 'web 1']) + expect(params.assoc('metric')).to eq(['metric', 'duration:1.5']) + expect(params.map(&:first)).not_to include(' co #42') + end + end + ['run', 'complete', 'fail', 'ok'].each do |state| context "Ping #{state}" do before(:all) do