feature: event origin whitelisting

This commit is contained in:
DecentM 2018-01-06 12:38:20 +01:00
parent aa31f53448
commit b9e6b660a7
No known key found for this signature in database
GPG Key ID: 6BDA2D2BC5EA5B10

View File

@ -2,15 +2,25 @@
(() => { (() => {
const manifest = chrome.runtime.getManifest(); const manifest = chrome.runtime.getManifest();
const allowedOrigins = [
'https://openusercss.org',
'https://openusercss.com'
];
// Tell the page that we exist and that it should send the handshake // Tell the page that we exist and that it should send the handshake
window.postMessage({ allowedOrigins.forEach(origin => {
'type': 'ouc-begin-handshake' window.postMessage({
}, '*'); 'type': 'ouc-begin-handshake'
}, origin);
});
// Wait for the handshake // Wait for the handshake
window.addEventListener('message', event => { window.addEventListener('message', event => {
if (event.data && event.data.type === 'ouc-handshake-question') { if (
event.data
&& event.data.type === 'ouc-handshake-question'
&& allowedOrigins.includes(event.origin)
) {
// This is a representation of features that Stylus is capable of // This is a representation of features that Stylus is capable of
const implementedFeatures = [ const implementedFeatures = [
'install-usercss', 'install-usercss',
@ -44,15 +54,16 @@
// We send the handshake response, which includes the key we got, plus some // We send the handshake response, which includes the key we got, plus some
// additional metadata // additional metadata
window.postMessage({ allowedOrigins.forEach(origin => {
'type': 'ouc-handshake-response', window.postMessage({
'key': event.data.key, 'type': 'ouc-handshake-response',
'extension': { 'key': event.data.key,
'name': manifest.name, 'extension': {
'version': manifest.version, 'name': manifest.name,
'capabilities': reportedFeatures 'capabilities': reportedFeatures
} }
}, '*'); }, origin);
});
} }
}); });
})(); })();