fix: browser WebSocket authentication via query param#38
Conversation
Browsers' native WebSocket doesn't support custom headers, so the Authorization header was being interpreted as a subprotocol string. Detect browser vs Node.js and pass apiKey as a URL query parameter in browsers while keeping header auth for Node.js. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello @pedrocrvz, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical authentication compatibility issue for WebSocket connections in browser environments. By dynamically adjusting the authentication method based on the execution environment, it ensures that browser-based applications can reliably establish WebSocket connections, which was previously hindered by limitations in native browser WebSocket implementations regarding custom headers. The inclusion of a dedicated browser example further facilitates adoption and testing of this new functionality. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly fixes WebSocket authentication for browser environments by switching from authorization headers to an apiKey query parameter. While this addresses a technical limitation, it introduces a security risk by exposing sensitive information in the URL. The core logic changes in src/ws/manager.ts are sound and well-supported by tests, and the browser-ws example is a valuable addition. I've also provided suggestions on the new example file to improve error handling in UI event listeners.
Summary
WebSocketclass doesn't support custom headers — the{ headers: { Authorization: ... } }options object was being interpreted as a subprotocol string ("[object Object]")apiKeyas a URL query parameter (wss://host/ws?apiKey=xxx) in browsers, keeping header-based auth for Node.jsTest plan
pnpm test)pnpm build)apiKeyquery parameter on the/wsendpointexamples/browser-wsproject🤖 Generated with Claude Code