Skip to content

Commit 3959cd2

Browse files
Put unescaped regexes in a raw string literal (#10331) (#10385)
Cherry-picks 12221f4 onto `release-2.9.x` --- [This PR](#10277) fixed an issue where regexes were being escaped multiple times and causing otherwise valid queries to fail our length check. It put the resulting regexes in double quotes which meant that any regexes that _needed_ escaping were now invalid. This PR make is so the unescaped regex is put in a raw string literal so any extra escaping in unnecessary. **What this PR does / why we need it**: **Which issue(s) this PR fixes**: Fixes #<issue number> **Special notes for your reviewer**: **Checklist** - [ ] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [ ] Documentation added - [ ] Tests updated - [ ] `CHANGELOG.md` updated - [ ] If the change is worth mentioning in the release notes, add `add-to-release-notes` label - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/setup/upgrade/_index.md` - [ ] For Helm chart changes bump the Helm chart version in `production/helm/loki/Chart.yaml` and update `production/helm/loki/CHANGELOG.md` and `production/helm/loki/README.md`. [Example PR](d10549e) Co-authored-by: Travis Patterson <[email protected]>
1 parent 622626c commit 3959cd2

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

pkg/logql/log/label_filter.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,16 @@ type lineFilterLabelFilter struct {
368368
filter Filterer
369369
}
370370

371+
// overrides the matcher.String() function in case there is a regexpFilter
372+
func (s *lineFilterLabelFilter) String() string {
373+
if unwrappedFilter, ok := s.filter.(regexpFilter); ok {
374+
rStr := unwrappedFilter.String()
375+
str := fmt.Sprintf("%s%s`%s`", s.Matcher.Name, s.Matcher.Type, rStr)
376+
return str
377+
}
378+
return s.Matcher.String()
379+
}
380+
371381
func (s *lineFilterLabelFilter) Process(_ int64, line []byte, lbs *LabelsBuilder) ([]byte, bool) {
372382
v := labelValue(s.Name, lbs)
373383
return line, s.filter.Filter(unsafeGetBytes(v))

pkg/logql/syntax/parser_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3465,3 +3465,22 @@ func TestNoOpLabelToString(t *testing.T) {
34653465
require.NoError(t, err)
34663466
require.Len(t, stages, 0)
34673467
}
3468+
3469+
func TestParseSampleExpr_String(t *testing.T) {
3470+
t.Run("it doesn't add escape characters when after getting parsed", func(t *testing.T) {
3471+
query := `sum(rate({cluster="beep", namespace="boop"} | msg=~` + "`" + `.*?(GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS) /loki/api/(?i)(\d+[a-z]|[a-z]+\d)\w*/query_range` + "`" + `[1d]))`
3472+
expr, err := ParseSampleExpr(query)
3473+
require.NoError(t, err)
3474+
3475+
require.Equal(t, query, expr.String())
3476+
})
3477+
3478+
t.Run("it removes escape characters after being parsed", func(t *testing.T) {
3479+
query := `{cluster="beep", namespace="boop"} | msg=~"\\w.*"`
3480+
expr, err := ParseExpr(query)
3481+
require.NoError(t, err)
3482+
3483+
// escaping is hard: the result is {cluster="beep", namespace="boop"} | msg=~`\w.*` which is equivalent to the original
3484+
require.Equal(t, "{cluster=\"beep\", namespace=\"boop\"} | msg=~`\\w.*`", expr.String())
3485+
})
3486+
}

0 commit comments

Comments
 (0)