Using postMessage() in JavaScript with iframes for cross domain communication

The name of the pictureThe name of the pictureThe name of the pictureClash 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>






share|improve this question



























    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>






    share|improve this question























      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>






      share|improve this question













      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>








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 10 at 19:35









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Jul 10 at 18:50









      Mihai Plavitu

      11




      11

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?