Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

jQuery.parseHTML: Disable inline event handlers when removing scripts#1508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
fhemberger wants to merge1 commit intojquery:masterfromfhemberger:parsehtml_eventhandler
Closed

jQuery.parseHTML: Disable inline event handlers when removing scripts#1508

fhemberger wants to merge1 commit intojquery:masterfromfhemberger:parsehtml_eventhandler

Conversation

fhemberger
Copy link
Contributor

When removing script tags from the parsed result, inline event handlers should
be removed as well. It would be very insecure to create a blacklist of
event handler attributes, in case of one being forgotten or new handlers
being added in the future.

Therefore, all attributes starting withon* should be replaced by a data
attribute of the same name. All code which might exist can still be accessed,
but is not executed after being injected into the DOM to avoid possible XSS
attacks.

See also PR#1505.

When removing script tags from the parsed result, inline event handlers shouldbe removed as well. It would be very insecure to create a blacklist ofevent handler attributes, in case of one being forgotten or new handlersbeing added in the future.Therefore, all attributes starting with `on*` should be replaced by a dataattribute of the same name. All code which might exist can still be accessed,but is not executed after being injected into the DOM to avoid possible XSSattacks.See also PR#1505.
@fhemberger
Copy link
ContributorAuthor

This PR should be applied tomaster and1.x-master, when accepted.

@@ -17,6 +17,11 @@ jQuery.parseHTML = function( data, context, keepScripts ) {
}
context = context || document;

// Disable event handlers like onload, onerror, etc. when scripts are removed
if ( !keepScripts ) {
data = data.replace( /\b(on[a-z]+)/gi, "data-$1" );
Copy link
Member

Choose a reason for hiding this comment

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

This will fail on input like"<img src='/path/to/src' onload='finished(this)' data-onload='true'/>" (attribute collision) or"Example vulnerability: <pre><img onerror='alert(\"Pwned!\")'/></pre>" (content alteration). I think consistency trumps security here, and we have to reject this approach.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Ok, I understand your first point, but the second example (content alteration) is desired in this case, disabling the XSS vector. This alteration doesn't differ much from removing<script>alert(1)</script> whenkeepScripts is not passed. As I've explained in the commit message, I don't see a safe and future-proof way to remove the event handler entirely.

So maybe a different valid prefix would be a middle ground we could agree on?

Copy link
Member

Choose a reason for hiding this comment

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

No, I meant that in the sense of altering elementcontent instead ofattributes (which is certainlynot desired). The difference is substantial: imagine the effects of this PR on$el.html( "Example vulnerability: <pre><img onerror='alert(\"Pwned!\")'/></pre>" ).

So maybe a different valid prefix would be a middle ground we could agree on?

There's no prefix that can both produce valid output and avoid attribute collision in all cases, but the value ofjQuery.expando is close enough that we could safely pretend. However, even disregarding the above issue (which I'm not prepared to do), there's still either a) the unrequested alteration of input, or b) the performance hit of revisiting newly-created elements to undo it, neither of which I like.

Personally, I prefer encouraging everyone to sanitize user input before passing it to jQuery as HTML (or better yet—not doing so) over paying the size/performance/complexity/maintenance costs of trying to solve every edge case ourselves.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Ah, now I got you. Ok, this might indeed be an unwanted side effect.

@dmethvin
Copy link
Member

I don't think any regex can really fix this. It would be possible to try and sanitize the HTML if it were first parsed per the earlier PRs, but that is most likely a lot of code and should be done in a plugin. There's a pretty good list of "dangerous" stuff athttp://msdn.microsoft.com/en-us/library/windows/apps/hh465388.aspx

@locklockbot locked asresolvedand limited conversation to collaboratorsJan 21, 2019
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@fhemberger@dmethvin@gibson042

[8]ページ先頭

©2009-2025 Movatter.jp