fix(websocket): stop requiring ACP subprotocol - #17
Open
roseengineering wants to merge 1 commit into
Open
roseengineering wants to merge 1 commit into
roseengineering wants to merge 1 commit into
Conversation
I was learning ACP against my Goose server and the browser websocket handshake kept failing. The client was always offering acp.v1, which can make a plain /acp websocket reject the upgrade if the server does not echo that subprotocol. This patch leaves normal ACP websocket connections subprotocol-free and only adds a subprotocol when bearer auth has to be tunneled through the browser API.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts the ACP WebSocket transport to avoid sending a Sec-WebSocket-Protocol header unless it’s actually needed for browser-auth tunneling, preventing browser handshake failures when servers don’t negotiate the ACP subprotocol.
Changes:
- Avoid passing an empty subprotocol list to the WebSocket constructor (omit the parameter entirely when none are needed).
- Make subprotocol advertisement conditional, so plain ACP WebSocket connections don’t always include
acp.v1.
Comments suppressed due to low confidence (1)
src/lib/transport/websocket.ts:294
buildSubprotocolscurrently advertisesacp.v1whenever anAuthorizationheader is present, even if it is not a Bearer token (or if the Bearer parse fails). That can reintroduce the browser handshake failure this PR is trying to avoid, because it will send a non-emptySec-WebSocket-Protocolheader without actually tunneling auth. Consider only returning subprotocols when a validAuthorization: Bearer …value is successfully parsed, and update the doc comment accordingly.
if (!headers) return [];
const auth = pickHeader(headers, 'authorization');
if (!auth) return [];
const protocols: string[] = [ACP_SUBPROTOCOL];
{
const m = /^Bearer\s+(.+)$/i.exec(auth.trim());
if (m) {
// Subprotocol tokens cannot contain whitespace; the bearer token in
// practice is base64-ish so this is safe, but we still strip just in
// case to avoid handing the browser an invalid header value.
const tok = m[1].replace(/\s+/g, '');
protocols.push(`bearer.${tok}`);
}
}
return protocols;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I’m learning ACP by testing this browser app against my Goose server, and I made this fix because the websocket handshake was failing in the browser path.
I reproduced the browser-side failure on
mainand confirmed it happens before session creation:Sec-WebSocket-Protocolheader on the websocket upgradeWebSocket connect failedThis patch removes the unconditional
acp.v1websocket subprotocol for plain ACP websocket connections. It only adds a subprotocol when bearer auth actually needs to be tunneled through the browser WebSocket API.