Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
fix(export-map): escape inline data (#3845)
Igor Dykhta5760dda21ce3
diff --git a/src/utils/src/export-map-html.ts b/src/utils/src/export-map-html.tsindex 2e8d24818..cd0d05f6c 100644--- a/src/utils/src/export-map-html.ts+++ b/src/utils/src/export-map-html.ts@@ -24,16 +24,27 @@ const STYLED_COMPONENTS_VERSION = '6.1.19';// document (native ES modules/import maps are blocked under the file:// origin).const ES_MODULE_SHIMS_VERSION = '2.8.1';+/** Escape serialized JSON for interpolation into an inline `<script>`. */+function escapeScriptJSON(serialized: string): string {+ return serialized+ .replace(/</g, '\\u003c')+ .replace(/\u2028/g, '\\u2028')+ .replace(/\u2029/g, '\\u2029');+}+/*** Serialize a value for interpolation into an inline `<script>`. A literal `</script>`* anywhere in the data would close the script element early, and U+2028/U+2029 are* line terminators in JavaScript but legal inside a JSON string.*/function toScriptJSON(value: unknown): string {- return JSON.stringify(value ?? null)- .replace(/</g, '\\u003c')- .replace(/\u2028/g, '\\u2028')- .replace(/\u2029/g, '\\u2029');+ return escapeScriptJSON(JSON.stringify(value ?? null));+}++/** Preserve the legacy `undefined` runtime value for optional public arguments. */+function toScriptJSONOrUndefined(value: unknown): string {+ const serialized = JSON.stringify(value);+ return serialized === undefined ? 'undefined' : escapeScriptJSON(serialized);}/** Escape text interpolated directly into HTML markup. */@@ -88,6 +99,9 @@ export const exportMapToHTML = (options, version = KEPLER_GL_VERSION) => {typeof options.appName === 'string' && options.appName ? options.appName : KEPLER_GL_NAME;const appWebsite = normalizeAppWebsite(options.appWebsite);const branding = {appName, appWebsite};+ const mapboxApiAccessToken = options.mapboxApiAccessToken || 'PROVIDE_MAPBOX_TOKEN';+ const keplerStyleUrl = `https://unpkg.com/kepler.gl@${version}/umd/keplergl.min.css`;+ const keplerScriptUrl = `https://unpkg.com/kepler.gl@${version}/umd/keplergl.min.js`;return `<!DOCTYPE html>@@ -100,7 +114,7 @@ export const exportMapToHTML = (options, version = KEPLER_GL_VERSION) => {<link rel="stylesheet" href="https://d1a3f4spazzrp4.cloudfront.net/kepler.gl/uber-fonts/4.0.0/superfine.css"><!--Kepler css-->- <link href="https://unpkg.com/kepler.gl@${version}/umd/keplergl.min.css" rel="stylesheet">+ <link href="${escapeHTML(keplerStyleUrl)}" rel="stylesheet"><!--MapBox css--><link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.1/mapbox-gl.css" rel="stylesheet">@@ -229,7 +243,7 @@ export const exportMapToHTML = (options, version = KEPLER_GL_VERSION) => {// Load the kepler.gl UMD bundle after the globals are in place, then run// the app bootstrap scripts (marked as type="text/kepler-bootstrap").- loadScript('https://unpkg.com/kepler.gl@${version}/umd/keplergl.min.js')+ loadScript(${toScriptJSON(keplerScriptUrl)}).then(function() {var bootstraps = document.querySelectorAll('script[type="text/kepler-bootstrap"]');bootstraps.forEach(function(node) {@@ -291,7 +305,7 @@ export const exportMapToHTML = (options, version = KEPLER_GL_VERSION) => {/*** Provide your MapBox Token**/- window.MAPBOX_TOKEN = '${options.mapboxApiAccessToken || 'PROVIDE_MAPBOX_TOKEN'}';+ window.MAPBOX_TOKEN = ${toScriptJSON(mapboxApiAccessToken)};window.WARNING_MESSAGE = 'No Mapbox token provided. Mapbox basemaps will not be available. To enable them, edit this file and set MAPBOX_TOKEN to your Mapbox access key.';</script></head>@@ -321,7 +335,7 @@ export const exportMapToHTML = (options, version = KEPLER_GL_VERSION) => {// Pre-seed styleType so the correct basemap fetch starts on the very// first render, avoiding a flash of the default dark basemap.// options.config has the versioned envelope shape: { version, config: { mapStyle, ... } }- const savedStyleType = ${JSON.stringify(+ const savedStyleType = ${toScriptJSON(options.config?.config?.mapStyle?.styleType ?? null)};@@ -517,8 +531,8 @@ export const exportMapToHTML = (options, version = KEPLER_GL_VERSION) => {* to add new data and customize behavior*/(function customize(keplerGl, store) {- const datasets = ${JSON.stringify(options.datasets)};- const config = ${JSON.stringify(options.config)};+ const datasets = ${toScriptJSONOrUndefined(options.datasets)};+ const config = ${toScriptJSONOrUndefined(options.config)};const info = ${toScriptJSON(optionalMapInfo(options.info) ?? null)};const loadedData = keplerGl.KeplerGlSchema.load(diff --git a/test/node/utils/export-utils-test.js b/test/node/utils/export-utils-test.jsindex 0b2f4d46d..45071b093 100644--- a/test/node/utils/export-utils-test.js+++ b/test/node/utils/export-utils-test.js@@ -85,6 +85,93 @@ test('exportUtils -> ExportHtml escapes script terminators in map info', t => {t.end();});+test('exportUtils -> ExportHtml safely serializes inline data', t => {+ const datasetValue = '</script><div data-kepler-export-test="dataset"></div>\u2028\u2029';+ const configValue = '</script><div data-kepler-export-test="config"></div>\u2028\u2029';+ const savedStyleType =+ '</script><div data-kepler-export-test="style-type"></div>\u2028\u2029';+ const mapboxApiAccessToken =+ '\'</script><div data-kepler-export-test="mapbox-token"></div>\u2028\u2029';+ const version = 'test"></script><div data-kepler-export-test="version"></div>';+ const datasets = [{data: {label: datasetValue}}];+ const config = {+ version: 'v1',+ config: {+ mapStyle: {styleType: savedStyleType},+ metadata: configValue+ }+ };+ const html = exportMapToHTML(+ {+ datasets,+ config,+ mapboxApiAccessToken,+ mode: 'READ'+ },+ version+ );++ const parseAssignment = pattern => {+ const match = html.match(pattern);+ t.ok(match, `exported html should include ${pattern}`);+ if (!match) {+ return undefined;+ }+ try {+ return JSON.parse(match[1]);+ } catch {+ return undefined;+ }+ };++ t.notOk(+ html.includes('</script><div data-kepler-export-test='),+ 'inline values should not be able to close a script element or inject markup'+ );+ t.deepEqual(parseAssignment(/const datasets = (.+);/), datasets, 'datasets should round-trip');+ t.deepEqual(parseAssignment(/const config = (.+);/), config, 'config should round-trip');+ t.equal(+ parseAssignment(/const savedStyleType = (.+);/),+ savedStyleType,+ 'saved style type should round-trip'+ );+ t.equal(+ parseAssignment(/window\.MAPBOX_TOKEN = (.+);/),+ mapboxApiAccessToken,+ 'Mapbox token should round-trip'+ );+ t.ok(html.includes('\\u003c/script>'), 'inline JSON should escape opening angle brackets');+ t.ok(html.includes('\\u2028\\u2029'), 'inline JSON should escape JavaScript line terminators');++ t.end();+});++test('exportUtils -> ExportHtml preserves nullish dataset and config values', t => {+ const undefinedHtml = exportMapToHTML({+ datasets: undefined,+ config: undefined,+ mode: 'READ'+ });+ const nullHtml = exportMapToHTML({+ datasets: null,+ config: null,+ mode: 'READ'+ });++ t.ok(+ undefinedHtml.includes('const datasets = undefined;'),+ 'missing datasets should remain undefined at runtime'+ );+ t.ok(+ undefinedHtml.includes('const config = undefined;'),+ 'missing config should remain undefined at runtime'+ );+ t.ok(nullHtml.includes('const datasets = null;'), 'null datasets should remain null at runtime');+ t.ok(nullHtml.includes('const config = null;'), 'null config should remain null at runtime');++ t.end();+});+test('exportUtils -> ExportHtml preserves custom branding', t => {const branding = {appName: 'Atlas Lab',