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

chore(SEO): Sanitze zone names in URL #7407

Merged
merged 1 commit into from
Nov 13, 2024
Merged
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
26 changes: 14 additions & 12 deletions web/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,31 @@ export function ValidZoneIdGuardWrapper({ children }: { children: JSX.Element })
return <Navigate to={`/map/24h?${searchParameters}`} replace />;
}

const upperCaseZoneId = zoneId.toUpperCase();
if (zoneId !== upperCaseZoneId) {
return (
<Navigate
to={`/zone/${upperCaseZoneId}/${urlTimeAverage}?${searchParameters}`}
replace
/>
);
// Sanitize the zone ID by removing any special characters except for hyphens and making it uppercase
let sanitizedZoneId = zoneId.replaceAll(/[^\dA-Za-z-]/g, '').toUpperCase();

// Remove trailing hyphens
if (sanitizedZoneId.endsWith('-')) {
sanitizedZoneId = sanitizedZoneId.slice(0, -1);
}

// Handle legacy Australian zone IDs
if (sanitizedZoneId.startsWith('AUS')) {
sanitizedZoneId = sanitizedZoneId.replace('AUS', 'AU');
}

// Handle legacy Australia zone names
if (upperCaseZoneId.startsWith('AUS')) {
if (zoneId !== sanitizedZoneId) {
return (
<Navigate
to={`/zone/${zoneId.replace('AUS', 'AU')}/${urlTimeAverage}?${searchParameters}`}
to={`/zone/${sanitizedZoneId}/${urlTimeAverage}?${searchParameters}`}
replace
/>
);
}

// Only allow valid zone ids
// TODO: This should redirect to a 404 page specifically for zones
if (!zoneExists(upperCaseZoneId)) {
if (!zoneExists(sanitizedZoneId)) {
return <Navigate to={`/map/24h?${searchParameters}`} replace />;
}

Expand Down
Loading