Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i modify the host header

I am trying to develop a chrome extension that would set the "host" header on certain requests. But the documentation is contradicting as to if the "host" header can be modified or not.

Both of these issues indicate that a) it should not be possible and b) it is impossible https://code.google.com/p/chromium/issues/detail?id=154900 https://code.google.com/p/chromium/issues/detail?id=158073

Yet multiple extensions in the gallery state they do modify the "host" header. e.g. https://chrome.google.com/webstore/detail/header-hacker/phnffahgegfkcobeaapbenpmdnkifigc?hl=en https://chrome.google.com/webstore/detail/change-http-request-heade/ppmibgfeefcglejjlpeihfdimbkfbbnm

Is it possible to modify the "host" header in the windows version of chrome, and if so how?

Background: I want to be able to test load balanced web instances hitting each host directly via ip address. The "hosts" file is to cumbersome for a large number of hosts. At the moment I use curl to pass the modified "host" header, but I really need the solution in the browser and available for others.

like image 345
rob Avatar asked Oct 20 '25 02:10

rob


2 Answers

@kzahel was right, and the note about redirection was spot on, here is my basic working code.

chrome.webRequest.onBeforeSendHeaders.addListener(function (details) {
    if (details.url.indexOf('toast.txt') <= -1)
        return;
    details.requestHeaders.push({
        name: 'Host',
        value: 'testhost:80'
    });
    return { requestHeaders: details.requestHeaders };
}, {
    urls: ['http://*/*']
}, ['requestHeaders', 'blocking']);

chrome.webRequest.onBeforeRequest.addListener(function (details) {
    if (details.url.indexOf('sauce') <= -1)
        return;
    var url = 'http://127.0.0.1/toast.txt';
    return { redirectUrl: url };
}, {
    urls: ['http://*/*']
}, ['blocking']);

Admittedly a slightly odd example but it goes like this.

My local IIS has a site created that points to a folder that has a file "toast.txt", which is bound to "testhost".

Windows can no way of knowing about "testhost" e.g. cannot ping it.

With the extension running. Enter the address http://testhost/sauce

The extension notes the "sauce" in the "onBeforeRequest" method and redirects to "http://127.0.0.1/toast.txt" which in turn is picked up on by the "onBeforeSendHeaders" method where the "Host" header is added containing "testhost:80". All this occurs before the request leaves Chrome.

Now IIS receives the request "/toast.txt" that by itself would result in a 404, but because the "Host" header is set it pushes the request to the new website that is bound to "testhost" which then returns the "toast.txt" file.

Phew!

like image 160
rob Avatar answered Oct 21 '25 23:10

rob


It looks like you shouldn't have difficulty doing this. Use onBeforeRequest

onBeforeRequest: Fires when a request is about to occur. This event is sent before any TCP connection is made and can be used to cancel or redirect requests.

Since this is triggered before any connection to the server is made, you should be able to modify the host header then [edit: if host header is not available, then use a redirect]. Make sure you have the "requestHeaders" permission in the manifest or else you won't see the request headers at all.

like image 35
kzahel Avatar answered Oct 21 '25 22:10

kzahel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!