Skip to content

Commit 8cc5bae

Browse files
authoredJul 14, 2022
Checkboxradio: Don't re-evaluate text labels as HTML
If you generate a Checkboxradio from a checkbox/radio with a label that contains encoded HTML, e.g. `<em>test</em>` this will work fine at first. If, however a refresh is triggered on that instance (explicitly or e.g. by turning it into a `Controlgroup`), the previously escaped HTML will now be evaluated. If the label was created based on some user input, this could lead to unexpected code execution even though the initial output was escaped. Fixes gh-2101 Closes gh-2102
1 parent b53e7be commit 8cc5bae

File tree

4 files changed

+100
-8
lines changed

4 files changed

+100
-8
lines changed
 

‎tests/unit/checkboxradio/checkboxradio.html

+12
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@
6464
<label>
6565
<input type="checkbox" id="label-with-no-for"/>
6666
</label>
67+
<label>
68+
<input type="checkbox" id="label-with-no-for-with-html"/>
69+
<strong>Hi</strong>, <em>I'm a label</em>
70+
</label>
71+
<label>
72+
<input type="checkbox" id="label-with-no-for-with-text"/>
73+
Hi, I'm a label
74+
</label>
75+
<label>
76+
<input type="checkbox" id="label-with-no-for-with-html-like-text"/>
77+
&lt;em&gt;Hi, I'm a label&lt;/em&gt;
78+
</label>
6779

6880
<form id="form3"></form>
6981
<input type="radio" name="crazy-form" id="crazy-form-1" form="form3" checked="checked">

‎tests/unit/checkboxradio/core.js

+37
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,41 @@ QUnit.test( "Calling checkboxradio on an input with no label throws an error", f
131131
);
132132
} );
133133

134+
QUnit.test( "Inheriting label from initial HTML", function( assert ) {
135+
var tests = [
136+
{
137+
id: "label-with-no-for-with-html",
138+
expectedLabel: "<strong>Hi</strong>, <em>I'm a label</em>"
139+
},
140+
{
141+
id: "label-with-no-for-with-text",
142+
expectedLabel: "Hi, I'm a label"
143+
},
144+
{
145+
id: "label-with-no-for-with-html-like-text",
146+
expectedLabel: "&lt;em&gt;Hi, I'm a label&lt;/em&gt;"
147+
}
148+
];
149+
150+
assert.expect( tests.length );
151+
152+
tests.forEach( function( testData ) {
153+
var id = testData.id;
154+
var expectedLabel = testData.expectedLabel;
155+
var inputElem = $( "#" + id );
156+
var labelElem = inputElem.parent();
157+
158+
inputElem.checkboxradio( { icon: false } );
159+
160+
var labelWithoutInput = labelElem.clone();
161+
labelWithoutInput.find( "input" ).remove();
162+
163+
assert.strictEqual(
164+
labelWithoutInput.html().trim(),
165+
expectedLabel.trim(),
166+
"Label correct [" + id + "]"
167+
);
168+
} );
169+
} );
170+
134171
} );

‎tests/unit/checkboxradio/methods.js

+38
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,42 @@ QUnit.test( "Input wrapped in a label preserved on refresh", function( assert )
9696
assert.strictEqual( input.parent()[ 0 ], element[ 0 ], "Input preserved" );
9797
} );
9898

99+
QUnit.test( "Initial text label not turned to HTML on refresh", function( assert ) {
100+
var tests = [
101+
{
102+
id: "label-with-no-for-with-html",
103+
expectedLabel: "<strong>Hi</strong>, <em>I'm a label</em>"
104+
},
105+
{
106+
id: "label-with-no-for-with-text",
107+
expectedLabel: "Hi, I'm a label"
108+
},
109+
{
110+
id: "label-with-no-for-with-html-like-text",
111+
expectedLabel: "&lt;em&gt;Hi, I'm a label&lt;/em&gt;"
112+
}
113+
];
114+
115+
assert.expect( tests.length );
116+
117+
tests.forEach( function( testData ) {
118+
var id = testData.id;
119+
var expectedLabel = testData.expectedLabel;
120+
var inputElem = $( "#" + id );
121+
var labelElem = inputElem.parent();
122+
123+
inputElem.checkboxradio( { icon: false } );
124+
inputElem.checkboxradio( "refresh" );
125+
126+
var labelWithoutInput = labelElem.clone();
127+
labelWithoutInput.find( "input" ).remove();
128+
129+
assert.strictEqual(
130+
labelWithoutInput.html().trim(),
131+
expectedLabel.trim(),
132+
"Label correct [" + id + "]"
133+
);
134+
} );
135+
} );
136+
99137
} );

‎ui/widgets/checkboxradio.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ $.widget( "ui.checkboxradio", [ $.ui.formResetMixin, {
5050
},
5151

5252
_getCreateOptions: function() {
53-
var disabled, labels;
54-
var that = this;
53+
var disabled, labels, labelContents;
5554
var options = this._super() || {};
5655

5756
// We read the type here, because it makes more sense to throw a element type error first,
@@ -71,12 +70,18 @@ $.widget( "ui.checkboxradio", [ $.ui.formResetMixin, {
7170

7271
// We need to get the label text but this may also need to make sure it does not contain the
7372
// input itself.
74-
this.label.contents().not( this.element[ 0 ] ).each( function() {
75-
76-
// The label contents could be text, html, or a mix. We concat each element to get a
77-
// string representation of the label, without the input as part of it.
78-
that.originalLabel += this.nodeType === 3 ? $( this ).text() : this.outerHTML;
79-
} );
73+
// The label contents could be text, html, or a mix. We wrap all elements
74+
// and read the wrapper's `innerHTML` to get a string representation of
75+
// the label, without the input as part of it.
76+
labelContents = this.label.contents().not( this.element[ 0 ] );
77+
78+
if ( labelContents.length ) {
79+
this.originalLabel += labelContents
80+
.clone()
81+
.wrapAll( "<div></div>" )
82+
.parent()
83+
.html();
84+
}
8085

8186
// Set the label option if we found label text
8287
if ( this.originalLabel ) {

0 commit comments

Comments
 (0)
Please sign in to comment.