-
Notifications
You must be signed in to change notification settings - Fork 42
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
Some comments aren't visible on the classic Files Changed page.
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
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| // SPDX-FileCopyrightText: 2025 Mitch Gaffigan <[email protected]> | ||
|
|
||
| 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
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,3 +1,6 @@ | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| // SPDX-FileCopyrightText: Mirth Corporation | ||
| // SPDX-FileCopyrightText: 2025 Mitch Gaffigan <[email protected]> | ||
| /* | ||
| * Copyright (c) Mirth Corporation. All rights reserved. | ||
| * | ||
|
|
@@ -10,55 +13,65 @@ | |
| 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.ws.rs.ext.Provider; | ||
| 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 org.apache.commons.configuration2.PropertiesConfiguration; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| @Provider | ||
| public class RequestedWithFilter implements Filter { | ||
| import com.mirth.connect.server.api.DontRequireRequestedWith; | ||
|
|
||
| private boolean isRequestedWithHeaderRequired = true; | ||
| @Priority(Priorities.AUTHENTICATION + 100) | ||
tonygermano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public class RequestedWithFilter implements ContainerRequestFilter { | ||
mgaffigan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @Context | ||
| private ResourceInfo resourceInfo; | ||
|
|
||
| public RequestedWithFilter(PropertiesConfiguration mirthProperties) { | ||
|
|
||
| private static boolean isRequestedWithHeaderRequired = true; | ||
kpalang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // 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; | ||
|
|
||
| HttpServletRequest servletRequest = (HttpServletRequest)request; | ||
| String requestedWithHeader = (String) servletRequest.getHeader("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"); | ||
| public void filter(ContainerRequestContext requestContext) throws IOException { | ||
| if (!isRequestedWithHeaderRequired) { | ||
| return; | ||
| } | ||
| else { | ||
| chain.doFilter(request, response); | ||
|
|
||
| // 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; | ||
| } | ||
| } | ||
|
|
||
| List<String> header = requestContext.getHeaders().get("X-Requested-With"); | ||
|
|
||
| // if header is required and not present, send an error | ||
| 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()); | ||
| } | ||
| } | ||
|
|
||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| // SPDX-FileCopyrightText: Mirth Corporation | ||
| // SPDX-FileCopyrightText: 2025 Mitch Gaffigan <[email protected]> | ||
| package com.mirth.connect.server.api.providers; | ||
|
|
||
mgaffigan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import javax.servlet.FilterChain; | ||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import javax.ws.rs.container.ContainerRequestContext; | ||
| import javax.ws.rs.core.Response; | ||
|
|
||
| import org.apache.commons.configuration2.PropertiesConfiguration; | ||
| import org.junit.Test; | ||
| import org.mockito.ArgumentCaptor; | ||
| import org.mockito.ArgumentMatchers; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import com.mirth.connect.client.core.PropertiesConfigurationUtil; | ||
|
|
@@ -22,26 +27,34 @@ public class RequestedWithFilterTest extends TestCase { | |
| @Test | ||
| //assert that if property is set to false, isRequestedWithHeaderRequired = false | ||
| public void testConstructor() { | ||
|
|
||
| mirthProperties.clearProperty("server.api.require-requested-with"); | ||
| RequestedWithFilter.configure(mirthProperties); | ||
| assertEquals(true, RequestedWithFilter.isRequestedWithHeaderRequired()); | ||
|
|
||
| mirthProperties.setProperty("server.api.require-requested-with", "false"); | ||
| RequestedWithFilter requestedWithFilter = new RequestedWithFilter(mirthProperties); | ||
| assertEquals(requestedWithFilter.isRequestedWithHeaderRequired(), false); | ||
| RequestedWithFilter.configure(mirthProperties); | ||
| assertEquals(false, RequestedWithFilter.isRequestedWithHeaderRequired()); | ||
| } | ||
|
|
||
| @Test | ||
| //assert that HttpServletResponse.sendError() is called when X-Requested-With is required but not present | ||
| public void testDoFilterRequestedWithTrue() { | ||
|
|
||
| mirthProperties.setProperty("server.api.require-requested-with", "true"); | ||
| RequestedWithFilter testFilter = new RequestedWithFilter(mirthProperties); | ||
|
|
||
| HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class); | ||
| HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class); | ||
| FilterChain mockFilterChain = Mockito.mock(FilterChain.class); | ||
|
|
||
| RequestedWithFilter.configure(mirthProperties); | ||
|
|
||
| ContainerRequestContext mockCtx = Mockito.mock(ContainerRequestContext.class); | ||
| when(mockCtx.getHeaders()).thenReturn(new javax.ws.rs.core.MultivaluedHashMap<String, String>()); | ||
|
|
||
| try { | ||
| testFilter.doFilter(mockReq, mockResp, mockFilterChain); | ||
| verify(mockResp).sendError(HttpServletResponse.SC_BAD_REQUEST, "All requests must have 'X-Requested-With' header"); | ||
| RequestedWithFilter filter = new RequestedWithFilter(); | ||
| filter.filter(mockCtx); | ||
| ArgumentCaptor<Response> responseCaptor = | ||
| ArgumentCaptor.forClass(Response.class); | ||
| verify(mockCtx).abortWith(responseCaptor.capture()); | ||
| Response response = responseCaptor.getValue(); | ||
| assertEquals(400, response.getStatus()); | ||
| assertEquals("All requests must have 'X-Requested-With' header", response.getEntity()); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
@@ -52,15 +65,15 @@ public void testDoFilterRequestedWithTrue() { | |
| public void testDoFilterRequestedWithFalse() { | ||
|
|
||
| mirthProperties.setProperty("server.api.require-requested-with", "false"); | ||
| RequestedWithFilter testFilter = new RequestedWithFilter(mirthProperties); | ||
|
|
||
| HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class); | ||
| HttpServletResponse mockResp = Mockito.mock(HttpServletResponse.class); | ||
| FilterChain mockFilterChain = Mockito.mock(FilterChain.class); | ||
|
|
||
| RequestedWithFilter.configure(mirthProperties); | ||
|
|
||
| ContainerRequestContext mockCtx = Mockito.mock(ContainerRequestContext.class); | ||
| when(mockCtx.getHeaders()).thenReturn(new javax.ws.rs.core.MultivaluedHashMap<String, String>()); | ||
|
|
||
| try { | ||
| testFilter.doFilter(mockReq, mockResp, mockFilterChain); | ||
| verify(mockResp, never()).sendError(HttpServletResponse.SC_BAD_REQUEST, "All requests must have 'X-Requested-With' header"); | ||
| RequestedWithFilter filter = new RequestedWithFilter(); | ||
| filter.filter(mockCtx); | ||
| verify(mockCtx, never()).abortWith(ArgumentMatchers.any(javax.ws.rs.core.Response.class)); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
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.