@@ -661,10 +661,11 @@ Browser.runtime.onMessage.addListener(async (message, sender) => {
661661 break
662662 case 'PIN_TAB' : {
663663 console . log ( '[background] Processing PIN_TAB message:' , message . data )
664- let tabId = message . data . tabId ?? sender . tab ?. id
664+ const data = message . data ?? { }
665+ let tabId = data . tabId ?? sender . tab ?. id
665666 if ( tabId ) {
666667 await Browser . tabs . update ( tabId , { pinned : true } )
667- if ( message . data . saveAsChatgptConfig ) {
668+ if ( data . saveAsChatgptConfig ) {
668669 console . debug ( '[background] Saving pinned tab as ChatGPT config tab:' , tabId )
669670 await setUserConfig ( { chatgptTabId : tabId } )
670671 }
@@ -686,20 +687,28 @@ Browser.runtime.onMessage.addListener(async (message, sender) => {
686687 console . warn ( '[background] Invalid FETCH input:' , message . data ?. input )
687688 return [ null , { message : 'Invalid fetch input' } ]
688689 }
689- if ( ! fetchInput . startsWith ( 'https://' ) && ! fetchInput . startsWith ( 'http://' ) ) {
690- console . warn ( '[background] Rejecting FETCH for non-http(s) URL:' , fetchInput )
691- return [ null , { message : 'Unsupported fetch protocol' } ]
690+ let validatedUrl
691+ try {
692+ const url = new URL ( fetchInput )
693+ if ( url . protocol !== 'https:' && url . protocol !== 'http:' ) {
694+ console . warn ( '[background] Rejecting FETCH for non-http(s) URL:' , fetchInput )
695+ return [ null , { message : 'Unsupported fetch protocol' } ]
696+ }
697+ validatedUrl = url . toString ( )
698+ } catch ( error ) {
699+ console . warn ( '[background] Invalid FETCH input URL:' , fetchInput , error )
700+ return [ null , { message : 'Invalid fetch URL' } ]
692701 }
693702
694- console . log ( '[background] Processing FETCH message for URL:' , fetchInput )
695- if ( fetchInput . includes ( 'bing.com' ) ) {
703+ console . log ( '[background] Processing FETCH message for URL:' , validatedUrl )
704+ if ( validatedUrl . includes ( 'bing.com' ) ) {
696705 console . debug ( '[background] Fetching Bing access token for FETCH message.' )
697706 const accessToken = await getBingAccessToken ( )
698707 await setUserConfig ( { bingAccessToken : accessToken } )
699708 }
700709
701710 try {
702- const response = await fetch ( fetchInput , message . data ?. init )
711+ const response = await fetch ( validatedUrl , message . data ?. init )
703712 const text = await response . text ( )
704713 const responseObject = {
705714 // Defined for clarity before conditional error property
@@ -712,15 +721,15 @@ Browser.runtime.onMessage.addListener(async (message, sender) => {
712721 if ( ! response . ok ) {
713722 responseObject . error = `HTTP error ${ response . status } : ${ response . statusText } `
714723 console . warn (
715- `[background] FETCH received error status: ${ response . status } for ${ fetchInput } ` ,
724+ `[background] FETCH received error status: ${ response . status } for ${ validatedUrl } ` ,
716725 )
717726 }
718727 console . debug (
719- `[background] FETCH successful for ${ fetchInput } , status: ${ response . status } ` ,
728+ `[background] FETCH successful for ${ validatedUrl } , status: ${ response . status } ` ,
720729 )
721730 return [ responseObject , null ]
722731 } catch ( error ) {
723- console . error ( `[background] FETCH error for ${ fetchInput } :` , error )
732+ console . error ( `[background] FETCH error for ${ validatedUrl } :` , error )
724733 return [ null , { message : error . message } ]
725734 }
726735 }
@@ -750,6 +759,13 @@ Browser.runtime.onMessage.addListener(async (message, sender) => {
750759 console . warn ( '[background] Rejecting GET_COOKIE with invalid URL:' , cookieUrlInput )
751760 return null
752761 }
762+ if ( cookieUrl . protocol !== 'http:' && cookieUrl . protocol !== 'https:' ) {
763+ console . warn (
764+ '[background] Rejecting GET_COOKIE with disallowed protocol:' ,
765+ cookieUrl . protocol ,
766+ )
767+ return null
768+ }
753769
754770 const cookieName = cookieNameInput . trim ( )
755771 console . debug ( '[background] Processing GET_COOKIE message for:' , cookieUrl . href )
@@ -847,20 +863,24 @@ try {
847863 const headers = details . requestHeaders
848864 let modified = false
849865 for ( let i = 0 ; i < headers . length ; i ++ ) {
850- if ( ! headers [ i ] ) {
866+ const header = headers [ i ]
867+ if ( ! header || ! header . name ) {
851868 continue
852869 }
853- const headerNameLower = headers [ i ] . name ? .toLowerCase ( )
870+ const headerNameLower = header . name . toLowerCase ( )
854871 if ( headerNameLower === 'origin' ) {
855- headers [ i ] . value = 'https://www.bing.com'
872+ header . value = 'https://www.bing.com'
856873 modified = true
857874 } else if ( headerNameLower === 'referer' ) {
858- headers [ i ] . value = 'https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx'
875+ header . value = 'https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx'
859876 modified = true
860877 }
861878 }
862879 if ( modified ) {
863- console . debug ( '[background] Modified headers for Bing:' , headers )
880+ console . debug (
881+ '[background] Modified headers for Bing (names only):' ,
882+ headers . map ( ( header ) => header ?. name ) . filter ( Boolean ) ,
883+ )
864884 }
865885 return { requestHeaders : headers }
866886 } catch ( error ) {
@@ -883,11 +903,15 @@ try {
883903 ( details ) => {
884904 const headers = details . requestHeaders
885905 for ( let i = 0 ; i < headers . length ; i ++ ) {
886- const headerNameLower = headers [ i ] ?. name ?. toLowerCase ( )
906+ const header = headers [ i ]
907+ if ( ! header || ! header . name ) {
908+ continue
909+ }
910+ const headerNameLower = header . name . toLowerCase ( )
887911 if ( headerNameLower === 'origin' ) {
888- headers [ i ] . value = 'https://claude.ai'
912+ header . value = 'https://claude.ai'
889913 } else if ( headerNameLower === 'referer' ) {
890- headers [ i ] . value = 'https://claude.ai'
914+ header . value = 'https://claude.ai'
891915 }
892916 }
893917 return { requestHeaders : headers }
0 commit comments