Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

malformed uri#198

Open
kyleroush wants to merge 2 commits into
cerner:mainfrom
kyleroush:malformed_uri
Open

malformed uri#198
kyleroush wants to merge 2 commits into
cerner:mainfrom
kyleroush:malformed_uri

Conversation

@kyleroush
Copy link
Copy Markdown
Contributor

What was changed? Why is this necessary?

Explain the changes included in this request and the reasons for them.

I have added a new jaxrs filter that will check to see if the request is a correctly formatted URI and if it is not it will short circuit the request and return a 400 status code.
I have also updated the JSON field filtering logic to only apply the filter when the request is successful.

How was it tested?

I integrated these changes into a service I own and tested that when a 400 was responded instead of a 500

How to test

This is bare minimum acceptable testing

  • ./mvnw clean install -U

* @author Kyle Roush
*/
@Provider
@Priority(Priorities.AUTHENTICATION)
Copy link
Copy Markdown
Contributor

@nathanschile nathanschile Feb 25, 2021

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

try {
URI.create(requestContext.getUriInfo().getAbsolutePath().toString());
} catch (IllegalArgumentException e) {
requestContext.abortWith(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's log the exception.

Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream os) throws IOException {

if (httpServletResponse.getStatus() >= 400 ) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

    String fields = null;
    try {
      fields = uriInfo.getQueryParameters() == null ? null
          : uriInfo.getQueryParameters().getFirst("fields");
    } catch (Throwable e) {
      // Nothing to do. URI does not conform to grammar of RFC 2396.
    }

.in(Singleton.class);
}

@Provides
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add @Singleton

@@ -20,4 +22,9 @@ protected void configure() {
bind(CorrelationIdFilter.class).toProvider(CorrelationIdFilterProvider.class)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

@Override
public void filter(ContainerRequestContext requestContext) {
try {
URI.create(requestContext.getUriInfo().getAbsolutePath().toString());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do requestContext.getUriInfo().getRequestUri(); so it doesn't create a new URI.

import javax.ws.rs.core.{MediaType, Response, UriInfo}
import org.jboss.resteasy.specimpl.ResteasyUriInfo
import org.mockito
import org.mockito.{ArgumentCaptor, Mockito}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleanup imports

it("does not call abortWith") {
malformedRequestFilter.filter(containerRequestContext)

Mockito.verify(containerRequestContext).getUriInfo();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit. This can be verify(containerRequestContext).getUriInfo, since Scala doesn't require the parens and semicolon. You can also import org.mockito.Mockito.{verify, when} so you don't need to have Mockito.

package com.cerner.beadledom.jaxrs;

import com.cerner.beadledom.jaxrs.provider.CorrelationIdFilter;
import com.cerner.beadledom.jaxrs.provider.MalformedRequestFilter;
Copy link
Copy Markdown
Contributor

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants