Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 8ba83c0

Browse files
author
Gabriel Schulhof
committed
Selectmenu: Do not double-entity-encode option text
(cherry picked from commit b0e1202) Closes gh-7544 Fixes gh-7543
1 parent a6aef45 commit 8ba83c0

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

js/widgets/forms/select.custom.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,15 @@ $.widget( "mobile.selectmenu", $.mobile.selectmenu, {
460460
}
461461

462462
parent = option.parentNode;
463-
text = $option.getEncodedText();
464-
anchor = document.createElement( "a" );
465463
classes = [];
466464

465+
// Although using .text() here raises the risk that, when we later paste this into the
466+
// list item we end up pasting possibly malicious things like <script> tags, that risk
467+
// only arises if we do something like $( "<li><a href='#'>" + text + "</a></li>" ). We
468+
// don't do that. We do document.createTextNode( text ) instead, which guarantees that
469+
// whatever we paste in will end up as text, with characters like <, > and & escaped.
470+
text = $option.text();
471+
anchor = document.createElement( "a" );
467472
anchor.setAttribute( "href", "#" );
468473
anchor.appendChild( document.createTextNode( text ) );
469474

tests/integration/select/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
<div id="qunit"></div>
3737

3838
<div id="default" data-nstest-role="page" data-nstest-theme="c">
39+
<select id="encoding-test" data-nstest-native-menu="false">
40+
<option value="1">&lt;script&gt;$( "*" ).css( "background-color", "red" );&lt;/script&gt;</option>
41+
<option value="2">Another option</option>
42+
</select>
3943
<select name="small-select-change-after-close" id="small-select-change-after-close" data-nstest-native-menu="false">
4044
<option value="1">One</option>
4145
<option value="2">Two</option>

tests/integration/select/select_core.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919

2020
var homeWithSearch = $.mobile.path.parseUrl(location.pathname).pathname + location.search;
2121

22+
test( "No tags are accidentally injected during list building", function() {
23+
deepEqual( $( "#encoding-test-menu > li:first-child > a > script" ).length, 0,
24+
"No script tag has ended up inside the anchor" );
25+
});
26+
2227
module(libName, {
2328
setup: function() {
2429
$.mobile.navigate.history.stack = [];

0 commit comments

Comments
 (0)