fix: validate event.origin in handleConnect to prevent channel hijacking#9
Open
kapeka0 wants to merge 1 commit into
Open
fix: validate event.origin in handleConnect to prevent channel hijacking#9kapeka0 wants to merge 1 commit into
kapeka0 wants to merge 1 commit into
Conversation
A malicious window that knows the channelId can hijack a legitimate BIDC channel by sending a bidc-connect message with a matching channelId, since handleConnect never checked the message origin. Add an optional `origin` argument to createChannel. When provided, incoming connections whose event.origin differs are rejected, and the origin is used as the targetOrigin for outbound postMessage calls instead of '*'. The origin must be declared by the caller because it cannot be derived from a cross-origin target. Behaviour is unchanged when origin is omitted. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
handleConnectaccepts any incomingbidc-connectmessage as long as thechannelIdmatches — it never checks where the message came from. Since thedefault
channelIdis"bidc_default"and any window with a handle to thetarget (opener, parent, named frame) can
postMessageto it, a maliciouswindow that knows or guesses the
channelIdcan complete the handshake andhijack a legitimate channel, intercepting all subsequent communication.
A possible fix is to add an optional
originargument tocreateChannel. When provided:event.origindiffers from the declaredorigin are rejected in
handleConnect.targetOriginforpostMessageinstead of
'*', so handshake messages aren't broadcast to other origins.The expected origin has to be declared by the caller — it can't be derived
from a cross-origin target (
contentWindow.originthrows aSecurityError),and deriving it from the first handshake (trust-on-first-use) is a race an
attacker can win, so it wouldn't actually be safe.
Fully backward compatible: without origin, behaviour is identical to before.