Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The pipe forwards the client host. #1746

Closed
2 tasks done
royalpinto opened this issue Jun 8, 2021 · 3 comments
Closed
2 tasks done

The pipe forwards the client host. #1746

royalpinto opened this issue Jun 8, 2021 · 3 comments

Comments

@royalpinto
Copy link

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.
@szmarczak
Copy link
Collaborator

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

@szmarczak
Copy link
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
};
}
});

@szmarczak
Copy link
Collaborator

Or you can use a beforeRequest hook:

{
    hooks: {
        beforeRequest: [
            options => {
                delete options.headers.host;
            }
        ]
    }
  }
}

Repository owner locked and limited conversation to collaborators Jun 8, 2021
Repository owner unlocked this conversation Sep 7, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants