Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a bookmarklet on an iFrame that is coming from a different domain

Is there any way to run a bookmarklet on an iFrame which is from a different domain?

For example, I have a page loaded from http://example.com, which has an iFrame whose source is set to http://example2.com. When I run the bookmarklet, it is always run on http://example.com, since that is the main page. I want to run it on the other iFrame though.

When I attempt to interact with the iFrame (e.g. by changing its source attribute to javascript:alert('test')), Chrome shows the following error:

Unsafe JavaScript attempt to access frame with URL http://example.com from frame with URL http://example2.com. Domains, protocols and ports must match.

I tried dragging and dropping the bookmarklet into the frame, but it says:

Failed to load resource

Is there any way for me to interact with an iFrame using a bookmarklet in Chrome?

like image 425
Senseful Avatar asked Dec 19 '10 05:12

Senseful


People also ask

How do I access cross domain iframe?

To access cross-domain iframe, the best approach is to use Javascript's postMessage() method. This method provides a way to securely pass messages across domains.

Are iframes considered bad practice?

Iframes Bring Security Risks. If you create an iframe, your site becomes vulnerable to cross-site attacks. You may get a submittable malicious web form, phishing your users' personal data. A malicious user can run a plug-in.

How do bookmarklets work?

A bookmarklet is a bookmark stored in a web browser that contains JavaScript commands that add new features to the browser. They are stored as the URL of a bookmark in a web browser or as a hyperlink on a web page. Bookmarklets are usually small snippets of JavaScript executed when user clicks on them.

Do websites still use iframes?

iFrames are an HTML tag and have been around for absolutely ages having been introduced back in 1997. Despite their age, they are still commonly used and are supported by all modern browsers.


2 Answers

There is a way to do cross-domain message-passing (not arbitrary code execution) using window.postMessage, yet all a frame A can do to frame B (when they are not of the same origin) is passing it a message hoping that B has a callback function listening for this message.

So here if you control exemple2.com (what's in the frame that don't get the bookmarklet), you can make the bookmarklet pass a message to the iframe and handle it in the iframe.

Else I don't think you have a solution here, except very complicated ones (like proxying).

Other links:

  • In-depth article about same origin policy and its implementations in browsers
  • A cross-browser, backward compatible postMessage attempt (as jQuery plugin)
like image 200
instanceof me Avatar answered Sep 21 '22 08:09

instanceof me


iFrames have alot of security on them as do ajax calls.

Any attempt to use these in a cross-domain manner will result in a security error.

Imagine you were able to interact with other iFrames on different domains. You would be able to make an iFrame (like facebook login's page) that had width and height of 100% and add a function to execute on a submit event which would email you the username and pass before submitting.

So you're gonna have a lot of trouble accomplishing what you're trying to do. You basically can't mess with a page that you don't own. You can use firebug to edit it with the html tab though.

Hope that helps

like image 29
qwertymk Avatar answered Sep 20 '22 08:09

qwertymk