feat(core-origin-manager): add origin check manager - #2183
feat(core-origin-manager): add origin check manager#2183mateuszpiatkowski-da wants to merge 16 commits into
Conversation
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
…add-origin-check Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
…add-origin-check Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
…add-origin-check Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
| abstract class OriginManager { | ||
| protected allowedOrigins: Set<Location['origin']> = new Set() | ||
| protected abstract readonly messageToReceive: OriginHandshakeMessage | ||
| protected abstract readonly listenerCallback: (event: MessageEvent) => void |
There was a problem hiding this comment.
nitpick, maybe rename to handshakeCallback for clarity
| /** | ||
| * Starts periodically broadcasting handshake messages to the child origin. | ||
| */ | ||
| public poll(origin: Location['origin'], intervalMs = 500) { |
There was a problem hiding this comment.
another nitpick, I would maybe name this connect -- the polling is an implementation detail of connecting via the handshake
There was a problem hiding this comment.
(nvm, ignore this comment if you decide to make .poll an internal detail instead of public, as per the comment below)
| import { ParentWindowOriginManager } from '@canton-network/core-origin-check' | ||
|
|
||
| // Create a manager instance | ||
| const originManager = new ParentWindowOriginManager() | ||
|
|
||
| // Start polling for a specific origin | ||
| const childOrigin = 'https://child.example.com' | ||
| originManager.poll(childOrigin) | ||
|
|
||
| // Send a message using the safe postMessage method | ||
| // This will only succeed if the handshake is complete | ||
| originManager.postMessage({ type: 'greeting', data: 'hello' }, childOrigin) | ||
|
|
||
| // Or manually check before sending | ||
| if (originManager.assert(childOrigin)) { | ||
| window.postMessage(data, childOrigin) | ||
| } |
There was a problem hiding this comment.
I think ideally the polling / handshake process is made invisible to the user, so they don't need to call .poll manually. ParentWindowOriginManager can probably track the connected status per origin, then
const originManager = new ParentWindowOriginManager()
const childOrigin = 'https://child.example.com'
// first time -- call `.poll` under the hood. throws any connection errors
originManager.postMessage({ type: 'greeting', data: 'hello' }, childOrigin)
// second time -- already connected, msg goes straight through
originManager.postMessage({ type: 'greeting': data: 'hello-2' }, childOrigin)| // Send a message using the safe postMessage method | ||
| // This will only succeed if the handshake is complete | ||
| const parentOrigin = window.opener?.location.origin | ||
| if (parentOrigin) { | ||
| originManager.postMessage({ type: 'response', data: 'world' }, parentOrigin) | ||
| } |
There was a problem hiding this comment.
I would just move this check inside the implementation as well. The constructor could take an optional parentOrigin if the user wants to override it, but window.opener should be the default
There was a problem hiding this comment.
actually window.opener.location is problematic because browsers block access to this field across origins. The child manager should just use the origin it got from the handshake as the parent origin
No description provided.