Skip to content

Commit

Permalink
fix: undefined details.requestingUrl from session.setPermissionCheckH…
Browse files Browse the repository at this point in the history
…andler (#35409)

fix: undefined details.requestingUrl from setPermissionCheckHandler

Co-authored-by: deepak1556 <hop2deep@gmail.com>
  • Loading branch information
trop[bot] and deepak1556 committed Aug 23, 2022
1 parent 8d6c29e commit 95eb6ea
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
13 changes: 10 additions & 3 deletions shell/browser/electron_permission_manager.cc
Expand Up @@ -339,9 +339,16 @@ blink::mojom::PermissionStatus
ElectronPermissionManager::GetPermissionStatusForCurrentDocument(
blink::PermissionType permission,
content::RenderFrameHost* render_frame_host) {
return GetPermissionStatus(
permission, render_frame_host->GetLastCommittedOrigin().GetURL(),
content::PermissionUtil::GetLastCommittedOriginAsURL(render_frame_host));
base::Value::Dict details;
details.Set("embeddingOrigin",
content::PermissionUtil::GetLastCommittedOriginAsURL(
render_frame_host->GetMainFrame())
.spec());
bool granted = CheckPermissionWithDetails(
permission, render_frame_host,
render_frame_host->GetLastCommittedOrigin().GetURL(), std::move(details));
return granted ? blink::mojom::PermissionStatus::GRANTED
: blink::mojom::PermissionStatus::DENIED;
}

blink::mojom::PermissionStatus
Expand Down
86 changes: 85 additions & 1 deletion spec-main/api-session-spec.ts
Expand Up @@ -4,7 +4,7 @@ import * as https from 'https';
import * as path from 'path';
import * as fs from 'fs';
import * as ChildProcess from 'child_process';
import { app, session, BrowserWindow, net, ipcMain, Session } from 'electron/main';
import { app, session, BrowserWindow, net, ipcMain, Session, webFrameMain, WebFrameMain } from 'electron/main';
import * as send from 'send';
import * as auth from 'basic-auth';
import { closeAllWindows } from './window-helpers';
Expand Down Expand Up @@ -1043,6 +1043,90 @@ describe('session module', () => {
});
});

describe('ses.setPermissionCheckHandler(handler)', () => {
afterEach(closeAllWindows);
it('details provides requestingURL for mainFrame', async () => {
const w = new BrowserWindow({
show: false,
webPreferences: {
partition: 'very-temp-permission-handler'
}
});
const ses = w.webContents.session;
const loadUrl = 'https://myfakesite/';
let handlerDetails : Electron.PermissionCheckHandlerHandlerDetails;

ses.protocol.interceptStringProtocol('https', (req, cb) => {
cb('<html><script>console.log(\'test\');</script></html>');
});

ses.setPermissionCheckHandler((wc, permission, requestingOrigin, details) => {
if (permission === 'clipboard-read') {
handlerDetails = details;
return true;
}
return false;
});

const readClipboardPermission: any = () => {
return w.webContents.executeJavaScript(`
navigator.permissions.query({name: 'clipboard-read'})
.then(permission => permission.state).catch(err => err.message);
`, true);
};

await w.loadURL(loadUrl);
const state = await readClipboardPermission();
expect(state).to.equal('granted');
expect(handlerDetails!.requestingUrl).to.equal(loadUrl);
});

it('details provides requestingURL for cross origin subFrame', async () => {
const w = new BrowserWindow({
show: false,
webPreferences: {
partition: 'very-temp-permission-handler'
}
});
const ses = w.webContents.session;
const loadUrl = 'https://myfakesite/';
let handlerDetails : Electron.PermissionCheckHandlerHandlerDetails;

ses.protocol.interceptStringProtocol('https', (req, cb) => {
cb('<html><script>console.log(\'test\');</script></html>');
});

ses.setPermissionCheckHandler((wc, permission, requestingOrigin, details) => {
if (permission === 'clipboard-read') {
handlerDetails = details;
return true;
}
return false;
});

const readClipboardPermission: any = (frame: WebFrameMain) => {
return frame.executeJavaScript(`
navigator.permissions.query({name: 'clipboard-read'})
.then(permission => permission.state).catch(err => err.message);
`, true);
};

await w.loadFile(path.join(fixtures, 'api', 'blank.html'));
w.webContents.executeJavaScript(`
var iframe = document.createElement('iframe');
iframe.src = '${loadUrl}';
document.body.appendChild(iframe);
null;
`);
const [,, frameProcessId, frameRoutingId] = await emittedOnce(w.webContents, 'did-frame-finish-load');
const state = await readClipboardPermission(webFrameMain.fromId(frameProcessId, frameRoutingId));
expect(state).to.equal('granted');
expect(handlerDetails!.requestingUrl).to.equal(loadUrl);
expect(handlerDetails!.isMainFrame).to.be.false();
expect(handlerDetails!.embeddingOrigin).to.equal('file:///');
});
});

describe('ses.isPersistent()', () => {
afterEach(closeAllWindows);

Expand Down

0 comments on commit 95eb6ea

Please sign in to comment.