-
Notifications
You must be signed in to change notification settings - Fork 3
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
Adding domain check to mitigate SSRF attacks. #7
Draft
PasinduYeshan
wants to merge
2
commits into
wso2-extensions:main
Choose a base branch
from
PasinduYeshan:dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
package org.wso2.carbon.identity.conditional.auth.functions.entgra; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
|
@@ -47,6 +48,7 @@ | |
|
||
import java.io.IOException; | ||
import java.net.SocketTimeoutException; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
@@ -68,6 +70,8 @@ public class GetDeviceInfoEntgraFunctionImpl implements GetDeviceInfoEntgraFunct | |
|
||
private static final Log LOG = LogFactory.getLog(GetDeviceInfoEntgraFunctionImpl.class); | ||
private CloseableHttpClient client; | ||
private static final char DOMAIN_SEPARATOR = '.'; | ||
private final List<String> allowedDomains; | ||
|
||
public GetDeviceInfoEntgraFunctionImpl() { | ||
|
||
|
@@ -80,7 +84,7 @@ public GetDeviceInfoEntgraFunctionImpl() { | |
.setRelativeRedirectsAllowed(false) | ||
.build(); | ||
client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); | ||
|
||
allowedDomains = ConfigProvider.getInstance().getAllowedDomainsForHttpFunctions(); | ||
} | ||
|
||
@Override | ||
|
@@ -107,6 +111,17 @@ public void getDeviceInfoEntgra(JsAuthenticationContext context, String platform | |
try { | ||
HttpPost tokenRequest = getTokenRequest(tokenURL, clientKey, clientSecret); | ||
|
||
if (!isValidRequestDomain(tokenRequest.getURI())) { | ||
outcome = OUTCOME_FAIL; | ||
response = getErrorJsonObject(Constants.AuthResponseErrorCode.ACCESS_DENIED, | ||
"Access is denied. Please contact your administrator."); | ||
|
||
LOG.error("Provided Url does not contain a allowed domain. Invalid Url: " + | ||
tokenRequest.getURI().toString()); | ||
asyncReturn.accept(authenticationContext, response, outcome); | ||
return; | ||
} | ||
|
||
// For catching and logging errors. | ||
String errorURL = tokenURL; | ||
try (CloseableHttpResponse tResponse = client.execute(tokenRequest)) { | ||
|
@@ -117,10 +132,21 @@ public void getDeviceInfoEntgra(JsAuthenticationContext context, String platform | |
JSONParser parser = new JSONParser(); | ||
JSONObject jsonTokenResponse = (JSONObject) parser.parse(tJsonString); | ||
String accessToken = (String) jsonTokenResponse.get(Constants.ACCESS_TOKEN); | ||
tResponse.close(); | ||
|
||
HttpGet deviceInfoRequest = getDeviceInfoRequest(deviceInfoURL, accessToken); | ||
|
||
tResponse.close(); | ||
if (!isValidRequestDomain(deviceInfoRequest.getURI())) { | ||
outcome = OUTCOME_FAIL; | ||
response = getErrorJsonObject(Constants.AuthResponseErrorCode.ACCESS_DENIED, | ||
"Access is denied. Please contact your administrator."); | ||
|
||
LOG.error("Provided Url does not contain a allowed domain. Invalid Url: " + | ||
deviceInfoRequest.getURI().toString()); | ||
asyncReturn.accept(authenticationContext, response, outcome); | ||
return; | ||
} | ||
|
||
try (CloseableHttpResponse dResponse = client.execute(deviceInfoRequest)) { | ||
int dResponseCode = dResponse.getStatusLine().getStatusCode(); | ||
|
||
|
@@ -148,8 +174,11 @@ public void getDeviceInfoEntgra(JsAuthenticationContext context, String platform | |
Constants.AuthResponseErrorCode.DEVICE_NOT_ENROLLED_UNDER_CURRENT_USER, | ||
"Access is denied. Please contact your administrator."); | ||
} | ||
} else if (dResponseCode == 404) { | ||
outcome = OUTCOME_FAIL; | ||
response = getErrorJsonObject(Constants.AuthResponseErrorCode.DEVICE_NOT_ENROLLED, | ||
"Device is not recognized. Please register your device."); | ||
} else { | ||
|
||
LOG.error("Error while fetching device information from Entgra Server. " + | ||
"Response code: " + dResponseCode); | ||
outcome = OUTCOME_FAIL; | ||
|
@@ -158,14 +187,6 @@ public void getDeviceInfoEntgra(JsAuthenticationContext context, String platform | |
errorURL = deviceInfoURL; | ||
throw e; | ||
} | ||
|
||
} else if (tokenResponseCode == 404) { | ||
LOG.error("Error while requesting access token from Entgra Server. Response code: " | ||
+ tokenResponseCode); | ||
outcome = OUTCOME_FAIL; | ||
response = getErrorJsonObject(Constants.AuthResponseErrorCode.DEVICE_NOT_ENROLLED, | ||
"Device is not recognized. Please register your device."); | ||
|
||
} else { | ||
LOG.error("Error while requesting access token from Entgra Server. Response code: " | ||
+ tokenResponseCode); | ||
|
@@ -266,4 +287,62 @@ private JSONObject getErrorJsonObject(Constants.AuthResponseErrorCode errorCode, | |
errorMap.put("errorMessage", errorMessage); | ||
return errorMap; | ||
} | ||
|
||
/** | ||
* Return true if the request domain is valid or return false if the request domain is invalid. | ||
* @param url | ||
* @return validity | ||
*/ | ||
private boolean isValidRequestDomain(URI url) { | ||
|
||
if (url == null) { | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("Provided url for domain restriction checking is null"); | ||
} | ||
return false; | ||
} | ||
|
||
if (allowedDomains.isEmpty()) { | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("No domains configured for domain restriction. Allowing url by default. Url: " | ||
+ url.toString()); | ||
} | ||
return true; | ||
} | ||
|
||
String domain = getParentDomainFromUrl(url); | ||
if (StringUtils.isEmpty(domain)) { | ||
LOG.error("Unable to determine the domain of the url: " + url.toString()); | ||
return false; | ||
} | ||
|
||
if (allowedDomains.contains(domain)) { | ||
return true; | ||
} | ||
|
||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("Domain: " + domain + " extracted from url: " + url.toString() + " is not available in the " + | ||
"allowed domain list: " + StringUtils.join(allowedDomains, ',')); | ||
} | ||
return false; | ||
} | ||
|
||
private String getParentDomainFromUrl(URI url) { | ||
|
||
String parentDomain = null; | ||
String domain = url.getHost(); | ||
String[] domainArr; | ||
if (domain != null) { | ||
domainArr = StringUtils.split(domain, DOMAIN_SEPARATOR); | ||
if (domainArr.length != 0) { | ||
parentDomain = domainArr.length == 1 ? domainArr[0] : domainArr[domainArr.length - 2]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parent domain should be the joined string of the last two elements. |
||
parentDomain = parentDomain.toLowerCase(); | ||
} | ||
} | ||
|
||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("Parent domain: " + parentDomain + " extracted from url: " + url.toString()); | ||
} | ||
return parentDomain; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Isn't there any Framework util method for this?
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.
I got this from the http conditional auth function. It was implemented as a private method and I didn't find any util method for this.