-
Notifications
You must be signed in to change notification settings - Fork 37
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| // SPDX-FileCopyrightText: 2025 Mitch Gaffigan <mitch.gaffigan@comcast.net> | ||
|
|
||
| package com.mirth.connect.server.api; | ||
mgaffigan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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 { | ||
| } | ||
| 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 <mitch.gaffigan@comcast.net> | ||||||
| /* | ||||||
| * 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 { | ||||||
|
Comment on lines
+32
to
+33
|
||||||
|
|
||||||
| @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; | ||||||
| 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 header is required and not present, send an error | |
| // if header is required and not present, send an error |
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.
DontRequireRequestedWithis an annotation, not a provider class. It should not be added toproviderClasses. Instead,RequestedWithFilter.classshould be added here. The annotation itself doesn't need to be registered - it's just a marker used by the filter.