Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/platform-redirect/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {SmartLink} from 'sentry-docs/components/smartLink';
import {extractPlatforms, getDocsRootNode, nodeForPath} from 'sentry-docs/docTree';
import {setServerContext} from 'sentry-docs/serverContext';

import {sanitizeNext} from './utils';
import {sanitizeNext, sanitizeNextHash} from './utils';

export const metadata: Metadata = {
robots: 'noindex',
Expand All @@ -29,6 +29,7 @@ export default async function Page(props: {
}

const pathname = sanitizeNext(next);
const hash = sanitizeNextHash(next);
const isTracingAutomatedInstrumentation =
pathname.replace(/\/$/, '') === '/tracing/instrumentation/automatic-instrumentation';
const rootNode = await getDocsRootNode();
Expand Down Expand Up @@ -145,7 +146,7 @@ export default async function Page(props: {
p => p.key === requestedPlatform?.toLowerCase()
);
if (validPlatform) {
return redirect(`${validPlatform.url}${pathname}`);
return redirect(`${validPlatform.url}${pathname}${hash}`);
}
}

Expand All @@ -167,7 +168,7 @@ export default async function Page(props: {
<ul>
{platformOrGuideList.map(p => (
<li key={p.key} style={{marginLeft: p.shouldBeNested ? '20px' : '0'}}>
<SmartLink to={`${p.url}${pathname}`}>
<SmartLink to={`${p.url}${pathname}${hash}`}>
<PlatformIcon
size={16}
platform={p.icon}
Expand Down
34 changes: 33 additions & 1 deletion app/platform-redirect/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {describe, expect, it} from 'vitest';

import {sanitizeNext} from './utils';
import {sanitizeNext, sanitizeNextHash} from './utils';

describe('sanitizeNext', () => {
it('should return an empty string for external URLs', () => {
Expand Down Expand Up @@ -83,3 +83,35 @@ describe('sanitizeNext', () => {
expect(sanitizeNext(input)).toBe(expectedOutput);
});
});

describe('sanitizeNextHash', () => {
it('should return an empty string when there is no fragment', () => {
expect(sanitizeNextHash('/configuration/options/')).toBe('');
expect(sanitizeNextHash('/configuration/options/#')).toBe('');
});

it('should keep the fragment of an encoded next path', () => {
// `PlatformLink` encodes `next`, so the `#` arrives as `%23`
expect(sanitizeNextHash('%2Fconfiguration%2Foptions%2F%23ignoreErrors')).toBe(
'#ignoreErrors'
);
expect(sanitizeNextHash('/configuration/options/#tracing-options')).toBe(
'#tracing-options'
);
});

it('should keep dots and underscores used in option ids', () => {
expect(sanitizeNextHash('/configuration/options/#godot_logger.event_mask')).toBe(
'#godot_logger.event_mask'
);
});

it('should remove unsafe characters from the fragment', () => {
expect(sanitizeNextHash('/path/#a"><script>b')).toBe('#ascriptb');
expect(sanitizeNextHash('/path/#a/b?c=1')).toBe('#abc1');
});

it('should return an empty string for a malformed URI component', () => {
expect(sanitizeNextHash('/path/#%E0%A4%A')).toBe('');
});
});
22 changes: 22 additions & 0 deletions app/platform-redirect/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,25 @@ export const sanitizeNext = (next: string) => {

return pathname === '/' ? '' : pathname;
};

/**
* Returns the `#fragment` of `next` so the redirect can land on the same section,
* or an empty string if there is none. Only characters used in heading and option
* ids are kept.
*/
export const sanitizeNextHash = (next: string) => {
let decoded: string;
try {
decoded = decodeURIComponent(next);
} catch {
return '';
}

const hashIndex = decoded.indexOf('#');
if (hashIndex === -1) {
return '';
}

const fragment = decoded.slice(hashIndex + 1).replace(/[^\w\-.]/g, '');
return fragment ? `#${fragment}` : '';
};
Loading