Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merging HEAD into BODY while use white list #1555

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/jsoup/Jsoup.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static Document parse(URL url, int timeoutMillis) throws IOException {
@see Cleaner#clean(Document)
*/
public static String clean(String bodyHtml, String baseUri, Safelist safelist) {
Document dirty = parseBodyFragment(bodyHtml, baseUri);
Document dirty = parse(bodyHtml, baseUri);
Cleaner cleaner = new Cleaner(safelist);
Document clean = cleaner.clean(dirty);
return clean.body().html();
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/jsoup/safety/Cleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ public Document clean(Document dirtyDocument) {
Validate.notNull(dirtyDocument);

Document clean = Document.createShell(dirtyDocument.baseUri());
copySafeNodes(dirtyDocument.body(), clean.body());
Element head,body;
if(dirtyDocument.head().childNodeSize() > 0){
head = dirtyDocument.head();
copySafeNodes(head, clean.body());
}
if(dirtyDocument.body().childNodeSize() > 0){
body = dirtyDocument.body();
copySafeNodes(body, clean.body());
}
clean.outputSettings(dirtyDocument.outputSettings().clone());

return clean;
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/jsoup/safety/CleanerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,17 @@ public void bailsIfRemovingProtocolThatsNotSet() {
assertEquals(Document.OutputSettings.Syntax.xml, result.outputSettings().syntax());
assertEquals("<p>test<br /></p>", result.body().html());
}
@Test public void NoHeadCleanerTest(){
Safelist whitelist = Safelist.relaxed()
.addTags("!DOCTYPE html", "html","body","head","meta", "style")
.addAttributes("meta", "charset");
String value = "<html><head><style>.some {color: red}</style></head><body>3<script>alert('pwned')</script>4</body></html>";
String doc = Jsoup.clean(value, whitelist);
assertEquals("<head>\n" +
" <style>.some {color: red}</style>\n" +
"</head>\n" +
"<body>\n" +
" 34\n" +
"</body>",doc);
}
}