{"record":{"id":"1d789f783287f542","repo":"sveltejs/kit","slug":"the-port-header-header-specified-port-which","errorCode":null,"errorMessage":"The ${port_header} header specified ${port} which is an invalid port because it is not a number. The value should only contain the port number (e.g. 443)","messagePattern":"The (.+?) header specified (.+?) which is an invalid port because it is not a number\\. The value should only contain the port number \\(e\\.g\\. 443\\)","errorType":"http","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/adapter-bun/src/handler.js","lineNumber":88,"sourceCode":"\t\t(protocol_header && request.headers.get(protocol_header)) || 'https'\n\t);\n\tif (!/^https?$/i.test(protocol)) {\n\t\tthrow new Error(\n\t\t\t`The ${protocol_header} header specified ${protocol} which is an invalid protocol scheme. It should only contain the protocol scheme (e.g. \\`https\\`)`\n\t\t);\n\t}\n\n\tconst host =\n\t\t(host_header && request.headers.get(host_header)) || (request.headers.get('host') ?? url.host);\n\tif (!host) {\n\t\tthrow new Error(\n\t\t\t`Could not determine host from the ${host_header ? `${host_header} or ` : ''}host header`\n\t\t);\n\t}\n\n\tconst port = port_header ? request.headers.get(port_header) : null;\n\tif (port && isNaN(+port)) {\n\t\tthrow new Error(\n\t\t\t`The ${port_header} header specified ${port} which is an invalid port because it is not a number. The value should only contain the port number (e.g. 443)`\n\t\t);\n\t}\n\n\t// canonicalized so the caller's comparison with url.origin matches (case, default ports)\n\treturn new URL(`${protocol}://${host}${port ? `:${port}` : ''}`).origin;\n}\n\n/**\n * @param {Request} request\n * @param {BunServer<undefined>} bun_server\n * @returns {string}\n */\nfunction get_client_address(request, bun_server) {\n\tif (!address_header) {\n\t\t// requestIP() is null over unix sockets; undefined matches adapter-node\n\t\treturn /** @type {string} */ (bun_server.requestIP(request)?.address);\n\t}","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/sveltejs/kit/blob/03f1687fe612ce3d2d9131139b5b188d9cf90c64/packages/adapter-bun/src/handler.js#L70-L106","documentation":"If PORT_HEADER is configured, its value must be a pure numeric port (e.g. 443), because it is appended to `protocol://host` to rebuild the origin. A non-numeric value like 'https,443' or '443/tcp' makes isNaN(+port) true and the adapter throws.","triggerScenarios":"A proxy sends a composite value in the port header such as `x-forwarded-port: 8443,8443` (multiple hops) or `host:443` including the host part.","commonSituations":"Multi-layer proxies appending to X-Forwarded-Port; misconfigured proxy forwarding the full host:port pair into the port header.","solutions":["Configure the outermost/innermost proxy to send a single numeric port, e.g. nginx: `proxy_set_header X-Forwarded-Port $server_port;`.","Strip the header at the trusted edge so only one value survives: `proxy_set_header X-Forwarded-Port $http_x_forwarded_port;` replacing multi-value input.","Remove the PORT_HEADER config if you don't need a custom port header."],"exampleFix":"// before\nproxy_set_header X-Forwarded-Port \"$http_host\";\n// after\nproxy_set_header X-Forwarded-Port $server_port;","handlingStrategy":"validation","validationCode":"const port = req.headers.get('x-forwarded-port');\nif (port && isNaN(Number(port.trim()))) throw new Error(`Non-numeric forwarded port: ${port}`);","typeGuard":null,"tryCatchPattern":"try {\n  origin = getOrigin(req);\n} catch (err) {\n  if (/is an invalid port/.test(err.message)) {\n    console.error('Proxy must send a single numeric port in the port header');\n  }\n  throw err;\n}","preventionTips":["Use $server_port (nginx) or equivalent for the forwarded port header.","Overwrite rather than append multi-hop port values at the trusted edge.","Don't set PORT_HEADER unless you need origin-port fidelity."],"tags":["http-headers","proxy","adapter-bun"],"backgroundTag":"invalid-forwarded-header","analyzedSha":"03f1687fe612ce3d2d9131139b5b188d9cf90c64","analyzedAt":"2026-09-02T02:01:50.504Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}