Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protecting iFrame - Only allow it to work on one domain

I have a Widget that I created and I am embedding it on other websites using an iFrame. What I want to do is make sure no one can view the source and copy the iFrame code and put it on their own website.

I can store the URL that it should be allowed on in the database. I've seen it done before, one site had a long encrypted code and if it didn't match with the domain then it said Access Denied..

Does anyone know how I can do this?

Thanks!

like image 944
Drew Avatar asked Nov 17 '11 15:11

Drew


People also ask

How do I restrict an iframe?

The X-Frame-Options response headerDENY : The page cannot be displayed in a frame, regardless of the site attempting to do so. SAMEORIGIN : The page can only be displayed in a frame on the same origin as the page itself. ALLOW-FROM uri : The page can only be displayed in a frame on the specified origin.

How do I allow a website to be embedded in iframe?

Select the Share or embed map option. Select the Embed map option, which will give you some <iframe> code — copy this. Insert it into the Input box below, and see what the result is in the Output.

Are iFrames still supported?

The iframe element is supported by all modern desktop and mobile browsers. However, some browsers don't yet respond consistently to the three new HTML5 attributes for this element.


2 Answers

No you can't do this. The best thing you can do is the following:

if (window.top.location.host != "hostname") {
    document.body.innerHTML = "Access Denied";
}

Add the above to your JavaScript and then use a JavaSript obfuscator

like image 198
noob Avatar answered Oct 21 '22 06:10

noob


You cannot prevent people from looking at your HTML, but there are some headers can allow you to specify what sites can embed your iframe. Take a look at the X-Frame-Options header and the frame-ancestors directive of Content-Security-Policy. Browsers that respect it will refuse to load the iframe when embedded into someone else's site.

like image 20
weiyin Avatar answered Oct 21 '22 04:10

weiyin