diff --git a/src/WAC.php b/src/WAC.php index 6ead2d6..b169ec4 100644 --- a/src/WAC.php +++ b/src/WAC.php @@ -38,12 +38,42 @@ public function addWACHeaders($request, $response, $webId) { $wacHeaders[] = "public=\"$publicGrants\""; } - $response = $response->withAddedHeader("Link", '<.acl>; rel="acl"'); + $response = $response->withAddedHeader("Link", '<' . $this->getAclLinkTarget($uri) . '>; rel="acl"'); $response = $response->withHeader("WAC-Allow", implode(",", $wacHeaders)); $this->adapter->setFormat($currentFormat); return $response; } + /** + * Returns the URI reference, relative to the resource, of the ACL resource + * that a client must use to read or change the access to that resource. + * + * Every resource has its own ACL resource, whether it exists yet or not: + * `<.acl>` for a container and `` for any other resource. The + * inherited ACL of an ancestor container must not be advertised here. A + * client resolves the link against the resource URI, so advertising + * `<.acl>` for `/foo/bar.ttl` points it at the ACL of the container + * `/foo/`, and a client that then writes "the ACL of bar.ttl" replaces + * the rules for every resource in that container. + * + * @see https://solidproject.org/TR/wac#acl-resource-discovery + */ + public function getAclLinkTarget($uri): string { + $path = $uri->getPath(); + + if ($path === '' || substr($path, -1) === '/') { + return '.acl'; + } + + $name = substr($path, strrpos($path, '/') + 1); + + if (substr($name, -4) === '.acl') { + return $name; + } + + return $name . '.acl'; + } + /** * Checks the requested filename (path+name) and user (webid) to see if the request * is allowed to continue, according to the web acl diff --git a/tests/unit/WACTest.php b/tests/unit/WACTest.php new file mode 100644 index 0000000..b0b186f --- /dev/null +++ b/tests/unit/WACTest.php @@ -0,0 +1,61 @@ +getAclLinkTarget(new Uri($resourceUri)); + + $this->assertSame($expected, $actual); + } + + /** + * @testdox WAC should advertise an ACL link that resolves to the resource's own ACL resource, not to the ACL of its container + * + * @covers ::getAclLinkTarget + * + * @dataProvider provideResourceUris + */ + public function testAclLinkResolvesToOwnAcl(string $resourceUri, string $target, string $expectedAclUri): void + { + $wac = new WAC(null, null); + + $link = $wac->getAclLinkTarget(new Uri($resourceUri)); + + // A relative reference that is a single path segment resolves against the "directory" of the resource + $resolved = substr($resourceUri, 0, strrpos($resourceUri, '/') + 1) . $link; + + $this->assertSame($expectedAclUri, $resolved); + } + + public static function provideResourceUris(): array + { + return [ + 'root container' => ['https://pod.example/', '.acl', 'https://pod.example/.acl'], + 'container' => ['https://pod.example/private/', '.acl', 'https://pod.example/private/.acl'], + 'resource in container' => ['https://pod.example/private/note.ttl', 'note.ttl.acl', 'https://pod.example/private/note.ttl.acl'], + 'resource at root' => ['https://pod.example/note.ttl', 'note.ttl.acl', 'https://pod.example/note.ttl.acl'], + 'percent-encoded name' => ['https://pod.example/private/my%20note.ttl', 'my%20note.ttl.acl', 'https://pod.example/private/my%20note.ttl.acl'], + 'ACL resource of a resource' => ['https://pod.example/private/note.ttl.acl', 'note.ttl.acl', 'https://pod.example/private/note.ttl.acl'], + 'ACL resource of a container' => ['https://pod.example/private/.acl', '.acl', 'https://pod.example/private/.acl'], + ]; + } +}