|
@@ -1,6 +1,6 @@
|
|
|
import * as chai from 'chai'
|
|
|
import * as chaiAsPromised from 'chai-as-promised'
|
|
|
-import { BrowserWindow, WebContents, session, ipcMain } from 'electron'
|
|
|
+import { BrowserWindow, webContents, WebContents, session, ipcMain } from 'electron'
|
|
|
import { emittedOnce } from './events-helpers'
|
|
|
import { closeAllWindows } from './window-helpers'
|
|
|
import * as https from 'https'
|
|
@@ -351,3 +351,163 @@ describe('iframe using HTML fullscreen API while window is OS-fullscreened', ()
|
|
|
w.loadFile(path.join(fixturesPath, 'pages', 'fullscreen-ipif.html'))
|
|
|
})
|
|
|
})
|
|
|
+
|
|
|
+describe('enableWebSQL webpreference', () => {
|
|
|
+ const standardScheme = (global as any).standardScheme;
|
|
|
+ const origin = `${standardScheme}://fake-host`;
|
|
|
+ const filePath = path.join(fixturesPath, 'pages', 'storage', 'web_sql.html');
|
|
|
+ const sqlPartition = 'web-sql-preference-test';
|
|
|
+ const sqlSession = session.fromPartition(sqlPartition);
|
|
|
+ const securityError = 'An attempt was made to break through the security policy of the user agent.';
|
|
|
+ let contents: WebContents, w: BrowserWindow;
|
|
|
+
|
|
|
+ before(() => {
|
|
|
+ sqlSession.protocol.registerFileProtocol(standardScheme, (request, callback) => {
|
|
|
+ callback({ path: filePath });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ after(() => {
|
|
|
+ sqlSession.protocol.unregisterProtocol(standardScheme);
|
|
|
+ });
|
|
|
+
|
|
|
+ afterEach(async () => {
|
|
|
+ if (contents) {
|
|
|
+ (contents as any).destroy();
|
|
|
+ contents = null as any;
|
|
|
+ }
|
|
|
+ await closeAllWindows();
|
|
|
+ (w as any) = null;
|
|
|
+ });
|
|
|
+
|
|
|
+ it('default value allows websql', async () => {
|
|
|
+ contents = (webContents as any).create({
|
|
|
+ session: sqlSession,
|
|
|
+ nodeIntegration: true
|
|
|
+ });
|
|
|
+ contents.loadURL(origin);
|
|
|
+ const [, error] = await emittedOnce(ipcMain, 'web-sql-response');
|
|
|
+ expect(error).to.be.null('error');
|
|
|
+ });
|
|
|
+
|
|
|
+ it('when set to false can disallow websql', async () => {
|
|
|
+ contents = (webContents as any).create({
|
|
|
+ session: sqlSession,
|
|
|
+ nodeIntegration: true,
|
|
|
+ enableWebSQL: false
|
|
|
+ });
|
|
|
+ contents.loadURL(origin);
|
|
|
+ const [, error] = await emittedOnce(ipcMain, 'web-sql-response');
|
|
|
+ expect(error).to.equal(securityError);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('when set to false does not disable indexedDB', async () => {
|
|
|
+ contents = (webContents as any).create({
|
|
|
+ session: sqlSession,
|
|
|
+ nodeIntegration: true,
|
|
|
+ enableWebSQL: false
|
|
|
+ });
|
|
|
+ contents.loadURL(origin);
|
|
|
+ const [, error] = await emittedOnce(ipcMain, 'web-sql-response');
|
|
|
+ expect(error).to.equal(securityError);
|
|
|
+ const dbName = 'random';
|
|
|
+ const result = await contents.executeJavaScript(`
|
|
|
+ new Promise((resolve, reject) => {
|
|
|
+ try {
|
|
|
+ let req = window.indexedDB.open('${dbName}');
|
|
|
+ req.onsuccess = (event) => {
|
|
|
+ let db = req.result;
|
|
|
+ resolve(db.name);
|
|
|
+ }
|
|
|
+ req.onerror = (event) => { resolve(event.target.code); }
|
|
|
+ } catch (e) {
|
|
|
+ resolve(e.message);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ `);
|
|
|
+ expect(result).to.equal(dbName);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('child webContents can override when the embedder has allowed websql', async () => {
|
|
|
+ w = new BrowserWindow({
|
|
|
+ show: false,
|
|
|
+ webPreferences: {
|
|
|
+ nodeIntegration: true,
|
|
|
+ webviewTag: true,
|
|
|
+ session: sqlSession
|
|
|
+ }
|
|
|
+ });
|
|
|
+ w.webContents.loadURL(origin);
|
|
|
+ const [, error] = await emittedOnce(ipcMain, 'web-sql-response');
|
|
|
+ expect(error).to.be.null('error');
|
|
|
+ const webviewResult = emittedOnce(ipcMain, 'web-sql-response');
|
|
|
+ await w.webContents.executeJavaScript(`
|
|
|
+ new Promise((resolve, reject) => {
|
|
|
+ const webview = new WebView();
|
|
|
+ webview.setAttribute('src', '${origin}');
|
|
|
+ webview.setAttribute('webpreferences', 'enableWebSQL=0');
|
|
|
+ webview.setAttribute('partition', '${sqlPartition}');
|
|
|
+ webview.setAttribute('nodeIntegration', 'on');
|
|
|
+ document.body.appendChild(webview);
|
|
|
+ webview.addEventListener('dom-ready', () => resolve());
|
|
|
+ });
|
|
|
+ `);
|
|
|
+ const [, childError] = await webviewResult;
|
|
|
+ expect(childError).to.equal(securityError);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('child webContents cannot override when the embedder has disallowed websql', async () => {
|
|
|
+ w = new BrowserWindow({
|
|
|
+ show: false,
|
|
|
+ webPreferences: {
|
|
|
+ nodeIntegration: true,
|
|
|
+ enableWebSQL: false,
|
|
|
+ webviewTag: true,
|
|
|
+ session: sqlSession
|
|
|
+ }
|
|
|
+ });
|
|
|
+ w.webContents.loadURL('data:text/html,<html></html>');
|
|
|
+ const webviewResult = emittedOnce(ipcMain, 'web-sql-response');
|
|
|
+ await w.webContents.executeJavaScript(`
|
|
|
+ new Promise((resolve, reject) => {
|
|
|
+ const webview = new WebView();
|
|
|
+ webview.setAttribute('src', '${origin}');
|
|
|
+ webview.setAttribute('webpreferences', 'enableWebSQL=1');
|
|
|
+ webview.setAttribute('partition', '${sqlPartition}');
|
|
|
+ webview.setAttribute('nodeIntegration', 'on');
|
|
|
+ document.body.appendChild(webview);
|
|
|
+ webview.addEventListener('dom-ready', () => resolve());
|
|
|
+ });
|
|
|
+ `);
|
|
|
+ const [, childError] = await webviewResult;
|
|
|
+ expect(childError).to.equal(securityError);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('child webContents can use websql when the embedder has allowed websql', async () => {
|
|
|
+ w = new BrowserWindow({
|
|
|
+ show: false,
|
|
|
+ webPreferences: {
|
|
|
+ nodeIntegration: true,
|
|
|
+ webviewTag: true,
|
|
|
+ session: sqlSession
|
|
|
+ }
|
|
|
+ });
|
|
|
+ w.webContents.loadURL(origin);
|
|
|
+ const [, error] = await emittedOnce(ipcMain, 'web-sql-response');
|
|
|
+ expect(error).to.be.null('error');
|
|
|
+ const webviewResult = emittedOnce(ipcMain, 'web-sql-response');
|
|
|
+ await w.webContents.executeJavaScript(`
|
|
|
+ new Promise((resolve, reject) => {
|
|
|
+ const webview = new WebView();
|
|
|
+ webview.setAttribute('src', '${origin}');
|
|
|
+ webview.setAttribute('webpreferences', 'enableWebSQL=1');
|
|
|
+ webview.setAttribute('partition', '${sqlPartition}');
|
|
|
+ webview.setAttribute('nodeIntegration', 'on');
|
|
|
+ document.body.appendChild(webview);
|
|
|
+ webview.addEventListener('dom-ready', () => resolve());
|
|
|
+ });
|
|
|
+ `);
|
|
|
+ const [, childError] = await webviewResult;
|
|
|
+ expect(childError).to.be.null('error');
|
|
|
+ });
|
|
|
+});
|