Skip to content

Commit 1696e35

Browse files
RubenVerborghdmitrizagidulin
authored andcommitted
Correct proxy error codes.
1 parent da8df9b commit 1696e35

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

lib/handlers/proxy.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,15 @@ function extractProxyConfig (req, res, next) {
3636
// Retrieve and validate the destination URL
3737
const uri = req.query.uri
3838
debug.settings(`Proxy request for ${uri}`)
39-
if (!uri) {
40-
return res.status(400)
41-
.send('Proxy has no uri param ')
42-
}
4339
if (!validUrl.isUri(uri)) {
44-
return res.status(406)
45-
.send('The uri passed is not valid')
40+
return res.status(400).send(`Invalid URL passed: ${uri || '(none)'}`)
4641
}
4742

4843
// Ensure the host is not a local IP
4944
// TODO: guard against hostnames such as 'localhost' as well
5045
const { protocol, host, hostname, path } = url.parse(uri)
5146
if (isIp(hostname) && LOCAL_IP_RANGES.some(r => ipRange(hostname, r))) {
52-
return res
53-
.status(406)
54-
.send('Cannot proxy this IP')
47+
return res.status(400).send(`Cannot proxy ${uri}`)
5548
}
5649

5750
// Add the proxy configuration to the request

test/integration/proxy.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,34 @@ describe('proxy', () => {
2020
.expect(200, done)
2121
})
2222

23-
it('should return error on local network requests', (done) => {
23+
it('should return 400 when the uri parameter is missing', (done) => {
24+
nock('https://192.168.0.0').get('/').reply(200)
25+
server.get('/proxy')
26+
.expect('Invalid URL passed: (none)')
27+
.expect(400)
28+
.end(done)
29+
})
30+
31+
it('should return 400 on local network requests', (done) => {
2432
nock('https://192.168.0.0').get('/').reply(200)
2533
server.get('/proxy?uri=https://192.168.0.0/')
26-
.expect(406, done)
34+
.expect('Cannot proxy https://192.168.0.0/')
35+
.expect(400)
36+
.end(done)
2737
})
2838

29-
it('should return error on invalid uri', (done) => {
39+
it('should return 400 on invalid uri', (done) => {
3040
server.get('/proxy?uri=HELLOWORLD')
31-
.expect(406, done)
41+
.expect('Invalid URL passed: HELLOWORLD')
42+
.expect(400)
43+
.end(done)
3244
})
3345

34-
it('should return error on relative paths', (done) => {
46+
it('should return 400 on relative paths', (done) => {
3547
server.get('/proxy?uri=../')
36-
.expect(406, done)
48+
.expect('Invalid URL passed: ../')
49+
.expect(400)
50+
.end(done)
3751
})
3852

3953
it('should return the same headers of proxied request', (done) => {

0 commit comments

Comments
 (0)