Skip to content

Commit 05c906c

Browse files
authored
fix: sanitized query strings on the website (#240)
1 parent ac2237b commit 05c906c

File tree

7 files changed

+27
-12
lines changed

7 files changed

+27
-12
lines changed

docs/ui/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@
9999
"gh-pages": "^3.2.3",
100100
"highlight.js": "^11.2.0",
101101
"parcel": "^2.11.0",
102+
"path-browserify": "^1.0.0",
102103
"posthtml": "^0.16.5",
103104
"posthtml-doctype": "^1.1.1",
104105
"posthtml-include": "^1.7.2",
105106
"posthtml-modules": "^0.7.4",
106-
"path-browserify": "^1.0.0",
107107
"process": "^0.11.10",
108108
"sass": "^1.23.6",
109109
"vm-browserify": "^1.1.2"

docs/ui/src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ loadIcons('./spectrum-css-icons.svg');
7070
loadIcons('./spectrum-icons.svg');
7171

7272
// Import local Javascript functions
73-
import {throttle} from './js/utils';
73+
import {throttle, sanitizeQueryString} from './js/utils';
7474
import {openPanelTab, openTab, openAppTab, openSideNavItem} from './js/tabs';
7575
import toggleTooltip from './js/tooltip';
7676

@@ -87,7 +87,7 @@ posthtml()
8787

8888
// Redirect for URL parameters
8989
let url = new URL(window.location);
90-
let params = new URLSearchParams(url.search.slice(1));
90+
let params = new URLSearchParams(sanitizeQueryString(url.search.slice(1)));
9191

9292
if (params.has('colorKeys') || params.has('name')) {
9393
let newURL;

docs/ui/src/js/addFromURL.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ governing permissions and limitations under the License.
1010
*/
1111

1212
import {addColorScaleUpdate} from './colorScale';
13+
import {sanitizeQueryString} from './utils';
1314

1415
function addFromURLDialog() {
1516
let button = document.getElementById('addFromURLButton');
@@ -33,7 +34,7 @@ function addFromURL() {
3334
let value = input.value;
3435

3536
let url = new URL(value);
36-
let params = new URLSearchParams(url.search.slice(1));
37+
let params = new URLSearchParams(sanitizeQueryString(url.search.slice(1)));
3738
let pathName = url.pathname;
3839

3940
let crs, ratios, mode;

docs/ui/src/js/params.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import {addColorScale} from './colorScale';
1414
import {addRatioInputs, sortRatios} from './ratios';
1515
import {getRandomColorName, getClosestColorName} from './predefinedColorNames';
1616
import {baseScaleOptions} from './createBaseScaleOptions';
17-
import {round} from './utils';
17+
import {round, sanitizeQueryString} from './utils';
1818
import {_theme, tempGray} from './initialTheme';
1919

2020
function paramSetup() {
2121
let setFirstColorSmoothing = false;
2222
let url = new URL(window.location);
23-
let params = new URLSearchParams(url.search.slice(1));
23+
let params = new URLSearchParams(sanitizeQueryString(url.search.slice(1)));
2424
let themeBase = document.getElementById('themeBase');
2525
let RATIOS;
2626
let RATIOCOLORS;

docs/ui/src/js/utils.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,19 @@ function colorPickerInput(e) {
778778
}
779779
}
780780

781+
function sanitizeQueryString(string) {
782+
const map = {
783+
'&': '&',
784+
'<': '&lt;',
785+
'>': '&gt;',
786+
'"': '&quot;',
787+
"'": '&#x27;',
788+
'/': '&#x2F;'
789+
};
790+
const reg = /[&<>"'/]/gi;
791+
return string.replace(reg, (match) => map[match]);
792+
}
793+
781794
module.exports = {
782795
randomId,
783796
throttle,
@@ -808,5 +821,6 @@ module.exports = {
808821
getChannelsAndFunction,
809822
orderColorsByLuminosity,
810823
shuffleArray,
811-
colorPickerInput
824+
colorPickerInput,
825+
sanitizeQueryString
812826
};

docs/ui/src/theme.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import ClipboardJS from 'clipboard';
7575
// Import local Javascript functions
7676
import {_theme} from './js/initialTheme';
7777
import {paramSetup} from './js/params';
78-
import {throttle} from './js/utils';
78+
import {throttle, sanitizeQueryString} from './js/utils';
7979
import {getThemeContrastRatios, getContrastRatioInputs} from './js/getThemeData';
8080
import {addColorScale, addColorScaleUpdate} from './js/colorScale';
8181
import {addColorsFromImage} from './js/addColorsFromImage';
@@ -132,7 +132,7 @@ function updateParams() {
132132
formula: _theme.formula
133133
};
134134
let url = new URL(window.location);
135-
let params = new URLSearchParams(url.search.slice(1));
135+
let params = new URLSearchParams(sanitizeQueryString(url.search.slice(1)));
136136

137137
params.set('name', name); // Theme name
138138
params.set('config', JSON.stringify(theme)); // Configurations

pnpm-lock.yaml

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)