Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #35875

Re: Send ref to frame element

Newsgroups comp.lang.javascript
Date 2018-12-09 02:06 -0800
References <O8CdneYHv5K8MZXBnZ2dnUU7-RPNnZ2d@westnet.com.au> <ae15c684-5377-4d14-9ce3-592a0bdb7bb8@googlegroups.com>
Message-ID <1bb3e32f-5d95-45ff-ae24-45e81e076ea8@googlegroups.com> (permalink)
Subject Re: Send ref to frame element
From Julio Di Egidio <julio@diegidio.name>

Show all headers | View raw


On Sunday, 9 December 2018 10:46:52 UTC+1, Julio Di Egidio  wrote:
> On Thursday, 6 December 2018 05:57:14 UTC+1, Andrew Poulos  wrote:
> 
> > I need a frameElement to send a self reference to the parent so I tried this
> > 
> > let obj = {};
> > obj.type = "ref";
> > 
> > if (window.frameElement !== null) {
> >    obj.value = window.frameElement;
> >    console.log(obj.value);
> >    window.parent.postMessage( JSON.stringify(obj), "*");
> > }
> > 
> > and in the parent
> > 
> > window.addEventListener('message',function(evt) {
> >    let rec = JSON.parse(evt.data);
> >    if (rec.type == "ref") console.log(rec.value);
> > }, false);
> > 
> > While obj.value is a frameElement rec.value is only ever an empty object.
> > 
> > Can postMessage send a window reference? If so, how?
> 
> Read the docs:
> 
> <https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage>
> 
> window.postMessage is about intercommunication between any two *windows*: it
> does not really care about frames vs popups vs other.
> 
> The first parameter to postMessage is serialised for you already.
> 
> OTOH, you cannot (usually) serialise/deserialise complex runtime objects such
> as window or document, so do not even try: rather use the postMessage facility
> as a messaging system only, to send "data/commands" across (use postMessage
> to reply back as well if you need: i.e., again, use it as a messaging system
> where each party can potentially send *messages* to the other instead of
> directly accessing each other's properties or methods).
> 
> That said, check the properties of the *event* object you get in onmessage,
> i.e. on the receiving end: in particular, there is a 'source' property which
> is a reference to the sender window...  But, again, I use it only to post a
> message back if I need.
> 
> Beware also of other CORS issues: e.g. if it's a frame within a parent, set
> the Content-Security-Policy frame-src (or equivalent) on the parent...

Sorry, let me clarify on that out-of-the-blue last paragraph:

Actually, to validate origins, as also shown in the examples in the docs, you
should check the event.origin property against some list of allowed URLs.

But, if all you have is a main window communicating with an iframe, the CSP
frame-src setting in the parent page is enough to filter out unwanted content
in the iframe, then you can skip programmatic checks of the event-origin *in
the parent*: you'd still need to check origins in the framed page...

Julio

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-06 15:57 +1100
  Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-06 19:02 +1100
  Re: Send ref to frame element "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-12-06 10:05 +0100
    Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-09 19:44 +1100
      Re: Send ref to frame element JJ <jj4public@vfemail.net> - 2018-12-09 16:22 +0700
  Re: Send ref to frame element Julio Di Egidio <julio@diegidio.name> - 2018-12-09 01:46 -0800
    Re: Send ref to frame element Julio Di Egidio <julio@diegidio.name> - 2018-12-09 02:06 -0800
      Re: Send ref to frame element Julio Di Egidio <julio@diegidio.name> - 2018-12-09 02:51 -0800
        Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-10 10:11 +1100
          Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-10 10:23 +1100
            Re: Send ref to frame element Julio Di Egidio <julio@diegidio.name> - 2018-12-09 16:10 -0800
              Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-10 12:44 +1100
                Re: Send ref to frame element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-12-10 04:17 +0100
                Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-10 15:25 +1100
                Re: Send ref to frame element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-12-10 21:28 +0100
                Re: Send ref to frame element "Mark-" <nospam@mapson.xyz> - 2018-12-11 15:41 +0000
                Re: Send ref to frame element Julio Di Egidio <julio@diegidio.name> - 2018-12-10 14:21 -0800
                Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-11 14:48 +1100
                Re: Send ref to frame element Julio Di Egidio <julio@diegidio.name> - 2018-12-10 23:37 -0800
                Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-12 07:51 +1100
                Re: Send ref to frame element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-12-11 22:52 +0100
                Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-12 09:37 +1100
                Re: Send ref to frame element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-12-13 17:31 +0100
                Re: Send ref to frame element "Mark-" <nospam@mapson.xyz> - 2018-12-13 17:33 +0000
                Re: Send ref to frame element "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-12-13 19:19 +0100
                Re: Send ref to frame element "Mark-" <nospam@mapson.xyz> - 2018-12-13 20:27 +0000
                Re: Send ref to frame element "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-12-13 19:17 +0100
                Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-14 07:56 +1100
                Re: Send ref to frame element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-12-13 22:44 +0100
                Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-14 09:21 +1100
                Re: Send ref to frame element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-12-14 01:08 +0100
                Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-14 12:54 +1100
                Re: Send ref to frame element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-12-14 16:04 +0100
                Re: Send ref to frame element Andrew Poulos <ap_prog@hotmail.com> - 2018-12-15 06:32 +1100
                Re: Send ref to frame element Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-12-15 01:11 +0100

csiph-web