

Securely Bypassing X-Frame-Options or Content-Security-Policy in WebExtension
source link: https://usamaejaz.com/bypassing-security-iframe-webextension/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Securely Bypassing X-Frame-Options or Content-Security-Policy in WebExtension
To embed third party content using iframe
, the WebExtension may need to intercept HTTP response and modify headers but isn’t it bad in terms of security? That’s what I thought.
Bypassing X-Frame-Options
If your browser extension is using iframe
to embed third party content where the source is dynamic, you have no option other than to bypass the X-Frame-Options header.
chrome.webRequest.onHeadersReceived.addListener(info => {
const headers = info.responseHeaders; // original headers
for (let i=headers.length-1; i>=0; --i) {
let header = headers[i].name.toLowerCase();
if (header === "x-frame-options" || header === "frame-options") {
headers.splice(i, 1); // Remove the header
}
}
// return modified headers
return {responseHeaders: headers};
}, {
urls: [ "<all_urls>" ], // match all pages
types: [ "sub_frame" ] // for framing only
}, ["blocking", "responseHeaders"]);
This may be required when you are providing some functionality which relies on iframe
. For me, the reason was a feature which allowed the user to embed third party content or web-app by its URL on the extension’s page.
But wait… what about the Content-Security-Policy
header?
Content-Security-Policy
header also has frame-ancestors
directive which can be used to control if a page can be loaded in an iframe
or not.
frame-ancestors
directive can specify a list of allowed sources which can load the page in an iframe or prevent this for all parent origins.
Example CSP header
script-src https://script-source1.com http://script-source2.com; frame-ancestors 'self';
The above header is not allowing any third party parent origin to load this page in iframe.
Bypassing Content-Security-Policy
This header can be bypassed the same way as shown above.
chrome.webRequest.onHeadersReceived.addListener(info => {
const headers = info.responseHeaders; // original headers
for (let i=headers.length-1; i>=0; --i) {
let header = headers[i].name.toLowerCase();
if (header === "content-security-policy") { // csp header is found
// modifying frame-ancestors; this implies that the directive is already present
headers[i].value = headers[i].value.replace("frame-ancestors", "frame-ancestors https://yourpage.com/");
}
}
// return modified headers
return {responseHeaders: headers};
}, {
urls: [ "<all_urls>" ], // match all pages
types: [ "sub_frame" ] // for framing only
}, ["blocking", "responseHeaders"]);
Now your origin page is whitelisted and can make use of iframe
freely.
Security?
Simply bypassing the header by removing X-Frame-Options
header can be enough for you. But if its bypassed, remember that the browser is vulnerable to attacks which make use of iframe
s like the famous click-jacking technique. There are many possibilities.
However, you can do this securely by making use of Content-Security-Policy
(CSP) header. For instance, if you remove X-Frame-Options, make sure you add / modify CSP header with frame-ancestors
directive to whitelist only your origin URL. So, it will not be open for everyone.
Recommend
-
148
The author of the popular Firefox add-on Tab Mix Plus released the first version of the upcoming WebExtensions-based version of Tab Mix Plus yesterday.
-
115
-
146
-
68
README.md Dat-Firefox This is a prototype browser extension which makes dat:// urls function in Firefox using a slightly modified
-
81
GitHub is where people build software. More than 28 million people use GitHub to discover, fork, and contribute to over 85 million projects.
-
63
README.md
-
48
NEW (nobody) in WebExtensions - General. Last updated 2018-11-16.
-
57
Readme.md DownThemAll! WE The DownThemAll! WebExtension. For those still on supported browser:
-
6
Bypassing PowerShell Execution Policy Let me be absolutely clear about this post. I do not in any way encourage or support people who wish to use the below information to circumvent the controls put in place by c...
-
25
Closed Bug 1536094 Opened 2 years ago Closed 1 month ago...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK