Skip to content
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

Use config server port number #3684

Merged
merged 1 commit into from
Nov 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import org.obiba.opal.web.ws.intercept.RequestCyclePreProcess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import javax.annotation.Nullable;
Expand All @@ -32,13 +35,27 @@ public class CSRFInterceptor extends AbstractSecurityComponent implements Reques

private static final String REFERER_HEADER = "Referer";

private final String serverPort;

@Autowired
public CSRFInterceptor(@Value("${org.obiba.opal.http.port:8080}") String port) {
serverPort = port;
}

@Nullable
@Override
public Response preProcess(HttpRequest request, ResourceMethodInvoker method) {
String host = request.getHttpHeaders().getHeaderString(HOST_HEADER);
String referer = request.getHttpHeaders().getHeaderString(REFERER_HEADER);
if (referer != null) {
boolean forbidden = !referer.startsWith(String.format("http://%s/", host)) && !referer.startsWith(String.format("https://%s/", host));
String localhost = String.format("localhost:%s", serverPort);
boolean forbidden = false;

if (localhost.equals(host)) {
if (!referer.startsWith(String.format("http://%s/", host)))
forbidden = true;
} else if (!referer.startsWith(String.format("https://%s/", host)))
forbidden = true;
if (forbidden) {
log.warn("CSRF detection: Host={}, Referer={}", host, referer);
return Response.status(Status.FORBIDDEN).build();
Expand Down