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

Referer header is an uri not url #7137

Merged
merged 41 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
3279481
Deactivated records should get 409 on GET requests
amontenegro Feb 27, 2024
b3660c2
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Feb 28, 2024
2cc66ab
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 1, 2024
f2dc713
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 4, 2024
b4f8223
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 7, 2024
b0026c3
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 12, 2024
53e7616
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 19, 2024
bf82372
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Mar 25, 2024
23b6afb
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 4, 2024
182c67d
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 4, 2024
c52ef13
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 8, 2024
d4f779b
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 8, 2024
27b0033
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 17, 2024
585b896
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Apr 18, 2024
9c9dfef
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro May 8, 2024
42ac636
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro May 28, 2024
3f0d771
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jun 10, 2024
e65ec79
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jun 20, 2024
e69191a
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jun 24, 2024
cff8029
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 3, 2024
ddf3e67
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 4, 2024
a091bb3
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 5, 2024
97024f0
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 15, 2024
3201e7e
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 18, 2024
c85de91
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 22, 2024
3132245
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Jul 30, 2024
3672cfd
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 6, 2024
6ef71b1
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 12, 2024
3ccccec
Sequence should start at 100000
amontenegro Aug 12, 2024
50b85a9
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 12, 2024
3338fbb
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 13, 2024
ebd6d1c
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 16, 2024
13e0a6f
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 19, 2024
ca4085d
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Aug 30, 2024
fb76e20
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Sep 19, 2024
10c36ed
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Oct 28, 2024
b114590
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Oct 31, 2024
907abff
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Oct 31, 2024
2c8babc
Merge branch 'main' of https://github.com/ORCID/ORCID-Source
amontenegro Nov 15, 2024
2de4bd5
Referer header is an URI and not a URL, thats why it was failing for …
amontenegro Nov 15, 2024
019de32
Fix unit tests
amontenegro Nov 15, 2024
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
@@ -1,13 +1,16 @@
package org.orcid.core.web.filters;

import java.io.IOException;
import java.net.URISyntaxException;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.filter.OncePerRequestFilter;

/**
Expand All @@ -18,6 +21,8 @@

public class CorsFilter extends OncePerRequestFilter {

private static Log log = LogFactory.getLog(CorsFilter.class);

@Resource
CrossDomainWebManger crossDomainWebManger;

Expand All @@ -28,11 +33,23 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
// CORS "pre-flight" request
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
if(crossDomainWebManger.allowed(request)) {
response.addHeader("Access-Control-Allow-Headers", "X-Requested-With,Origin,Content-Type,Accept,x-csrf-token");
}else{
response.addHeader("Access-Control-Allow-Headers", "X-Requested-With,Origin,Content-Type, Accept");
}

boolean allowCrossDomain = false;

try {
allowCrossDomain = crossDomainWebManger.allowed(request);
} catch (URISyntaxException e) {
String origin = request.getHeader("origin");
String referer = request.getHeader("referer");
log.error("Unable to process your request due an invalid URI exception, please check your origin and request headers: origin = '" + origin + "' referer = '" + referer + "'" , e);
// Lets log the exception and assume cross domain call was rejected
}

if(allowCrossDomain) {
response.addHeader("Access-Control-Allow-Headers", "X-Requested-With,Origin,Content-Type,Accept,x-csrf-token");
} else {
response.addHeader("Access-Control-Allow-Headers", "X-Requested-With,Origin,Content-Type, Accept");
}
}
filterChain.doFilter(request, response);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.orcid.core.web.filters;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -10,6 +11,8 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.orcid.core.manager.impl.OrcidUrlManager;
import org.orcid.pojo.ajaxForm.PojoUtil;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -21,25 +24,34 @@

public class CorsFilterWeb extends OncePerRequestFilter {

private static Log log = LogFactory.getLog(CorsFilterWeb.class);

@Resource
CrossDomainWebManger crossDomainWebManger;

@Value("${org.orcid.core.baseUri}")
@Value("${org.orcid.core.web.filters}")
private String baseUri;

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (crossDomainWebManger.allowed(request)) {
String origin = request.getHeader("origin");
response.addHeader("Access-Control-Allow-Origin", origin);
response.addHeader("Access-Control-Allow-Credentials", "true");

if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
// CORS "pre-flight" request
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.addHeader("Access-Control-Allow-Headers", "X-Requested-With,Origin,Content-Type,Accept,Authorization,x-csrf-token,x-xsrf-token");
return;
try {
if (crossDomainWebManger.allowed(request)) {
String origin = request.getHeader("origin");
response.addHeader("Access-Control-Allow-Origin", origin);
response.addHeader("Access-Control-Allow-Credentials", "true");

if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
// CORS "pre-flight" request
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
response.addHeader("Access-Control-Allow-Headers", "X-Requested-With,Origin,Content-Type,Accept,Authorization,x-csrf-token,x-xsrf-token");
return;
}
}
} catch (URISyntaxException e) {
String origin = request.getHeader("origin");
String referer = request.getHeader("referer");
log.error("Unable to process your request due an invalid URI exception, please check your origin and request headers: origin = '" + origin + "' referer = '" + referer + "'" , e);
throw new ServletException("Unable to process your request due an invalid URI exception", e);
}

filterChain.doFilter(request, response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.orcid.core.web.filters;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -24,7 +26,7 @@ public class CrossDomainWebManger {

private List<String> domainsRegex;

public boolean allowed(HttpServletRequest request) throws MalformedURLException {
public boolean allowed(HttpServletRequest request) throws URISyntaxException {
String path = OrcidUrlManager.getPathWithoutContextPath(request);

// Check origin header
Expand All @@ -35,9 +37,10 @@ public boolean allowed(HttpServletRequest request) throws MalformedURLException
}
} else {
// Check referer header for localhost
if (!PojoUtil.isEmpty(request.getHeader("referer"))) {
URL netUrl = new URL(request.getHeader("referer"));
String domain = netUrl.getHost();
String referer = request.getHeader("referer");
if (!PojoUtil.isEmpty(referer)) {
URI uri = new URI(request.getHeader("referer"));
String domain = uri.getHost();
if (LOCALHOST.equals(domain)) {
return true;
}
Expand All @@ -52,12 +55,14 @@ public boolean allowed(HttpServletRequest request) throws MalformedURLException
return false;
}

public boolean validateDomain(String url) throws MalformedURLException {
URL netUrl = new URL(url);
String domain = netUrl.getHost();
for (String allowedDomain : getAllowedDomainsRegex()) {
if (domain.matches(allowedDomain)) {
return true;
public boolean validateDomain(String url) throws URISyntaxException {
URI uri = new URI(url);
String domain = uri.getHost();
if(domain != null) {
for (String allowedDomain : getAllowedDomainsRegex()) {
if (domain.matches(allowedDomain)) {
return true;
}
}
}
return false;
Expand All @@ -76,8 +81,7 @@ private List<String> getAllowedDomainsRegex() {
}

private String transformPatternIntoRegex(String domainPattern) {
String result = domainPattern.replace(".", "\\.");
return result;
return domainPattern.replace(".", "\\.");
}

public boolean validatePath(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.util.Map;

import javax.annotation.Resource;
Expand Down Expand Up @@ -40,7 +41,19 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
Map<String, String[]> parms = httpRequest.getParameterMap();

if (parms.containsKey("callback")) {
if(crossDomainWebManger.allowed(request)) {

boolean allowCrossDomain = false;

try {
allowCrossDomain = crossDomainWebManger.allowed(request);
} catch (URISyntaxException e) {
String origin = request.getHeader("origin");
String referer = request.getHeader("referer");
log.error("Unable to process your request due an invalid URI exception, please check your origin and request headers: origin = '" + origin + "' referer = '" + referer + "'" , e);
// Lets log the exception and assume this was rejected so it is not considered a JSONP call
}

if(allowCrossDomain) {
if (log.isDebugEnabled())
log.debug("Wrapping response with JSONP callback '" + parms.get("callback")[0] + "'");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.net.MalformedURLException;
import java.net.URISyntaxException;

import javax.annotation.Resource;

Expand All @@ -25,15 +26,15 @@ public class CrossDomainWebMangerTest {
CrossDomainWebManger crossDomainWebManger;

String [] allowedDomains = {"http://localhost", "https://localhost"};
String [] forbiddenDomains = {"http://.orcid.org", "http://www.otherorcid.org", "http://www.myorcid.org", "http://www.ihateorcid.org", "http://qa.ihateorcid.org", "https://.orcid.org", "https://www.otherorcid.org", "https://www.myorcid.org", "https://www.ihateorcid.org", "https://qa.ihateorcid.org"};
String [] forbiddenDomains = {"http://.orcid.org", "http://www.otherorcid.org", "http://www.myorcid.org", "http://www.testorcid.org", "http://qa.testorcid.org", "https://.orcid.org", "https://www.otherorcid.org", "https://www.myorcid.org", "https://www.testorcid.org", "https://qa.testorcid.org"};

String [] allowedPaths = {"/lang.json","/userStatus.json","/oauth/userinfo","/oauth/jwks","/.well-known/openid-configuration"};
String [] forbiddenPaths = {"/oauth","/whatever/oauth","/whatever/oauth/","/whatever/oauth/other",
"/whatever/userStatus.json","/userstatus.json","/userStatus.json/","/userStatus.json/whatever",
"/userStatus.jsonwhatever/test","/userStatus.json/whatever","/userStatus.jsonwhatever","/userStatus.jsonwhatever/test"};

@Test
public void testDomains() throws MalformedURLException {
public void testDomains() throws URISyntaxException {
for(String allowed : allowedDomains) {
assertTrue("testing: " + allowed, crossDomainWebManger.validateDomain(allowed));
}
Expand All @@ -44,7 +45,7 @@ public void testDomains() throws MalformedURLException {
}

@Test
public void testPaths() throws MalformedURLException {
public void testPaths() throws URISyntaxException {
for(String allowed : allowedPaths) {
assertTrue("testing: " + allowed, crossDomainWebManger.validatePath(allowed));
}
Expand Down