feat: add enableWebSQL webpreference (#23311)

* feat: add enableWebSQL webpreference

* chore: update indexedDB test
This commit is contained in:
Robo 2020-05-06 12:52:59 -07:00 committed by GitHub
parent 2a680e107b
commit a707a3eda3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 187 additions and 3 deletions

View file

@ -1010,6 +1010,166 @@ describe('chromium features', () => {
testLocalStorageAfterXSiteRedirect('after a cross-site redirect');
testLocalStorageAfterXSiteRedirect('after a cross-site redirect in sandbox mode', { sandbox: true });
});
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();
});
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();
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();
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();
});
});
});
ifdescribe(features.isPDFViewerEnabled())('PDF Viewer', () => {