Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.logging.log4j.util.StringBuilders;

/**
* This class implements the {@code <log:dump>} tag.
Expand Down Expand Up @@ -60,8 +61,8 @@ public int doEndTag() throws JspException {
final String name = names.nextElement();
final Object value = this.pageContext.getAttribute(name, this.scope);

this.pageContext.getOut().write("<dt><code>" + name + "</code></dt>");
this.pageContext.getOut().write("<dd><code>" + value + "</code></dd>");
this.pageContext.getOut().write("<dt><code>" + escapeHtml(name) + "</code></dt>");
this.pageContext.getOut().write("<dd><code>" + escapeHtml(String.valueOf(value)) + "</code></dd>");
}
this.pageContext.getOut().write("</dl>");
} catch (final IOException e) {
Expand All @@ -70,4 +71,10 @@ public int doEndTag() throws JspException {

return Tag.EVAL_PAGE;
}

private static String escapeHtml(final String value) {
final StringBuilder builder = new StringBuilder(value);
StringBuilders.escapeXml(builder, 0);
return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ void testDoEndTagDefaultPageScope() throws Exception {
"The output is not correct.");
}

@Test
void testDoEndTagEscapesHtml() throws Exception {
this.context.setAttribute("<name>", "<script>alert('xss')</script>", PageContext.PAGE_SCOPE);

final int returnValue = this.tag.doEndTag();
assertEquals(Tag.EVAL_PAGE, returnValue, "The return value is not correct.");

this.writer.flush();
final String output = new String(this.output.toByteArray(), UTF8);
assertEquals(
"<dl>" + "<dt><code>&lt;name&gt;</code></dt>"
+ "<dd><code>&lt;script&gt;alert(&apos;xss&apos;)&lt;/script&gt;</code></dd>"
+ "</dl>",
output,
"Attribute names and values must be HTML-escaped.");
}

@Test
void testDoEndTagSessionScopeNoAttributes() throws Exception {
this.context.setAttribute("badAttribute01", "skippedValue01", PageContext.PAGE_SCOPE);
Expand Down
12 changes: 12 additions & 0 deletions src/changelog/.2.x.x/fix_taglib_dump_tag_xss.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="https://logging.apache.org/xml/ns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
https://logging.apache.org/xml/ns
https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="4316" link="https://github.com/apache/logging-log4j2/pull/4316"/>
<description format="asciidoc">
HTML-escape scope attribute names and values rendered by the `log:dump` JSP tag (`DumpTag`) to prevent cross-site scripting
</description>
</entry>
Loading