Dashboards: Escape tags (#74437)

This commit is contained in:
Fabrizio 2023-09-09 14:11:36 +02:00 committed by GitHub
parent 85e629dc7d
commit 3bb4e24458
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -118,7 +118,8 @@ export default class TempoLanguageProvider extends LanguageProvider {
};
async getOptionsV1(tag: string): Promise<Array<SelectableValue<string>>> {
const response = await this.request(`/api/search/tag/${tag}/values`);
const encodedTag = this.encodeTag(tag);
const response = await this.request(`/api/search/tag/${encodedTag}/values`);
let options: Array<SelectableValue<string>> = [];
if (response && response.tagValues) {
options = response.tagValues.map((v: string) => ({
@ -130,7 +131,8 @@ export default class TempoLanguageProvider extends LanguageProvider {
}
async getOptionsV2(tag: string, query?: string): Promise<Array<SelectableValue<string>>> {
const response = await this.request(`/api/v2/search/tag/${tag}/values`, query ? { q: query } : {});
const encodedTag = this.encodeTag(tag);
const response = await this.request(`/api/v2/search/tag/${encodedTag}/values`, query ? { q: query } : {});
let options: Array<SelectableValue<string>> = [];
if (response && response.tagValues) {
response.tagValues.forEach((v: { type: string; value?: string }) => {
@ -145,4 +147,16 @@ export default class TempoLanguageProvider extends LanguageProvider {
}
return options;
}
/**
* Encode (serialize) a given tag for use in a URL.
*
* @param tag the tag to encode
* @returns the encoded tag
*/
private encodeTag = (tag: string): string => {
// If we call `encodeURIComponent` only once, we still get an error when issuing a request to the backend
// Reference: https://stackoverflow.com/a/37456192
return encodeURIComponent(encodeURIComponent(tag));
};
}