Skip to content
Open
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
16 changes: 15 additions & 1 deletion core/src/main/java/org/apache/struts2/result/PostbackResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@

// Render
PrintWriter pw = new PrintWriter(response.getOutputStream());
pw.write("<!DOCTYPE html><html><body><form action=\"" + finalLocation + "\" method=\"POST\">");
String safeLocation = encodeHtml(finalLocation);
pw.write("<!DOCTYPE html><html><body><form action=\"" + safeLocation + "\" method=\"POST\">");

Check warning

Code scanning / CodeQL

Information exposure through an error message Medium

Error information
can be exposed to an external user.
Error information
can be exposed to an external user.

Check warning

Code scanning / CodeQL

Cross-site scripting Medium

Cross-site scripting vulnerability due to a
user-provided value
.
writeFormElements(request, pw);
writePrologueScript(pw);
pw.write("</html>");
Expand Down Expand Up @@ -213,6 +214,19 @@
this.prependServletContext = prependServletContext;
}

/**
* Encodes special HTML characters to prevent injection in HTML attribute context.
*/
private static String encodeHtml(String value) {
if (value == null) {
return "";
}
return value.replace("&", "&amp;")
.replace("\"", "&quot;")
.replace("<", "&lt;")
.replace(">", "&gt;");
}

protected void writeFormElement(PrintWriter pw, String name, String[] values) throws UnsupportedEncodingException {
for (String value : values) {
String encName = URLEncoder.encode(name, StandardCharsets.UTF_8);
Expand Down
Loading