coder/code-server · error · HttpError

Invalid port

Error message

Invalid port

What it means

Thrown by getProxyTarget() in the path proxy route when req.params.port cannot be parsed into a valid number via parseInt(..., 10). It is raised as an HttpError with status 400 (HttpCode.BadRequest), meaning Express's error handler converts it into a proper HTTP 400 response rather than a 500. The proxy is the /:port/... route that forwards requests to a local port.

Source

Thrown at src/node/routes/pathProxy.ts:19

import { Request, Response } from "express"
import * as path from "path"
import { HttpCode, HttpError } from "../../common/http"
import { ensureProxyEnabled, authenticated, ensureAuthenticated, ensureOrigin, redirect, self } from "../http"
import { proxy as _proxy } from "../proxy"
import type { WebsocketRequest } from "../wsRouter"

const getProxyTarget = (
  req: Request,
  opts?: {
    proxyBasePath?: string
  },
): string => {
  // If there is a base path, strip it out.
  const base = (req as any).base || ""
  // Cast since we only have one port param.
  const port = parseInt(req.params.port as string, 10)
  if (isNaN(port)) {
    throw new HttpError("Invalid port", HttpCode.BadRequest)
  }
  return `http://0.0.0.0:${port}${opts?.proxyBasePath || ""}/${req.originalUrl.slice(base.length)}`
}

export async function proxy(
  req: Request,
  res: Response,
  opts?: {
    passthroughPath?: boolean
    proxyBasePath?: string
  },
): Promise<void> {
  ensureProxyEnabled(req)

  if (req.method === "OPTIONS" && req.args["skip-auth-preflight"]) {
    // Allow preflight requests with `skip-auth-preflight` flag
  } else if (!(await authenticated(req))) {
    // If visiting the root (/:port only) redirect to the login page.

View on GitHub (pinned to 51f90a376b)

Solutions

  1. Correct the URL so the :port segment is a positive integer (1-65535), e.g. /proxy/3000/.
  2. If generating proxy links programmatically, coerce and validate the port with Number.isInteger before constructing the URL.
  3. Ensure your routing prefix is not consuming the port segment (check req.params and the base-path config).
  4. If 400s appear unexpectedly, inspect the request URL logged upstream to find the malformed segment.

Example fix

// before
const url = `/proxy/${maybePort}/`  // maybePort could be 'abc'
// after
const n = Number(maybePort)
if (!Number.isInteger(n) || n < 1 || n > 65535) throw new RangeError('bad port')
const url = `/proxy/${n}/`
Defensive patterns

Strategy: type-guard

Validate before calling

// Validate the port before constructing a proxy URL
function validPort(p: unknown): p is number {
  const n = Number(p)
  return Number.isInteger(n) && n > 0 && n <= 65535
}
if (!validPort(portParam)) throw new RangeError('port must be 1-65535')

Type guard

function isPort(v: unknown): v is number {
  return typeof v === 'number' ? Number.isInteger(v) && v > 0 && v <= 65535
    : typeof v === 'string' && /^\d+$/.test(v) && +v > 0 && +v <= 65535
}

Try / catch

// Express converts the thrown HttpError to a 400 via errorHandler. In a client:
try {
  const res = await fetch(`/proxy/${port}/`)
  if (res.status === 400) throw new Error('Server rejected the port')
} catch (e) { /* log and surface */ }

Prevention

When it happens

Trigger: A request to the port-proxy route whose :port route parameter is missing, empty, or non-numeric (e.g. /proxy/abc/ or a misrouted URL where the port segment is a word). parseInt returns NaN for these, triggering the guard before the http://0.0.0.0:${port} target is constructed.

Common situations: A bad link or bookmark pointing to /proxy/<garbage>/; a route misconfiguration that lets a non-numeric segment reach the proxy handler; a client building the proxy URL from an unvalidated variable; a port passed as a string with whitespace or a leading slash.

Related errors


AI-assisted analysis of coder/code-server@51f90a376b (2026-08-12). Data as JSON: /api/errors/c02c13c4477f5347. Report an issue: GitHub.