Skip to content

Commit 32a0afe

Browse files
author
Chad Norvell
committed
rustdoc: Prevent JS injection from localStorage
1 parent 021861a commit 32a0afe

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/librustdoc/html/static/js/storage.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ const getVar = (function getVar(name) {
101101
});
102102

103103
function switchTheme(newThemeName, saveTheme) {
104+
const themeNames = getVar("themes").split(",").filter(t => t);
105+
themeNames.push(...builtinThemes);
106+
107+
// Ensure that the new theme name is among the defined themes
108+
if (themeNames.indexOf(newThemeName) === -1) {
109+
return;
110+
}
111+
104112
// If this new value comes from a system setting or from the previously
105113
// saved theme, no need to save it.
106114
if (saveTheme) {
@@ -115,7 +123,7 @@ function switchTheme(newThemeName, saveTheme) {
115123
window.currentTheme = null;
116124
}
117125
} else {
118-
const newHref = getVar("root-path") + newThemeName +
126+
const newHref = getVar("root-path") + encodeURIComponent(newThemeName) +
119127
getVar("resource-suffix") + ".css";
120128
if (!window.currentTheme) {
121129
// If we're in the middle of loading, document.write blocks

0 commit comments

Comments
 (0)