Skip to content

Commit

Permalink
Do not url encode username and password (#67)
Browse files Browse the repository at this point in the history
* Do not url encode username and password

* Updated test
  • Loading branch information
delvedor authored May 2, 2022
1 parent be60918 commit 6492166
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HttpProxyAgent extends http.Agent {
}

if (this.proxy.username || this.proxy.password) {
const base64 = Buffer.from(`${this.proxy.username || ''}:${this.proxy.password || ''}`).toString('base64')
const base64 = Buffer.from(`${decodeURIComponent(this.proxy.username || '')}:${decodeURIComponent(this.proxy.password || '')}`).toString('base64')
requestOptions.headers['proxy-authorization'] = `Basic ${base64}`
}

Expand Down Expand Up @@ -80,7 +80,7 @@ class HttpsProxyAgent extends https.Agent {
}

if (this.proxy.username || this.proxy.password) {
const base64 = Buffer.from(`${this.proxy.username || ''}:${this.proxy.password || ''}`).toString('base64')
const base64 = Buffer.from(`${decodeURIComponent(this.proxy.username || '')}:${decodeURIComponent(this.proxy.password || '')}`).toString('base64')
requestOptions.headers['proxy-authorization'] = `Basic ${base64}`
}

Expand Down
37 changes: 37 additions & 0 deletions test/http-http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,40 @@ test('Timeout', async t => {
server.close()
proxy.close()
})

test('Username and password should not be encoded', async t => {
const server = await createServer()
const proxy = await createProxy()
server.on('request', (req, res) => res.end('ok'))

proxy.authenticate = function (req, fn) {
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('username_with_=:password_with_=').toString('base64')}`)
}

const response = await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
agent: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://username_with_=:password_with_=@${proxy.address().address}:${proxy.address().port}`
})
})

let body = ''
response.setEncoding('utf8')
for await (const chunk of response) {
body += chunk
}

t.is(body, 'ok')
t.is(response.statusCode, 200)

server.close()
proxy.close()
})
2 changes: 1 addition & 1 deletion test/http-https.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ test('Timeout', async t => {
try {
await request({
method: 'GET',
hostname: server.address().address,
hostname: SERVER_HOSTNAME,
port: server.address().port,
path: '/',
timeout: 1,
Expand Down
2 changes: 1 addition & 1 deletion test/https-http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ test('Timeout', async t => {
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
proxy: `https://${PROXY_HOSTNAME}:${proxy.address().port}`
})
})
t.fail('Should throw')
Expand Down
41 changes: 39 additions & 2 deletions test/https-https.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ test('Timeout', async t => {
try {
await request({
method: 'GET',
hostname: server.address().address,
hostname: SERVER_HOSTNAME,
port: server.address().port,
path: '/',
timeout: 1,
Expand All @@ -330,7 +330,7 @@ test('Timeout', async t => {
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
proxy: `https://${PROXY_HOSTNAME}:${proxy.address().port}`
})
})
t.fail('Should throw')
Expand All @@ -341,3 +341,40 @@ test('Timeout', async t => {
server.close()
proxy.close()
})

test('Username and password should not be encoded', async t => {
const server = await createSecureServer()
const proxy = await createSecureProxy()
server.on('request', (req, res) => res.end('ok'))

proxy.authenticate = function (req, fn) {
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('username_with_=:password_with_=').toString('base64')}`)
}

const response = await request({
method: 'GET',
hostname: SERVER_HOSTNAME,
port: server.address().port,
path: '/',
agent: new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `https://username_with_=:password_with_=@${PROXY_HOSTNAME}:${proxy.address().port}`
})
})

let body = ''
response.setEncoding('utf8')
for await (const chunk of response) {
body += chunk
}

t.is(body, 'ok')
t.is(response.statusCode, 200)

server.close()
proxy.close()
})

0 comments on commit 6492166

Please sign in to comment.