Skip to content

Commit

Permalink
Core: Added escapeHtml option to avoid XSS attacks via showLabel meth…
Browse files Browse the repository at this point in the history
…ods (#2462)
  • Loading branch information
volkanceylan committed Feb 1, 2023
1 parent cfe74a1 commit 7a490d8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/core.js
Expand Up @@ -945,14 +945,23 @@ $.extend( $.validator, {
error.removeClass( this.settings.validClass ).addClass( this.settings.errorClass );

// Replace message on existing label
error.html( message );
if ( this.settings && this.settings.escapeHtml ) {
error.text( message || "" );
} else {
error.html( message || "" );
}
} else {

// Create error element
error = $( "<" + this.settings.errorElement + ">" )
.attr( "id", elementID + "-error" )
.addClass( this.settings.errorClass )
.html( message || "" );
.addClass( this.settings.errorClass );

if ( this.settings && this.settings.escapeHtml ) {
error.text( message || "" );
} else {
error.html( message || "" );
}

// Maintain reference to the element to be placed into the DOM
place = error;
Expand Down
57 changes: 57 additions & 0 deletions test/error-placement.js
Expand Up @@ -440,3 +440,60 @@ QUnit.test( "#1632: Error hidden, but input error class not removed", function(
assert.equal( v.numberOfInvalids(), 0, "There is no error" );
assert.equal( box2.hasClass( "error" ), false, "Box2 should not have an error class" );
} );

QUnit.test( "test settings.escapeHtml undefined", function( assert ) {
var form = $( "#escapeHtmlForm1" ),
field = $( "#escapeHtmlForm1text" );

form.validate( {
messages: {
escapeHtmlForm1text: {
required: "<script>console.log('!!!');</script>"
}
}
} );

assert.ok( !field.valid() );
assert.hasError( field, "required" );

var label = form.find( "label" );
assert.equal( label.length, 1 );
assert.equal( label.html(), "<script>console.log('!!!');</script>" );

label.html( "" );
assert.ok( !field.valid() );
assert.equal( label.html(), "<script>console.log('!!!');</script>" );

field.val( "foo" );
assert.ok( field.valid() );
assert.noErrorFor( field );
} );

QUnit.test( "test settings.escapeHtml true", function( assert ) {
var form = $( "#escapeHtmlForm2" ),
field = $( "#escapeHtmlForm2text" );

form.validate( {
escapeHtml: true,
messages: {
escapeHtmlForm2text: {
required: "<script>console.log('!!!');</script>"
}
}
} );

assert.ok( !field.valid() );
assert.hasError( field, "required" );

var label = form.find( "label" );
assert.equal( label.length, 1 );
assert.equal( label.html(), "&lt;script&gt;console.log('!!!');&lt;/script&gt;" );

label.html( "" );
assert.ok( !field.valid() );
assert.equal( label.html(), "&lt;script&gt;console.log('!!!');&lt;/script&gt;" );

field.val( "foo" );
assert.ok( field.valid() );
assert.noErrorFor( field );
} );
6 changes: 6 additions & 0 deletions test/index.html
Expand Up @@ -467,6 +467,12 @@ <h3></h3>
<form id="testForm28">
<input type="text" name="f28input" required>
</form>
<form id="escapeHtmlForm1">
<input name="escapeHtmlForm1text" id="escapeHtmlForm1text" data-rule-required="true" />
</form>
<form id="escapeHtmlForm2">
<input name="escapeHtmlForm2text" id="escapeHtmlForm2text" data-rule-required="true" />
</form>
</div>
</body>
</html>

2 comments on commit 7a490d8

@coskunaydinoglu
Copy link

@coskunaydinoglu coskunaydinoglu commented on 7a490d8 Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add on your tests? I could make XSS attack by giving and img tag with wrong src and fallback to onError event of img and execute the script

"<img src='bogus.url' onError='alert(\"you are hacked 2\")'>"

@volkanceylan
Copy link
Contributor Author

@volkanceylan volkanceylan commented on 7a490d8 Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has nothing to do with img source and general XSS attacks. This is only to html escape the jquery validate messages. And you must pass escapeHtml: true while creating the validator.

Please sign in to comment.