Skip to content

Commit 17e33bc

Browse files
Rob--WGolmote
authored andcommitted
Reduce risk of XSS (#1051)
* Skip non-own properties of env.attributes Use `Object.keys` instead of a for-in loop to find optional attributes. The former only grabs keys that are own properties, the latter also includes inherit properties from `Object.prototype`. This reduces the risk of XSS if an attacker somehow manages to manipulate the prototype chain of the Object prototype. * Fix root cause of XSS in autolinker plugin #1054 * command-line plugin: Safely encode attributes If an attacker has control over the values of the attributes "data-prompt", "data-user", or "data-host", then XSS was possible. This fixes the issue, by encoding quotes as the `"` entity. * show-language plugin: innerHTML -> textContent There is no need for `innerHTML` here. At best nothing happens, at worst XSS is possible (though the odds are negligible since the attacker would have to control the detected language). * toolbar plugin: innerHTML -> textContent
1 parent 0251471 commit 17e33bc

9 files changed

+19
-19
lines changed

components/prism-core.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,9 @@ Token.stringify = function(o, language, parent) {
445445

446446
_.hooks.run('wrap', env);
447447

448-
var attributes = '';
449-
450-
for (var name in env.attributes) {
451-
attributes += (attributes ? ' ' : '') + name + '="' + (env.attributes[name] || '') + '"';
452-
}
448+
var attributes = Object.keys(env.attributes).map(function(name) {
449+
return name + '="' + (env.attributes[name] || '').replace(/"/g, '"') + '"';
450+
}).join(' ');
453451

454452
return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + (attributes ? ' ' + attributes : '') + '>' + env.content + '</' + env.tag + '>';
455453

components/prism-core.min.js

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

plugins/command-line/prism-command-line.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@ Prism.hooks.add('complete', function (env) {
3434
pre.className += ' command-line';
3535
}
3636

37+
var getAttribute = function(key, defaultValue) {
38+
return (pre.getAttribute(key) || defaultValue).replace(/"/g, '&quot');
39+
};
40+
3741
// Create the "rows" that will become the command-line prompts. -- cwells
3842
var lines = new Array(1 + env.code.split('\n').length);
39-
var promptText = pre.getAttribute('data-prompt') || '';
43+
var promptText = getAttribute('data-prompt', '');
4044
if (promptText !== '') {
4145
lines = lines.join('<span data-prompt="' + promptText + '"></span>');
4246
} else {
43-
var user = pre.getAttribute('data-user') || 'user';
44-
var host = pre.getAttribute('data-host') || 'localhost';
47+
var user = getAttribute('data-user', 'user');
48+
var host = getAttribute('data-host', 'localhost');
4549
lines = lines.join('<span data-user="' + user + '" data-host="' + host + '"></span>');
4650
}
4751

plugins/command-line/prism-command-line.min.js

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

plugins/show-language/prism-show-language.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Prism.plugins.toolbar.registerButton('show-language', function(env) {
2020
var language = pre.getAttribute('data-language') || Languages[env.language] || (env.language.substring(0, 1).toUpperCase() + env.language.substring(1));
2121

2222
var element = document.createElement('span');
23-
element.innerHTML = language;
23+
element.textContent = language;
2424

2525
return element;
2626
});

plugins/show-language/prism-show-language.min.js

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

plugins/toolbar/prism-toolbar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
element = document.createElement('span');
121121
}
122122

123-
element.innerHTML = text;
123+
element.textContent = text;
124124
}
125125

126126
return element;

0 commit comments

Comments
 (0)