Skip to content

Commit

Permalink
api_trusted_hosts_only_whitelist_path_regexp setting
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrenn committed Nov 21, 2024
1 parent ef1ce50 commit 7b1b38f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.phoenixctms.ctsms.web.jersey.provider;

import java.util.regex.Pattern;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
Expand All @@ -25,12 +27,15 @@ public class TrustedHostFilter extends ExceptionMapperBase implements ContainerR

@Override
public ContainerRequest filter(ContainerRequest request) {
if (Settings.getBoolean(SettingCodes.API_TRUSTED_HOSTS_ONLY, Bundle.SETTINGS, DefaultSettings.API_TRUSTED_HOSTS_ONLY) && !WebUtil.isTrustedHost(this.request)) {
AuthorisationException ex = new AuthorisationException(Messages.getMessage(MessageCodes.HOST_NOT_ALLOWED_OR_UNKNOWN_HOST, WebUtil.getRemoteHost(this.request)));
ex.setErrorCode(AuthorisationExceptionCodes.HOST_NOT_ALLOWED_OR_UNKNOWN_HOST);
throw new WebApplicationException(ex);
} else {
return request;
if (Settings.getBoolean(SettingCodes.API_TRUSTED_HOSTS_ONLY, Bundle.SETTINGS, DefaultSettings.API_TRUSTED_HOSTS_ONLY)) { // && !WebUtil.isTrustedHost(this.request)) {
Pattern whitelistRegExp = Settings.getRegexp(SettingCodes.API_TRUSTED_HOSTS_ONLY_WHITELIST_PATH_REGEXP, Bundle.SETTINGS,
DefaultSettings.API_TRUSTED_HOSTS_ONLY_WHITELIST_REGEXP);
if (whitelistRegExp != null && !whitelistRegExp.matcher(request.getRequestUri().getPath()).find()) {
AuthorisationException ex = new AuthorisationException(Messages.getMessage(MessageCodes.HOST_NOT_ALLOWED_OR_UNKNOWN_HOST, WebUtil.getRemoteHost(this.request)));
ex.setErrorCode(AuthorisationExceptionCodes.HOST_NOT_ALLOWED_OR_UNKNOWN_HOST);
throw new WebApplicationException(ex);
}
}
return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public final class DefaultSettings {
public final static String API_REALM = "api";
public final static String API_TITLE = "REST API";
public static final boolean API_TRUSTED_HOSTS_ONLY = true;
public final static String API_TRUSTED_HOSTS_ONLY_WHITELIST_REGEXP = null; // "/tools";
public final static String API_VERSION = "0.0.0";
public static final boolean ENABLE_TOOLTIPS = true;
public static final boolean TRIAL_STATUS_UPDATE_REQUIRES_PASSWORD = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ public interface SettingCodes {
public static final String API_REALM = "api_realm";
public static final String API_TITLE = "api_title";
public static final String API_TRUSTED_HOSTS_ONLY = "api_trusted_hosts_only";
public static final String API_TRUSTED_HOSTS_ONLY_WHITELIST_PATH_REGEXP = "api_trusted_hosts_only_whitelist_path_regexp";
public static final String API_VERSION = "api_version";
public static final String INPUT_FIELD_DELTA_SUMMARY_MAX = "input_field_delta_summary_max";
public static final String FIELD_CALCULATION_DEBUG_LEVEL = "field_calculation_debug_level";
Expand Down
15 changes: 15 additions & 0 deletions web/src/main/java/org/phoenixctms/ctsms/web/util/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
Expand Down Expand Up @@ -282,6 +284,19 @@ public static Sex getSex(String key, Bundle bundle, Sex defaultValue) {
}
}

public static Pattern getRegexp(String key, Bundle bundle, String defaultValue) {
String pattern = CommonUtil.getValue(key, getBundle(bundle), defaultValue);
if (pattern != null && pattern.length() > 0) {
try {
return java.util.regex.Pattern.compile(pattern);
} catch (PatternSyntaxException e) {
throw new IllegalArgumentException(e);
}
} else {
return null;
}
}

public static String getString(String key, Bundle bundle, String defaultValue) {
return CommonUtil.getValue(key, getBundle(bundle), defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ api_title=${application.abbreviation} REST API
api_version=${application.version}
api_realm=api
api_trusted_hosts_only=true
api_trusted_hosts_only_whitelist_path_regexp=

enable_tooltips=true

Expand Down

0 comments on commit 7b1b38f

Please sign in to comment.