Skip to content

Commit

Permalink
[APD-8271] disable node integration for admob windows
Browse files Browse the repository at this point in the history
  • Loading branch information
NaLLiFFuNT committed Aug 16, 2019
1 parent a92b582 commit c28d04e
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 90 deletions.
272 changes: 190 additions & 82 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
"repository": "https://github.com/appodeal/admob-sync-app",
"description": "Appodeal AdMob Sync application",
"private": true,
"version": "0.1.25",
"version": "0.1.26",
"scripts": {
"start": "webpack --watch --progress --config=webpack/development.ts",
"start": "node --max_old_space_size=4096 node_modules/.bin/webpack --watch --progress --config=webpack/development.ts",
"test": "jest",
"electron": "cd build && electron .",
"build": "webpack --progress --config=webpack/production.ts",
"build": "node --max_old_space_size=4096 node_modules/.bin/webpack --progress --config=webpack/production.ts",
"dist": "electron-builder",
"dist:all": "node tools/dist.js",
"postdist:all": "webpack -p --config=download-page/webpack.config.ts",
"postdist:all": "node --max_old_space_size=4096 node_modules/.bin/webpack -p --config=download-page/webpack.config.ts",
"release:github": "node tools/release.js",
"release:sentry": "sentry-cli releases new $npm_package_version && sentry-cli releases files $npm_package_version upload-sourcemaps build/*.{map,js} && sentry-cli releases finalize $npm_package_version",
"release:deploy": "sentry-cli releases deploys $npm_package_version new"
Expand Down Expand Up @@ -146,7 +146,7 @@
"ts-loader": "^5.3.3",
"ts-node": "^8.0.2",
"typescript": "^3.3.3",
"webpack": "^4.29.5",
"webpack": "^4.39.2",
"webpack-cli": "^3.2.3",
"webpack-dev-server": "^3.2.0",
"webpack-merge": "^4.2.1"
Expand Down
6 changes: 5 additions & 1 deletion src/core/admob-api/admob-sessions.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import {retry} from 'lib/retry';
import {openWindow, waitForNavigation} from 'lib/window';
import uuid from 'uuid';
import {decodeOctString} from '../../lib/oct-decode';
import {cutElectronFromUserAgent} from '../../lib/user-agent';


export namespace AdMobSessions {

let SESSIONS: Map<string, string>;

function sessionFromPartition (sessionID: string) {
return session.fromPartition(`persist:${sessionID}`);
const sess = session.fromPartition(`persist:${sessionID}`);
sess.setUserAgent(cutElectronFromUserAgent(sess.getUserAgent()));
return sess;
}

export function init () {
Expand Down Expand Up @@ -131,6 +134,7 @@ export namespace AdMobSessions {
minHeight: 700,
height: 700,
webPreferences: {
nodeIntegration: false,
session
}
});
Expand Down
8 changes: 8 additions & 0 deletions src/lib/user-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import packageInfo from '../../package.json';


export function cutElectronFromUserAgent (originalUA: string): string {
let appName = packageInfo.productName.split(' ').join(''),
electron = 'Electron';
return originalUA.split(' ').filter(text => !(text.match(appName) || text.match(electron))).join(' ');
}
3 changes: 3 additions & 0 deletions src/lib/window.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {BrowserWindow, BrowserWindowConstructorOptions, dialog, ipcMain, remote, Session} from 'electron';
import {Debug} from 'lib/debug';
import * as path from 'path';
import {isMacOS} from './platform';
import {getBgColor} from './theme';

Expand All @@ -18,6 +19,7 @@ function getConfig (config: BrowserWindowConstructorOptions, backgroundColor: st
show: false,
webPreferences: {
nodeIntegration: true,
preload: path.resolve(environment.development ? process.env.PWD : process.resourcesPath, `preload.js`),
...(config.webPreferences || {})
}
};
Expand Down Expand Up @@ -49,6 +51,7 @@ export function openWindow (
}

window.webContents.once('dom-ready', readyListener);
// window.webContents.toggleDevTools();

ipcMain.on('windowControl', commandListener);

Expand Down
6 changes: 5 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {SyncScheduler} from 'core/sync-apps/sync-scheduler';
import {SyncService} from 'core/sync-apps/sync.service';
import {SyncConnector} from 'core/sync-connector';
import {UpdatesConnector} from 'core/updates-connector';
import {app} from 'electron';
import {app, session} from 'electron';
import {createAppMenu} from 'lib/app-menu';
import {Preferences} from 'lib/app-preferences';
import {AppTray} from 'lib/app-tray';
Expand All @@ -31,6 +31,7 @@ import {
} from 'lib/ui-windows';
import {UpdatesService} from 'lib/updates';
import path from 'path';
import {cutElectronFromUserAgent} from './lib/user-agent';


const QUIT_DEADLINE_TIMOUT = 5000;
Expand Down Expand Up @@ -222,6 +223,9 @@ function runApp () {
let {checkPeriod, customOptions} = store.state.preferences.updates;
updatesConnector.checkForUpdates(true, 'notification');
updatesConnector.runScheduler(checkPeriod, customOptions);

session.defaultSession.setUserAgent(cutElectronFromUserAgent(session.defaultSession.getUserAgent()));
setTimeout(() => openSettingsWindow(), 100)
});

}
Expand Down
22 changes: 22 additions & 0 deletions src/preload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {cutElectronFromUserAgent} from './lib/user-agent';


function overrideGetter (target: any, key: string, getter: () => any) {
if (Object.defineProperty) {
Object.defineProperty(target, key, {
get: getter
});
} else {
// @ts-ignore
if (Object.prototype.__defineGetter__) {
// @ts-ignore
navigator.__proto__.__defineGetter__('target', getter);
}
}
}

const defaultUserAgent = navigator.userAgent;
const defaultAppVersion = navigator.appVersion;

overrideGetter(navigator, 'userAgent', () => cutElectronFromUserAgent(defaultUserAgent));
overrideGetter(navigator, 'appVersion', () => cutElectronFromUserAgent(defaultAppVersion));
3 changes: 2 additions & 1 deletion webpack/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import signIn from './entries/sign-in';
import accounts from './entries/accounts';
import about from './entries/about';
import clearData from './entries/clear-data';
import preload from './entries/preload';

export const SRC_PATH = path.resolve(__dirname, '../src');
export const BUILD_PATH = path.resolve(__dirname, '../build');
export const PACKAGE = require(path.join(__dirname, '../package.json'));

export const entries = [main, settings, signIn, accounts, about, clearData].map(entry => (env: webpack.Configuration): webpack.Configuration => {
export const entries = [main, settings, signIn, accounts, about, clearData, preload].map(entry => (env: webpack.Configuration): webpack.Configuration => {
return merge(entry(env), {
output: {
path: BUILD_PATH,
Expand Down
14 changes: 14 additions & 0 deletions webpack/entries/preload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as path from 'path';
import webpack from 'webpack';
import {SRC_PATH} from '../default';


export default (env: webpack.Configuration): webpack.Configuration => {
return {
entry: {
preload: path.join(SRC_PATH, './preload.ts')

},
target: 'electron-preload'
};
}

0 comments on commit c28d04e

Please sign in to comment.