← Back
Editing: KidValidator.php
<?php namespace GoDaddy\Auth; /** * Validates JWT key ID (kid) headers to prevent path traversal attacks. */ class KidValidator { /** * Maximum allowed length for a key ID. * Real key IDs are short (UUIDs, hex digests, base64url strings). */ private const MAX_KID_BYTES = 256; /** * Allowed characters pattern: ASCII letters, digits, and - _ . ~ + = : * These cover UUID, hex, base64, and base64url key IDs. * Refused: / \ ? # % @ whitespace, control characters, and non-ASCII. */ private const ALLOWED_PATTERN = '/^[A-Za-z0-9\-_.~+=:]+$/'; public const REASON_EMPTY_OR_INVALID_TYPE = 1; public const REASON_TOO_LONG = 2; public const REASON_DISALLOWED_CHARACTERS = 3; public const REASON_PATH_TRAVERSAL = 4; /** * Validates a JWT kid header value for security. * * The kid reaches the path of the SSO key API (/v1/api/key/{kid}) and the * signing-key cache key. The value is not signed, so a caller controls it * before any signature check. A kid holding /, .., ?, #, or % could * otherwise send the key request to a different endpoint. * * @param string|null $kid The kid value from the JWT header * @return string The same kid, when it is safe to use * @throws AuthException When the kid breaks security rules */ public static function validate($kid): string { // Check type and emptiness if (!is_string($kid) || empty($kid)) { throw new AuthException('Bad kid', self::REASON_EMPTY_OR_INVALID_TYPE); } // Check length if (strlen($kid) > self::MAX_KID_BYTES) { throw new AuthException('Bad kid', self::REASON_TOO_LONG); } // Check allowed characters if (!preg_match(self::ALLOWED_PATTERN, $kid)) { throw new AuthException('Bad kid', self::REASON_DISALLOWED_CHARACTERS); } // Check for path traversal sequences // '.' is permitted inside a key ID, but path-traversal sequences are not if ($kid === '.' || strpos($kid, '..') !== false) { throw new AuthException('Bad kid', self::REASON_PATH_TRAVERSAL); } return $kid; } }
Save File
Cancel