From 6de530940cb8680fb51c0a63e7af8ae77d814941 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 01:37:38 +0000 Subject: [PATCH] Add failing tests for endpoint URLs that replace the API host. list, get, create, update, and delete currently forward an absolute or protocol-relative endpoint to the HTTP client with the API key header. These tests require that input to be rejected before any request is sent. --- tests/EndpointUrlSecurityTest.php | 169 ++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 tests/EndpointUrlSecurityTest.php diff --git a/tests/EndpointUrlSecurityTest.php b/tests/EndpointUrlSecurityTest.php new file mode 100644 index 0000000..6a7dd1b --- /dev/null +++ b/tests/EndpointUrlSecurityTest.php @@ -0,0 +1,169 @@ +> */ + private $container; + + protected function setUp(): void + { + $this->handlerStack = HandlerStack::create(); + $this->container = []; + $container = &$this->container; + + $this->mock = new MockHandler([]); + $this->handlerStack->setHandler($this->mock); + $this->handlerStack->push(Middleware::history($container)); + } + + /** + * @dataProvider unsafeEndpointProvider + */ + public function testUnsafeEndpointIsRejectedBeforeAnyRequest(string $operation, string $endpoint, ?string $contentId): void + { + $this->mock->append(new Response(200, [], '{"id":"content-id"}')); + $client = new Client( + self::SERVICE_DOMAIN, + self::API_KEY, + new \GuzzleHttp\Client(['handler' => $this->handlerStack]) + ); + + try { + $this->invoke($client, $operation, $endpoint, $contentId); + } catch (\InvalidArgumentException $exception) { + $this->assertNotSame('', $exception->getMessage()); + $this->assertSame( + [], + $this->container, + 'The API key request must not be handed to the HTTP client before it is refused' + ); + + return; + } + + $this->fail($this->describeRequests($operation, $endpoint)); + } + + /** + * @return array + */ + public function unsafeEndpointProvider(): array + { + $endpoints = [ + 'https scheme' => 'https://attacker.example/collect', + 'http scheme' => 'http://attacker.example/collect', + 'protocol-relative' => '//attacker.example/collect', + 'uppercase scheme' => 'HTTPS://attacker.example/collect', + 'userinfo host' => 'https://service-domain.microcms.io@attacker.example/collect', + 'dotted suffix host' => 'https://service-domain.microcms.io.attacker.example/collect', + ]; + + $cases = []; + foreach ($endpoints as $name => $endpoint) { + $cases['list ' . $name] = ['list', $endpoint, null]; + $cases['get ' . $name] = ['get', $endpoint, '']; + $cases['get with content id ' . $name] = ['get', $endpoint, 'content-id']; + $cases['create ' . $name] = ['create', $endpoint, null]; + $cases['create with content id ' . $name] = ['create', $endpoint, 'content-id']; + $cases['update ' . $name] = ['update', $endpoint, null]; + $cases['update with content id ' . $name] = ['update', $endpoint, 'content-id']; + $cases['delete ' . $name] = ['delete', $endpoint, 'content-id']; + } + + return $cases; + } + + private function invoke(Client $client, string $operation, string $endpoint, ?string $contentId): void + { + switch ($operation) { + case 'list': + $client->list($endpoint, ['limit' => 1]); + + return; + case 'get': + $client->get($endpoint, $contentId ?? '', ['depth' => 1]); + + return; + case 'create': + $body = ['title' => 'title']; + if ($contentId !== null) { + $body['id'] = $contentId; + } + $client->create($endpoint, $body, ['status' => 'draft']); + + return; + case 'update': + $body = ['title' => 'title']; + if ($contentId !== null) { + $body['id'] = $contentId; + } + $client->update($endpoint, $body); + + return; + case 'delete': + $client->delete($endpoint, $contentId ?? 'content-id'); + + return; + default: + $this->fail('Unknown operation ' . $operation); + } + } + + private function describeRequests(string $operation, string $endpoint): string + { + if ($this->container === []) { + return sprintf( + '%s accepted endpoint %s and returned without refusing it', + $operation, + $endpoint + ); + } + + $lines = []; + foreach ($this->container as $entry) { + $request = $entry['request']; + if (!$request instanceof RequestInterface) { + $lines[] = 'non-request entry'; + + continue; + } + + $lines[] = sprintf( + '%s %s host=%s api-key-header=%s', + $request->getMethod(), + (string) $request->getUri(), + $request->getUri()->getHost(), + $request->hasHeader('X-MICROCMS-API-KEY') ? 'present' : 'absent' + ); + } + + return sprintf( + '%s sent %d request(s) for endpoint %s: %s', + $operation, + count($this->container), + $endpoint, + implode('; ', $lines) + ); + } +}