-
-
Notifications
You must be signed in to change notification settings - Fork 40
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
Possible code injection #21
Comments
Yeah, I'm fully aware of this, that's why there is a note for that example:
But maybe I should either:
Note that even your suggested change of using What do you think? |
First, the URLs really should not include characters not allowed in URLs. According to this post on Stackoverflow double quotes are not legal. Giving an example in the readme is important, I wouldn't have learned about the AutoLink class otherwise. As a general effort to spread knowledge about important security best practices, it's maybe a good thing to include usage of If you are using the library as a tool to create HTML out of some kind of plaintext, that plaintext needs to be sanitized before using it to autolink. That's what I do in my real world application; only during autolinking some of it becomes unwanted code again. |
I was following Rinku's behavior in this case. Having said that, I don't think stopping URL's at I'll update the example when I find some time. |
With spans, the code for renderLinks can just be written as a normal loop with an if statement. This makes it possible to render the text between links in a special way too (e.g. escape it).
Ok, I've:
Iterable<Span> spans = linkExtractor.extractSpans(input);
StringBuilder sb = new StringBuilder();
for (Span span : spans) {
String text = input.substring(span.getBeginIndex(), span.getEndIndex());
if (span instanceof LinkSpan) {
// span is a URL
sb.append("<a href=\"");
sb.append(Encode.forHtmlAttribute(text));
sb.append("\">");
sb.append(Encode.forHtml(text));
sb.append("</a>");
} else {
// span is plain text before/after link
sb.append(Encode.forHtml(text));
}
}
result = sb.toString(); |
@mindhaq nothing? Ok. I'm gonna merge that PR. |
Add extractSpans method so that we can deprecate renderLinks (#21)
I have since released this change as 0.9.0, see CHANGELOG: https://github.com/robinst/autolink-java/blob/master/CHANGELOG.md#090---2018-06-04 |
When using autolink on a text including a link like this one
And render the output as it is suggested in the example:
the output will be
which is strictly speaking invalid HTML, but browsers will still execute the click handler. See https://jsfiddle.net/vLjLLo8n/2/ to try it out.
I understand that appending a subsequence to the StringBuilder is the more efficient than providing the link as a String, but to make this secure, you would need to get the substring and perform encoding on it.
So, for example using OWASP Java Encoder, the rendering needs to be done like this:
resulting in a safe output:
Easiest fix for this particular problem would probably be if autolink would not include
single ordouble quotes, or any other character not legal in a URL.(EDIT: single quotes are legal characters)
A possibly breaking API change would be to provide the linkString as part of the LinkSpan interface.
The text was updated successfully, but these errors were encountered: