45 lines
1.9 KiB
JavaScript
45 lines
1.9 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Media permission policy for the app's own origin. Electron reports origins as a URL spec with a
|
|
* trailing slash ("app://teamspeach/"), so origins are compared after normalisation. Only audio is
|
|
* ever allowed: microphone capture on request, device enumeration and output selection on check.
|
|
* Camera, screen capture and every other permission stay denied.
|
|
*/
|
|
|
|
// WHATWG URL reports "null" as the origin of non-special schemes such as app:, so the origin is rebuilt from
|
|
// the parsed protocol and host. Inputs with credentials or backslashes (lookalikes such as
|
|
// "app://teamspeach\@evil" or "app://teamspeach@evil") never match.
|
|
function normalizeOrigin(origin) {
|
|
if (typeof origin !== 'string' || origin.includes('\\')) return '';
|
|
let url;
|
|
try {
|
|
url = new URL(origin);
|
|
} catch {
|
|
return '';
|
|
}
|
|
if (url.username || url.password || !url.host) return '';
|
|
return `${url.protocol}//${url.host}`.toLowerCase();
|
|
}
|
|
|
|
function isAppOrigin(origin, appOrigin) {
|
|
return normalizeOrigin(origin) === appOrigin;
|
|
}
|
|
|
|
/** setPermissionCheckHandler: enumerateDevices labels/ids and setSinkId depend on this. */
|
|
function permissionCheckAllowed(permission, requestingOrigin, details, appOrigin) {
|
|
if (!isAppOrigin(requestingOrigin, appOrigin)) return false;
|
|
if (permission === 'media') return Boolean(details) && details.mediaType === 'audio';
|
|
return permission === 'speaker-selection';
|
|
}
|
|
|
|
/** setPermissionRequestHandler: getUserMedia for audio only, from a page of the app. */
|
|
function permissionRequestAllowed(permission, details, appOrigin) {
|
|
if (permission !== 'media' || !details) return false;
|
|
if (!isAppOrigin(details.requestingUrl, appOrigin)) return false;
|
|
const types = Array.isArray(details.mediaTypes) ? details.mediaTypes : [];
|
|
return types.length > 0 && types.every((t) => t === 'audio');
|
|
}
|
|
|
|
module.exports = { normalizeOrigin, permissionCheckAllowed, permissionRequestAllowed };
|