-
Notifications
You must be signed in to change notification settings - Fork 29
malformed uri #198
base: main
Are you sure you want to change the base?
malformed uri #198
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package com.cerner.beadledom.jaxrs; | ||
|
|
||
| import com.cerner.beadledom.jaxrs.provider.CorrelationIdFilter; | ||
| import com.cerner.beadledom.jaxrs.provider.MalformedRequestFilter; | ||
| import com.google.inject.AbstractModule; | ||
| import com.google.inject.Provides; | ||
| import javax.inject.Singleton; | ||
|
|
||
| /** | ||
|
|
@@ -20,4 +22,9 @@ protected void configure() { | |
| bind(CorrelationIdFilter.class).toProvider(CorrelationIdFilterProvider.class) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the class Javadoc to indicate that the module now provides the MalformedRequestFilter |
||
| .in(Singleton.class); | ||
| } | ||
|
|
||
| @Provides | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
| MalformedRequestFilter provideMalformedRequestFilter() { | ||
| return new MalformedRequestFilter(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| import java.lang.annotation.Annotation; | ||
| import java.lang.reflect.Type; | ||
| import javax.inject.Inject; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.ws.rs.Produces; | ||
| import javax.ws.rs.core.Context; | ||
| import javax.ws.rs.core.MediaType; | ||
|
|
@@ -32,6 +33,9 @@ public class FilteringJacksonJsonProvider extends JacksonJsonProvider { | |
| @Context | ||
| UriInfo uriInfo; | ||
|
|
||
| @Context | ||
| HttpServletResponse httpServletResponse; | ||
|
|
||
| /** | ||
| * Creates a new instance of {@link FilteringJacksonJsonProvider}. | ||
| */ | ||
|
|
@@ -52,6 +56,12 @@ public long getSize( | |
| public void writeTo( | ||
| Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, | ||
| MultivaluedMap<String, Object> httpHeaders, OutputStream os) throws IOException { | ||
|
|
||
| if (httpServletResponse.getStatus() >= 400 ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of pulling in the servlet-api, can we update the code to something like |
||
| super.writeTo(o, type, genericType, annotations, mediaType, httpHeaders, os); | ||
| return; | ||
| } | ||
|
|
||
| String fields = uriInfo.getQueryParameters() == null ? null | ||
| : uriInfo.getQueryParameters().getFirst("fields"); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.cerner.beadledom.jaxrs.provider; | ||
|
|
||
| import com.cerner.beadledom.json.common.model.JsonError; | ||
| import java.net.URI; | ||
| import javax.annotation.Priority; | ||
| import javax.ws.rs.Priorities; | ||
| import javax.ws.rs.container.ContainerRequestContext; | ||
| import javax.ws.rs.container.ContainerRequestFilter; | ||
| import javax.ws.rs.container.PreMatching; | ||
| import javax.ws.rs.core.MediaType; | ||
| import javax.ws.rs.core.Response; | ||
| import javax.ws.rs.ext.Provider; | ||
|
|
||
| /** | ||
| * The MalformedRequestFilter reads the request context and determines if the request has a valid uri structure if it | ||
| * does not then it aborts the request and responds with a 400 | ||
| * | ||
| * @author Kyle Roush | ||
| */ | ||
| @Provider | ||
| @Priority(Priorities.AUTHENTICATION) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we lower this to a USER priority? Probably Not. I see that ForwardedHeaderFilter also uses UriInfo.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this error occur at the first call of UriInfo in the stack? |
||
| @PreMatching | ||
| public class MalformedRequestFilter implements ContainerRequestFilter { | ||
|
|
||
| @Override | ||
| public void filter(ContainerRequestContext requestContext) { | ||
| try { | ||
| URI.create(requestContext.getUriInfo().getAbsolutePath().toString()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do |
||
| } catch (IllegalArgumentException e) { | ||
| requestContext.abortWith( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's log the exception. |
||
| Response.status(400) | ||
| .type(MediaType.APPLICATION_JSON) | ||
| .entity(JsonError.builder().code(400).message("Malformed URI").build()) | ||
| .build()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package com.cerner.beadledom.jaxrs.provider | ||
|
|
||
| import java.net.URI | ||
|
|
||
| import com.cerner.beadledom.json.common.model.JsonError | ||
| import javax.ws.rs.container.ContainerRequestContext | ||
| import javax.ws.rs.core.{MediaType, Response, UriInfo} | ||
| import org.jboss.resteasy.specimpl.ResteasyUriInfo | ||
| import org.mockito | ||
| import org.mockito.{ArgumentCaptor, Mockito} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cleanup imports |
||
| import org.mockito.ArgumentMatchers.any | ||
| import org.scalatest._ | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
| import org.scalatestplus.mockito.MockitoSugar | ||
| import org.scalatest.funspec.AnyFunSpec | ||
| import org.scalatest.matchers.should.Matchers | ||
|
|
||
| class MalformedRequestFilterSpec | ||
| extends AnyFunSpec with BeforeAndAfterAll with Matchers with MockitoSugar { | ||
|
|
||
| describe("MalformedRequestFilter with a valid URI") { | ||
|
|
||
| val containerRequestContext = Mockito.mock(classOf[ContainerRequestContext]) | ||
| val uriInfo = new ResteasyUriInfo("http://localhost:8080", "") | ||
| Mockito.when(containerRequestContext.getUriInfo).thenReturn(uriInfo) | ||
|
|
||
| val malformedRequestFilter = new MalformedRequestFilter() | ||
|
|
||
| it("does not call abortWith") { | ||
| malformedRequestFilter.filter(containerRequestContext) | ||
|
|
||
| Mockito.verify(containerRequestContext).getUriInfo(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit. This can be |
||
| Mockito.verifyNoMoreInteractions(containerRequestContext); | ||
| } | ||
|
|
||
| } | ||
| describe("MalformedRequestFilter with an invalid URI") { | ||
|
|
||
| val containerRequestContext = Mockito.mock(classOf[ContainerRequestContext]) | ||
| val uriInfo = new ResteasyUriInfo("http://localhost:8080", "?test=%9") | ||
| Mockito.when(containerRequestContext.getUriInfo).thenReturn(uriInfo) | ||
|
|
||
| val malformedRequestFilter = new MalformedRequestFilter() | ||
| it("calls abortWith") { | ||
| malformedRequestFilter.filter(containerRequestContext) | ||
|
|
||
| Mockito.verify(containerRequestContext) | ||
| .abortWith(any(classOf[Response])) | ||
|
|
||
| } | ||
|
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update CHANGELOG.md with the added class.