From 3bf2f94d5f2aad72b17cb212c55bc124b2a1df7b Mon Sep 17 00:00:00 2001 From: Tom Stark Date: Tue, 13 Oct 2026 10:31:28 +0200 Subject: [PATCH 1/2] fix: stop advertising the RSL license on plain allowed responses --- README.md | 2 + src/License/ResponseBuilder.php | 14 ++--- tests/License/ResponseBuilderTest.php | 46 ++++++++++---- tests/SupertabConnectTest.php | 89 ++++++++++++++++++++++++--- 4 files changed, 123 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 4ea2f09..1f099a2 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ The `EnforcementMode` enum controls how `handleRequest()` responds to detected b Default is `OBSERVE`. +The license is advertised with `Link: <…/license.xml>; rel="license"` on the `OBSERVE` signal and on blocked responses only. Plain allowed responses (non-bot, valid token, `DISABLED`) carry no RSL headers. + --- ## Analytics diff --git a/src/License/ResponseBuilder.php b/src/License/ResponseBuilder.php index 3c97826..535c5bc 100644 --- a/src/License/ResponseBuilder.php +++ b/src/License/ResponseBuilder.php @@ -11,16 +11,16 @@ final class ResponseBuilder { /** - * Build an allowed result that advertises the governing RSL license via the - * Link header. Per the RSL Crawler Authorization Protocol, served responses - * must reference the license so clients can discover the terms — independent - * of enforcement mode or bot detection. + * Build an allowed result. A plain allow (non-bot, valid token, DISABLED) + * carries no RSL headers: the license is advertised only once the request + * is treated as a bot's, on the OBSERVE signal and on blocks. Matches the + * TypeScript SDK. + * + * `$requestUrl` is unused and kept for signature compatibility. */ public static function buildAllowResult(string $requestUrl): AllowResult { - return new AllowResult([ - 'Link' => self::licenseLinkHeader($requestUrl), - ]); + return new AllowResult; } /** diff --git a/tests/License/ResponseBuilderTest.php b/tests/License/ResponseBuilderTest.php index af8e9da..dae6a98 100644 --- a/tests/License/ResponseBuilderTest.php +++ b/tests/License/ResponseBuilderTest.php @@ -4,6 +4,7 @@ namespace Supertab\Connect\Tests\License; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Supertab\Connect\Enum\HandlerAction; use Supertab\Connect\Enum\LicenseTokenInvalidReason; @@ -19,24 +20,47 @@ public function test_build_signal_result(): void $this->assertInstanceOf(AllowResult::class, $result); $this->assertSame(HandlerAction::ALLOW, $result->action); - $this->assertSame('token_required', $result->headers['X-RSL-Status']); - $this->assertSame('missing', $result->headers['X-RSL-Reason']); - $this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']); - $this->assertStringContainsString('rel="license"', $result->headers['Link']); + // Matches the TypeScript SDK: the bot signal advertises the license. + $this->assertSame( + [ + 'Link' => '; rel="license"; type="application/rsl+xml"', + 'X-RSL-Status' => 'token_required', + 'X-RSL-Reason' => 'missing', + ], + $result->headers, + ); } - public function test_build_allow_result_advertises_license_link(): void + public function test_build_allow_result_carries_no_headers(): void { $result = ResponseBuilder::buildAllowResult('https://example.com/article'); $this->assertInstanceOf(AllowResult::class, $result); $this->assertSame(HandlerAction::ALLOW, $result->action); - $this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']); - $this->assertStringContainsString('rel="license"', $result->headers['Link']); - $this->assertStringContainsString('type="application/rsl+xml"', $result->headers['Link']); - // Discovery only — the request-state signal headers stay on the soft-signal path. - $this->assertArrayNotHasKey('X-RSL-Status', $result->headers); - $this->assertArrayNotHasKey('X-RSL-Reason', $result->headers); + $this->assertSame([], $result->headers); + } + + #[DataProvider('blockReasonProvider')] + public function test_every_block_result_advertises_license_link(LicenseTokenInvalidReason|string $reason): void + { + $result = ResponseBuilder::buildBlockResult($reason, 'error', 'https://example.com/article'); + + $this->assertSame( + '; rel="license"; type="application/rsl+xml"', + $result->headers['Link'], + ); + } + + /** + * @return iterable + */ + public static function blockReasonProvider(): iterable + { + foreach (LicenseTokenInvalidReason::cases() as $reason) { + yield $reason->value => [$reason]; + } + + yield 'string reason' => ['custom_reason']; } public function test_build_block_result_missing_token(): void diff --git a/tests/SupertabConnectTest.php b/tests/SupertabConnectTest.php index 00e8636..c23a07f 100644 --- a/tests/SupertabConnectTest.php +++ b/tests/SupertabConnectTest.php @@ -283,9 +283,9 @@ public function test_handle_request_ignores_non_license_auth_header_with_bot(): $this->assertArrayHasKey('X-RSL-Status', $result->headers); } - // --- RSL discovery: Link header on allowed responses --- + // --- RSL discovery: Link header on bot signals and blocks, never on plain allows --- - public function test_handle_request_disabled_no_token_advertises_license_link(): void + public function test_handle_request_disabled_no_token_sends_no_license_link(): void { $stc = new SupertabConnect( apiKey: 'test-key', @@ -298,11 +298,10 @@ public function test_handle_request_disabled_no_token_advertises_license_link(): )); $this->assertInstanceOf(AllowResult::class, $result); - $this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']); - $this->assertStringContainsString('rel="license"', $result->headers['Link']); + $this->assertArrayNotHasKey('Link', $result->headers); } - public function test_handle_request_disabled_with_token_advertises_license_link(): void + public function test_handle_request_disabled_with_token_sends_no_license_link(): void { $stc = new SupertabConnect( apiKey: 'test-key', @@ -315,10 +314,10 @@ public function test_handle_request_disabled_with_token_advertises_license_link( )); $this->assertInstanceOf(AllowResult::class, $result); - $this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']); + $this->assertArrayNotHasKey('Link', $result->headers); } - public function test_handle_request_non_bot_allow_advertises_license_link(): void + public function test_handle_request_non_bot_allow_sends_no_license_link(): void { $botDetector = $this->createMock(BotDetectorInterface::class); $botDetector->method('isBot')->willReturn(false); @@ -335,10 +334,10 @@ public function test_handle_request_non_bot_allow_advertises_license_link(): voi )); $this->assertInstanceOf(AllowResult::class, $result); - $this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']); + $this->assertArrayNotHasKey('Link', $result->headers); } - public function test_handle_request_bot_disabled_allow_advertises_license_link(): void + public function test_handle_request_bot_disabled_allow_sends_no_license_link(): void { $botDetector = $this->createMock(BotDetectorInterface::class); $botDetector->method('isBot')->willReturn(true); @@ -355,7 +354,7 @@ public function test_handle_request_bot_disabled_allow_advertises_license_link() )); $this->assertInstanceOf(AllowResult::class, $result); - $this->assertStringContainsString('https://example.com/license.xml', $result->headers['Link']); + $this->assertArrayNotHasKey('Link', $result->headers); } public function test_handle_request_observe_signal_keeps_link_and_custom_headers(): void @@ -380,6 +379,76 @@ public function test_handle_request_observe_signal_keeps_link_and_custom_headers $this->assertSame('missing', $result->headers['X-RSL-Reason']); } + public function test_handle_request_enforce_bot_without_token_block_advertises_license_link(): void + { + $botDetector = $this->createMock(BotDetectorInterface::class); + $botDetector->method('isBot')->willReturn(true); + + $stc = new SupertabConnect( + apiKey: 'test-key', + enforcement: EnforcementMode::ENFORCE, + botDetector: $botDetector, + ); + + $result = $stc->handleRequest(new RequestContext( + url: 'https://example.com/article', + authorizationHeader: null, + )); + + $this->assertInstanceOf(BlockResult::class, $result); + $this->assertStringContainsString('; rel="license"', $result->headers['Link']); + } + + public function test_handle_request_invalid_token_block_advertises_license_link(): void + { + $stc = new SupertabConnect( + apiKey: 'test-key', + enforcement: EnforcementMode::OBSERVE, + httpClient: $this->createEventSinkHttpClient(), + ); + + $result = $stc->handleRequest(new RequestContext( + url: 'https://example.com/article', + authorizationHeader: 'License not-a-real-jwt', + )); + + $this->assertInstanceOf(BlockResult::class, $result); + $this->assertSame(401, $result->status); + $this->assertStringContainsString('; rel="license"', $result->headers['Link']); + } + + public function test_handle_request_empty_license_token_block_advertises_license_link(): void + { + $stc = new SupertabConnect( + apiKey: 'test-key', + enforcement: EnforcementMode::OBSERVE, + httpClient: $this->createEventSinkHttpClient(), + ); + + // "License " with nothing after it takes the token-present path as MISSING_TOKEN. + $result = $stc->handleRequest(new RequestContext( + url: 'https://example.com/article', + authorizationHeader: 'License ', + )); + + $this->assertInstanceOf(BlockResult::class, $result); + $this->assertSame(401, $result->status); + $this->assertStringContainsString('invalid_request', $result->headers['WWW-Authenticate']); + $this->assertStringContainsString('; rel="license"', $result->headers['Link']); + } + + /** + * HTTP client that accepts the fire-and-forget billing event POST that the + * token-present path records, so these tests never touch the network. + */ + private function createEventSinkHttpClient(): HttpClientInterface + { + $httpClient = $this->createStub(HttpClientInterface::class); + $httpClient->method('post')->willReturn(['statusCode' => 200, 'body' => '']); + + return $httpClient; + } + // --- Bot Detection Integration --- public function test_handle_request_allows_non_bot_without_token(): void From 568170e708cf9d6a44790c0bdd01bbddf4e73002 Mon Sep 17 00:00:00 2001 From: Tom Stark Date: Tue, 13 Oct 2026 10:32:30 +0200 Subject: [PATCH 2/2] chore: prepare v2.1.0 release --- CHANGELOG.md | 11 +++++++++++ composer.json | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55affea..ec8b6c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,17 @@ All notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.1.0] - 2026-10-13 + +### Changed + +- `handleRequest()` no longer sends the RSL license `Link` header + (`Link: <…/license.xml>; rel="license"; type="application/rsl+xml"`) on + plain allowed responses: non-bot requests, valid tokens and `DISABLED` + mode. It is still sent on the `OBSERVE` missing-token signal and on every + blocked response, which matches the TypeScript SDK. Reverts #15. + `ResponseBuilder` method signatures are unchanged. + ## [2.0.1] - 2026-08-27 Re-release of 2.0.0 with no code changes. Packagist crawled an early, diff --git a/composer.json b/composer.json index 7e098fc..88321c6 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "getsupertab/connect-sdk-php", "description": "Supertab Connect PHP SDK", "type": "library", - "version": "v2.0.1", + "version": "v2.1.0", "license": "MIT", "keywords": ["supertab", "connect", "license", "jwt", "rsl"], "homepage": "https://github.com/getsupertab/connect-sdk-php",