diff --git a/args.js b/args.js index 0e0307d6..a0b2e101 100644 --- a/args.js +++ b/args.js @@ -23,13 +23,14 @@ const DEFAULT_ARGUMENTS = { module.exports = function parseArgs (args) { dotenv.config() + const TRUST_PROXY = process.env.TRUST_PROXY || '' const commandLineArguments = argv(args, { configuration: { 'populate--': true }, number: ['port', 'inspect-port', 'body-limit', 'plugin-timeout', 'close-grace-delay'], string: ['log-level', 'address', 'socket', 'prefix', 'ignore-watch', 'logging-module', 'debug-host', 'lang', 'require', 'config', 'method'], - boolean: ['pretty-logs', 'options', 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks'], + boolean: ['pretty-logs', 'options', 'trustProxy', 'watch', 'verbose-watch', 'debug', 'standardlint', 'common-prefix', 'include-hooks'], envPrefix: 'FASTIFY_', alias: { port: ['p'], @@ -65,7 +66,9 @@ module.exports = function parseArgs (args) { // Merge objects from lower to higher priority const parsedArgs = { ...DEFAULT_ARGUMENTS, ...configFileOptions, ...commandLineArguments } - + const isNumber = isNaN(Number(TRUST_PROXY)) === false + const isTrue = TRUST_PROXY.toLowerCase() === 'true' + const trustProxy = isNumber ? Number(TRUST_PROXY) : isTrue return { _: parsedArgs._, '--': additionalArgs, @@ -91,6 +94,7 @@ module.exports = function parseArgs (args) { lang: parsedArgs.lang, method: parsedArgs.method, commonPrefix: parsedArgs.commonPrefix, - includeHooks: parsedArgs.includeHooks + includeHooks: parsedArgs.includeHooks, + trustProxy: parsedArgs.trustProxy || Boolean(trustProxy) } } diff --git a/start.js b/start.js index 610f37a9..5464091f 100755 --- a/start.js +++ b/start.js @@ -138,7 +138,9 @@ async function runFastify (args, additionalOptions, serverOptions) { if (opts.options && file.options) { options = deepmerge(options, file.options) } - + if (opts.trustProxy) { + options.trustProxy = opts.trustProxy + } const fastify = Fastify(options) if (opts.prefix) { diff --git a/test/args.test.js b/test/args.test.js index 9b1a59fb..8e927a79 100644 --- a/test/args.test.js +++ b/test/args.test.js @@ -53,7 +53,8 @@ test('should parse args correctly', t => { lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: false }) }) @@ -108,7 +109,8 @@ test('should parse args with = assignment correctly', t => { lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: false }) }) @@ -181,7 +183,8 @@ test('should parse env vars correctly', t => { lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: false }) }) @@ -272,7 +275,8 @@ test('should parse custom plugin options', t => { lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: false }) }) @@ -310,7 +314,8 @@ test('should parse config file correctly and prefer config values over default o lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: true }) }) @@ -352,6 +357,7 @@ test('should prefer command line args over config file options', t => { lang: 'js', method: undefined, commonPrefix: false, - includeHooks: undefined + includeHooks: undefined, + trustProxy: true }) }) diff --git a/test/data/custom-config.js b/test/data/custom-config.js index 71dbcd2d..73aa7e71 100644 --- a/test/data/custom-config.js +++ b/test/data/custom-config.js @@ -6,5 +6,6 @@ module.exports = { prettyLogs: true, debugPort: 4000, pluginTimeout: 9 * 1000, - closeGraceDelay: 1000 + closeGraceDelay: 1000, + trustProxy: true }