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

Override URL encoding when serializing results to HTML #87

Merged
merged 2 commits into from
Jun 24, 2021
Merged
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
13 changes: 13 additions & 0 deletions src/main/java/org/owasp/validator/html/scan/ASHTMLSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import org.apache.xml.serialize.HTMLdtd;
import org.apache.xml.serialize.OutputFormat;
import org.owasp.validator.html.InternalPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.Writer;

@SuppressWarnings("deprecation")
public class ASHTMLSerializer extends org.apache.xml.serialize.HTMLSerializer {

private static final Logger logger = LoggerFactory.getLogger(ASHTMLSerializer.class);
private boolean encodeAllPossibleEntities;

public ASHTMLSerializer(Writer w, OutputFormat format, InternalPolicy policy) {
Expand Down Expand Up @@ -67,4 +70,14 @@ public void endElementIO(String namespaceURI, String localName,
_printer.flush();
}

@Override
protected String escapeURI(String uri) {
String originalURI = uri;
try {
printEscaped(uri);
} catch (IOException e) {
logger.error("URI escaping failed for value: " + originalURI);
}
return "";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this method supposed to ALWAYS return an empty string? Seems strange to me.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It is. On the HTMLSerializer, that function is supposed to returned the URL with escaped quotes and nothing else. I'm reusing printEscaped which does the encoding we want and internally "prints" the result in the XML that is being built. If I return a non-empty string, that string is also "printed" because the call to escapeURI is something like print(escapeURI()). I mentioned this on our emails, it may be explained better there.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add all the necessary explanation to this code as comments so it stands alone? Otherwise future reviewers will have the same concerns.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've done a separate PR with the comment. I tried to push directly to 1.6.4 and had git issues, but I'm kinda in a hurry and wanted to have this solved fast.

}
}
12 changes: 12 additions & 0 deletions src/test/java/org/owasp/validator/html/test/AntiSamyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1490,5 +1490,17 @@ public void testGithubIssue81() throws ScanException, PolicyException {
assertThat(as.scan("<p style=\"color: red\">Some Text</p>", policy, AntiSamy.DOM).getCleanHTML(), not(containsString("!important")));
assertThat(as.scan("<p style=\"color: red\">Some Text</p>", policy, AntiSamy.SAX).getCleanHTML(), not(containsString("!important")));
}

@Test
public void entityReferenceEncodedInHtmlAttribute() throws ScanException, PolicyException {
// Concern is that "&" is not being encoded and "#00058" was not being interpreted as ":"
// so the validations based on regexp passed and a browser would load "&:" together.
// All this when not using the XHTML serializer.
Policy revised = policy.cloneWithDirective("useXHTML","false");
assertThat(as.scan("<p><a href=\"javascript&#00058x=1,%61%6c%65%72%74%28%22%62%6f%6f%6d%22%29\">xss</a></p>", revised, AntiSamy.DOM).getCleanHTML(),
containsString("javascript&amp;#00058"));
assertThat(as.scan("<p><a href=\"javascript&#00058x=1,%61%6c%65%72%74%28%22%62%6f%6f%6d%22%29\">xss</a></p>", revised, AntiSamy.SAX).getCleanHTML(),
containsString("javascript&amp;#00058"));
}
}