← Back
Editing: JwtKidSecurityTest.php
<?php namespace GoDaddy\Auth\Tests; use GoDaddy\Auth\AuthException; use GoDaddy\Auth\AuthHttpClient; use GoDaddy\Auth\AuthHttpClientInterface; use GoDaddy\Auth\AuthManager; use GoDaddy\Auth\AuthResult; use GoDaddy\Auth\HttpClientInterface; use PHPUnit\Framework\TestCase; class JwtKidSecurityTest extends TestCase { /** * Test that forged kid in JWT header is rejected before any HTTP request */ public function testRejectsForgedKidBeforeHttpRequest() { // Create mock HTTP client that should NOT be called $mockHttpClient = $this->createMock(HttpClientInterface::class); $mockHttpClient->expects($this->never()) ->method('get'); $mockHttpClient->expects($this->never()) ->method('post'); $mockAuthClient = $this->createMock(AuthHttpClientInterface::class); $mockAuthClient->expects($this->never()) ->method('getPublicKey'); $authManager = new AuthManager($mockAuthClient, null, 'test-app'); // Create JWT with malicious kid $header = json_encode([ 'alg' => 'RS256', 'kid' => '../../api/federation/cross-domain?app=attacker.example%23' ]); $payload = json_encode(['test' => 'data']); $signature = 'fake-signature'; $token = base64_encode($header) . '.' . base64_encode($payload) . '.' . base64_encode($signature); // Should throw exception before any HTTP request $this->expectException(AuthException::class); $authManager->getAuthPayloadShopper('sso.godaddy.com', $token, null, 1); } /** * Test all malicious kid patterns are blocked */ public function testBlocksMaliciousKidPatterns() { $maliciousKids = [ '../../etc/passwd', '../../../admin', 'https://evil.com/key', '//evil.com/key', 'key?evil=param', 'key#fragment', 'key%2e%2e%2f', 'key/../other', '.', '..', 'key with space', "key\nX-Injected: header", "key\r\nSet-Cookie: evil", 'key@evil.com', ]; $mockAuthClient = $this->createMock(AuthHttpClientInterface::class); $mockAuthClient->expects($this->never()) ->method('getPublicKey'); $authManager = new AuthManager($mockAuthClient, null, 'test-app'); foreach ($maliciousKids as $kid) { $header = json_encode(['alg' => 'RS256', 'kid' => $kid]); $payload = json_encode(['test' => 'data']); $token = base64_encode($header) . '.' . base64_encode($payload) . '.signature'; try { $authManager->getAuthPayloadShopper('sso.godaddy.com', $token, null, 1); $this->fail("Should have rejected malicious kid: $kid"); } catch (AuthException $e) { // Expected - kid should be rejected $this->assertNotNull($e); } } } /** * Test that valid kids are accepted and processed normally */ public function testAcceptsValidKids() { $validKids = [ 'test_kid', '0e2f4b1c-9a3d-4a5e-8b7c', 'MIIBIjANBgkqhkiG9w0=', 'key-2024', 'v1.2.3', ]; // Create mock that returns a valid key response $mockKeyResponse = json_encode([ 'code' => 1, 'message' => 'success', 'data' => [ 'n' => 'test-modulus', 'e' => 'AQAB' ] ]); foreach ($validKids as $kid) { $mockHttpClient = $this->createMock(HttpClientInterface::class); $mockHttpClient->expects($this->once()) ->method('get') ->with($this->matchesRegularExpression('/\/v1\/api\/key\//')) ->willReturn($mockKeyResponse); $mockAuthClient = new class($mockHttpClient, 'test-app') extends AuthHttpClient { public function __construct($httpClient, $appCode) { // Use injected mock parent::__construct($httpClient, $appCode); } public function getPublicKey(string $host, string $publicKeyId): AuthResult { // This should be called with validated kid $this->validateKidFormat($publicKeyId); return parent::getPublicKey($host, $publicKeyId); } private function validateKidFormat($kid) { // Verify the kid passed here is already validated (no path traversal chars) if (strpos($kid, '..') !== false || strpos($kid, '/') !== false || strpos($kid, '?') !== false || strpos($kid, '#') !== false) { throw new \Exception("Unvalidated kid reached HTTP layer: $kid"); } } }; // This should work without throwing try { $authManager = new AuthManager($mockAuthClient, null, 'test-app'); $header = json_encode(['alg' => 'RS256', 'kid' => $kid]); $payload = json_encode(['test' => 'data']); $token = base64_encode($header) . '.' . base64_encode($payload) . '.fake-sig'; // This will fail at signature verification, but that's ok // We're testing that the kid validation passes $authManager->getAuthPayloadShopper('sso.godaddy.com', $token, null, 1); } catch (AuthException $e) { // Should only fail at signature verification, not kid validation $this->assertStringNotContainsString('Bad kid', $e->getMessage()); } } } }
Save File
Cancel