{"record":{"id":"1f09cded8aabbf6c","repo":"getgrav/grav","slug":"uri-port-must-be-null-or-an-integer-between-0-and","errorCode":null,"errorMessage":"Uri port must be null or an integer between 0 and 65535","messagePattern":"Uri port must be null or an integer between 0 and 65535","errorType":"validation","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"system/src/Grav/Framework/Uri/UriPartsFilter.php","lineNumber":93,"sourceCode":"        return strtolower($host);\n    }\n\n    /**\n     * Filter Uri port.\n     *\n     * This method\n     *\n     * @param int|null $port\n     * @return int|null\n     * @throws InvalidArgumentException If the port is invalid.\n     */\n    public static function filterPort($port = null)\n    {\n        if (null === $port || (is_int($port) && ($port >= 0 && $port <= 65535))) {\n            return $port;\n        }\n\n        throw new InvalidArgumentException('Uri port must be null or an integer between 0 and 65535');\n    }\n\n    /**\n     * Filter Uri path.\n     *\n     * This method percent-encodes all reserved characters in the provided path string. This method\n     * will NOT double-encode characters that are already percent-encoded.\n     *\n     * @param  string $path The raw uri path.\n     * @return string       The RFC 3986 percent-encoded uri path.\n     * @throws InvalidArgumentException If the path is invalid.\n     * @link   http://www.faqs.org/rfcs/rfc3986.html\n     */\n    public static function filterPath($path)\n    {\n        if (!is_string($path)) {\n            throw new InvalidArgumentException('Uri path must be a string');\n        }","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/getgrav/grav/blob/6040efed04efa69b8209448ed81308e7c24147c2/system/src/Grav/Framework/Uri/UriPartsFilter.php#L75-L111","documentation":"UriPartsFilter::filterPort() accepts only null or an integer in [0, 65535]; anything else — a numeric string (is_int fails), a bool, a float, or an out-of-range integer — throws InvalidArgumentException. It backs AbstractUri::withPort() (declared `?int`, which coerces numeric strings) and the parts constructor, which casts to int before calling, so range violations are the usual live trigger there.","triggerScenarios":"Passing '8080' as a string directly to filterPort(); a port parsed as int but out of range (0, 65536, 70000, or a negative value); port math that produced a float (`$port * 1.0`) or a boolean from a bad comparison.","commonSituations":"Ports read from environment variables or headers as strings and passed unfiltered; concatenation/parsing bugs yielding 0 or >65535; config YAML storing the port as a string.","solutions":["Cast to int before validating: `$port = (int) $port`","Validate the range and treat invalid as 'no port': `($port >= 1 && $port <= 65535) ? $port : null`","Use the typed `$uri->withPort($port)` API so string ports coerce early"],"exampleFix":"// before\n$port = UriPartsFilter::filterPort($env['SERVER_PORT']); // '8080' string or 65536 -> throws\n\n// after\n$port = (int) $env['SERVER_PORT'];\n$port = ($port >= 1 && $port <= 65535) ? $port : null;\n$uri = $uri->withPort($port);","handlingStrategy":"validation","validationCode":"function normalizePort(mixed $port): ?int\n{\n    $port = (int) $port;\n    return ($port >= 1 && $port <= 65535) ? $port : null;\n}","typeGuard":"function isValidPort(mixed $port): bool\n{\n    return $port === null || (is_int($port) && $port >= 0 && $port <= 65535);\n}","tryCatchPattern":null,"preventionTips":["Cast env/config-derived ports to int before URI work","Treat out-of-range or zero ports as 'no port' rather than passing them on","Store ports as integers in YAML/config, not quoted strings"],"tags":["uri","port","type-check","validation"],"backgroundTag":"invalid-port-number","analyzedSha":"6040efed04efa69b8209448ed81308e7c24147c2","analyzedAt":"2026-08-17T05:07:31.593Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}