From 83e77a7e12259a6746481135809af74b9f4893ac Mon Sep 17 00:00:00 2001 From: Lumina Date: Sun, 15 Nov 2026 15:42:52 -0500 Subject: [PATCH] Advertise the resource's own ACL in the Link rel="acl" header addWACHeaders() added `Link: <.acl>; rel="acl"` to every response. A client resolves that reference against the resource URI, so for /foo/bar.ttl it points at /foo/.acl, the ACL of the container, instead of /foo/bar.ttl.acl. Clients that change access to a single resource write to the advertised ACL. Inrupt's solid-client (universalAccess.setAgentAccess) does exactly that: it builds a new ACL with only the requested rule and saves it to the advertised URL. With the old header that replaced the rules for the whole container, so the owner lost access to everything in it. Advertise `` for a resource and `<.acl>` for a container, as WAC's ACL resource discovery requires. The ACL resource does not have to exist yet. Co-Authored-By: Claude Opus 5.5 --- src/WAC.php | 32 +++++++++++++++++++++- tests/unit/WACTest.php | 61 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tests/unit/WACTest.php 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'], + ]; + } +}