Paragon512
Paragon512

Reputation: 153

Firefox Extension - Filter & modify HTTP POST data using onBeforeRequest

Using browser.webRequest.onBeforeRequest.addListener() I am able to filter HTTP requests and modify the source of pages before they are returned. I need to intercept HTTP POST requests and modify the data submitted by a form before sending it to the server. Check the code:

browser.webRequest.onBeforeRequest.addListener(
    function(details) {
        // The code in this if is just some experimentation.
        if(details.method == "POST") {
            let filter = browser.webRequest.filterResponseData(details.requestId)
            let decoder = new TextDecoder("utf-8")
            let encoder = new TextEncoder()
            filter.ondata = event => {
                let str = decoder.decode(event.data, {
                    stream: true
                });
                console.log(str)
            }
            // This is a where I attempt to modify POST data.
            let formData = details.requestBody.formData;
            //console.log(formData)
            if(formData) {
                Object.keys(formData).forEach(key => {
                    formData[key].forEach(value => {
                        //console.log(key)
                        if(key == 'testField'){
                            //console.log("found", formData[key])
                            details.requestBody.formData[key] = "INJECTED"
                            console.log('injected', details.requestBody.formData[key])
                        }
                    })
                })
            }

            return {details: details};
        }
    },
    {urls: ["https://example.com/*"]},
    ["blocking", "requestBody"]
);

The last console.log prints the modified value but returning details does not forward the modified value to the server. How can I use onBeforeRequest (Or any other method) in a Firefox extension to modify POST variables before they are sent to the server?

I do not want to inject code into the web page. I want to filter the traffic.

Upvotes: 3

Views: 1939

Answers (1)

Juan
Juan

Reputation: 61

This seems to be a missing feature.

Relevant bugs:

Upvotes: 3

Related Questions