MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / splitHostPort

Function splitHostPort

src/ui-server/security.ts:119–138  ·  view source on GitHub ↗

* Split a `Host` header into hostname and port, or `null` if it is malformed. * * An unbracketed IPv6 literal (`::1`) is malformed per RFC 7230 and is rejected * rather than guessed at — no browser produces one, so accepting it would only * widen the parser for an attacker's benefit.

(host: string)

Source from the content-addressed store, hash-verified

117 * widen the parser for an attacker's benefit.
118 */
119function splitHostPort(host: string): HostParts | null {
120 const trimmed = host.trim();
121 if (!trimmed) return null;
122
123 if (trimmed.startsWith('[')) {
124 const end = trimmed.indexOf(']');
125 if (end < 0) return null;
126 const port = parsePortSuffix(trimmed.slice(end + 1));
127 if (port === null) return null;
128 return { hostname: trimmed.slice(1, end), port };
129 }
130
131 const colon = trimmed.indexOf(':');
132 if (colon === -1) return { hostname: trimmed, port: undefined };
133 // A second colon without brackets is a bare IPv6 literal or junk.
134 if (trimmed.indexOf(':', colon + 1) !== -1) return null;
135 const port = parsePortSuffix(trimmed.slice(colon));
136 if (port === null) return null;
137 return { hostname: trimmed.slice(0, colon), port };
138}
139
140/**
141 * Parse the `:1234` tail of a `Host` header.

Callers 1

isAllowedHostFunction · 0.85

Calls 1

parsePortSuffixFunction · 0.85

Tested by

no test coverage detected