Using postMessage() in JavaScript with iframes for cross domain communication
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I am trying to enable communication via JavaScript from an iframe and its parent window when they are on different domains. I did a lot of research and it turned out that postMessage()
is a good approach if used correctly. I have read the vast resources about how to use it to make sure itâÂÂs implemented in a secure way and came up with the code. However, as I am still new to coding, I am paranoiac about XSS vulnerabilities and this code looks too simple to me to be fully secure. I would appreciate if someone can review it.
Context:
- The message must be sent from the iframe to the parent window only
- The iframe is HTTPS, while the parent is HTTP
- The message doesnâÂÂt contain sensitive data â itâÂÂs just a simple indicator for the parent window to do trigger a google tag manager custom event
- Parent window domain in the example is:
http://my-main-web.com
- Iframe domain in the example is:
https://my-iframe-web.com
Questions:
- Is the code exposed to security risks?
- Are there other risks apart that the attacker might find out the message content is 'download'? For example, can meticulous code be injected through the message?
Sender (iframe):
<script type="text/javascript">
parent.postMessage("download occurred", "http://my-main-web.com");
</script>
Receiver (parent window):
<script type="text/javascript">
function receiveMessage(event) ;
window.addEventListener("message", receiveMessage, false);
</script>
javascript security event-handling
add a comment |Â
up vote
0
down vote
favorite
I am trying to enable communication via JavaScript from an iframe and its parent window when they are on different domains. I did a lot of research and it turned out that postMessage()
is a good approach if used correctly. I have read the vast resources about how to use it to make sure itâÂÂs implemented in a secure way and came up with the code. However, as I am still new to coding, I am paranoiac about XSS vulnerabilities and this code looks too simple to me to be fully secure. I would appreciate if someone can review it.
Context:
- The message must be sent from the iframe to the parent window only
- The iframe is HTTPS, while the parent is HTTP
- The message doesnâÂÂt contain sensitive data â itâÂÂs just a simple indicator for the parent window to do trigger a google tag manager custom event
- Parent window domain in the example is:
http://my-main-web.com
- Iframe domain in the example is:
https://my-iframe-web.com
Questions:
- Is the code exposed to security risks?
- Are there other risks apart that the attacker might find out the message content is 'download'? For example, can meticulous code be injected through the message?
Sender (iframe):
<script type="text/javascript">
parent.postMessage("download occurred", "http://my-main-web.com");
</script>
Receiver (parent window):
<script type="text/javascript">
function receiveMessage(event) ;
window.addEventListener("message", receiveMessage, false);
</script>
javascript security event-handling
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to enable communication via JavaScript from an iframe and its parent window when they are on different domains. I did a lot of research and it turned out that postMessage()
is a good approach if used correctly. I have read the vast resources about how to use it to make sure itâÂÂs implemented in a secure way and came up with the code. However, as I am still new to coding, I am paranoiac about XSS vulnerabilities and this code looks too simple to me to be fully secure. I would appreciate if someone can review it.
Context:
- The message must be sent from the iframe to the parent window only
- The iframe is HTTPS, while the parent is HTTP
- The message doesnâÂÂt contain sensitive data â itâÂÂs just a simple indicator for the parent window to do trigger a google tag manager custom event
- Parent window domain in the example is:
http://my-main-web.com
- Iframe domain in the example is:
https://my-iframe-web.com
Questions:
- Is the code exposed to security risks?
- Are there other risks apart that the attacker might find out the message content is 'download'? For example, can meticulous code be injected through the message?
Sender (iframe):
<script type="text/javascript">
parent.postMessage("download occurred", "http://my-main-web.com");
</script>
Receiver (parent window):
<script type="text/javascript">
function receiveMessage(event) ;
window.addEventListener("message", receiveMessage, false);
</script>
javascript security event-handling
I am trying to enable communication via JavaScript from an iframe and its parent window when they are on different domains. I did a lot of research and it turned out that postMessage()
is a good approach if used correctly. I have read the vast resources about how to use it to make sure itâÂÂs implemented in a secure way and came up with the code. However, as I am still new to coding, I am paranoiac about XSS vulnerabilities and this code looks too simple to me to be fully secure. I would appreciate if someone can review it.
Context:
- The message must be sent from the iframe to the parent window only
- The iframe is HTTPS, while the parent is HTTP
- The message doesnâÂÂt contain sensitive data â itâÂÂs just a simple indicator for the parent window to do trigger a google tag manager custom event
- Parent window domain in the example is:
http://my-main-web.com
- Iframe domain in the example is:
https://my-iframe-web.com
Questions:
- Is the code exposed to security risks?
- Are there other risks apart that the attacker might find out the message content is 'download'? For example, can meticulous code be injected through the message?
Sender (iframe):
<script type="text/javascript">
parent.postMessage("download occurred", "http://my-main-web.com");
</script>
Receiver (parent window):
<script type="text/javascript">
function receiveMessage(event) ;
window.addEventListener("message", receiveMessage, false);
</script>
javascript security event-handling
edited Jul 10 at 19:35
Jamalâ¦
30.1k11114225
30.1k11114225
asked Jul 10 at 18:50
Mihai Plavitu
11
11
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f198238%2fusing-postmessage-in-javascript-with-iframes-for-cross-domain-communication%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password