Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify who initiated the http request in Firefox?

I am developing a new Firefox addon that intercept all Firefox's network traffic (http(s) requests with http-on-modify-request)

With my current code, I am able to separate requests coming from web-pages/tabs and all the other components (RSS feed updates, XHR requests from XPCOM components, extensions, extensions manager, etc.)

I would like to identify who initiate a request other than the tab's traffic with precision not just the whole group? (RSS, XPCOM components, extensions, extensions manager, etc)

Example: a hypothetical custom variable requestRequestor would have a value to identify a specific addon or RSS update etc.

I found this similar question but without a solution.

Current code to identify the whole group (getting the browser that fires the http-on-modify-request notification) is:

Components.utils.import('resource://gre/modules/Services.jsm'); Services.obs.addObserver(httpObs, 'http-on-modify-request', false); //Services.obs.removeObserver(httpObs, 'http-on-modify-request'); //uncomment this line, or run this line when you want to remove the observer  var httpObs = {     observe: function (aSubject, aTopic, aData) {         if (aTopic == 'http-on-modify-request') {             /*start - do not edit here*/             var oHttp = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel); //i used nsIHttpChannel but i guess you can use nsIChannel, im not sure why though             var interfaceRequestor = oHttp.notificationCallbacks.QueryInterface(Components.interfaces.nsIInterfaceRequestor);             //var DOMWindow = interfaceRequestor.getInterface(Components.interfaces.nsIDOMWindow); //not to be done anymore because: https://developer.mozilla.org/en-US/docs/Updating_extensions_for_Firefox_3.5#Getting_a_load_context_from_a_request //instead do the loadContext stuff below             var loadContext;             try {                 loadContext = interfaceRequestor.getInterface(Components.interfaces.nsILoadContext);             } catch (ex) {                 try {                     loadContext = aSubject.loadGroup.notificationCallbacks.getInterface(Components.interfaces.nsILoadContext);                     //in ff26 aSubject.loadGroup.notificationCallbacks was null for me, i couldnt find a situation where it wasnt null, but whenever this was null, and i knew a loadContext is supposed to be there, i found that "interfaceRequestor.getInterface(Components.interfaces.nsILoadContext);" worked fine, so im thinking in ff26 it doesnt use aSubject.loadGroup.notificationCallbacks anymore, but im not sure                 } catch (ex2) {                     loadContext = null;                     //this is a problem i dont know why it would get here                 }             }             /*end do not edit here*/             /*start - do all your edits below here*/             var url = oHttp.URI.spec; //can get url without needing loadContext             if (loadContext) {                 var contentWindow = loadContext.associatedWindow; //this is the HTML window of the page that just loaded                 //aDOMWindow this is the firefox window holding the tab                 var aDOMWindow = contentWindow.top.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShellTreeItem).rootTreeItem.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);                 var gBrowser = aDOMWindow.gBrowser; //this is the gBrowser object of the firefox window this tab is in                 var aTab = gBrowser._getTabForContentWindow(contentWindow.top); //this is the clickable tab xul element, the one found in the tab strip of the firefox window, aTab.linkedBrowser is same as browser var above //can stylize tab like aTab.style.backgroundColor = 'blue'; //can stylize the tab like aTab.style.fontColor = 'red';                 var browser = aTab.linkedBrowser; //this is the browser within the tab //this is what the example in the previous section gives                 //end getting other useful stuff             } else {                 Components.utils.reportError('EXCEPTION: Load Context Not Found!!');                 //this is likely no big deal as the channel proably has no associated window, ie: the channel was loading some resource. but if its an ajax call you may end up here             }         }     } }; 
like image 480
intika Avatar asked Jun 30 '15 04:06

intika


People also ask

How can I tell where a HTTP request came from?

There is absolutely no way to know with certainty if a request came from a browser or something else making an HTTP request. The HTTP protocol allows for the client to set the User Agent arbitrarily.

How do I view HTTP requests in Firefox?

Select the Network tab or directly press Ctrl+Shift+E together from your computer keyboard. 3. Reload the page, select any HTTP request, and the HTTP headers will be displayed on the right panel.

How do I stop a Firefox request?

Hover over the item you want to block in the Request List. Select Block URL from the context menu.


1 Answers

As of June 2020, there is no official method/way of achieving http request requester filtering/identification.

Currently the only possibility is what is done on the question's code which is separating requests from web-pages/tabs and the other Firefox's components (feed updates, extensions requests, XHR requests from XPCOM components, etc).

As mentioned on the comments this is an internal limitation of Firefox. The current Firefox's core code does not implement a requester tracking and thus does not know who initiated the request and why. It may be useful to know that Chrome dev tools got recently this feature.

like image 112
intika Avatar answered Sep 23 '22 06:09

intika