Skip to content

Commit 2166431

Browse files
authored
no-append-html: Check appendTo/prependTo (#310)
One would usually pass an existing DOM node to these methods, but passing an HTML string is supported, so we should check for it.
1 parent 1ca9c26 commit 2166431

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

docs/rules/no-append-html.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# no-append-html
44

5-
Disallows using [`.append`](https://api.jquery.com/append/)/[`.prepend`](https://api.jquery.com/prepend/)/[`.before`](https://api.jquery.com/before/)/[`.after`](https://api.jquery.com/after/)/[`.replaceWith`](https://api.jquery.com/replaceWith/)/[`.add`](https://api.jquery.com/add/) to inject HTML, in order to prevent possible XSS bugs.
5+
Disallows using [`.append`](https://api.jquery.com/append/)/[`.prepend`](https://api.jquery.com/prepend/)/[`.before`](https://api.jquery.com/before/)/[`.after`](https://api.jquery.com/after/)/[`.replaceWith`](https://api.jquery.com/replaceWith/)/[`.add`](https://api.jquery.com/add/)/[`.appendTo`](https://api.jquery.com/appendTo/)/[`.prependTo`](https://api.jquery.com/prependTo/) to inject HTML, in order to prevent possible XSS bugs.
66

77
## Rule details
88

@@ -14,6 +14,8 @@ $div.before( '<xss>' );
1414
$div.after( '<xss>' );
1515
$div.replaceWith( '<xss>' );
1616
$els.add( '<xss>' );
17+
$els.appendTo( '<xss>' );
18+
$els.prependTo( '<xss>' );
1719
$div.append( code + '<xss>' );
1820
$div.append( test ? $el : '<xss>' );
1921
$div.append( $el, '<xss>' );
@@ -29,7 +31,9 @@ $div.prepend( $el );
2931
$div.before( $el );
3032
$div.after( $el );
3133
$div.replaceWith( $el );
32-
$els.add( $el );
34+
$div.add( $el );
35+
$div.appendTo( $el );
36+
$div.prependTo( $el );
3337
$div.append( this.$el );
3438
$div.append( this.foo.$el );
3539
$div.append( $el1, $el2 );

src/rules/no-append-html.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const utils = require( '../utils.js' );
4-
const methods = [ 'append', 'prepend', 'before', 'after', 'replaceWith', 'add' ];
4+
const methods = [ 'append', 'prepend', 'before', 'after', 'replaceWith', 'add', 'appendTo', 'prependTo' ];
55

66
function alljQueryOrEmpty( context, node ) {
77
if ( node.type === 'ConditionalExpression' ) {

tests/rules/no-append-html.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ ruleTester.run( 'no-append-html', rule, {
1313
'$div.before($el)',
1414
'$div.after($el)',
1515
'$div.replaceWith($el)',
16-
'$els.add($el)',
16+
'$div.add($el)',
17+
'$div.appendTo($el)',
18+
'$div.prependTo($el)',
1719
'$div.append(this.$el)',
1820
'$div.append(this.foo.$el)',
1921
'$div.append($el1, $el2)',
@@ -55,6 +57,14 @@ ruleTester.run( 'no-append-html', rule, {
5557
code: '$els.add("<xss>")',
5658
errors: [ error ]
5759
},
60+
{
61+
code: '$els.appendTo("<xss>")',
62+
errors: [ error ]
63+
},
64+
{
65+
code: '$els.prependTo("<xss>")',
66+
errors: [ error ]
67+
},
5868
{
5969
code: '$div.append(code + "<xss>")',
6070
errors: [ error ]

0 commit comments

Comments
 (0)