-
Notifications
You must be signed in to change notification settings - Fork 36
Refactor to allow CSRF bypass, improve error logging for API authoring #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mgaffigan
wants to merge
2
commits into
OpenIntegrationEngine:main
Choose a base branch
from
mgaffigan:maint/add-server-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
15 changes: 15 additions & 0 deletions
15
server/src/com/mirth/connect/server/api/DontRequireRequestedWith.java
This file contains hidden or 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,15 @@ | ||
| package com.mirth.connect.server.api; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * If this annotation is present on a method or class, the X-Requested-With header | ||
| * requirement will not be enforced for that resource. | ||
| */ | ||
| @Target({ElementType.METHOD, ElementType.TYPE}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface DontRequireRequestedWith { | ||
| } |
This file contains hidden or 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
85 changes: 44 additions & 41 deletions
85
server/src/com/mirth/connect/server/api/providers/RequestedWithFilter.java
This file contains hidden or 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,64 +1,67 @@ | ||
| /* | ||
| * Copyright (c) Mirth Corporation. All rights reserved. | ||
| * | ||
| * http://www.mirthcorp.com | ||
| * | ||
| * The software in this package is published under the terms of the MPL license a copy of which has | ||
| * been included with this distribution in the LICENSE.txt file. | ||
| */ | ||
|
|
||
| package com.mirth.connect.server.api.providers; | ||
|
|
||
| import java.io.IOException; | ||
| import java.lang.reflect.Method; | ||
| import java.util.List; | ||
|
|
||
| import javax.servlet.Filter; | ||
| import javax.servlet.FilterChain; | ||
| import javax.servlet.FilterConfig; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.ServletRequest; | ||
| import javax.servlet.ServletResponse; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| 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.ResourceInfo; | ||
| import javax.ws.rs.core.Context; | ||
| import javax.ws.rs.core.Response; | ||
| import javax.ws.rs.ext.Provider; | ||
|
|
||
| import org.apache.commons.configuration2.PropertiesConfiguration; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import com.mirth.connect.server.api.DontRequireRequestedWith; | ||
|
|
||
| @Provider | ||
| public class RequestedWithFilter implements Filter { | ||
| @Priority(Priorities.AUTHENTICATION + 100) | ||
| public class RequestedWithFilter implements ContainerRequestFilter { | ||
|
|
||
| private boolean isRequestedWithHeaderRequired = true; | ||
| @Context | ||
| private ResourceInfo resourceInfo; | ||
|
|
||
| private static boolean isRequestedWithHeaderRequired = true; | ||
|
|
||
| public RequestedWithFilter(PropertiesConfiguration mirthProperties) { | ||
|
|
||
| // Jax requires a no-arg constructor to instantiate providers via classpath scanning. | ||
| public RequestedWithFilter() { | ||
| } | ||
|
|
||
| public static void configure(PropertiesConfiguration mirthProperties) { | ||
| isRequestedWithHeaderRequired = mirthProperties.getBoolean("server.api.require-requested-with", true); | ||
| } | ||
|
|
||
| @Override | ||
| public void init(FilterConfig filterConfig) throws ServletException {} | ||
| public static boolean isRequestedWithHeaderRequired() { | ||
| return isRequestedWithHeaderRequired; | ||
| } | ||
kpalang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | ||
| HttpServletResponse res = (HttpServletResponse) response; | ||
| public void filter(ContainerRequestContext requestContext) throws IOException { | ||
| if (!isRequestedWithHeaderRequired) { | ||
| return; | ||
| } | ||
|
|
||
| // If the resource method or class is annotated with DontRequireRequestedWith, skip the check | ||
| if (resourceInfo != null) { | ||
| Method method = resourceInfo.getResourceMethod(); | ||
| if (method != null && method.getAnnotation(DontRequireRequestedWith.class) != null) { | ||
| return; | ||
| } | ||
| Class<?> resourceClass = resourceInfo.getResourceClass(); | ||
| if (resourceClass != null && resourceClass.getAnnotation(DontRequireRequestedWith.class) != null) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| HttpServletRequest servletRequest = (HttpServletRequest)request; | ||
| String requestedWithHeader = (String) servletRequest.getHeader("X-Requested-With"); | ||
| List<String> header = requestContext.getHeaders().get("X-Requested-With"); | ||
|
|
||
| //if header is required and not present, send an error | ||
| if(isRequestedWithHeaderRequired && StringUtils.isBlank(requestedWithHeader)) { | ||
| res.sendError(400, "All requests must have 'X-Requested-With' header"); | ||
| if (header == null || header.isEmpty() || StringUtils.isBlank(header.get(0))) { | ||
| requestContext.abortWith(Response.status(400).entity("All requests must have 'X-Requested-With' header").build()); | ||
| } | ||
| else { | ||
| chain.doFilter(request, response); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public boolean isRequestedWithHeaderRequired() { | ||
| return isRequestedWithHeaderRequired; | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() {} | ||
| } | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.