Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion flow-typed/npm/ws_v7.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ declare class ws$WebSocketServer extends events$EventEmitter {
perMessageDeflate?: boolean | ws$PerMessageDeflateOptions,
port?: number,
server?: http$Server | https$Server,
verifyClient?: () => unknown,
verifyClient?: (
info: Readonly<{
origin: string,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should fix · origin is typed as string, but ws passes undefined when the client sends no Origin header

ws 7.x builds the info object as origin: req.headers[version === 8 ? 'sec-websocket-origin' : 'origin'] (ws/lib/websocket-server.js:252-257). Browsers always send that header. Node clients, CLI tools, native apps and many proxies often don't, and then info.origin is undefined.

With this libdef, verifyClient: info => info.origin.startsWith('http://localhost') type-checks, but it throws a TypeError inside the server's upgrade handling when such a client connects. The stated motivation is callers that read origin, and an origin allowlist is exactly where a missing-header case matters, so the type should make callers handle it.

Suggested fix: origin?: string (or origin: string | void). React Native's copy of this definition should get the same change.

Optional, since the old () => unknown type also rejected it: ws also supports an async form, (info, cb) => void, selected by verifyClient.length === 2. The new type still rejects it with "requires another argument". If you want it covered, add an optional callback?: (verified: boolean, code?: number, message?: string, headers?: {[string]: string}) => void parameter.

Reproduced: repro/origin-repro.js starts a ws 7.5.10 server with a verifyClient that logs info.origin, then connects with the Node ws client. It prints origin: undefined undefined. repro/__review_verifyClient.js, placed temporarily in packages/metro/src/lib, used info.origin.startsWith(...) and Flow accepted it with no error. The same file's two-argument (info, cb) form failed with incompatible-type, "requires another argument". · id 487658c6

secure: boolean,
req: http$IncomingMessage<>,
}>,
) => unknown,
},
callback?: () => unknown,
): this;
Expand Down
Loading