176

Why are iframes considered dangerous and a security risk? Can someone describe an example of a case where it can be used maliciously?

Daniel T.
  • 37,212
  • 36
  • 139
  • 206
  • 8
    That sounds like an old wives tale. Your browser window is basically just one big iframe. – Bill Criswell Sep 02 '11 at 21:00
  • 1
    It was already [asked](http://stackoverflow.com/questions/362730/are-iframes-considered-bad-practice) on stackoverflow – Samich Sep 02 '11 at 21:02
  • 2
    @Samich — No, that is about best practise, not specifically security issues (and the only security issue I can think of arises from *third parties* using iframes) – Quentin Sep 02 '11 at 21:03
  • Not so much security as its not considered best practice, see: http://stackoverflow.com/questions/1081315/why-developers-hate-iframes They were a lot more popular when people designed with tables also, divs all but eliminate the need for iframes. – RandomUs1r Jun 17 '14 at 14:50
  • 1
    Funnily enough an article popped up nearly a decade later that suggests that putting anything that contains a form in an iframe, isolated from all your third-party javascript etc, is possibly necessary to protect forms from being harvested. https://hackernoon.com/im-harvesting-credit-card-numbers-and-passwords-from-your-site-here-s-how-9a8cb347c5b5 – GordonM Jan 09 '18 at 10:08

6 Answers6

180

The IFRAME element may be a security risk if your site is embedded inside an IFRAME on hostile site. Google "clickjacking" for more details. Note that it does not matter if you use <iframe> or not. The only real protection from this attack is to add HTTP header X-Frame-Options: DENY and hope that the browser knows its job.

If anybody claims that using an <iframe> element on your site is dangerous and causes a security risk, they do not understand what <iframe> element does, or they are speaking about possibility of <iframe> related vulnerabilities in browsers. Security of <iframe src="..."> tag is equal to <img src="..." or <a href="..."> as long there are no vulnerabilities in the browser. And if there's a suitable vulnerability, it might be possible to trigger it even without using <iframe>, <img> or <a> element, so it's not worth considering for this issue.

In addition, IFRAME element may be a security risk if any page on your site contains an XSS vulnerability which can be exploited. In that case the attacker can expand the XSS attack to any page within the same domain that can be persuaded to load within an <iframe> on the page with XSS vulnerability. This is because vulnerable content from the same origin (same domain) inside <iframe> is allowed to access the parent content DOM (practically execute JavaScript in the "host" document). The only real protection methods from this attack is to add HTTP header X-Frame-Options: DENY and/or always correctly encode all user submitted data (that is, never have an XSS vulnerability on your site - easier said than done).

However, be warned that content from <iframe> can initiate top level navigation by default. That is, content within the <iframe> is allowed to automatically open a link over current page location (the new location will be visible in the address bar). The only way to avoid that is to add sandbox attribute without value allow-top-navigation. For example, <iframe sandbox="allow-forms allow-scripts" ...>. Unfortunately, sandbox also disables all plugins, always. For example, historically Youtube couldn't be sandboxed because Flash player was still required to view all Youtube content. No browser supports using plugins and disallowing top level navigation at the same time. However, unless you have some very special reasons, you cannot trust any plugins to work at all for majority of your users in 2021, so you can just use sandbox always and guard your site against forced redirects from user generated content, too. Note that this will break poorly implemented content that tries to modify document.top.location. The content in sandboxed <iframe> can still open links in new tabs so well implemented content will work just fine. Also notice that if you use <iframe sandbox="... allow-scripts allow-same-origin ..." src="blog:..."> any XSS attack within the blob: content can be extended to host document because blob: URLs always inherit the origin of their parent document. You cannot wrap unfiltered user content in blob: and render it as an <iframe> any more than you can put that content directly on your own page.

Example attack goes like this: assume that users can insert user generated content with an iframe; an <iframe> without an attribute sandbox can be used to run JS code saying document.top.location.href = ... and force a redirect to another page. If that redirect goes to a well executed phishing site and your users do not pay attention to address bar, the attacker has a good change to get your users to leak their credentials. They cannot fake the address bar but they can force the redirect and control all content that users can see after that. Leaving allow-top-navigation out of sandbox attribute value avoids this problem. However, due historical reasons, <iframe> elements do not have this limitation by default, so you'll be more vulnerable to phishing if your users can add <iframe> element without attribute sandbox.

Note that X-Frame-Options: DENY also protects from rendering performance side-channel attack that can read content cross-origin (also known as "Pixel perfect Timing Attacks").

That's the technical side of the issue. In addition, there's the issue of user interface. If you teach your users to trust that URL bar is supposed to not change when they click links (e.g. your site uses a big iframe with all the actual content), then the users will not notice anything in the future either in case of actual security vulnerability. For example, you could have an XSS vulnerability within your site that allows the attacker to load content from hostile source within your iframe. Nobody could tell the difference because the URL bar still looks identical to previous behavior (never changes) and the content "looks" valid even though it's from hostile domain requesting user credentials.

Update year 2023:

Content-Security-Policy (CSP) supports directive frame-ancestors which will override the non-standard de-facto header X-Frame-Options: deny.

As a result, you could emit both X-Frame-Options: deny and Content-Security-Policy: frame-ancestors none to block click jacking attacks for both old and modern user agents / internet browsers.

In practice, all browsers that do not natively support CSP directive frame-ancestors are probably old enough to have at least one critical security vulnerability so you can stop serving X-Frame-Options: deny if you define policy for header Content-Security-Policy directive frame-ancestors. (Browsers with critical vulnerabilities are not safe to use no matter what headers you emit.)

Mikko Rantalainen
  • 14,132
  • 10
  • 74
  • 112
  • 1
    Nice, but shouldn't `"This is because content from the same origin (same domain) is allowed to access the parent content DOM (practically execute JavaScript in the "host" document)."` be rephrased in the direction of the (parent) document containing an XSS vulnerability to the (child) document in the iframe? – Shuzheng Feb 05 '20 at 10:30
  • 1
    @Shuzheng the vulnerability goes in both ways and if you use ` – Mikko Rantalainen Feb 12 '20 at 11:52
  • This answer could probably do with an update since Content-Security-Policy obsoletes X-Frame-Options. – Quentin Jun 27 '23 at 10:44
119

As soon as you're displaying content from another domain, you're basically trusting that domain not to serve-up malware.

There's nothing wrong with iframes per se. If you control the content of the iframe, they're perfectly safe.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • 25
    As soon as you link to content from another domain etc etc … There's nothing iframe specific about this. – Quentin Sep 03 '11 at 18:04
  • 7
    A correctly implemented browsers (a.k.a. User Agent) will not allow the iframe contents to leak outside the iframe. If the host document (one containing the ` – Mikko Rantalainen Dec 11 '12 at 12:23
  • 2
    What about a hidden iframe that belongs to the same domain? Is this totally safe? – Ciaran Gallagher Dec 18 '13 at 10:04
  • 2
    Hidden ` – Mikko Rantalainen Mar 07 '14 at 15:41
  • 3
    Interestingly enough, an iFrame might actually be a useful protection from the reverse case. If you have a lot of third party scripts on your site you need to isolate forms from them. One suggested way of doing that was putting the form in its own minimal page with no third-party javascript, and displaying it in an iframe in the host page. https://hackernoon.com/im-harvesting-credit-card-numbers-and-passwords-from-your-site-here-s-how-9a8cb347c5b5 – GordonM Jan 09 '18 at 10:10
  • 1
    So basically Google is just one big malware distribution engine @Quentin – NotoriousPyro Feb 20 '21 at 13:31
21

IFRAMEs are okay; urban legends are not.

When you "use iframes", it doesn't just mean one thing. It's a lexical ambiguity. Depending on the use case, "using iframes" may mean one of the following situations:

  1. Someone else displays your content in an iframe
  2. You display domeone else's content in an iframe
  3. You display your own content in an iframe

So which of these cases can put you in risk?

1. Someone else displays your content

This case is almost always referred to as clickjacking - mimicking your site's behaviour, trying to lure your users into using a fake UI instead of the real site. The misunderstanding here is that you using or not using iframes is irrelevant, it's simply not your call - it's someone else using iframes, which you can do nothing about. Btw, even they don't need them specifically: they can copy your site any other way, stealing your html, implementing a fake site from scratch, etc.

So, ditching iframes in attempt to prevent clickjacking - it makes exactly zero sense.

2. You display someone else's content

Of the three above, this is the only one that's somewhat risky, but most of the scary articles you read all the time come from a world before same-origin policy was introduced. Right now, it's still not recommended to include just any site into your own (who knows what it will contain tomorrow?), but if it's a trusted source (accuweather, yahoo stock info etc), you can safely do it. The big no-no here is letting users (therefore, malicious users) control the src of the iframe, telling it what to display. Don't let users load arbitrary content into your page, that's the root of all evil. But it's true with or without iframes. It has nothing to do with them; it could happen using a script or a style tag (good luck living without them) - the problem is you let them out. Any output on your site containing any user-given content is RISKY. Without sanitizing (de-HTMLifying) it, you're basically opening your site up for XSS attacks, anyone can insert a <script> tag into your content, and that is bad news. Like, baaaad news.

Never output any user input without making dead sure it's harmless.

So, while iframes are innocent again, the takeaway is: don't make them display 3rd-party content unless you trust the source. In other words, don't include untrusted content in your site. (Also, don't jump in front of fast-approaching freight trains. Duuh.)

3. You display your own content in an iframe

This one is obviously harmless. Your page is trusted, the inner content of the iframe is trusted, nothing can go wrong. Iframe is no magic trick; it's just an encapsulation technique, you absolutely have the right to show a piece of your content in a sandbox. It's much like putting it inside a div or anything else, only it will have its own document environment.

TL;DR

  • Case 1: doesn't matter if you use iframes or not,
  • Case 2: not an iframe problem,
  • Case 3: absolutely harmless case.

Please stop believing urban legends. The truth is, iframe-s are totally safe. You could as well blame script tags for being dangerous; anything can cause trouble when maliciously inserted in a site. But how did they insert it in the first place? There must be an existing backend vulnerability if someone was able to inject html content into a site. Blaming one piece of technology for a common attack (instead of finding the real cause) is just a synonym for keeping security holes open. Find the dragon behind the fire.

Unsanitized output is bad; iframes are not.
Stop the witch-hunt.


UPDATE:
There is an attribute called sandbox, worth checking out: https://www.w3schools.com/tags/att_sandbox.asp

UPDATE 2:
Before you comment against iframes - please think about hammers. Hammers are dangerous. They also don't look very nice, they're difficult to swim with, bad for teeth, and some guy in a movie once misused a hammer causing serious injuries. Also, just googled it and tons of literature says mortals can't even move them. If this looks like a good reason to never ever use a hammer again, iframes may not be your real enemy. Sorry for going offroad.

dkellner
  • 8,726
  • 2
  • 49
  • 47
  • I beg to differ, in case 3, it's generally not a good idea to use `iframe` with your own content. It's a sign of poorly architect solutions. It may cause all issue with maintaining the website, such as: what if you need to include a global style/script inside your `iframe`? how to deal usability issues (mobile, screen-reader...)? Cross-domain strict limitation may cause some trust-issues, hence we cannot assume that inside `iframe`, everything should "just work"... – Mr. Duc Nguyen Oct 19 '21 at 23:36
  • 2
    @Mr.DucNguyen These are assumptions, imagined bad examples and unclear references to possible issues which may not be issues at all. I think it's very bad practice to decline use of a certain technique only based on just fears and perceived "signs of bad architecture". One may have very valid reasons to use iframes (yes, even plural) and yes, screen readers and portable devices can be managed properly, it's not a question of whether you use iframes or not. Also, "cross-domain strict limitations" should not affect you with your own content. Hence, your conclusion is unsupported. – dkellner Oct 20 '21 at 14:54
  • Thanks for your reply. I think you've got me wrong. I'm not "declining" to use `iframe` at all. I agreed with your 1 & 2, in fact, those are the only "good reasons" for `iframe` IMHO. You should be aware that subdomain is also considered "cross-domain" (i.e. `www.abc.com` is alien to `abc.com`), and if you need to use `iframe` to include a certain other sub-path in your website tree, that's a big sign of a cruft design. Just my 2 cents :) – Mr. Duc Nguyen Oct 21 '21 at 10:37
  • 1
    @Mr.DucNguyen Again, I don't think these are actual problems and/or signs of bad design, but okay, as you said - your 2 cents :) Opinions are always welcome. – dkellner Oct 22 '21 at 18:39
  • #1 is not entirely accurate. Iframes do introduce vulnerabilities beyond making a fake site, because iframes can access cookies and localstorage from their domain. E.g., you're logged into a bank account and your bank's server has a policy allowing cross-origin iframes. Somebody can make a malicious website with an invisble full-screen iframe of the bank, and get the user to click certain places corresponding to the buttons on the bank website to send money. – nnsk Apr 18 '23 at 20:12
  • @nnsk This is not an iframe problem; first of all why would any bank allow cross-origin, and for the deceptive fullscreen part see the article, I think I covered this case. Anyone can create a fake site without iframes. – dkellner Apr 19 '23 at 09:48
  • @dkellner actually it is an iframe problem; a fake site cannot do this because they cannot access the target website's cookies like the iframe can. To prevent this attack, the common advice is to deny iframes through X-Frame-Options, but I think a captcha within the iframe instead could suffice if iframes are needed. – nnsk Apr 19 '23 at 14:38
  • @nnsk Again, FALSE, and this is why some people have fear of iframes. No you can't access "the target website's cookies" if it's a different domain. That would be a serious security concern about the browser itself. – dkellner Apr 26 '23 at 09:00
  • Downvoted for writing nonsense. You CAN protect yourself against #1 nowadays with X-Frame-Options. – Søren Boisen Jun 26 '23 at 07:24
  • You downvoted because it's a user right. Be happy with it, and we're not gonna have this argument because of your offensive & condescending tone. End of story. – dkellner Jun 26 '23 at 12:17
20

I'm assuming cross-domain iFrame since presumably the risk would be lower if you controlled it yourself.

  • Clickjacking is a problem if your site is included as an iframe
  • A compromised iFrame could display malicious content (imagine the iFrame displaying a login box instead of an ad)
  • An included iframe can make certain JS calls like alert and prompt which could annoy your user
  • An included iframe can redirect via location.href (yikes, imagine a 3p frame redirecting the customer from bankofamerica.com to bankofamerica.fake.com)
  • Malware inside the 3p frame (java/flash/activeX) could infect your user
Joe Zack
  • 3,268
  • 2
  • 31
  • 37
0

iframe is also vulnerable to Cross Frame Scripting:

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    These are not iframe-specific; you could as well say "script tags are vulnerable to XSS", these attacks first need an existing vulnerability to exploit. – dkellner Apr 29 '21 at 09:02
  • 2
    People say this and it is true but XFS isn't an iframe issue. It's a browser bug mostly in an old version of IE. This could be fixed by disallowing certain old versions of IE and any other affected browsers within the iframe – nnsk Apr 18 '23 at 20:16
-1

I came here to find out whether the HTML/CSS/JS of a remote site <iframe> can be obtained by anyone locally, such as through Browser > Settings > View Page Source or Developer Tools, because I thought to use an <iframe> to establish client-side security. I don't see a clear answer. I checked in MDM and I don't see any way to prevent access to <iframe> contents. I tried an experiment and I could see the remote source using both of these methods. So, alas, <iframe> can't provide client security.

David Spector
  • 1,520
  • 15
  • 21
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). You can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/low-quality-posts/34606161) – Sweta Jain Jun 30 '23 at 10:37
  • It is true that my answer does not answer the original question, which already was answered. My answer is a closely related question, which took me to this page in a web search. Client security is an important issue, and there is little help in this area, so I felt that my answer was relevant here. – David Spector Jul 01 '23 at 12:21
  • I think these are good points, so I will post a new question, give my answer, and then delete my off-topic posts here, if I can. – David Spector Jul 08 '23 at 16:08
  • I used the Ask Question button to ask this as a question (https://stackoverflow.com/questions/76643916/can-the-source-html-css-js-of-a-remote-site-shown-in-an-iframe-be-accessed/76643917#76643917), but so far it and its answer have received a -1 rating, without much of any explanation. Does this mean that iframes CAN provide security? I have no idea. – David Spector Jul 09 '23 at 10:17