Skip to content

feat(core-origin-manager): add origin check manager - #2183

Open
mateuszpiatkowski-da wants to merge 16 commits into
mainfrom
mateuszpiatkowski-da/add-origin-check
Open

feat(core-origin-manager): add origin check manager#2183
mateuszpiatkowski-da wants to merge 16 commits into
mainfrom
mateuszpiatkowski-da/add-origin-check

Conversation

@mateuszpiatkowski-da

Copy link
Copy Markdown
Contributor

No description provided.

Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
@mateuszpiatkowski-da
mateuszpiatkowski-da requested a review from a team as a code owner July 21, 2026 17:37
@mateuszpiatkowski-da mateuszpiatkowski-da self-assigned this Jul 21, 2026
Comment thread core/provider-dapp/src/DappAsyncProvider.ts Outdated
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>
alexmatson-da
alexmatson-da previously approved these changes Jul 29, 2026

@alexmatson-da alexmatson-da left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks good

Comment thread internal/origin-check/package.json Outdated
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>
@mateuszpiatkowski-da mateuszpiatkowski-da changed the title fix: add origin check fix: add origin check solution Aug 1, 2026
@mateuszpiatkowski-da mateuszpiatkowski-da changed the title fix: add origin check solution feat(core-origin-manager): add origin check solution Aug 1, 2026
Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>
Comment thread core/test-token/README.md
@mateuszpiatkowski-da mateuszpiatkowski-da changed the title feat(core-origin-manager): add origin check solution feat(core-origin-manager): add origin check manger Aug 3, 2026
@mateuszpiatkowski-da mateuszpiatkowski-da changed the title feat(core-origin-manager): add origin check manger feat(core-origin-manager): add origin check manager Aug 3, 2026
…add-origin-check

Signed-off-by: Mateusz Piątkowski <mateusz.piatkowski@digitalasset.com>

@alexmatson-da alexmatson-da left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nice stuff!

abstract class OriginManager {
protected allowedOrigins: Set<Location['origin']> = new Set()
protected abstract readonly messageToReceive: OriginHandshakeMessage
protected abstract readonly listenerCallback: (event: MessageEvent) => void

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nitpick, maybe rename to handshakeCallback for clarity

/**
* Starts periodically broadcasting handshake messages to the child origin.
*/
public poll(origin: Location['origin'], intervalMs = 500) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

another nitpick, I would maybe name this connect -- the polling is an implementation detail of connecting via the handshake

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(nvm, ignore this comment if you decide to make .poll an internal detail instead of public, as per the comment below)

Comment on lines +56 to +72
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)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Comment on lines +85 to +90
// 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)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants