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

Allow wildcards in the service whitelist #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -23,6 +23,8 @@
*/
package com.bigdata.rdf.sparql.ast.service;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
Expand Down Expand Up @@ -410,13 +412,10 @@ public Iterator<CustomServiceFactory> customServices() {
*/
public ServiceFactory get(final URI serviceURI) {

if (serviceURI == null)
throw new IllegalArgumentException();

if (isWhitelistEnabled() && !serviceWhitelist.contains(serviceURI.stringValue())) {
throw new IllegalArgumentException("Service URI " + serviceURI + " is not allowed");
if (serviceURI == null) {
throw new IllegalArgumentException("Null service URI");
}

checkServiceUri(serviceURI);
final URI alias = aliases.get(serviceURI);

if (alias != null) {
Expand Down Expand Up @@ -465,14 +464,17 @@ public final ServiceCall<? extends Object> toServiceCall(

}

ServiceFactory f;
if (isWhitelistEnabled() && !serviceWhitelist.contains(serviceURI.stringValue())) {
ServiceFactory f = null;
try {
checkServiceUri(serviceURI);
} catch(IllegalArgumentException e) {
if (serviceNode.isSilent()) {
f = nullServiceFactory;
} else {
throw new IllegalArgumentException("Service URI " + serviceURI + " is not allowed");
throw e;
}
} else {
}
if (f == null) {
f = services.get(serviceURI);

if (f == null) {
Expand Down Expand Up @@ -507,11 +509,8 @@ public final ServiceCall<? extends Object> toServiceCall(
* as fallback
*/
public ServiceFactory getServiceFactoryByServiceURI(URI serviceUri) {

if (isWhitelistEnabled() && !serviceWhitelist.contains(serviceUri.stringValue())) {
throw new IllegalArgumentException("Service URI " + serviceUri + " is not allowed");
}

checkServiceUri(serviceUri);

final ServiceFactory serviceFactory =
serviceUri==null ?
getDefaultServiceFactory() : services.get(serviceUri);
Expand All @@ -520,6 +519,32 @@ public ServiceFactory getServiceFactoryByServiceURI(URI serviceUri) {

}

private void checkServiceUri(final URI serviceUri) {
if (!isWhitelistEnabled()) {
return;
}
if (serviceWhitelist.contains(serviceUri.stringValue())) {
return;
}
try {
URL url = new URL(serviceUri.toString());
URL serverWildcard = new URL(url.getProtocol(), url.getHost(), url.getPort(), "/");
if (serviceWhitelist.contains(serverWildcard.toString() + "*")) {
// Allow mask http://server.com/*
return;
}
URL pathWildcard = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath());
if (serviceWhitelist.contains(pathWildcard.toString() + '*')) {
// Allow mask http://server.com/path*
return;
}
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Service URI " + serviceUri + " is not well-formed");
}

throw new IllegalArgumentException("Service URI " + serviceUri + " is not allowed");
}

private static class ServiceCallCreateParamsImpl implements ServiceCallCreateParams {

private final URI serviceURI;
Expand Down