From afec2786fce8bfecd1e7e1c4af8efda362d7a8c6 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 2 Jun 2022 14:20:29 -0400 Subject: [PATCH] Loki: Fix uncaught errors if `labelKey` contains special characters (#49887) (#50067) * added regex check of labelKeys - labelKeys should not contain any special characters - added encoding of labelKeys in the URL - don't offer autocomplete if label with special characters is detected * removed additional regex check for labels (cherry picked from commit d7139e75fb303e4753338cba2b2fb8bc9619bd48) Co-authored-by: svennergr --- .../plugins/datasource/loki/language_provider.test.ts | 9 +++++++++ public/app/plugins/datasource/loki/language_provider.ts | 3 ++- public/app/plugins/datasource/loki/mocks.ts | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/loki/language_provider.test.ts b/public/app/plugins/datasource/loki/language_provider.test.ts index de2bcdd94ae..441341dfdd1 100644 --- a/public/app/plugins/datasource/loki/language_provider.test.ts +++ b/public/app/plugins/datasource/loki/language_provider.test.ts @@ -248,6 +248,15 @@ describe('Language completion provider', () => { expect(requestSpy).toHaveBeenCalledTimes(1); expect(nextLabelValues).toEqual(['label1_val1', 'label1_val2']); }); + + it('should encode special characters', async () => { + const datasource = makeMockLokiDatasource({ '`\\"testkey': ['label1_val1', 'label1_val2'], label2: [] }); + const provider = await getLanguageProvider(datasource); + const requestSpy = jest.spyOn(provider, 'request'); + await provider.fetchLabelValues('`\\"testkey'); + + expect(requestSpy).toHaveBeenCalledWith('label/%60%5C%22testkey/values', expect.any(Object)); + }); }); }); diff --git a/public/app/plugins/datasource/loki/language_provider.ts b/public/app/plugins/datasource/loki/language_provider.ts index 0d7cb538ade..ca50bfcd770 100644 --- a/public/app/plugins/datasource/loki/language_provider.ts +++ b/public/app/plugins/datasource/loki/language_provider.ts @@ -439,7 +439,8 @@ export default class LokiLanguageProvider extends LanguageProvider { } async fetchLabelValues(key: string): Promise { - const interpolatedKey = this.datasource.interpolateString(key); + const interpolatedKey = encodeURIComponent(this.datasource.interpolateString(key)); + const url = `label/${interpolatedKey}/values`; const rangeParams = this.datasource.getTimeRangeParams(); const { start, end } = rangeParams; diff --git a/public/app/plugins/datasource/loki/mocks.ts b/public/app/plugins/datasource/loki/mocks.ts index 47fce43c4dd..bab7b758a9b 100644 --- a/public/app/plugins/datasource/loki/mocks.ts +++ b/public/app/plugins/datasource/loki/mocks.ts @@ -18,7 +18,8 @@ interface SeriesForSelector { } export function makeMockLokiDatasource(labelsAndValues: Labels, series?: SeriesForSelector): LokiDatasource { - const lokiLabelsAndValuesEndpointRegex = /^label\/(\w*)\/values/; + // added % to allow urlencoded labelKeys. Note, that this is not confirm with Loki, as loki does not allow specialcharacters in labelKeys, but needed for tests. + const lokiLabelsAndValuesEndpointRegex = /^label\/([%\w]*)\/values/; const lokiSeriesEndpointRegex = /^series/; const lokiLabelsEndpoint = 'labels';