Files
client/app/main/permissions.js
T

49 lines
2.1 KiB
JavaScript

'use strict';
/**
* 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. Allowed: microphone capture
* on request, device enumeration and output selection on check, and HTML full screen on request (the
* stream player; Electron asks the request handler before entering it). 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 (details?.isMainFrame === false) return false;
if (!isAppOrigin(requestingOrigin, appOrigin)) return false;
if (permission === 'media') return Boolean(details) && details.mediaType === 'audio';
return permission === 'speaker-selection';
}
/** setPermissionRequestHandler: getUserMedia for audio only, and full screen, from a page of the app. */
function permissionRequestAllowed(permission, details, appOrigin) {
if (details?.isMainFrame === false) return false;
if (!details || !isAppOrigin(details.requestingUrl, appOrigin)) return false;
if (permission === 'fullscreen') return true;
if (permission !== 'media') return false;
const types = Array.isArray(details.mediaTypes) ? details.mediaTypes : [];
return types.length > 0 && types.every((t) => t === 'audio');
}
module.exports = { normalizeOrigin, permissionCheckAllowed, permissionRequestAllowed };