Skip to content

Commit

Permalink
feat(request): allow to use a boolean for trustProxy (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz authored Oct 17, 2023
1 parent 8dd52ef commit b44e29e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/define_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ import proxyAddr from 'proxy-addr'
import string from '@poppinss/utils/string'
import type { ServerConfig } from './types/server.js'

type UserDefinedServerConfig = Partial<
Omit<ServerConfig, 'trustProxy'> & {
trustProxy: ((address: string, distance: number) => boolean) | boolean | string
}
>

/**
* Define configuration for the HTTP server
*/
export function defineConfig(config: Partial<ServerConfig>): ServerConfig {
export function defineConfig(config: UserDefinedServerConfig): ServerConfig {
const normalizedConfig = {
allowMethodSpoofing: false,
trustProxy: proxyAddr.compile('loopback'),
Expand Down Expand Up @@ -52,5 +58,13 @@ export function defineConfig(config: Partial<ServerConfig>): ServerConfig {
normalizedConfig.cookie.maxAge = string.seconds.parse(normalizedConfig.cookie.maxAge)
}

if (typeof normalizedConfig.trustProxy === 'boolean') {
const tpValue = normalizedConfig.trustProxy
normalizedConfig.trustProxy = (_, __) => tpValue
} else if (typeof normalizedConfig.trustProxy === 'string') {
const tpValue = normalizedConfig.trustProxy
normalizedConfig.trustProxy = proxyAddr.compile(tpValue)
}

return normalizedConfig

Check failure on line 69 in src/define_config.ts

View workflow job for this annotation

GitHub Actions / typecheck / typecheck

Type '{ qs: QSParserConfig; cookie: Partial<CookieOptions>; etag: boolean; subdomainOffset: number; generateRequestId: boolean; allowMethodSpoofing: boolean; getIp?: ((request: any) => string) | undefined; jsonpCallbackName: string; useAsyncLocalStorage: boolean; trustProxy: string | ... 1 more ... | ((address: string, di...' is not assignable to type 'ServerConfig'.
}
12 changes: 12 additions & 0 deletions tests/define_config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ test.group('Define config', () => {
useAsyncLocalStorage: false,
})
})

test('compile trustProxy config when boolean', ({ assert }) => {
const config = defineConfig({ trustProxy: true })

assert.typeOf(config.trustProxy, 'function')
})

test('comfile trustProxy config when string', ({ assert }) => {
const config = defineConfig({ trustProxy: 'loopback' })

assert.typeOf(config.trustProxy, 'function')
})
})
85 changes: 85 additions & 0 deletions tests/request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pem from 'pem'
import supertest from 'supertest'
import proxyAddr from 'proxy-addr'
import { test } from '@japa/runner'
import Middleware from '@poppinss/middleware'
import { createServer as httpsServer } from 'node:https'
Expand Down Expand Up @@ -517,6 +518,90 @@ test.group('Request', () => {
})
})

test('do not trust proxy when trustProxy does not allow it', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
req.headers['x-forwarded-for'] = '10.10.10.10'
const request = new RequestFactory()
.merge({
req,
res,
encryption,
config: {
trustProxy: proxyAddr.compile('192.168.1.0/24'),
},
})
.create()
res.writeHead(200, { 'content-type': 'application/json' })
res.end(JSON.stringify({ ip: request.ip() }))
})

const { body } = await supertest(url).get('/')
assert.notEqual(body.ip, '10.10.10.10')
})

test('trust proxy when trustProxy allows it', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
req.headers['x-forwarded-for'] = '10.10.10.10'
const request = new RequestFactory()
.merge({
req,
res,
encryption,
config: {
trustProxy: proxyAddr.compile('loopback'),
},
})
.create()
res.writeHead(200, { 'content-type': 'application/json' })
res.end(JSON.stringify({ ip: request.ip() }))
})

const { body } = await supertest(url).get('/')
assert.equal(body.ip, '10.10.10.10')
})

test('trust all proxies when trustProxy is true', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
req.headers['x-forwarded-for'] = '10.10.10.10'
const request = new RequestFactory()
.merge({
req,
res,
encryption,
config: {
trustProxy: (_, __) => true,
},
})
.create()
res.writeHead(200, { 'content-type': 'application/json' })
res.end(JSON.stringify({ ip: request.ip() }))
})

const { body } = await supertest(url).get('/')
assert.equal(body.ip, '10.10.10.10')
})

test('trust no proxy when trustProxy is false', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
req.headers['x-forwarded-for'] = '10.10.10.10'
const request = new RequestFactory()
.merge({
req,
res,
encryption,
config: {
trustProxy: (_, __) => false,
},
})
.create()
res.writeHead(200, { 'content-type': 'application/json' })
res.end(JSON.stringify({ ip: request.ip() }))
})

const { body } = await supertest(url).get('/')
assert.notEqual(body.ip, '10.10.10.10')
})

test('return request url without query string', async ({ assert }) => {
const { url } = await httpServer.create((req, res) => {
const request = new RequestFactory().merge({ req, res, encryption }).create()
Expand Down

0 comments on commit b44e29e

Please sign in to comment.