|
9 | 9 |
|
10 | 10 | import pem from 'pem' |
11 | 11 | import supertest from 'supertest' |
| 12 | +import proxyAddr from 'proxy-addr' |
12 | 13 | import { test } from '@japa/runner' |
13 | 14 | import Middleware from '@poppinss/middleware' |
14 | 15 | import { createServer as httpsServer } from 'node:https' |
@@ -517,6 +518,90 @@ test.group('Request', () => { |
517 | 518 | }) |
518 | 519 | }) |
519 | 520 |
|
| 521 | + test('do not trust proxy when trustProxy does not allow it', async ({ assert }) => { |
| 522 | + const { url } = await httpServer.create((req, res) => { |
| 523 | + req.headers['x-forwarded-for'] = '10.10.10.10' |
| 524 | + const request = new RequestFactory() |
| 525 | + .merge({ |
| 526 | + req, |
| 527 | + res, |
| 528 | + encryption, |
| 529 | + config: { |
| 530 | + trustProxy: proxyAddr.compile('192.168.1.0/24'), |
| 531 | + }, |
| 532 | + }) |
| 533 | + .create() |
| 534 | + res.writeHead(200, { 'content-type': 'application/json' }) |
| 535 | + res.end(JSON.stringify({ ip: request.ip() })) |
| 536 | + }) |
| 537 | + |
| 538 | + const { body } = await supertest(url).get('/') |
| 539 | + assert.notEqual(body.ip, '10.10.10.10') |
| 540 | + }) |
| 541 | + |
| 542 | + test('trust proxy when trustProxy allows it', async ({ assert }) => { |
| 543 | + const { url } = await httpServer.create((req, res) => { |
| 544 | + req.headers['x-forwarded-for'] = '10.10.10.10' |
| 545 | + const request = new RequestFactory() |
| 546 | + .merge({ |
| 547 | + req, |
| 548 | + res, |
| 549 | + encryption, |
| 550 | + config: { |
| 551 | + trustProxy: proxyAddr.compile('loopback'), |
| 552 | + }, |
| 553 | + }) |
| 554 | + .create() |
| 555 | + res.writeHead(200, { 'content-type': 'application/json' }) |
| 556 | + res.end(JSON.stringify({ ip: request.ip() })) |
| 557 | + }) |
| 558 | + |
| 559 | + const { body } = await supertest(url).get('/') |
| 560 | + assert.equal(body.ip, '10.10.10.10') |
| 561 | + }) |
| 562 | + |
| 563 | + test('trust all proxies when trustProxy is true', async ({ assert }) => { |
| 564 | + const { url } = await httpServer.create((req, res) => { |
| 565 | + req.headers['x-forwarded-for'] = '10.10.10.10' |
| 566 | + const request = new RequestFactory() |
| 567 | + .merge({ |
| 568 | + req, |
| 569 | + res, |
| 570 | + encryption, |
| 571 | + config: { |
| 572 | + trustProxy: (_, __) => true, |
| 573 | + }, |
| 574 | + }) |
| 575 | + .create() |
| 576 | + res.writeHead(200, { 'content-type': 'application/json' }) |
| 577 | + res.end(JSON.stringify({ ip: request.ip() })) |
| 578 | + }) |
| 579 | + |
| 580 | + const { body } = await supertest(url).get('/') |
| 581 | + assert.equal(body.ip, '10.10.10.10') |
| 582 | + }) |
| 583 | + |
| 584 | + test('trust no proxy when trustProxy is false', async ({ assert }) => { |
| 585 | + const { url } = await httpServer.create((req, res) => { |
| 586 | + req.headers['x-forwarded-for'] = '10.10.10.10' |
| 587 | + const request = new RequestFactory() |
| 588 | + .merge({ |
| 589 | + req, |
| 590 | + res, |
| 591 | + encryption, |
| 592 | + config: { |
| 593 | + trustProxy: (_, __) => false, |
| 594 | + }, |
| 595 | + }) |
| 596 | + .create() |
| 597 | + res.writeHead(200, { 'content-type': 'application/json' }) |
| 598 | + res.end(JSON.stringify({ ip: request.ip() })) |
| 599 | + }) |
| 600 | + |
| 601 | + const { body } = await supertest(url).get('/') |
| 602 | + assert.notEqual(body.ip, '10.10.10.10') |
| 603 | + }) |
| 604 | + |
520 | 605 | test('return request url without query string', async ({ assert }) => { |
521 | 606 | const { url } = await httpServer.create((req, res) => { |
522 | 607 | const request = new RequestFactory().merge({ req, res, encryption }).create() |
|
0 commit comments