-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Access the top window in a safe manner (#550)
Fixes Uncaught DOMException: Failed to read a named property 'addEventListener' from 'Window': Blocked a frame with origin "xxx" from accessing a cross-origin frame.
- Loading branch information
1 parent
e35706d
commit fa60029
Showing
5 changed files
with
49 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,11 @@ | ||
export default function getTopWindow() { | ||
// intercept the exception if we have cross-origin iframe | ||
try { | ||
if (window.top.location.href !== undefined) { | ||
return window.top; | ||
} | ||
} catch (e) { | ||
// provide fallback | ||
} | ||
return window; | ||
} |
This file contains 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,31 @@ | ||
import { expect } from 'chai'; | ||
import getTopWindow from './getTopWindow'; | ||
|
||
describe('utils/getTopWindow()', () => { | ||
it('returns top window if no iframe', () => { | ||
const window = { location: { href: 'http://example.com' } }; | ||
window.top = window; | ||
|
||
global.window = window; | ||
expect(getTopWindow()).to.equal(window.top); | ||
delete global.window; | ||
}); | ||
|
||
it('returns top window if no cross-origin iframe', () => { | ||
const window = { location: { href: 'http://example.com/viewer.html' } }; | ||
window.top = { location: { href: 'http://example.com/index.html' } }; | ||
|
||
global.window = window; | ||
expect(getTopWindow()).to.equal(window.top); | ||
delete global.window; | ||
}); | ||
|
||
it('returns window if called inside cross-origin iframe', () => { | ||
const window = { location: { href: 'http://example.com:8000' } }; | ||
window.top = { locationRestricted: { href: 'http://example.com:8001' } }; | ||
|
||
global.window = window; | ||
expect(getTopWindow()).to.equal(window); | ||
delete global.window; | ||
}); | ||
}); |