Skip to content
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

adding support of array for checkOrigin parameter of iframeResizer #203

Merged
merged 1 commit into from
May 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ Override the default body margin style in the iFrame. A string can be any valid
### checkOrigin

default: true
type: boolean
type: boolean || array

When set to true, only allow incoming messages from the domain listed in the `src` property of the iFrame tag. If your iFrame navigates between different domains, ports or protocols; then you will need to disable this option.
When set to true, only allow incoming messages from the domain listed in the `src` property of the iFrame tag. If your iFrame navigates between different domains, ports or protocols; then you will need to provide an array of domains or disable this option.

### enableInPageLinks

Expand Down
29 changes: 24 additions & 5 deletions src/iframeResizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,39 @@
messageData[dimension]=''+size;
}

function checkAllowedOrigin(origin,checkOrigin,remoteHost){
function checkList(){
log(' Checking connection is from list of origins: ' + checkOrigin);
var i;
for (i = 0; i < checkOrigin.length; i++) {
if (checkOrigin[i] === origin) {
return true;
}
}
return false;
}

function checkSingle(){
log(' Checking connection is from: '+remoteHost);
return origin == remoteHost;
}

return checkOrigin.constructor === Array ? checkList() : checkSingle();
}

function isMessageFromIFrame(){
var
origin = event.origin,
remoteHost = messageData.iframe.src.split('/').slice(0,3).join('/');

if (settings[iframeID].checkOrigin) {
log(' Checking connection is from: '+remoteHost);

if ((''+origin !== 'null') && (origin !== remoteHost)) {
var checkOrigin = settings[iframeID].checkOrigin;
if (checkOrigin) {
if ((''+origin !== 'null') && !checkAllowedOrigin(origin,checkOrigin,remoteHost)) {
throw new Error(
'Unexpected message received from: ' + origin +
' for ' + messageData.iframe.id +
'. Message was: ' + event.data +
'. This error can be disabled by adding the checkOrigin: false option.'
'. This error can be disabled by adding the checkOrigin: false option or providing of array of trusted domains.'
);
}
}
Expand Down