-
Notifications
You must be signed in to change notification settings - Fork 890
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RESTEASY-1485] added classes and adjusted test cases. Created util, …
…escapeXml. This closes #957
- Loading branch information
Showing
11 changed files
with
123 additions
and
21 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...va/org/jboss/resteasy/plugins/providers/jackson/UnrecognizedPropertyExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package org.jboss.resteasy.plugins.providers.jackson; | ||
|
|
||
| import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; | ||
| import javax.ws.rs.core.MediaType; | ||
| import javax.ws.rs.core.Response; | ||
| import javax.ws.rs.ext.ExceptionMapper; | ||
| import javax.ws.rs.ext.Provider; | ||
| import org.jboss.resteasy.util.HttpResponseCodes; | ||
|
|
||
| /** | ||
| * (RESTEASY-1485) Address concerns of a possible XSS attack by removing some | ||
| * details of the exception. | ||
| * | ||
| * User: rsearls | ||
| * Date: 9/22/16 | ||
| */ | ||
| @Provider | ||
| public class UnrecognizedPropertyExceptionHandler implements ExceptionMapper<UnrecognizedPropertyException> { | ||
| @Override | ||
| public Response toResponse(UnrecognizedPropertyException exception) | ||
| { | ||
| return Response.status(HttpResponseCodes.SC_BAD_REQUEST) | ||
| .type(MediaType.TEXT_HTML) | ||
| .entity(exception.getOriginalMessage()) | ||
| .build(); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
providers/jackson2/src/main/resources/META-INF/services/javax.ws.rs.ext.Providers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider | ||
| org.jboss.resteasy.plugins.providers.jackson.UnrecognizedPropertyExceptionHandler |
73 changes: 73 additions & 0 deletions
73
...jboss/resteasy/plugins/interceptors/encoding/MessageSanitizerContainerResponseFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package org.jboss.resteasy.plugins.interceptors.encoding; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import javax.annotation.Priority; | ||
| import javax.ws.rs.Priorities; | ||
| import javax.ws.rs.container.ContainerRequestContext; | ||
| import javax.ws.rs.container.ContainerResponseContext; | ||
| import javax.ws.rs.container.ContainerResponseFilter; | ||
| import javax.ws.rs.ext.Provider; | ||
| import org.jboss.resteasy.util.HttpResponseCodes; | ||
|
|
||
| /** | ||
| * (RESTEASY-1485) Thwart select XSS attack by escaping special chars in | ||
| * Exception message. | ||
| * | ||
| * User: rsearls | ||
| * Date: 9/16/16 | ||
| */ | ||
| @Provider | ||
| @Priority(Priorities.ENTITY_CODER) | ||
| public class MessageSanitizerContainerResponseFilter implements ContainerResponseFilter { | ||
| @Override | ||
| public void filter(ContainerRequestContext requestContext, | ||
| ContainerResponseContext responseContext) throws IOException { | ||
|
|
||
| if (HttpResponseCodes.SC_BAD_REQUEST == responseContext.getStatus()) { | ||
| Object entity = responseContext.getEntity(); | ||
| if (entity != null && entity instanceof String) { | ||
| String escapedMsg = escapeXml((String) entity); | ||
| responseContext.setEntity(escapedMsg); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // set of replacement chars to hex encoding | ||
| private static final HashMap<String, String> replacementMap; | ||
| static { | ||
| replacementMap = new HashMap<String, String>(); | ||
| replacementMap.put("/", "/"); | ||
| replacementMap.put("<", "<"); | ||
| replacementMap.put(">", ">"); | ||
| replacementMap.put("&", "&"); | ||
| replacementMap.put("\"", """); | ||
| replacementMap.put("'", "'"); | ||
| } | ||
|
|
||
| /** | ||
| * Replace char with the hex encoding | ||
| * @param str | ||
| * @return | ||
| */ | ||
| private String escapeXml(String str) { | ||
| StringBuilder sb = new StringBuilder(); | ||
| if (!str.isEmpty()) { | ||
| // the vertical bar, |, is a special regular expression char that | ||
| // causes splitting on individual characters. | ||
| for (String key : str.split("|")) { | ||
| String value = replacementMap.get(key); | ||
| if (value == null) { | ||
| sb.append(key); | ||
| } else { | ||
| sb.append(value); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| } | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters