Skip to content

Fix escaping for non-string label values - #792

Open
Hashim1999164 wants to merge 1 commit into
prometheus:mainfrom
Hashim1999164:fix/escape-non-string-label-values
Open

Fix escaping for non-string label values#792
Hashim1999164 wants to merge 1 commit into
prometheus:mainfrom
Hashim1999164:fix/escape-non-string-label-values

Conversation

@Hashim1999164

@Hashim1999164 Hashim1999164 commented Jul 31, 2026

Copy link
Copy Markdown

Summary

  • Non-string label values (for example arrays) skipped escapeLabelValue and were stringified only during template interpolation, so quotes and newlines could break Prometheus text format.
  • Coerce with `${str}` before escapeString / quote escaping, matching how string labels are already handled.
  • Adds a regression test for quote and newline cases from Non-string label values bypass escaping and can produce malformed exposition #791.

Fixes #791

Test plan

  • npx jest test/registerTest.js -t "should escape"
  • Confirm exposition with array label values passes promtool check metrics

Comment thread lib/registry.js Outdated
return str;
}
return escapeString(str).replace(/"/g, '\\"');
return escapeString(`${str}`).replace(/"/g, '\\"');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will still throw for Symbol values. I'm not sure if it's worth trying to handle those or not.

@Hashim1999164

Copy link
Copy Markdown
Author

Updated escapeLabelValue to use String(value) so Symbol values no longer throw, and added a regression test. Also rewrote the commit with a DCO sign-off.

@Hashim1999164
Hashim1999164 force-pushed the fix/escape-non-string-label-values branch from ab68655 to dcd68f6 Compare July 31, 2026 19:11
Coerce label values with String() before escape so quotes and newlines
in array (and other non-string) values cannot break Prometheus text
format, and Symbol values do not throw.

Signed-off-by: Hashim Khan <sardarhashim30@gmail.com>
@Hashim1999164
Hashim1999164 force-pushed the fix/escape-non-string-label-values branch from dcd68f6 to e9a077c Compare July 31, 2026 19:11
@Hashim1999164

Copy link
Copy Markdown
Author

Updated to use String(value) so Symbol label values no longer throw, and added a regression test for that case. DCO sign-off is on the rewritten commit as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-string label values bypass escaping and can produce malformed exposition

2 participants