import { WebRequest } from 'electron/main' ; param webRequest export function installSameOriginFilter ( webRequest ) { const requestOrigin = new Map < number , string > ( ) ; const safeProtocols = new Set ( [ 'devtools:' , 'file:' , 'chrome-extension:' ] ) ; const unsafeHeaders = new Set ( [ 'authentication' , 'authorization' , 'cookie' ] ) ; webRequest . onBeforeRequest ( ( details , cb ) => { const { protocol , origin } = new URL ( details . url ) ; if ( ! safeProtocols . has ( protocol ) && ! requestOrigin . has ( details . id ) ) { requestOrigin . set ( details . id , origin ) ; } cb ( { } ) ; } ) ; webRequest . onBeforeSendHeaders ( ( details , cb ) => { const initialOrigin = requestOrigin . get ( details . id ) ; const { origin } = new URL ( details . url ) ; if ( initialOrigin === undefined || initialOrigin === origin ) { return cb ( { requestHeaders : details . requestHeaders } ) ; } const sanitizedHeaders = { } ; for ( const [ k , v ] of Object . entries ( details . requestHeaders ) ) { if ( ! unsafeHeaders . has ( k . toLowerCase ( ) ) ) { sanitizedHeaders [ k ] = v ; } } return cb ( { requestHeaders : sanitizedHeaders } ) ; } ) ; webRequest . onCompleted ( details => requestOrigin . delete ( details . id ) ) ; }