Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: inject.preload.js

Issue 29586710: Issue 5382 - Wrap DOM mutation APIs to protect frames (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Bind function to Function.prototype.apply and handle missing descriptors Created Feb. 21, 2018, 4:31 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 contentWindow[eventName] = checkRequest; 65 contentWindow[eventName] = checkRequest;
66 contentWindow.eval( 66 contentWindow.eval(
67 "(" + injectedToString() + ")('" + eventName + "', true);" 67 "(" + injectedToString() + ")('" + eventName + "', true);"
68 ); 68 );
69 delete contentWindow[eventName]; 69 delete contentWindow[eventName];
70 } 70 }
71 catch (e) {} 71 catch (e) {}
72 } 72 }
73 } 73 }
74 74
75 for (let element of [HTMLFrameElement, HTMLIFrameElement, HTMLObjectElement]) 75 function injectIntoAllFrames()
76 { 76 {
77 let contentDocumentDesc = Object.getOwnPropertyDescriptor( 77 for (let i = 0; i < window.length; i++)
78 element.prototype, "contentDocument" 78 injectIntoContentWindow(window[i]);
79 ); 79 }
80 let contentWindowDesc = Object.getOwnPropertyDescriptor(
81 element.prototype, "contentWindow"
82 );
83 80
84 // Apparently in HTMLObjectElement.prototype.contentWindow does not exist 81 function wrapAPIForInjection(object, api, callback)
85 // in older versions of Chrome such as 42. 82 {
86 if (!contentWindowDesc) 83 let func = object[api];
87 continue; 84 if (!func || typeof func != "function")
85 return;
86 let applyFunc = Function.prototype.apply.bind(func);
87 Object.defineProperty(object, api, {
88 value(...args)
89 {
90 let returnValue = applyFunc(this, args);
91 callback(returnValue);
92 return returnValue;
93 }
94 });
95 }
88 96
89 let getContentDocument = Function.prototype.call.bind( 97 function wrapPropertyAPIForInjection(object, api, method, callback)
90 contentDocumentDesc.get 98 {
91 ); 99 let descriptor = Object.getOwnPropertyDescriptor(object, api);
92 let getContentWindow = Function.prototype.call.bind( 100 // Apparently HTMLObjectElement.prototype.contentWindow does not exist in
93 contentWindowDesc.get 101 // older versions of Chrome such as 42.
94 ); 102 if (!descriptor)
103 return;
104 wrapAPIForInjection(descriptor, method, callback);
105 Object.defineProperty(object, api, descriptor);
106 }
95 107
96 contentWindowDesc.get = function() 108 wrapAPIForInjection(Node.prototype, "appendChild", injectIntoAllFrames);
109 wrapAPIForInjection(Node.prototype, "insertBefore", injectIntoAllFrames);
110 wrapAPIForInjection(Node.prototype, "replaceChild", injectIntoAllFrames);
111
112 wrapPropertyAPIForInjection(Element.prototype,
113 "innerHTML", "set", injectIntoAllFrames);
114
115 wrapPropertyAPIForInjection(HTMLObjectElement.prototype,
116 "contentWindow", "get", injectIntoContentWindow);
117 wrapPropertyAPIForInjection(
118 HTMLObjectElement.prototype,
119 "contentDocument", "get",
120 contentDocument =>
97 { 121 {
98 let contentWindow = getContentWindow(this); 122 if (contentDocument)
99 injectIntoContentWindow(contentWindow); 123 injectIntoContentWindow(contentDocument.defaultView);
100 return contentWindow; 124 }
101 }; 125 );
102 contentDocumentDesc.get = function()
103 {
104 injectIntoContentWindow(getContentWindow(this));
105 return getContentDocument(this);
106 };
107 Object.defineProperty(element.prototype, "contentWindow",
108 contentWindowDesc);
109 Object.defineProperty(element.prototype, "contentDocument",
110 contentDocumentDesc);
111 }
112 126
113 /* 127 /*
114 * Shadow root getter wrapper 128 * Shadow root getter wrapper
115 * 129 *
116 * After creating our shadowRoot we must wrap the getter to prevent the 130 * After creating our shadowRoot we must wrap the getter to prevent the
117 * website from accessing it (#4191, #4298). This is required as a 131 * website from accessing it (#4191, #4298). This is required as a
118 * workaround for the lack of user style support in Chrome. 132 * workaround for the lack of user style support in Chrome.
119 * See https://bugs.chromium.org/p/chromium/issues/detail?id=632009&desc=2 133 * See https://bugs.chromium.org/p/chromium/issues/detail?id=632009&desc=2
120 */ 134 */
121 if ("shadowRoot" in Element.prototype) 135 if ("shadowRoot" in Element.prototype)
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 // Firefox 58 only bypasses site CSPs when assigning to 'src'. 422 // Firefox 58 only bypasses site CSPs when assigning to 'src'.
409 let url = URL.createObjectURL(new Blob([ 423 let url = URL.createObjectURL(new Blob([
410 "(" + injected + ")('" + randomEventName + "');" 424 "(" + injected + ")('" + randomEventName + "');"
411 ])); 425 ]));
412 script.src = url; 426 script.src = url;
413 document.documentElement.appendChild(script); 427 document.documentElement.appendChild(script);
414 document.documentElement.removeChild(script); 428 document.documentElement.removeChild(script);
415 URL.revokeObjectURL(url); 429 URL.revokeObjectURL(url);
416 } 430 }
417 } 431 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld