Alerting: Make shareable alert rule link work if rule name contains forward slashes (#75362)

This commit is contained in:
Domas 2023-10-04 14:48:55 +03:00 committed by GitHub
parent d0592fce2f
commit aaaef71337
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 5 deletions

View File

@ -12,6 +12,7 @@ import { RuleViewerLayout } from './components/rule-viewer/RuleViewerLayout';
import { useCloudCombinedRulesMatching } from './hooks/useCombinedRule';
import { getRulesSourceByName } from './utils/datasource';
import { createViewLink } from './utils/misc';
import { unescapePathSeparators } from './utils/rule-id';
const pageTitle = 'Find rule';
const subUrl = config.appSubUrl;
@ -27,8 +28,7 @@ function useRuleFindParams() {
return useMemo(() => {
const segments = location.pathname?.replace(subUrl, '').split('/') ?? []; // ["", "alerting", "{sourceName}", "{name}]
const name = decodeURIComponent(segments[3]);
const name = unescapePathSeparators(decodeURIComponent(unescapePathSeparators(segments[3])));
const sourceName = decodeURIComponent(segments[2]);
const searchParams = new URLSearchParams(location.search);

View File

@ -4,6 +4,7 @@ import { UrlQueryMap, Labels, DataSourceInstanceSettings, DataSourceJsonData } f
import { GrafanaEdition } from '@grafana/data/src/types/config';
import { config } from '@grafana/runtime';
import { DataSourceRef } from '@grafana/schema';
import { escapePathSeparators } from 'app/features/alerting/unified/utils/rule-id';
import { alertInstanceKey } from 'app/features/alerting/unified/utils/rules';
import { SortOrder } from 'app/plugins/panel/alertlist/types';
import { Alert, CombinedRule, FilterState, RulesSource, SilenceFilterState } from 'app/types/unified-alerting';
@ -57,7 +58,9 @@ export function createMuteTimingLink(muteTimingName: string, alertManagerSourceN
export function createShareLink(ruleSource: RulesSource, rule: CombinedRule): string {
if (isCloudRulesSource(ruleSource)) {
return createAbsoluteUrl(`/alerting/${encodeURIComponent(ruleSource.name)}/${encodeURIComponent(rule.name)}/find`);
return createAbsoluteUrl(
`/alerting/${encodeURIComponent(ruleSource.name)}/${encodeURIComponent(escapePathSeparators(rule.name))}/find`
);
}
return window.location.href.split('?')[0];

View File

@ -106,11 +106,11 @@ function unescapeDollars(value: string): string {
* we'll use some non-printable characters from the ASCII table that will get encoded properly but very unlikely
* to ever be used in a rule name or namespace
*/
function escapePathSeparators(value: string): string {
export function escapePathSeparators(value: string): string {
return value.replace(/\//g, '\x1f').replace(/\\/g, '\x1e');
}
function unescapePathSeparators(value: string): string {
export function unescapePathSeparators(value: string): string {
return value.replace(/\x1f/g, '/').replace(/\x1e/g, '\\');
}