From 7fd20fa7b55b1615193d390e284e10d02cdb3110 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Thu, 23 Jan 2020 14:04:16 -0400 Subject: [PATCH] Replace DetectRTC package with standard web APIs (#7887) The only web API that our usage of DetectRTC relied upon was 'enumerateDevices', which is supported and stable among all of our supported browsers. Note that the error handling here is a little... non-standard, and the logic around how Firefox and Brave are handled should be justified, but I've left the logic as-is for now to keep this PR small. --- package.json | 1 - ui/lib/webcam-utils.js | 56 +++++++++++++++++++++--------------------- yarn.lock | 5 ---- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index 8fda3ae0f3a5..5c406ae33803 100644 --- a/package.json +++ b/package.json @@ -105,7 +105,6 @@ "deep-extend": "^0.5.1", "deep-freeze-strict": "1.1.1", "detect-node": "^2.0.3", - "detectrtc": "^1.3.6", "dnode": "^1.2.2", "end-of-stream": "^1.1.0", "eth-block-tracker": "^4.4.2", diff --git a/ui/lib/webcam-utils.js b/ui/lib/webcam-utils.js index a94ff825aaf6..2e9b5c2a09e3 100644 --- a/ui/lib/webcam-utils.js +++ b/ui/lib/webcam-utils.js @@ -1,6 +1,5 @@ 'use strict' -import DetectRTC from 'detectrtc' import { ENVIRONMENT_TYPE_POPUP, PLATFORM_BRAVE, @@ -9,34 +8,35 @@ import { import { getEnvironmentType, getPlatform } from '../../app/scripts/lib/util' class WebcamUtils { - static checkStatus () { - return new Promise((resolve, reject) => { - const isPopup = - getEnvironmentType(window.location.href) === ENVIRONMENT_TYPE_POPUP - const isFirefoxOrBrave = - getPlatform() === (PLATFORM_FIREFOX || PLATFORM_BRAVE) - try { - DetectRTC.load(_ => { - if (DetectRTC.hasWebcam) { - let environmentReady = true - if ( - (isFirefoxOrBrave && isPopup) || - (isPopup && !DetectRTC.isWebsiteHasWebcamPermissions) - ) { - environmentReady = false - } - resolve({ - permissions: DetectRTC.isWebsiteHasWebcamPermissions, - environmentReady, - }) - } else { - reject({ type: 'NO_WEBCAM_FOUND' }) - } - }) - } catch (e) { - reject({ type: 'UNKNOWN_ERROR' }) + static async checkStatus () { + const isPopup = + getEnvironmentType(window.location.href) === ENVIRONMENT_TYPE_POPUP + const isFirefoxOrBrave = + getPlatform() === (PLATFORM_FIREFOX || PLATFORM_BRAVE) + + const devices = await window.navigator.mediaDevices.enumerateDevices() + const webcams = devices.filter(device => device.kind === 'videoinput') + const hasWebcam = webcams.length > 0 + // A non-empty-string label implies that the webcam has been granted permission, as + // otherwise the label is kept blank to prevent fingerprinting + const hasWebcamPermissions = webcams.some( + webcam => webcam.label && webcam.label.length > 0 + ) + + if (hasWebcam) { + let environmentReady = true + if ((isFirefoxOrBrave && isPopup) || (isPopup && !hasWebcamPermissions)) { + environmentReady = false + } + return { + permissions: hasWebcamPermissions, + environmentReady, } - }) + } else { + const error = new Error('No webcam found') + error.type = 'NO_WEBCAM_FOUND' + throw error + } } } diff --git a/yarn.lock b/yarn.lock index 4ade45e1735b..2edc645baaa4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9278,11 +9278,6 @@ detective@^5.0.2: defined "^1.0.0" minimist "^1.1.1" -detectrtc@^1.3.6: - version "1.3.6" - resolved "https://registry.yarnpkg.com/detectrtc/-/detectrtc-1.3.6.tgz#dabc0353981a3da7732de969071c08b6dddd5b59" - integrity sha1-2rwDU5gaPadzLelpBxwItt3dW1k= - dezalgo@^1.0.0: version "1.0.3" resolved "https://registry.yarnpkg.com/dezalgo/-/dezalgo-1.0.3.tgz#7f742de066fc748bc8db820569dddce49bf0d456"