fix: truncate baggage/tag values to prevent metrics cardinality explosion#861
Open
SajanGhimire1 wants to merge 2 commits into
Open
fix: truncate baggage/tag values to prevent metrics cardinality explosion#861SajanGhimire1 wants to merge 2 commits into
SajanGhimire1 wants to merge 2 commits into
Conversation
…sion Baggage header values written to metrics dimensions without length validation could cause unbounded time series growth if values are externally controlled (e.g. user-supplied correlation IDs passed via W3C baggage header). Each unique value creates a permanent new time series in the metrics backend, causing: - Metrics cardinality explosion - Metrics backend memory exhaustion - Loss of observability (alerts stop firing) - Unexpected Azure Monitor cost spikes Fix: truncate baggage and tag object values to 256 characters before writing to metrics dimensions. Related: GHSA-rcjv-mgp8-qvmr (same class, OpenTelemetry-Go) CWE-400: Uncontrolled Resource Consumption
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds defensive truncation for Activity baggage/tag values before emitting metric dimensions to help reduce risk from unbounded external inputs.
Changes:
- Introduced
MaxMetricTagValueLengthconstant for tag value length limits - Truncated W3C baggage-derived metric tag values before adding to
TagList - Converted
Activitytag values to strings and truncated before adding toTagList
Comment on lines
+91
to
+96
| // Truncate string representation to prevent | ||
| // cardinality explosion via high-cardinality tag values | ||
| string tagValue = tagItem.ToString() ?? string.Empty; | ||
| tagList.Add(dimension, tagValue.Length > MaxMetricTagValueLength | ||
| ? tagValue[..MaxMetricTagValueLength] | ||
| : tagValue); |
Comment on lines
+80
to
+82
| tagList.Add(dimension, baggageItem.Length > MaxMetricTagValueLength | ||
| ? baggageItem[..MaxMetricTagValueLength] | ||
| : baggageItem); |
Comment on lines
+93
to
+96
| string tagValue = tagItem.ToString() ?? string.Empty; | ||
| tagList.Add(dimension, tagValue.Length > MaxMetricTagValueLength | ||
| ? tagValue[..MaxMetricTagValueLength] | ||
| : tagValue); |
Comment on lines
+27
to
+29
| /// Maximum length for metric tag values to prevent | ||
| /// cardinality explosion via externally controlled | ||
| /// baggage or tag values. |
- Preserve original type for non-string tag values - Use surrogate-pair-safe string truncation - Simplify constant comment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
SendActivityMetric()writes W3C baggage header valuesdirectly into metrics tag dimensions without any length
validation:
In distributed systems, baggage propagates across service
boundaries via HTTP headers. If baggage values are externally
controlled (e.g. user-supplied correlation IDs), each unique
value creates a new permanent time series in the metrics backend.
This can cause:
Same class of issue as GHSA-rcjv-mgp8-qvmr in
OpenTelemetry-Go, which was assigned a CVE.
Fix
Added
MaxMetricTagValueLength = 256constant and truncateboth baggage values and tag object values before writing
to metrics dimensions.
Impact of Fix
References