This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Fix tag balancing #23
Open
adon-at-work
wants to merge
4
commits into
master
Choose a base branch
from
fix-tag-balancing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,18 +31,31 @@ Authors: Aditya Mahendrakar <[email protected]> | |
assert.equal(output, '<h1 id="foo" title="asd" checked>hello world 2</h1>'); | ||
}); | ||
|
||
it('should always balance unopened tags', function(){ | ||
it('should balance tags', function(){ | ||
var html = "</div>foo</h2>bar<a href=\"123\">hello<b>world</a><embed>123</embed><br /><br/><p>"; | ||
|
||
// with tag balancing enabled by default | ||
var output = (new Purifier()).purify(html); | ||
assert.equal(output, 'foobar<a href=\"123\">hello<b>world</b></a><embed />123<br /><br /><p></p>'); | ||
var output = (new Purifier({tagBalance:{enabled:true}})).purify(html); | ||
assert.equal(output, 'foobar<a href="123">hello<b>world</a><embed />123<br /><br /><p></b>'); | ||
}); | ||
|
||
it('should balance remaining tags and drop inputs when there are too many unclosed tags', function(){ | ||
var html = "<b>1<b>2<b>3<b>4<b>5<b>6</b></b></b></b>"; | ||
|
||
// with tag balancing enabled by default | ||
var output = (new Purifier({tagBalance:{enabled:true, stackSize:3}})).purify(html); | ||
assert.equal(output, '<b>1<b>2<b>3</b></b></b>'); | ||
}); | ||
|
||
it('should not balance tags if disabled', function(){ | ||
var html = "</div>foo</h2>bar<a href=\"123\">hello<b>world</a><embed>123</embed><br /><br/><p>"; | ||
|
||
// with tag balancing disabled | ||
var output = (new Purifier({enableTagBalancing:false})).purify(html); | ||
assert.equal(output, "</div>foo</h2>bar<a href=\"123\">hello<b>world</a><embed />123</embed><br /><br /><p>"); | ||
var output = (new Purifier({tagBalance:{enabled:false}})).purify(html); | ||
assert.equal(output, '</div>foo</h2>bar<a href="123">hello<b>world</a><embed />123</embed><br /><br /><p>'); | ||
}); | ||
|
||
|
||
it('should handle all vectors mentioned in https://html5sec.org', function(){ | ||
var output, i, vector; | ||
for (var i = 0; i < html5secVectors.length; i++) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another approach on handling stackOverflow is to :
a. clear the stack
b. disregard the check "if tag exists in the stack".
(in other words, behave as if tag balancing was disabled)
That would prevent truncating valid/balanced input just because it has nesting > stack size(as currently seen in the test case at https://github.com/yahoo/html-purify/pull/23/files#diff-cfdb6a0bb85a9c9dcb6708d2225134cfR42). Also, our assertion that "we won't do tag balacing when nesting > stack size" still holds true. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
really want to let an untrusted input to disable tag balancing?
an attacker can first pad
<b>
n+1 times, where n equals the stack size limit, optionally close all of them, and then start crafting "dangerous" tags. Examples:<b>...<b><textarea>
<b>...<b><div style="display:none;visibility:hidden;font-size:0;color:#000">
(accepting/allowing either of those CSS will result in a consequence literally like<!--
. It's common to allow an untrusted email to specify font size/color)<div style="display:none;visibility:hidden;font-size:0;color:#000">
n+1 timesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me come up with a simple use case, as for email threads/conversations:
I hope this use case can explain why we do tag balancing. No one would accept:
(1) the
purify(email_content1)
can hide the buttons like Reply, Forward, and Delete from the trusted Mail app(2) even worst,
purify(email_content1)
can collude withpurify(email_content3)
to hide email 2, whenemail_content1
is</div><div style="display:none;..."><div>
andemail_content3
is</div></div><div>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thoughts? this pretty much explained the need of self-containing the untrusted data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should make sure the output is safe, or we just return empty output with an error.