Skip to content
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

Wrong applying encodeURIComponent to credentials for Basic Auth #1033

Closed
2 tasks done
izonder opened this issue Jan 21, 2020 · 3 comments
Closed
2 tasks done

Wrong applying encodeURIComponent to credentials for Basic Auth #1033

izonder opened this issue Jan 21, 2020 · 3 comments

Comments

@izonder
Copy link

izonder commented Jan 21, 2020

Describe the bug

Passing credentials in options.username and options.password that contain "unsafe" characters leads to applying encodeURIComponent to them with wrong Base64-encoded string as a result.

Actual behavior

Making a call with {"username": "test@test", "password": "123456"} results to having the following authorization header:

Authorization: Basic dGVzdCU0MHRlc3Q6MTIzNDU2

After decoding we get the string: test%40test:123456

Expected behavior

Reference call via cURL will result differently:

curl -u 'test@test:123456' -v 'http://localhost:3000/'
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
* Server auth using Basic with user 'test@test'
> GET / HTTP/1.1
> Host: localhost:3000
> Authorization: Basic dGVzdEB0ZXN0OjEyMzQ1Ng==
> User-Agent: curl/7.58.0
> Accept: */*

curl -v 'http://test%40test:123456@localhost:3000/'
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
* Server auth using Basic with user 'test@test'
> GET / HTTP/1.1
> Host: localhost:3000
> Authorization: Basic dGVzdEB0ZXN0OjEyMzQ1Ng==
> User-Agent: curl/7.58.0
> Accept: */*

Decoding the Authorization header results to test@test:123456 as expected.

Code to reproduce

got.get('http://localhost:3000/', {
    username: 'test@test',
    password: '123456'
})
  .then((r) => console.log(r))
  .catch((e) => console.log(e));

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.
@izonder
Copy link
Author

izonder commented Jan 21, 2020

The issue caused by URL object behavior: https://nodejs.org/dist/latest-v12.x/docs/api/url.html#url_url_username

@izonder izonder closed this as completed Jan 21, 2020
@izonder
Copy link
Author

izonder commented Jan 21, 2020

Just as a comment: very easily reproducing. Launch Node.js CLI (node -i), and then type:

const {URL} = require('url');
const http = require('http');
const url = new URL('http://localhost'); //doesn't matter whether there is listening server here or not
url.username = 'test@test';
url.password = '123456';
console.log(http.get(url, () => {}).outputData);

You will see the output:

[
  {
    data: 'GET /profile HTTP/1.1\r\n' +
      'Host: localhost\r\n' +
      'Authorization: Basic dGVzdCU0MHRlc3Q6MTIzNDU2\r\n' +
      'Connection: close\r\n' +
      '\r\n',
    encoding: 'latin1',
    callback: [Function: bound onFinish]
  }
]

Decoding Authorization header results to test%40test:123456

@izonder
Copy link
Author

izonder commented Jan 21, 2020

nodejs/node#31439

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant