Skip to content

Support for non-friendly chars in url username and password #858

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

Merged
merged 2 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Connection {
this.ssl = opts.ssl || null
this.id = opts.id || stripAuth(opts.url.href)
this.headers = opts.headers || null
this.auth = opts.auth || { username: null, password: null }
this.deadCount = 0
this.resurrectTimeout = 0

Expand Down Expand Up @@ -180,6 +181,7 @@ class Connection {

buildRequestObject (params) {
const url = this.url
const { username, password } = this.auth
const request = {
protocol: url.protocol,
hostname: url.hostname[0] === '['
Expand All @@ -194,8 +196,8 @@ class Connection {
// https://github.com/elastic/elasticsearch-js/issues/843
port: url.port !== '' ? url.port : undefined,
headers: this.headers,
auth: !!url.username === true || !!url.password === true
? `${url.username}:${url.password}`
auth: username != null && password != null
? `${username}:${password}`
: undefined,
agent: this.agent
}
Expand Down Expand Up @@ -224,7 +226,7 @@ class Connection {
}

// Handles console.log and utils.inspect invocations.
// We want to hide `agent` and `ssl` since they made
// We want to hide `auth`, `agent` and `ssl` since they made
// the logs very hard to read. The user can still
// access them with `instance.agent` and `instance.ssl`.
[inspect.custom] (depth, options) {
Expand Down
21 changes: 12 additions & 9 deletions lib/ConnectionPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,24 @@ class ConnectionPool {
// we can add it to them once the connection instance has been created
if (opts.url.username !== '' && opts.url.password !== '') {
this._auth = {
username: opts.url.username,
password: opts.url.password
username: decodeURIComponent(opts.url.username),
password: decodeURIComponent(opts.url.password)
}
opts.auth = this._auth
}

if (this._auth != null) {
if (opts.auth == null || (opts.auth.username == null && opts.auth.password == null)) {
opts.auth = this._auth
opts.url.username = this._auth.username
opts.url.password = this._auth.password
}
}

if (opts.ssl == null) opts.ssl = this._ssl
if (opts.agent == null) opts.agent = this._agent

const connection = new this.Connection(opts)
if (connection.url.username === '' &&
connection.url.password === '' &&
this._auth != null) {
connection.url.username = this._auth.username
connection.url.password = this._auth.password
}

debug('Adding a new connection', connection)
if (this.connections.has(connection.id)) {
throw new Error(`Connection with id '${connection.id}' is already present`)
Expand Down
19 changes: 17 additions & 2 deletions test/unit/connection-pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,23 @@ test('API', t => {
t.deepEqual(pool._auth, { username: 'foo', password: 'bar' })

pool.addConnection('http://localhost:9201')
t.strictEqual(pool.connections.get('http://localhost:9201/').url.username, 'foo')
t.strictEqual(pool.connections.get('http://localhost:9201/').url.password, 'bar')
const conn = pool.connections.get('http://localhost:9201/')
t.strictEqual(conn.url.username, 'foo')
t.strictEqual(conn.url.password, 'bar')
t.strictEqual(conn.auth.username, 'foo')
t.strictEqual(conn.auth.password, 'bar')
t.end()
})

t.test('addConnection should handle not-friendly url parameters for user and password', t => {
const pool = new ConnectionPool({ Connection })
const href = 'http://us"er:p@assword@localhost:9200/'
pool.addConnection(href)
const conn = pool.getConnection()
t.strictEqual(conn.url.username, 'us%22er')
t.strictEqual(conn.url.password, 'p%40assword')
t.strictEqual(conn.auth.username, 'us"er')
t.strictEqual(conn.auth.password, 'p@assword')
t.end()
})

Expand Down
3 changes: 2 additions & 1 deletion test/unit/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,8 @@ test('Url with auth', t => {

buildServer(handler, ({ port }, server) => {
const connection = new Connection({
url: new URL(`http://foo:bar@localhost:${port}`)
url: new URL(`http://foo:bar@localhost:${port}`),
auth: { username: 'foo', password: 'bar' }
})
connection.request({
path: '/hello',
Expand Down