Files

5 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

import test from 'node:test';import assert from 'node:assert/strict';import {readFileSync} from 'node:fs';import vm from 'node:vm';const source=readFileSync(new URL('../apps/browser/transport-deny.cjs',import.meta.url),'utf8'),definition=source.slice(source.indexOf('function denyUninspectedRTC(){'),source.indexOf('\nif(process.sandboxed'));
function world(){const context=vm.createContext({DOMException,RTCPeerConnection:function Native(){},webkitRTCPeerConnection:function NativeAlias(){},mozRTCPeerConnection:function OtherAlias(){}});vm.runInContext(definition+';denyUninspectedRTC();',context);return context;}
test('unsupported RTC constructors and aliases fail with fixed NotAllowedError before native invocation',()=>{const c=world();for(const name of ['RTCPeerConnection','webkitRTCPeerConnection','mozRTCPeerConnection'])assert.throws(()=>vm.runInContext('new '+name+'()',c),e=>e.name==='NotAllowedError'&&e.message==='Managed WebRTC inspection is unavailable');});
test('globals and their prototype constructor cannot expose or restore the native constructor',()=>{const c=world();assert.equal(vm.runInContext('Object.getOwnPropertyDescriptor(globalThis,"RTCPeerConnection").writable',c),false);assert.equal(vm.runInContext('Object.getOwnPropertyDescriptor(globalThis,"RTCPeerConnection").configurable',c),false);assert.throws(()=>vm.runInContext('new RTCPeerConnection.prototype.constructor()',c),e=>e.name==='NotAllowedError');assert.throws(()=>vm.runInContext('Object.defineProperty(globalThis,"RTCPeerConnection",{value:function(){}})',c));vm.runInContext('RTCPeerConnection=function(){}',c);assert.throws(()=>vm.runInContext('new RTCPeerConnection()',c),e=>e.name==='NotAllowedError');});
test('preload rejects missing sandbox and installs only the fixed main-world function without a page bridge',()=>{for(const secure of [false,true]){let sends=0,stops=0,installed=0;const c=vm.createContext({process:{sandboxed:secure,contextIsolated:secure},window:{stop:()=>stops++},require:()=>({ipcRenderer:{send:()=>sends++},contextBridge:{executeInMainWorld:({func})=>{assert.equal(func.name,'denyUninspectedRTC');installed++;}}})});if(secure){vm.runInContext(source,c);assert.equal(installed,1);assert.equal(sends,0);}else{assert.throws(()=>vm.runInContext(source,c));assert.equal(installed,0);assert.equal(sends,1);assert.equal(stops,1);}}assert.doesNotMatch(source,/exposeInMainWorld|ipcRenderer\.invoke|node:|fs\./);});