forked from rrweb-io/rrweb
-
Notifications
You must be signed in to change notification settings - Fork 2
SR-478 Add cross origin support #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dad07de
Add cross origin support
dhaval-valotia efa288a
Apply formatting changes
dhaval-valotia f97aa16
update tests
dhaval-valotia 1079fe7
Rename to allowedIframOrigin and fallback to *
dhaval-valotia 42c8574
Apply formatting changes
dhaval-valotia fea6fa4
Remove check on parent side
dhaval-valotia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| function toOrigin(url: string): string | null { | ||
| try { | ||
| const origin = new URL(url).origin; | ||
| return origin !== 'null' ? origin : null; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| export function buildAllowedOriginSet(origins: string[]): ReadonlySet<string> { | ||
| if (!Array.isArray(origins) || origins.length === 0) { | ||
| throw new Error( | ||
| '[rrweb] allowedIframeOrigins must be a non-empty array of origin strings.', | ||
| ); | ||
| } | ||
|
|
||
| const set = new Set<string>(); | ||
| for (let i = 0; i < origins.length; i++) { | ||
| const entry = origins[i]; | ||
| if (typeof entry !== 'string') { | ||
| throw new Error( | ||
| `[rrweb] allowedIframeOrigins[${i}] must be a string, got ${typeof entry}.`, | ||
| ); | ||
| } | ||
| const origin = toOrigin(entry); | ||
| if (origin) { | ||
| set.add(origin); | ||
| } | ||
| } | ||
|
|
||
| return Object.freeze(set); | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ import { | |
| registerErrorHandler, | ||
| unregisterErrorHandler, | ||
| } from './error-handler'; | ||
| import { buildAllowedOriginSet } from './cross-origin-utils'; | ||
| import dom from '@rrweb/utils'; | ||
|
|
||
| let wrappedEmit!: (e: eventWithoutTime, isCheckout?: boolean) => void; | ||
|
|
@@ -92,7 +93,8 @@ function record<T = eventWithTime>( | |
| mousemoveWait, | ||
| recordDOM = true, | ||
| recordCanvas = false, | ||
| recordCrossOriginIframes = false, | ||
| recordCrossOriginIframes: _recordCrossOriginIframes = false, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this change?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was from before, but we don't need this anymore. reverted |
||
| allowedIframeOrigins, | ||
| recordAfter = options.recordAfter === 'DOMContentLoaded' | ||
| ? options.recordAfter | ||
| : 'load', | ||
|
|
@@ -107,6 +109,19 @@ function record<T = eventWithTime>( | |
|
|
||
| registerErrorHandler(errorHandler); | ||
|
|
||
| const recordCrossOriginIframes = _recordCrossOriginIframes; | ||
| let validatedOrigins: ReadonlySet<string> | undefined; | ||
| if ( | ||
| recordCrossOriginIframes && | ||
| allowedIframeOrigins && | ||
| allowedIframeOrigins.length > 0 | ||
| ) { | ||
| validatedOrigins = buildAllowedOriginSet(allowedIframeOrigins); | ||
| if (validatedOrigins.size === 0) { | ||
| validatedOrigins = undefined; | ||
| } | ||
| } | ||
|
|
||
| const inEmittingFrame = recordCrossOriginIframes | ||
| ? window.parent === window | ||
| : true; | ||
|
|
@@ -231,7 +246,13 @@ function record<T = eventWithTime>( | |
| origin: window.location.origin, | ||
| isCheckout, | ||
| }; | ||
| window.parent.postMessage(message, '*'); | ||
| if (validatedOrigins) { | ||
| for (const targetOrigin of validatedOrigins) { | ||
| window.parent.postMessage(message, targetOrigin); | ||
| } | ||
| } else { | ||
| window.parent.postMessage(message, '*'); | ||
| } | ||
| } | ||
|
|
||
| if (e.type === EventType.FullSnapshot) { | ||
|
|
@@ -304,6 +325,7 @@ function record<T = eventWithTime>( | |
| stylesheetManager: stylesheetManager, | ||
| recordCrossOriginIframes, | ||
| wrappedEmit, | ||
| allowedIframeOrigins: validatedOrigins, | ||
| }); | ||
|
|
||
| /** | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this renamed? is this test not working or not running
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test was working since we had the fallback to all origins '*'. Ideally should have another assertion to check the target origin in the post message or another test entirely.