Skip to content

Commit ed49a6f

Browse files
committed
CSRF check not applied to localhost and loopback host
1 parent 8910296 commit ed49a6f

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

mica-rest/src/main/java/org/obiba/mica/config/JerseyConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public JerseyConfiguration(Environment environment) {
4040
register(AuthenticationInterceptor.class);
4141
register(ConfigurationInterceptor.class);
4242
register(AuditInterceptor.class);
43-
register(new CSRFInterceptor(getServerPort(environment), environment.acceptsProfiles(Profiles.PROD), environment.getProperty("csrf.allowed", "")));
43+
register(new CSRFInterceptor(environment.acceptsProfiles(Profiles.PROD), environment.getProperty("csrf.allowed", "")));
4444
register(MultiPartFeature.class);
4545
// validation errors will be sent to the client
4646
property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);

mica-rest/src/main/java/org/obiba/mica/web/rest/security/CSRFInterceptor.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.net.URI;
2424
import java.util.List;
25+
import java.util.regex.Pattern;
2526

2627
public class CSRFInterceptor implements ContainerRequestFilter {
2728

@@ -31,14 +32,15 @@ public class CSRFInterceptor implements ContainerRequestFilter {
3132

3233
private static final String REFERER_HEADER = "Referer";
3334

34-
private final String serverPort;
35+
private static final Pattern localhostPattern = Pattern.compile("^http[s]?://localhost:");
36+
37+
private static final Pattern loopbackhostPattern = Pattern.compile("^http[s]?://127\\.0\\.0\\.1:");
3538

3639
private final boolean productionMode;
3740

3841
private final List<String> csrfAllowed;
3942

40-
public CSRFInterceptor(String port, boolean productionMode, String csrfAllowed) {
41-
serverPort = port;
43+
public CSRFInterceptor(boolean productionMode, String csrfAllowed) {
4244
this.productionMode = productionMode;
4345
this.csrfAllowed = Strings.isNullOrEmpty(csrfAllowed) ? Lists.newArrayList() : Splitter.on(",").splitToList(csrfAllowed.trim());
4446
}
@@ -61,13 +63,8 @@ public void filter(ContainerRequestContext requestContext)
6163
// explicitly ok
6264
if (csrfAllowed.contains(refererHostPort)) return;
6365

64-
String localhost = String.format("localhost:%s", serverPort);
65-
String loopbackhost = String.format("127.0.0.1:%s", serverPort);
6666
boolean forbidden = false;
67-
if (localhost.equals(host) || loopbackhost.equals(host)) {
68-
if (!referer.startsWith(String.format("http://%s/", host)))
69-
forbidden = true;
70-
} else if (!referer.startsWith(String.format("https://%s/", host))) {
67+
if (!localhostPattern.matcher(host).matches() && !loopbackhostPattern.matcher(host).matches() && !referer.startsWith(String.format("https://%s/", host))) {
7168
forbidden = true;
7269
}
7370

0 commit comments

Comments
 (0)