CloudWatch: Fix apostrophes in dimension values not being escaped (#87182)

This commit is contained in:
Kevin Yu 2024-05-02 06:15:11 -07:00 committed by GitHub
parent a979417700
commit 6851ad9f8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 4 deletions

View File

@ -99,7 +99,7 @@ func buildSearchExpression(query *models.CloudWatchQuery, stat string) string {
} }
sort.Strings(keys) sort.Strings(keys)
for _, key := range keys { for _, key := range keys {
values := escapeDoubleQuotes(knownDimensions[key]) values := escapeQuotes(knownDimensions[key])
valueExpression := join(values, " OR ", `"`, `"`) valueExpression := join(values, " OR ", `"`, `"`)
if len(knownDimensions[key]) > 1 { if len(knownDimensions[key]) > 1 {
valueExpression = fmt.Sprintf(`(%s)`, valueExpression) valueExpression = fmt.Sprintf(`(%s)`, valueExpression)
@ -150,10 +150,11 @@ func buildSearchExpressionLabel(query *models.CloudWatchQuery) string {
return label return label
} }
func escapeDoubleQuotes(arr []string) []string { func escapeQuotes(arr []string) []string {
result := []string{} result := []string{}
for _, value := range arr { for _, value := range arr {
value = strings.ReplaceAll(value, `"`, `\"`) value = strings.ReplaceAll(value, `"`, `\"`)
value = strings.ReplaceAll(value, `'`, `\'`)
result = append(result, value) result = append(result, value)
} }

View File

@ -472,7 +472,7 @@ func TestMetricDataQueryBuilder(t *testing.T) {
Namespace: "AWS/EC2", Namespace: "AWS/EC2",
MetricName: "CPUUtilization", MetricName: "CPUUtilization",
Dimensions: map[string][]string{ Dimensions: map[string][]string{
"lb4": {`lb4""`}, "lb4": {`lb4's""'`},
}, },
Period: 300, Period: 300,
Expression: "", Expression: "",
@ -480,7 +480,7 @@ func TestMetricDataQueryBuilder(t *testing.T) {
} }
res := buildSearchExpression(query, "Average") res := buildSearchExpression(query, "Average")
assert.Contains(t, res, `lb4\"\"`, "Expected escape double quotes") assert.Contains(t, res, `lb4\'s\"\"\'`, "Expected escaped quotes")
}) })
} }