Skip to content

Commit 7bd5ce1

Browse files
josh-vinMygod
andauthored
fix: escape gpx xml (#1186)
* Escapes XML in GPX route data Ensures route names and descriptions are properly escaped when generating GPX files, preventing potential XML parsing issues. Sanitizes the filename to be downloaded. * Prettier * fix: filename sanitizer to also strip backslashes for Windows safety --------- Co-authored-by: Mygod <[email protected]>
1 parent 5c907c5 commit 7bd5ce1

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/features/route/RoutePopup.jsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,24 @@ export function RoutePopup({ end, inline = false, ...props }) {
388388
}
389389

390390
function DownloadRouteGPX({ route }) {
391+
const escapeXml = (value = '') =>
392+
String(value).replace(
393+
/[<>&'"]/g,
394+
(c) =>
395+
({
396+
'<': '&lt;',
397+
'>': '&gt;',
398+
'&': '&amp;',
399+
"'": '&apos;',
400+
'"': '&quot;',
401+
})[c],
402+
)
403+
404+
const sanitizeFilename = (name = '') =>
405+
String(name)
406+
.replace(/[\\/:*?"<>|]/g, '')
407+
.slice(0, 200) || 'route'
408+
391409
const GPXContent = React.useMemo(() => {
392410
if (!route.waypoints.length) {
393411
return null
@@ -396,8 +414,8 @@ function DownloadRouteGPX({ route }) {
396414
return `<?xml version="1.0" encoding="UTF-8"?>
397415
<gpx version="1.1" creator="ReactMap" xmlns="http://www.topografix.com/GPX/1/1">
398416
<rte>
399-
<name>${route.name}</name>
400-
<desc>${route.description}</desc>
417+
<name>${escapeXml(route.name)}</name>
418+
<desc>${escapeXml(route.description)}</desc>
401419
${route.waypoints
402420
.map(
403421
(waypoint) =>
@@ -417,7 +435,7 @@ function DownloadRouteGPX({ route }) {
417435
href={`data:application/gpx;charset=utf-8,${encodeURIComponent(
418436
GPXContent,
419437
)}`}
420-
download={`${route.name}.gpx`}
438+
download={`${sanitizeFilename(route.name)}.gpx`}
421439
size="small"
422440
style={{ color: 'inherit' }}
423441
>

0 commit comments

Comments
 (0)