Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v10.1.x] Alerting: Make shareable alert rule link work if rule name contains forward slashes #75950

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion public/app/features/alerting/unified/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { sortBy } from 'lodash';

import { UrlQueryMap, Labels, DataSourceInstanceSettings, DataSourceJsonData } from '@grafana/data';
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';
Expand Down Expand Up @@ -55,7 +56,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];
Expand Down
4 changes: 2 additions & 2 deletions public/app/features/alerting/unified/utils/rule-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\');
}

Expand Down