Skip to content

The pipe forwards the client host. #1746

Closed

Description

@royalpinto

Describe the bug

  • Node.js version: v14
  • OS & version: macOS (10)
  • Got version: 11.8.2

Actual behavior

The pipe request forwards all the client headers including the "Host"

Expected behavior

The pipe request should forwards all the client headers but not the "Host"

Code to reproduce

npm install got nock express request 
const got = require('got')
const nock = require('nock')
const express = require('express')
const request = require('request')

nock('http://www.google.com')
  .persist()
  .get('/echo')
  .reply(function (uri, body) {
    return [200, { headers: this.req.headers }]
  })

const proxy = express()
proxy.get('/got/pipe', (req, res) => {
  req.pipe(got.stream('http://www.google.com/echo')).pipe(res)
})

proxy.get('/request/pipe', (req, res) => {
  req.pipe(request.get('http://www.google.com/echo')).pipe(res)
})

proxy.listen(3000, () => {
  console.log('Try the following:')
  console.log("curl -H 'Hi: Bye' -H 'Host: Value' 'http://127.0.0.1:3000/got/pipe'")
  console.log("curl -H 'Hi: Bye' -H 'Host: Value' 'http://127.0.0.1:3000/request/pipe'")
})

Output

// With Got
curl -H 'Hi: Bye' -H 'Host: Value' 'http://127.0.0.1:3000/got/pipe'
{
    "headers": {
        "accept": "*/*",
        "accept-encoding": "gzip, deflate, br",
        "hi": "Bye",
        "host": "Value", **// Should not have been this.**
        "user-agent": "got (https://github.com/sindresorhus/got)"
    }
}

// With Request
curl -H 'Hi: Bye' -H 'Host: Value' 'http://127.0.0.1:3000/request/pipe'
{
    "headers": {
        "accept": "*/*",
        "hi": "Bye",
        "host": "www.google.com",
        "user-agent": "curl/7.64.1"
    }
}

Since the host doesn't match the hostname in the URL, it leads to a certificate error when hitting the actual service than the mocked one.

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

Activity

  1. szmarczak commented on Jun 8, 2021

    @szmarczak
    Collaborator

    Simply delete req.headers.host; before piping to got.

  2. szmarczak commented on Jun 8, 2021

    @szmarczak
    Collaborator

    got/source/core/index.ts

    Lines 1415 to 1422 in f896aa5

    this.on('pipe', source => {
    if (source instanceof IncomingMessage) {
    this.options.headers = {
    ...source.headers,
    ...this.options.headers
    };
    }
    });

  3. szmarczak commented on Jun 8, 2021

    @szmarczak
    Collaborator

    Or you can use a beforeRequest hook:

    {
        hooks: {
            beforeRequest: [
                options => {
                    delete options.headers.host;
                }
            ]
        }
      }
    }
  4. Repository owner locked and limited conversation to collaborators on Jun 8, 2021
  5. Repository owner unlocked this conversation on Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions