diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d43e3499e..e452523987 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +## [Unreleased] +### Added +- A query federates across the dataspaces of one deployment: `SERVICE ` names another dataspace by its public endpoint, the platform authorizes the agent for that endpoint as a `GET` of it would be (public read, or `acl:Read` in that dataspace's admin store) and rewrites the clause to the dataspace's store, where the triplestore reaches it directly (`ServiceRewriter`); a dataspace the agent may not read fails the whole query with `403`, a dataspace named by anything but its endpoint, a variable target, or any other internal or loopback address with `400`. Public `SERVICE` targets go through the egress proxy as before + +### Changed +- A `SERVICE` clause naming an internal address answers `400` instead of the empty result `SILENT` gave it: the platform decides before the query goes out, and `ALLOW_INTERNAL_URLS` does not open it (the platform's own addresses are matched as dataspaces first; what is left is a store or a service of the deployment) +- The access control decision is `DataspaceAuthorization`, made for any dataspace of the deployment; `AuthorizationFilter` makes it for the request's +- Fuseki's JVM bypasses the egress proxy for the host `fuseki` alone (`http.nonProxyHosts`), so a rewritten `SERVICE` reaches a sibling dataset; every other internal target is refused by the platform first + ## [6.5.2] - 2026-11-12 ### Fixed - The `ldh` plugin's app skill holds no shell interpreter line, which the plugin directory reads as an inline shell snippet diff --git a/docker-compose.yml b/docker-compose.yml index 6e9208a637..0c5af0500a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -137,13 +137,17 @@ services: - egress environment: - JAVA_OPTIONS=-Xmx2304m -Xms2304m # the former heaps combined: 768m admin + 1536m end-user - # every outbound request (SPARQL SERVICE, LOAD) goes through egress, which only lets it reach public addresses. - # JAVA_TOOL_OPTIONS rather than JAVA_OPTIONS, which an override setting the heap would replace. The empty - # nonProxyHosts is load-bearing: by default the JVM bypasses the proxy for localhost, and - # SERVICE would read a sibling dataset in this same server. + # every outbound request (SPARQL SERVICE, LOAD) goes through egress, which only lets it reach public addresses, + # except one to this server itself: a query federating to another dataspace of the deployment reaches that + # dataspace's dataset here, under the address the platform rewrote its SERVICE clause to after authorizing the + # agent for the dataspace's endpoint (ServiceRewriter). The platform is the only client that sends queries here + # and refuses every other internal SERVICE target - localhost included, which the JVM would otherwise exempt + # from the proxy, and with ALLOW_INTERNAL_URLS too - before a query goes out, so the exception reads no dataset + # the agent may not read. + # JAVA_TOOL_OPTIONS rather than JAVA_OPTIONS, which an override setting the heap would replace. # ExitOnOutOfMemoryError: a query that exhausts the heap kills the JVM and restart: on-failure brings it back in # seconds - the query timeout bounds time, not memory, and a JVM past an OutOfMemoryError keeps running broken - - JAVA_TOOL_OPTIONS=-Dhttp.proxyHost=${EGRESS_PROXY_HOST:-egress} -Dhttp.proxyPort=3128 -Dhttps.proxyHost=${EGRESS_PROXY_HOST:-egress} -Dhttps.proxyPort=3128 -Dhttp.nonProxyHosts= -XX:+ExitOnOutOfMemoryError + - JAVA_TOOL_OPTIONS=-Dhttp.proxyHost=${EGRESS_PROXY_HOST:-egress} -Dhttp.proxyPort=3128 -Dhttps.proxyHost=${EGRESS_PROXY_HOST:-egress} -Dhttps.proxyPort=3128 -Dhttp.nonProxyHosts=fuseki -XX:+ExitOnOutOfMemoryError expose: - 3030 volumes: diff --git a/formal/acl/README.md b/formal/acl/README.md index 732250e58e..ba028daeb5 100644 --- a/formal/acl/README.md +++ b/formal/acl/README.md @@ -72,7 +72,7 @@ formal/acl/tracks-code.sh origin/develop | `Auth.principal` | `aclQuery`'s agent branches, `AuthorizationParams.get` (`server/util/`) | | `Auth.matchRow`, `Auth.covers` | `aclQuery`'s target branches over the injected `VALUES (?Rel ?Target)` | | `Reader`, `authIn`, `memberIn` | `aclQuery`'s `?g foaf:primaryTopic ?auth` and `?groupG foaf:primaryTopic ?Group` | -| `grant`, `allow` | `AuthorizationFilter.authorize`, including the `PUT` fallback to the parent | +| `grant`, `allow` | `DataspaceAuthorization.authorize` (`server/security/`), the decision for a resource of any dataspace, which `AuthorizationFilter.authorize` makes for the request's dataspace, including the `PUT` fallback to the parent | | `allowRequest` | `AuthorizationFilter.filter`, `getAccessMode` | | `EndUserOnly` (class part) | the `adm:HierarchyClassAccess` constraint on `acl:Authorization` (`app/admin/adm.ttl`) | | `ownerAccess` | `acl/authorizations/owner-access/` (`platform/namespace-ontology.trig.template`) | @@ -85,6 +85,12 @@ formal/acl/tracks-code.sh origin/develop `AuthorizationFilter` lives in `server/filter/request/`, under `src/main/java/com/atomgraph/linkeddatahub/`. +Since the federation of dataspaces (`ServiceRewriter`, `server/util/`), the decision is also made for a dataspace other +than the request's: a `SERVICE` clause naming a dataspace's endpoint is allowed when `grant` holds for the agent, the +endpoint and `acl:Read` in that dataspace, as a `GET` of the endpoint would be. The model is unchanged by it: the +decision's inputs and rule are the same, only the dataspace they are read from is a parameter now, which is why the +code moved from `AuthorizationFilter` to `DataspaceAuthorization` without a change to the definitions here. + ## Results Proven with no `sorry`, using only the axioms `propext`, `Quot.sound` and, through case splits on presence, diff --git a/formal/acl/tracks-code.sh b/formal/acl/tracks-code.sh index ef81a18632..6e2f5386b0 100755 --- a/formal/acl/tracks-code.sh +++ b/formal/acl/tracks-code.sh @@ -21,6 +21,7 @@ sources=( src/main/java/com/atomgraph/linkeddatahub/server/util/DocumentContext.java src/main/java/com/atomgraph/linkeddatahub/server/util/AuthorizationParams.java src/main/java/com/atomgraph/linkeddatahub/server/filter/request/AuthorizationFilter.java + src/main/java/com/atomgraph/linkeddatahub/server/security/DataspaceAuthorization.java src/main/java/com/atomgraph/linkeddatahub/server/mapper/auth/AuthorizationExceptionMapper.java src/main/resources/com/atomgraph/linkeddatahub/app/admin/adm.ttl platform/namespace-ontology.trig.template diff --git a/formal/uploads/README.md b/formal/uploads/README.md index ae876c2e99..88745bd42d 100644 --- a/formal/uploads/README.md +++ b/formal/uploads/README.md @@ -71,7 +71,7 @@ the model. | `Records`, `recordOne`, `record`, `respondR` | `UploadRecords.record`, as `commitFiles` calls it with the request's document | | `describe` | web.xml `fileQuery` (`DESCRIBE $this`), as `Item.describe()` runs it | | `uploadContext` | web.xml `documentContextQuery`: the `acl:accessTo` branch and the `nfo:FileDataObject` one | -| `uploadReadable` | `AuthorizationFilter.authorize`, then `authorizeUpload` for a read that the upload's own context does not grant | +| `uploadReadable` | `AuthorizationFilter.authorize` (the decision itself is `DataspaceAuthorization.authorize`, `server/security/`, since the federation of dataspaces; the filter's part and the model are unchanged by the move), then `authorizeUpload` for a read that the upload's own context does not grant | | `serve`, `addressTag`, `recordedFormat` | `Item.getResponseBuilder`, `Item.getEntityTag`, `Item.getMediaType` | | `describedTag`, `describedFormat` | `Item.getEntityTag` and `Item.getMediaType` before this model: `getRequiredProperty(FOAF.sha1)`, `getPropertyResourceValue(DCTerms.format)` | | `select`, `Spec` | `ByteRange.parse` | diff --git a/src/main/java/com/atomgraph/linkeddatahub/server/filter/request/AuthorizationFilter.java b/src/main/java/com/atomgraph/linkeddatahub/server/filter/request/AuthorizationFilter.java index 816d5fa504..ae589a2f6b 100644 --- a/src/main/java/com/atomgraph/linkeddatahub/server/filter/request/AuthorizationFilter.java +++ b/src/main/java/com/atomgraph/linkeddatahub/server/filter/request/AuthorizationFilter.java @@ -16,21 +16,17 @@ */ package com.atomgraph.linkeddatahub.server.filter.request; -import com.atomgraph.linkeddatahub.dataspaces.model.EndUserDataspace; -import com.atomgraph.linkeddatahub.client.SesameProtocolClient; import com.atomgraph.linkeddatahub.server.exception.auth.AuthorizationException; import com.atomgraph.linkeddatahub.model.auth.Agent; import com.atomgraph.linkeddatahub.model.Service; -import com.atomgraph.linkeddatahub.server.security.AccessModes; import com.atomgraph.linkeddatahub.server.security.AuthorizationContext; -import com.atomgraph.linkeddatahub.server.util.AuthorizationParams; +import com.atomgraph.linkeddatahub.server.security.DataspaceAuthorization; import com.atomgraph.linkeddatahub.server.util.DocumentContext; import com.atomgraph.linkeddatahub.server.util.UploadRecords; import com.atomgraph.linkeddatahub.server.model.impl.DocumentHierarchyGraphStoreImpl; import com.atomgraph.linkeddatahub.vocabulary.ACL; import com.atomgraph.linkeddatahub.vocabulary.DH; import com.atomgraph.linkeddatahub.vocabulary.Default; -import com.atomgraph.spinrdf.vocabulary.SPIN; import java.io.IOException; import java.net.URI; import java.nio.file.Path; @@ -49,12 +45,10 @@ import jakarta.ws.rs.container.PreMatching; import jakarta.ws.rs.core.HttpHeaders; import jakarta.ws.rs.core.PathSegment; -import jakarta.ws.rs.core.Response; import org.apache.jena.irix.IRIx; import java.util.Set; import org.apache.jena.query.ParameterizedSparqlString; import org.apache.jena.query.QuerySolutionMap; -import org.apache.jena.query.ResultSet; import org.apache.jena.query.ResultSetRewindable; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.Resource; @@ -89,7 +83,7 @@ public class AuthorizationFilter implements ContainerRequestFilter @Inject jakarta.inject.Provider> app; @Inject jakarta.inject.Provider> dataset; - private ParameterizedSparqlString documentContextQuery, aclQuery; + private DataspaceAuthorization authorization; /** * Post-construct initialization. @@ -97,8 +91,7 @@ public class AuthorizationFilter implements ContainerRequestFilter @PostConstruct public void init() { - documentContextQuery = new ParameterizedSparqlString(system.getDocumentContextQuery().toString()); - aclQuery = new ParameterizedSparqlString(getSystem().getACLQuery().toString()); + authorization = new DataspaceAuthorization(getSystem()); } @Override @@ -243,13 +236,7 @@ public Model authorizeUpload(ContainerRequestContext request, Resource agent) */ public Model authorize(Resource accessTo, DocumentContext context, Resource agent, Resource accessMode) { - ParameterizedSparqlString pss = new ParameterizedSparqlString(context.applyTo(getACLQuery().asQuery()).toString()); - Model authorizations = loadModel(getAdminService(), pss, new AuthorizationParams(getAdminBase(), accessTo, agent, context.isOwner(agent)).get()); - - // access denied if the agent has no authorization to the requested document with the requested ACL mode - if (getAuthorizationByMode(authorizations, accessMode) == null) return null; - - return authorizations; + return getAuthorization().authorize(getDataspace().get(), accessTo, context, agent, accessMode); } /** @@ -260,18 +247,7 @@ public Model authorize(Resource accessTo, DocumentContext context, Resource agen */ public DocumentContext getDocumentContext(Resource document) { - QuerySolutionMap qsm = new QuerySolutionMap(); - qsm.add(SPIN.THIS_VAR_NAME, document); - - ResultSetRewindable result = loadResultSet(getDataspace().get().getService(), getDocumentContextQuery(), qsm); - try - { - return new DocumentContext(result); - } - finally - { - result.close(); - } + return getAuthorization().getDocumentContext(getDataspace().get(), document); } /** @@ -305,10 +281,7 @@ public static Resource getAccessMode(String method, String ifNoneMatch) */ public Resource getAuthorizationByMode(Model authModel, Resource accessMode) { - return AccessModes.covering(accessMode).stream(). - flatMap(mode -> authModel.listResourcesWithProperty(ACL.mode, mode).toList().stream()). - findFirst(). - orElse(null); + return DataspaceAuthorization.getAuthorizationByMode(authModel, accessMode); } /** @@ -322,26 +295,7 @@ public Resource getAuthorizationByMode(Model authModel, Resource accessMode) */ protected Model loadModel(com.atomgraph.linkeddatahub.model.Service service, ParameterizedSparqlString pss, QuerySolutionMap qsm) { - if (service == null) throw new IllegalArgumentException("Service cannot be null"); - if (pss == null) throw new IllegalArgumentException("ParameterizedSparqlString cannot be null"); - if (qsm == null) throw new IllegalArgumentException("QuerySolutionMap cannot be null"); - - // send query bindings separately from the query if the service supports the Sesame protocol - com.atomgraph.linkeddatahub.model.ServiceContext serviceContext = getSystem().getServiceContext(service); - if (serviceContext.getSPARQLClient() instanceof SesameProtocolClient sesameProtocolClient) - try (Response cr = sesameProtocolClient.query(pss.asQuery(), Model.class, qsm)) // register(new CacheControlFilter(CacheControl.valueOf("no-cache"))). // add Cache-Control: no-cache to request - { - return cr.readEntity(Model.class); - } - else - { - pss.setParams(qsm); - try (Response cr = serviceContext.getSPARQLClient(). // register(new CacheControlFilter(CacheControl.valueOf("no-cache"))). // add Cache-Control: no-cache to request - query(pss.asQuery(), Model.class)) - { - return cr.readEntity(Model.class); - } - } + return getAuthorization().loadModel(service, pss, qsm); } /** @@ -355,28 +309,9 @@ protected Model loadModel(com.atomgraph.linkeddatahub.model.Service service, Par */ protected ResultSetRewindable loadResultSet(com.atomgraph.linkeddatahub.model.Service service, ParameterizedSparqlString pss, QuerySolutionMap qsm) { - if (service == null) throw new IllegalArgumentException("Service cannot be null"); - if (pss == null) throw new IllegalArgumentException("ParameterizedSparqlString cannot be null"); - if (qsm == null) throw new IllegalArgumentException("QuerySolutionMap cannot be null"); - - // send query bindings separately from the query if the service supports the Sesame protocol - com.atomgraph.linkeddatahub.model.ServiceContext serviceContext = getSystem().getServiceContext(service); - if (serviceContext.getSPARQLClient() instanceof SesameProtocolClient sesameProtocolClient) - try (Response cr = sesameProtocolClient.query(pss.asQuery(), ResultSet.class, qsm)) // register(new CacheControlFilter(CacheControl.valueOf("no-cache"))). // add Cache-Control: no-cache to request - { - return cr.readEntity(ResultSetRewindable.class); - } - else - { - pss.setParams(qsm); - try (Response cr = serviceContext.getSPARQLClient(). // register(new CacheControlFilter(CacheControl.valueOf("no-cache"))). // add Cache-Control: no-cache to request - query(pss.asQuery(), ResultSet.class)) - { - return cr.readEntity(ResultSetRewindable.class); - } - } + return getAuthorization().loadResultSet(service, pss, qsm); } - + /** * Returns the SPARQL service for agent data. * @@ -384,9 +319,7 @@ protected ResultSetRewindable loadResultSet(com.atomgraph.linkeddatahub.model.Se */ protected Service getAdminService() { - return getDataspace().get().canAs(EndUserDataspace.class) ? - getDataspace().get().as(EndUserDataspace.class).getAdminDataspace().getService() : - getDataspace().get().getService(); + return DataspaceAuthorization.getAdminService(getDataspace().get()); } /** @@ -397,9 +330,17 @@ protected Service getAdminService() */ protected Resource getAdminBase() { - return getDataspace().get().canAs(EndUserDataspace.class) ? - getDataspace().get().as(EndUserDataspace.class).getAdminDataspace().getBase() : - getDataspace().get().getBase(); + return DataspaceAuthorization.getAdminBase(getDataspace().get()); + } + + /** + * Returns the access control decision this filter makes for the request's dataspace. + * + * @return authorization + */ + public DataspaceAuthorization getAuthorization() + { + return authorization; } /** @@ -439,18 +380,18 @@ public com.atomgraph.linkeddatahub.Application getSystem() */ public ParameterizedSparqlString getDocumentContextQuery() { - return documentContextQuery.copy(); + return getAuthorization().getDocumentContextQuery(); } - + /** * Returns authorization query. * Used on the documents of either dataspace. - * + * * @return SPARQL string */ public ParameterizedSparqlString getACLQuery() { - return aclQuery.copy(); + return getAuthorization().getACLQuery(); } } \ No newline at end of file diff --git a/src/main/java/com/atomgraph/linkeddatahub/server/model/impl/SPARQLEndpointImpl.java b/src/main/java/com/atomgraph/linkeddatahub/server/model/impl/SPARQLEndpointImpl.java index b078ffd771..b25c26674c 100644 --- a/src/main/java/com/atomgraph/linkeddatahub/server/model/impl/SPARQLEndpointImpl.java +++ b/src/main/java/com/atomgraph/linkeddatahub/server/model/impl/SPARQLEndpointImpl.java @@ -21,11 +21,15 @@ import com.atomgraph.core.util.ModelUtils; import com.atomgraph.core.vocabulary.SD; import com.atomgraph.linkeddatahub.model.Service; +import com.atomgraph.linkeddatahub.model.auth.Agent; +import com.atomgraph.linkeddatahub.server.security.DataspaceAuthorization; +import com.atomgraph.linkeddatahub.server.util.ServiceRewriter; import java.net.URI; import java.util.List; import java.util.Optional; import jakarta.inject.Inject; import jakarta.ws.rs.core.HttpHeaders; +import jakarta.ws.rs.core.SecurityContext; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.FormParam; import jakarta.ws.rs.HttpMethod; @@ -37,6 +41,7 @@ import jakarta.ws.rs.core.Request; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.UriInfo; +import org.apache.jena.query.Query; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.Resource; import org.apache.jena.update.UpdateRequest; @@ -44,12 +49,17 @@ /** * LinkedDataHub SPARQL endpoint implementation. * We need to subclass the Core class because we're injecting an optional Service. + * A query's SERVICE clauses are decided for the agent before the store runs it + * ({@link ServiceRewriter}): the dataspaces of this deployment it names are authorized and reached at their stores, + * other targets are validated as remote. * * @author Martynas Jusevičius {@literal } */ public class SPARQLEndpointImpl extends com.atomgraph.core.model.impl.SPARQLEndpointImpl { + private final ServiceRewriter serviceRewriter; + /** * Constructs endpoint. * @@ -58,15 +68,57 @@ public class SPARQLEndpointImpl extends com.atomgraph.core.model.impl.SPARQLEndp * @param uriInfo current request URI information * @param service SPARQL service * @param mediaTypes registry of readable/writable media types + * @param securityContext security context, holding the agent if authenticated * @param system system application */ @Inject public SPARQLEndpointImpl(@Context Request request, @Context HttpHeaders httpHeaders, @Context UriInfo uriInfo, Optional service, MediaTypes mediaTypes, - com.atomgraph.linkeddatahub.Application system) + @Context SecurityContext securityContext, com.atomgraph.linkeddatahub.Application system) { super(request, httpHeaders, uriInfo, system.getServiceContext(service.get()).getQueryEndpointAccessor(system.getMaxQueryResponseLength()), mediaTypes); + this.serviceRewriter = new ServiceRewriter(uri -> dataspaceOf(system, uri), new DataspaceAuthorization(system), system.getURLValidator(), + securityContext.getUserPrincipal() instanceof Agent agent ? agent : null); } - + + /** + * Returns the dataspace of this deployment a URI belongs to, or null if none does. + * + * @param system system application + * @param uri URI + * @return dataspace or null + */ + protected static com.atomgraph.linkeddatahub.dataspaces.model.Dataspace dataspaceOf(com.atomgraph.linkeddatahub.Application system, URI uri) + { + Resource app = system.matchApp(uri); + if (app == null || !app.canAs(com.atomgraph.linkeddatahub.dataspaces.model.Dataspace.class)) return null; + + return app.as(com.atomgraph.linkeddatahub.dataspaces.model.Dataspace.class); + } + + /** + * Runs the query with its SERVICE targets decided for the agent. + * + * @param query query as the client sent it + * @param defaultGraphUris default graph URIs + * @param namedGraphUris named graph URIs + * @return response builder + */ + @Override + public Response.ResponseBuilder getResponseBuilder(Query query, List defaultGraphUris, List namedGraphUris) + { + return super.getResponseBuilder(getServiceRewriter().rewrite(query), defaultGraphUris, namedGraphUris); + } + + /** + * Returns the rewriter of the query's SERVICE clauses. + * + * @return rewriter + */ + public ServiceRewriter getServiceRewriter() + { + return serviceRewriter; + } + /** * Returns the query languages this endpoint supports. * Only queries are advertised: writes go through the Graph Store Protocol. diff --git a/src/main/java/com/atomgraph/linkeddatahub/server/security/DataspaceAuthorization.java b/src/main/java/com/atomgraph/linkeddatahub/server/security/DataspaceAuthorization.java new file mode 100644 index 0000000000..e266a5eb6b --- /dev/null +++ b/src/main/java/com/atomgraph/linkeddatahub/server/security/DataspaceAuthorization.java @@ -0,0 +1,273 @@ +/** + * Copyright 2026 Martynas Jusevičius + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.atomgraph.linkeddatahub.server.security; + +import com.atomgraph.linkeddatahub.client.SesameProtocolClient; +import com.atomgraph.linkeddatahub.dataspaces.model.Dataspace; +import com.atomgraph.linkeddatahub.dataspaces.model.EndUserDataspace; +import com.atomgraph.linkeddatahub.model.Service; +import com.atomgraph.linkeddatahub.server.util.AuthorizationParams; +import com.atomgraph.linkeddatahub.server.util.DocumentContext; +import com.atomgraph.linkeddatahub.vocabulary.ACL; +import com.atomgraph.spinrdf.vocabulary.SPIN; +import jakarta.ws.rs.core.Response; +import org.apache.jena.query.ParameterizedSparqlString; +import org.apache.jena.query.QuerySolutionMap; +import org.apache.jena.query.ResultSet; +import org.apache.jena.query.ResultSetRewindable; +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.Resource; + +/** + * The access control decision for a resource of any dataspace of this deployment. + * One decision serves both dataspaces of a pair: the document context query reads what the dataspace's store says of + * the resource - itself, its ancestors, its types and its owner, each from the resource's own graph - and the ACL + * query matches the authorizations in the pair's admin store against it. The + * {@link com.atomgraph.linkeddatahub.server.filter.request.AuthorizationFilter} decides this way for the request's + * dataspace; a query that federates to another dataspace of this deployment + * ({@link com.atomgraph.linkeddatahub.server.util.ServiceRewriter}) decides it for the dataspace each + * SERVICE clause names, since the store the clause reaches holds no notion of the agent. + * + * @author Martynas Jusevičius {@literal } + */ +public class DataspaceAuthorization +{ + + private final com.atomgraph.linkeddatahub.Application system; + private final ParameterizedSparqlString documentContextQuery, aclQuery; + + /** + * Constructs the decision over the system's queries. + * + * @param system system application + */ + public DataspaceAuthorization(com.atomgraph.linkeddatahub.Application system) + { + if (system == null) throw new IllegalArgumentException("Application cannot be null"); + + this.system = system; + documentContextQuery = new ParameterizedSparqlString(system.getDocumentContextQuery().toString()); + aclQuery = new ParameterizedSparqlString(system.getACLQuery().toString()); + } + + /** + * Returns the agent's authorizations granting the access mode on a resource of a dataspace, or null + * if none does. + * + * @param dataspace the dataspace the resource belongs to + * @param accessTo resource URI + * @param agent agent resource or null + * @param accessMode ACL access mode + * @return authorizations model or null if access denied + */ + public Model authorize(Dataspace dataspace, Resource accessTo, Resource agent, Resource accessMode) + { + return authorize(dataspace, accessTo, getDocumentContext(dataspace, accessTo), agent, accessMode); + } + + /** + * Returns the agent's authorizations granting the access mode on a resource of a dataspace, given what the + * dataspace's store says of it, or null if none does. + * The authorizations of every mode the agent holds are returned, so a caller can tell every mode the agent has. + * + * @param dataspace the dataspace the resource belongs to + * @param accessTo resource URI + * @param context what the dataspace's store says of the resource + * @param agent agent resource or null + * @param accessMode ACL access mode + * @return authorizations model or null if access denied + */ + public Model authorize(Dataspace dataspace, Resource accessTo, DocumentContext context, Resource agent, Resource accessMode) + { + if (dataspace == null) throw new IllegalArgumentException("Dataspace cannot be null"); + if (accessTo == null) throw new IllegalArgumentException("Resource cannot be null"); + if (context == null) throw new IllegalArgumentException("DocumentContext cannot be null"); + if (accessMode == null) throw new IllegalArgumentException("Access mode cannot be null"); + + ParameterizedSparqlString pss = new ParameterizedSparqlString(context.applyTo(getACLQuery().asQuery()).toString()); + Model authorizations = loadModel(getAdminService(dataspace), pss, new AuthorizationParams(getAdminBase(dataspace), accessTo, agent, context.isOwner(agent)).get()); + + // access denied if the agent has no authorization to the requested document with the requested ACL mode + if (getAuthorizationByMode(authorizations, accessMode) == null) return null; + + return authorizations; + } + + /** + * Returns what a dataspace's store says of a document, for the ACL query to match authorizations against. + * + * @param dataspace the dataspace the document belongs to + * @param document document URI + * @return document context + */ + public DocumentContext getDocumentContext(Dataspace dataspace, Resource document) + { + if (dataspace == null) throw new IllegalArgumentException("Dataspace cannot be null"); + if (document == null) throw new IllegalArgumentException("Resource cannot be null"); + + QuerySolutionMap qsm = new QuerySolutionMap(); + qsm.add(SPIN.THIS_VAR_NAME, document); + + ResultSetRewindable result = loadResultSet(dataspace.getService(), getDocumentContextQuery(), qsm); + try + { + return new DocumentContext(result); + } + finally + { + result.close(); + } + } + + /** + * Returns an authorization that grants the access mode, or null if none in the model does. + * A grant of a mode covers the modes it covers ({@link AccessModes#covering(Resource)}). + * + * @param authModel model with authorizations + * @param accessMode ACL access mode + * @return authorization resource or null + */ + public static Resource getAuthorizationByMode(Model authModel, Resource accessMode) + { + return AccessModes.covering(accessMode).stream(). + flatMap(mode -> authModel.listResourcesWithProperty(ACL.mode, mode).toList().stream()). + findFirst(). + orElse(null); + } + + /** + * Returns the SPARQL service holding a dataspace's authorizations: its admin dataspace's, or its own when it is + * the admin dataspace. + * + * @param dataspace dataspace + * @return service resource + */ + public static Service getAdminService(Dataspace dataspace) + { + return dataspace.canAs(EndUserDataspace.class) ? + dataspace.as(EndUserDataspace.class).getAdminDataspace().getService() : + dataspace.getService(); + } + + /** + * Returns the base URI of the dataspace holding a dataspace's authorizations. + * + * @param dataspace dataspace + * @return admin dataspace's base URI + */ + public static Resource getAdminBase(Dataspace dataspace) + { + return dataspace.canAs(EndUserDataspace.class) ? + dataspace.as(EndUserDataspace.class).getAdminDataspace().getBase() : + dataspace.getBase(); + } + + /** + * Loads RDF graph from a service. + * + * @param service SPARQL service + * @param pss auth query string + * @param qsm query solution map (applied to the query string or sent as request params, depending on the protocol) + * @return authorization graph (can be empty) + * @see com.atomgraph.linkeddatahub.vocabulary.LDHC#aclQuery + */ + public Model loadModel(Service service, ParameterizedSparqlString pss, QuerySolutionMap qsm) + { + if (service == null) throw new IllegalArgumentException("Service cannot be null"); + if (pss == null) throw new IllegalArgumentException("ParameterizedSparqlString cannot be null"); + if (qsm == null) throw new IllegalArgumentException("QuerySolutionMap cannot be null"); + + // send query bindings separately from the query if the service supports the Sesame protocol + com.atomgraph.linkeddatahub.model.ServiceContext serviceContext = getSystem().getServiceContext(service); + if (serviceContext.getSPARQLClient() instanceof SesameProtocolClient sesameProtocolClient) + try (Response cr = sesameProtocolClient.query(pss.asQuery(), Model.class, qsm)) + { + return cr.readEntity(Model.class); + } + else + { + pss.setParams(qsm); + try (Response cr = serviceContext.getSPARQLClient().query(pss.asQuery(), Model.class)) + { + return cr.readEntity(Model.class); + } + } + } + + /** + * Loads SPARQL result set from a service. + * + * @param service SPARQL service + * @param pss query string + * @param qsm query solution map (applied to the query string or sent as request params, depending on the protocol) + * @return result set + */ + public ResultSetRewindable loadResultSet(Service service, ParameterizedSparqlString pss, QuerySolutionMap qsm) + { + if (service == null) throw new IllegalArgumentException("Service cannot be null"); + if (pss == null) throw new IllegalArgumentException("ParameterizedSparqlString cannot be null"); + if (qsm == null) throw new IllegalArgumentException("QuerySolutionMap cannot be null"); + + // send query bindings separately from the query if the service supports the Sesame protocol + com.atomgraph.linkeddatahub.model.ServiceContext serviceContext = getSystem().getServiceContext(service); + if (serviceContext.getSPARQLClient() instanceof SesameProtocolClient sesameProtocolClient) + try (Response cr = sesameProtocolClient.query(pss.asQuery(), ResultSet.class, qsm)) + { + return cr.readEntity(ResultSetRewindable.class); + } + else + { + pss.setParams(qsm); + try (Response cr = serviceContext.getSPARQLClient().query(pss.asQuery(), ResultSet.class)) + { + return cr.readEntity(ResultSetRewindable.class); + } + } + } + + /** + * Returns system application. + * + * @return JAX-RS application + */ + public com.atomgraph.linkeddatahub.Application getSystem() + { + return system; + } + + /** + * Returns a query that loads what a dataspace's store says of a document. + * + * @return SPARQL string + */ + public ParameterizedSparqlString getDocumentContextQuery() + { + return documentContextQuery.copy(); + } + + /** + * Returns authorization query. + * Used on the documents of either dataspace. + * + * @return SPARQL string + */ + public ParameterizedSparqlString getACLQuery() + { + return aclQuery.copy(); + } + +} diff --git a/src/main/java/com/atomgraph/linkeddatahub/server/util/ServiceRewriter.java b/src/main/java/com/atomgraph/linkeddatahub/server/util/ServiceRewriter.java new file mode 100644 index 0000000000..15c941caaa --- /dev/null +++ b/src/main/java/com/atomgraph/linkeddatahub/server/util/ServiceRewriter.java @@ -0,0 +1,200 @@ +/** + * Copyright 2026 Martynas Jusevičius + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.atomgraph.linkeddatahub.server.util; + +import com.atomgraph.linkeddatahub.dataspaces.model.Dataspace; +import com.atomgraph.linkeddatahub.model.auth.Agent; +import com.atomgraph.linkeddatahub.server.exception.auth.AuthorizationException; +import com.atomgraph.linkeddatahub.server.security.DataspaceAuthorization; +import com.atomgraph.linkeddatahub.vocabulary.ACL; +import jakarta.ws.rs.BadRequestException; +import java.net.URI; +import java.util.function.Function; +import org.apache.jena.graph.Node; +import org.apache.jena.graph.NodeFactory; +import org.apache.jena.query.Query; +import org.apache.jena.rdf.model.ResourceFactory; +import org.apache.jena.sparql.syntax.Element; +import org.apache.jena.sparql.syntax.ElementService; +import org.apache.jena.sparql.syntax.syntaxtransform.ElementTransform; +import org.apache.jena.sparql.syntax.syntaxtransform.ElementTransformCopyBase; +import org.apache.jena.sparql.syntax.syntaxtransform.ExprTransformApplyElementTransform; +import org.apache.jena.sparql.syntax.syntaxtransform.QueryTransformOps; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Rewrites the SERVICE clauses of a query before the store runs it, so that a query federates across the + * dataspaces of this deployment under the agent's own access rights. + *

+ * A query names another dataspace by its public SPARQL endpoint, SERVICE <https://people.example/sparql>, + * the address a person would also query directly. The store that runs the query knows no agent, so left as it is the + * clause would either fail (the store reaches public addresses only, through the egress proxy) or, pointed at the + * sibling dataset's own address, read what the ACL never granted. So the platform decides for each clause before the + * query goes out: + *

    + *
  • a target that is a dataspace of this deployment is authorized for the agent as a GET of that + * endpoint would be - public read, or acl:Read on the endpoint in that dataspace's admin store - and + * the target becomes the dataspace's store, the address the platform itself queries it at; a refusal is a + * 403 naming the endpoint, for the whole query;
  • + *
  • a dataspace named by anything but its SPARQL endpoint is refused with 400: documents are read + * through the endpoint's query, not reached by SERVICE;
  • + *
  • any other target is validated as a remote address ({@link URLValidator#validateRemote(URI)}): an internal or + * loopback address is refused with 400, whether or not the deployment allows internal URLs + * elsewhere, a public one is left as it is and the store calls it through its egress proxy, as before;
  • + *
  • a target that is a variable is refused with 400: it cannot be authorized before the query runs.
  • + *
+ * SILENT is kept: it suppresses a failure of the call, not the decision whether the call may be made. + * Sub-queries and EXISTS patterns are rewritten too. + * + * @author Martynas Jusevičius {@literal } + */ +public class ServiceRewriter +{ + + private static final Logger log = LoggerFactory.getLogger(ServiceRewriter.class); + + /** The path of a dataspace's SPARQL endpoint under its base */ + public static final String SPARQL_PATH = "sparql"; + + private final Function dataspaces; + private final DataspaceAuthorization authorization; + private final URLValidator validator; + private final Agent agent; + + /** + * Constructs the rewriter for one agent. + * + * @param dataspaces the dataspace a URI belongs to, or null for none of this deployment's + * @param authorization the access control decision + * @param validator the deployment's URL validator + * @param agent the agent the query is run for, or null for an anonymous one + */ + public ServiceRewriter(Function dataspaces, DataspaceAuthorization authorization, URLValidator validator, Agent agent) + { + if (dataspaces == null) throw new IllegalArgumentException("Dataspace lookup cannot be null"); + if (authorization == null) throw new IllegalArgumentException("DataspaceAuthorization cannot be null"); + if (validator == null) throw new IllegalArgumentException("URLValidator cannot be null"); + + this.dataspaces = dataspaces; + this.authorization = authorization; + this.validator = validator; + this.agent = agent; + } + + /** + * Returns the query with every SERVICE target decided. + * A query without SERVICE comes back as it is. + * + * @param query query as the client sent it + * @return query for the store + * @throws AuthorizationException if the agent may not read a dataspace a clause names + * @throws BadRequestException if a target cannot be decided + * @throws com.atomgraph.linkeddatahub.server.exception.InternalURLException if a target is an internal address + */ + public Query rewrite(Query query) + { + if (query == null) throw new IllegalArgumentException("Query cannot be null"); + + ElementTransform transform = new ElementTransformCopyBase() + { + + @Override + public Element transform(ElementService el, Node service, Element subElt) + { + return new ElementService(target(service), subElt, el.getSilent()); + } + + }; + + return QueryTransformOps.transform(query, transform, new ExprTransformApplyElementTransform(transform)); + } + + /** + * Returns the node the store is to call for a SERVICE target. + * + * @param service the target as the query names it + * @return the target for the store + */ + public Node target(Node service) + { + if (!service.isURI()) throw new BadRequestException("A SERVICE target must be an IRI: " + service + " cannot be authorized before the query runs"); + + URI target = URI.create(service.getURI()); + Dataspace dataspace = getDataspaces().apply(target); + if (dataspace != null) + { + URI endpoint = dataspace.getBaseURI().resolve(SPARQL_PATH); + if (!endpoint.equals(target)) throw new BadRequestException("A dataspace is federated through its SPARQL endpoint <" + endpoint + ">, not <" + target + ">"); + + if (!dataspace.isReadAllowed() && + getAuthorization().authorize(dataspace, ResourceFactory.createResource(endpoint.toString()), getAgent(), ACL.Read) == null) + { + if (log.isDebugEnabled()) log.debug("SERVICE <{}> not authorized for agent {}", target, getAgent()); + throw new AuthorizationException("Access to the SERVICE endpoint not authorized", target, ACL.Read, getAgent()); + } + + Node store = NodeFactory.createURI(dataspace.getService().getSPARQLEndpoint().getURI()); + if (log.isDebugEnabled()) log.debug("SERVICE <{}> authorized for agent {}, rewritten to <{}>", target, getAgent(), store); + return store; + } + + getValidator().validateRemote(target); + return service; + } + + /** + * Returns the lookup of a URI's dataspace. + * + * @return lookup function + */ + public Function getDataspaces() + { + return dataspaces; + } + + /** + * Returns the access control decision. + * + * @return authorization + */ + public DataspaceAuthorization getAuthorization() + { + return authorization; + } + + /** + * Returns the URL validator. + * + * @return validator + */ + public URLValidator getValidator() + { + return validator; + } + + /** + * Returns the agent the query runs for. + * + * @return agent or null + */ + public Agent getAgent() + { + return agent; + } + +} diff --git a/src/main/java/com/atomgraph/linkeddatahub/server/util/URLValidator.java b/src/main/java/com/atomgraph/linkeddatahub/server/util/URLValidator.java index 9a1c6fbd13..f2ba8be1e0 100644 --- a/src/main/java/com/atomgraph/linkeddatahub/server/util/URLValidator.java +++ b/src/main/java/com/atomgraph/linkeddatahub/server/util/URLValidator.java @@ -70,6 +70,39 @@ public URLValidator(boolean allowInternal) * @throws InternalURLException if the URI resolves to an internal IP address */ public URI validate(URI uri) + { + return validate(uri, false); + } + + /** + * Validates a URI another service of the deployment is to call on the platform's behalf, refusing loopback + * addresses as well, and refusing internal addresses whether or not the deployment allows them elsewhere. + *

+ * The triplestore executes a query's SERVICE clauses, and its JVM is itself what loopback names + * there, so a loopback target would read a sibling dataset of the same server without the proxy that keeps it to + * public addresses. The platform's own addresses that loopback stands for in local deployments - its dataspaces - + * are matched and authorized before a target reaches this check ({@link ServiceRewriter}), so what is left on + * loopback is internal. {@code ALLOW_INTERNAL_URLS} does not open this: it lets the platform dereference its own + * documents and WebIDs on an internal address, and a query's targets that are the platform's own are matched as + * dataspaces, never validated here, so an internal target here is a store or another service of the deployment, + * which no deployment mode may read past the ACL. + * + * @param uri the URI to validate + * @return the validated URI + * @throws IllegalArgumentException if the URI is null or its host is null + * @throws InternalURLException if the URI resolves to an internal or a loopback IP address + */ + public URI validateRemote(URI uri) + { + return validate(uri, true, false); + } + + private URI validate(URI uri, boolean refuseLoopback) + { + return validate(uri, refuseLoopback, allowInternal); + } + + private URI validate(URI uri, boolean refuseLoopback, boolean allowInternal) { if (uri == null) throw new IllegalArgumentException("URI cannot be null"); @@ -78,7 +111,7 @@ public URI validate(URI uri) String host = uri.getHost(); if (host == null) throw new IllegalArgumentException("URI host cannot be null"); - // Resolve hostname to all IPs and reject if any is wildcard/private/internal (loopback intentionally allowed) + // Resolve hostname to all IPs and reject if any is wildcard/private/internal (loopback allowed unless asked) try { for (InetAddress address : InetAddress.getAllByName(host)) @@ -89,6 +122,8 @@ public URI validate(URI uri) throw new InternalURLException(uri, address.getHostAddress()); if (address.isSiteLocalAddress()) throw new InternalURLException(uri, address.getHostAddress()); + if (refuseLoopback && address.isLoopbackAddress()) + throw new InternalURLException(uri, address.getHostAddress()); } } catch (UnknownHostException e) diff --git a/src/test/java/com/atomgraph/linkeddatahub/server/util/ServiceRewriterTest.java b/src/test/java/com/atomgraph/linkeddatahub/server/util/ServiceRewriterTest.java new file mode 100644 index 0000000000..c3f264851f --- /dev/null +++ b/src/test/java/com/atomgraph/linkeddatahub/server/util/ServiceRewriterTest.java @@ -0,0 +1,241 @@ +/** + * Copyright 2026 Martynas Jusevičius + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package com.atomgraph.linkeddatahub.server.util; + +import com.atomgraph.linkeddatahub.dataspaces.model.Dataspace; +import com.atomgraph.linkeddatahub.model.Service; +import com.atomgraph.linkeddatahub.model.auth.Agent; +import com.atomgraph.linkeddatahub.server.exception.InternalURLException; +import com.atomgraph.linkeddatahub.server.exception.auth.AuthorizationException; +import com.atomgraph.linkeddatahub.server.security.DataspaceAuthorization; +import com.atomgraph.linkeddatahub.vocabulary.ACL; +import jakarta.ws.rs.BadRequestException; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.apache.jena.query.Query; +import org.apache.jena.query.QueryFactory; +import org.apache.jena.rdf.model.ModelFactory; +import org.apache.jena.rdf.model.Resource; +import org.apache.jena.rdf.model.ResourceFactory; +import org.apache.jena.sparql.syntax.Element; +import org.apache.jena.sparql.syntax.ElementService; +import org.apache.jena.sparql.syntax.ElementVisitorBase; +import org.apache.jena.sparql.syntax.ElementWalker; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Unit tests for {@link ServiceRewriter}: what becomes of each kind of SERVICE target. + * The dataspaces are two of one deployment, people (read by its ACL) and open (public + * read), reached at their stores; the agent is asked about through a mocked {@link DataspaceAuthorization}. Hosts + * are IP literals, so no name is resolved. + * + * @author Martynas Jusevičius {@literal } + */ +public class ServiceRewriterTest +{ + + private static final URI PEOPLE = URI.create("https://people.example/"), OPEN = URI.create("https://open.example/"); + private static final String PEOPLE_STORE = "http://fuseki:3030/people.end-user/", OPEN_STORE = "http://fuseki:3030/open.end-user/"; + + private Dataspace people, open; + private DataspaceAuthorization authorization; + private Agent agent; + private ServiceRewriter rewriter; + + @BeforeEach + public void setUp() + { + people = dataspace(PEOPLE, PEOPLE_STORE, false); + open = dataspace(OPEN, OPEN_STORE, true); + authorization = mock(DataspaceAuthorization.class); + agent = mock(Agent.class); + Map byOrigin = Map.of(PEOPLE, people, OPEN, open); + rewriter = new ServiceRewriter(uri -> byOrigin.get(uri.resolve("/")), authorization, new URLValidator(false), agent); + } + + private static Dataspace dataspace(URI base, String store, boolean readAllowed) + { + Service service = mock(Service.class); + when(service.getSPARQLEndpoint()).thenReturn(ResourceFactory.createResource(store)); + Dataspace dataspace = mock(Dataspace.class); + when(dataspace.getBaseURI()).thenReturn(base); + when(dataspace.getService()).thenReturn(service); + when(dataspace.isReadAllowed()).thenReturn(readAllowed); + return dataspace; + } + + private static Resource endpoint(URI base) + { + return ResourceFactory.createResource(base.resolve("sparql").toString()); + } + + private static List services(Query query) + { + List services = new ArrayList<>(); + ElementWalker.walk(query.getQueryPattern(), new ElementVisitorBase() + { + + @Override + public void visit(ElementService el) + { + services.add(el); + } + + }); + return services; + } + + @Test + public void testAuthorizedDataspaceIsReachedAtItsStore() + { + when(authorization.authorize(eq(people), eq(endpoint(PEOPLE)), eq(agent), eq(ACL.Read))).thenReturn(ModelFactory.createDefaultModel()); + + Query rewritten = rewriter.rewrite(QueryFactory.create("SELECT * { SERVICE { ?s ?p ?o } }")); + + List services = services(rewritten); + assertEquals(1, services.size()); + assertEquals(PEOPLE_STORE, services.get(0).getServiceNode().getURI()); + assertFalse(services.get(0).getSilent()); + } + + @Test + public void testUnauthorizedDataspaceIsRefused() + { + when(authorization.authorize(eq(people), eq(endpoint(PEOPLE)), eq(agent), eq(ACL.Read))).thenReturn(null); + + AuthorizationException ex = assertThrows(AuthorizationException.class, + () -> rewriter.rewrite(QueryFactory.create("SELECT * { SERVICE SILENT { ?s ?p ?o } } LIMIT 1"))); + assertEquals(PEOPLE.resolve("sparql"), ex.getAbsolutePath()); + assertEquals(ACL.Read, ex.getMode()); + } + + @Test + public void testAnonymousAgentIsAskedAboutAsNull() + { + ServiceRewriter anonymous = new ServiceRewriter(uri -> people, authorization, new URLValidator(false), null); + when(authorization.authorize(eq(people), eq(endpoint(PEOPLE)), isNull(), eq(ACL.Read))).thenReturn(null); + + assertThrows(AuthorizationException.class, + () -> anonymous.rewrite(QueryFactory.create("SELECT * { SERVICE { ?s ?p ?o } }"))); + } + + @Test + public void testPublicDataspaceIsReachedWithoutAsking() + { + Query rewritten = rewriter.rewrite(QueryFactory.create("SELECT * { SERVICE SILENT { ?s ?p ?o } }")); + + List services = services(rewritten); + assertEquals(OPEN_STORE, services.get(0).getServiceNode().getURI()); + assertTrue(services.get(0).getSilent()); + verify(authorization, never()).authorize(any(), any(), any(), any()); + } + + @Test + public void testDataspaceNamedByAnythingButItsEndpointIsRefused() + { + assertThrows(BadRequestException.class, + () -> rewriter.rewrite(QueryFactory.create("SELECT * { SERVICE { ?s ?p ?o } }"))); + assertThrows(BadRequestException.class, + () -> rewriter.rewrite(QueryFactory.create("SELECT * { SERVICE { ?s ?p ?o } }"))); + verify(authorization, never()).authorize(any(), any(), any(), any()); + } + + @Test + public void testVariableTargetIsRefused() + { + assertThrows(BadRequestException.class, + () -> rewriter.rewrite(QueryFactory.create("SELECT * { ?s ?endpoint . SERVICE ?endpoint { ?s ?p ?o } }"))); + } + + @Test + public void testInternalTargetIsRefused() + { + assertThrows(InternalURLException.class, + () -> rewriter.rewrite(QueryFactory.create("SELECT * { SERVICE SILENT { ?s ?p ?o } }"))); + } + + @Test + public void testLoopbackTargetIsRefused() + { + assertThrows(InternalURLException.class, + () -> rewriter.rewrite(QueryFactory.create("SELECT * { SERVICE { ?s ?p ?o } }"))); + } + + @Test + public void testPublicTargetIsLeftAsItIs() + { + Query query = QueryFactory.create("SELECT * { SERVICE SILENT { ?s ?p ?o } }"); + + Query rewritten = rewriter.rewrite(query); + + List services = services(rewritten); + assertEquals("http://93.184.216.34/sparql", services.get(0).getServiceNode().getURI()); + assertTrue(services.get(0).getSilent()); + assertEquals(query, rewritten); + } + + @Test + public void testQueryWithoutServiceIsUnchanged() + { + Query query = QueryFactory.create("SELECT * { ?s ?p ?o } LIMIT 1"); + + assertEquals(query, rewriter.rewrite(query)); + } + + @Test + public void testNestedServicesAreRewritten() + { + when(authorization.authorize(eq(people), eq(endpoint(PEOPLE)), eq(agent), eq(ACL.Read))).thenReturn(ModelFactory.createDefaultModel()); + + Query rewritten = rewriter.rewrite(QueryFactory.create(""" + SELECT * { + { SELECT ?s { SERVICE { ?s a } } } + FILTER NOT EXISTS { SERVICE { ?s ?e } } + }""")); + + // the walker of services() stops at a sub-query and a filter, so the serialized query is checked instead + String serialized = rewritten.toString(); + assertTrue(serialized.contains("<" + PEOPLE_STORE + ">"), serialized); + assertTrue(serialized.contains("<" + OPEN_STORE + ">"), serialized); + assertFalse(serialized.contains("people.example"), serialized); + assertFalse(serialized.contains("open.example"), serialized); + } + + @Test + public void testOneRefusalFailsTheWholeQuery() + { + when(authorization.authorize(eq(people), eq(endpoint(PEOPLE)), eq(agent), eq(ACL.Read))).thenReturn(null); + + Element pattern = QueryFactory.create("SELECT * { SERVICE { ?s ?p ?o } SERVICE { ?s ?p ?o } }").getQueryPattern(); + assertThrows(AuthorizationException.class, () -> rewriter.rewrite(QueryFactory.create("SELECT * " + pattern))); + } + +} diff --git a/src/test/java/com/atomgraph/linkeddatahub/server/util/URLValidatorTest.java b/src/test/java/com/atomgraph/linkeddatahub/server/util/URLValidatorTest.java index 735e23c096..45e1e0943f 100644 --- a/src/test/java/com/atomgraph/linkeddatahub/server/util/URLValidatorTest.java +++ b/src/test/java/com/atomgraph/linkeddatahub/server/util/URLValidatorTest.java @@ -119,4 +119,34 @@ public void testAllowInternalLoopbackAllowed() // When allowInternal=true, loopback addresses should pass through without exception (dev escape hatch) new URLValidator(true).validate(URI.create("http://127.0.0.1:3030/ds")); } + + @Test + public void testRemoteLoopbackBlocked() + { + // a target another service calls on the platform's behalf (a SERVICE clause the triplestore executes) names + // that service itself by loopback, so loopback is internal there + assertThrows(InternalURLException.class, () -> new URLValidator(false).validateRemote(URI.create("http://127.0.0.1:3030/admin/"))); + assertThrows(InternalURLException.class, () -> new URLValidator(false).validateRemote(URI.create("http://localhost:3030/admin/"))); + } + + @Test + public void testRemoteInternalBlocked() + { + assertThrows(InternalURLException.class, () -> new URLValidator(false).validateRemote(URI.create("http://10.0.0.1:3030/admin/"))); + } + + @Test + public void testRemoteExternalAllowed() + { + new URLValidator(false).validateRemote(URI.create("http://8.8.8.8:80/sparql")); + } + + @Test + public void testRemoteInternalBlockedEvenWhenInternalAllowed() + { + // ALLOW_INTERNAL_URLS lets the platform dereference itself on an internal address; a target another service + // calls on its behalf is a store or a service of the deployment, refused in every mode + assertThrows(InternalURLException.class, () -> new URLValidator(true).validateRemote(URI.create("http://127.0.0.1:3030/admin/"))); + assertThrows(InternalURLException.class, () -> new URLValidator(true).validateRemote(URI.create("http://10.0.0.1:3030/admin/"))); + } } diff --git a/tests/http/sparql-protocol/query/GET-sparql-service-dataspace-403.sh b/tests/http/sparql-protocol/query/GET-sparql-service-dataspace-403.sh new file mode 100644 index 0000000000..7443376d32 --- /dev/null +++ b/tests/http/sparql-protocol/query/GET-sparql-service-dataspace-403.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +set -euo pipefail + +initialize_dataset "$END_USER_BASE_URL" "$TMP_END_USER_DATASET" "$END_USER_ENDPOINT_URL" +initialize_dataset "$ADMIN_BASE_URL" "$TMP_ADMIN_DATASET" "$ADMIN_ENDPOINT_URL" +purge_cache "$END_USER_VARNISH_SERVICE" +purge_cache "$ADMIN_VARNISH_SERVICE" +purge_cache "$FRONTEND_VARNISH_SERVICE" +reset_packages +clear_ontology + +# The federation leg is the agent's own: a signed-up agent may query the end-user endpoint (GET-sparql.sh) but not +# the admin dataspace's, which the owners alone read, so a SERVICE naming it is refused with 403 for the whole +# query - SILENT or not, since the refusal is the platform's decision, not a failed call + +for silent in "" "SILENT" +do + status=$(curl -k -w "%{http_code}" -o /dev/null -s -G \ + -E "$AGENT_CERT_FILE":"$AGENT_CERT_PWD" \ + -H "Accept: application/sparql-results+xml" \ + "${END_USER_BASE_URL}sparql" \ + --data-urlencode "query=SELECT ?g { SERVICE ${silent} <${ADMIN_BASE_URL}sparql> { GRAPH ?g { ?s ?p ?o } } } LIMIT 1") + + echo "DEBUG: SERVICE ${silent} Expected: $STATUS_FORBIDDEN Got: $status" + [ "$status" = "$STATUS_FORBIDDEN" ] || exit 1 +done diff --git a/tests/http/sparql-protocol/query/GET-sparql-service-dataspace.sh b/tests/http/sparql-protocol/query/GET-sparql-service-dataspace.sh new file mode 100644 index 0000000000..2a1e441834 --- /dev/null +++ b/tests/http/sparql-protocol/query/GET-sparql-service-dataspace.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -euo pipefail + +initialize_dataset "$END_USER_BASE_URL" "$TMP_END_USER_DATASET" "$END_USER_ENDPOINT_URL" +initialize_dataset "$ADMIN_BASE_URL" "$TMP_ADMIN_DATASET" "$ADMIN_ENDPOINT_URL" +purge_cache "$END_USER_VARNISH_SERVICE" +purge_cache "$ADMIN_VARNISH_SERVICE" +purge_cache "$FRONTEND_VARNISH_SERVICE" +reset_packages +clear_ontology + +# A query federates to another dataspace of the deployment by naming that dataspace's public SPARQL endpoint. The +# platform authorizes the agent for the endpoint as a GET of it would be - here the owner reading the admin +# dataspace's - and the store reaches the dataspace's dataset: the admin dataspace's root document is a result the +# end-user endpoint alone never has + +results=$(curl -k -f -s -G \ + -E "$OWNER_CERT_FILE":"$OWNER_CERT_PWD" \ + -H "Accept: application/sparql-results+xml" \ + "${END_USER_BASE_URL}sparql" \ + --data-urlencode "query=SELECT ?g { SERVICE <${ADMIN_BASE_URL}sparql> { GRAPH ?g { <${ADMIN_BASE_URL}> ?p ?o } } } LIMIT 1") + +echo "DEBUG: results: ${results}" + +grep -q "${ADMIN_BASE_URL}" <<< "$results" || { echo "SERVICE <${ADMIN_BASE_URL}sparql> did not answer with the admin root document"; exit 1; } diff --git a/tests/http/sparql-protocol/query/GET-sparql-service-internal.sh b/tests/http/sparql-protocol/query/GET-sparql-service-internal.sh index a3dcb377d9..289bc1e419 100755 --- a/tests/http/sparql-protocol/query/GET-sparql-service-internal.sh +++ b/tests/http/sparql-protocol/query/GET-sparql-service-internal.sh @@ -13,21 +13,18 @@ clear_ontology # executes it, so an internal target would hand the agent data the ACL never grants, such as the admin store's agents # and authorizations. Targets: the admin store, the admin store through its backend cache, and a store over loopback, # which the JVM would otherwise exempt from an outbound proxy. -# SERVICE SILENT turns a refused call into a single empty solution, so the results are checked for data rather than -# the response for a status code: a target that answered binds ?g +# The platform refuses the query before it goes out, with 400: a dataspace of the deployment is federated through its +# public endpoint (GET-sparql-service-dataspace.sh), anything else internal is not reached. SILENT does not help: it +# suppresses a failed call, not the decision whether the call may be made for endpoint in "http://fuseki:3030/admin/" "http://varnish-admin/admin/" "http://localhost:3030/admin/" do - results=$(curl -k -f -s -G \ + status=$(curl -k -w "%{http_code}" -o /dev/null -s -G \ -E "$AGENT_CERT_FILE":"$AGENT_CERT_PWD" \ -H "Accept: application/sparql-results+xml" \ "${END_USER_BASE_URL}sparql" \ --data-urlencode "query=SELECT ?g { SERVICE SILENT <${endpoint}> { GRAPH ?g { ?s ?p ?o } } } LIMIT 1") - echo "DEBUG: SERVICE <${endpoint}> results: ${results}" - - if grep -q '' <<< "$results"; then - echo "SERVICE <${endpoint}> returned data" - exit 1 - fi + echo "DEBUG: SERVICE <${endpoint}> Expected: $STATUS_BAD_REQUEST Got: $status" + [ "$status" = "$STATUS_BAD_REQUEST" ] || exit 1 done diff --git a/tests/http/sparql-protocol/query/POST-sparql-service-internal.sh b/tests/http/sparql-protocol/query/POST-sparql-service-internal.sh index 6a01ac7752..c972591137 100755 --- a/tests/http/sparql-protocol/query/POST-sparql-service-internal.sh +++ b/tests/http/sparql-protocol/query/POST-sparql-service-internal.sh @@ -10,12 +10,11 @@ reset_packages clear_ontology # The POST form of GET-sparql-service-internal.sh: a query sent in the request body must not reach the admin store -# through SPARQL SERVICE either. SERVICE SILENT turns a refused call into a single empty solution, so a bound ?g means -# the admin store answered +# through SPARQL SERVICE either. The platform refuses it with 400 before it goes out endpoint="http://fuseki:3030/admin/" -results=$(curl -k -f -s \ +status=$(curl -k -w "%{http_code}" -o /dev/null -s \ -E "$AGENT_CERT_FILE":"$AGENT_CERT_PWD" \ -X POST \ -H "Content-Type: application/x-www-form-urlencoded" \ @@ -23,9 +22,5 @@ results=$(curl -k -f -s \ --data-urlencode "query=SELECT ?g { SERVICE SILENT <${endpoint}> { GRAPH ?g { ?s ?p ?o } } } LIMIT 1" \ "${END_USER_BASE_URL}sparql") -echo "DEBUG: SERVICE <${endpoint}> results: ${results}" - -if grep -q '' <<< "$results"; then - echo "SERVICE <${endpoint}> returned data" - exit 1 -fi +echo "DEBUG: SERVICE <${endpoint}> Expected: $STATUS_BAD_REQUEST Got: $status" +[ "$status" = "$STATUS_BAD_REQUEST" ] || exit 1