From 5dc56fac043eab22187f9ae1dd7e73d2160fd7ae Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 5 Jul 2017 10:39:25 +0800 Subject: [PATCH] feat: ignore any key contains "secret" (#1156) --- config/config.default.js | 6 +++++- lib/core/utils.js | 10 +++++++++- .../apps/demo/config/config.default.js | 19 ++++++++++++++++++- test/lib/egg.test.js | 15 +++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/config/config.default.js b/config/config.default.js index de9c96dc43..765665843e 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -115,7 +115,11 @@ module.exports = appInfo => { * @property {Set} ignore - keys to ignore */ dump: { - ignore: new Set([ 'pass', 'pwd', 'passd', 'passwd', 'password', 'keys', 'secret' ]), + ignore: new Set([ + 'pass', 'pwd', 'passd', 'passwd', 'password', 'keys', 'masterKey', 'accessKey', + // ignore any key contains "secret" keyword + /secret/i, + ]), }, /** diff --git a/lib/core/utils.js b/lib/core/utils.js index 767017f317..3aec6c8660 100644 --- a/lib/core/utils.js +++ b/lib/core/utils.js @@ -18,7 +18,15 @@ function convertObject(obj, ignore) { function convertValue(key, value, ignore) { if (is.nullOrUndefined(value)) return value; - if (!ignore.includes(key)) { + let hit; + for (const matchKey of ignore) { + if (typeof matchKey === 'string' && matchKey === key) { + hit = true; + } else if (is.regExp(matchKey) && matchKey.test(key)) { + hit = true; + } + } + if (!hit) { if (is.symbol(value) || is.regExp(value)) return value.toString(); if (is.primitive(value)) return value; if (is.array(value)) return value; diff --git a/test/fixtures/apps/demo/config/config.default.js b/test/fixtures/apps/demo/config/config.default.js index 5225fec61f..084ff4ad9e 100644 --- a/test/fixtures/apps/demo/config/config.default.js +++ b/test/fixtures/apps/demo/config/config.default.js @@ -1,3 +1,20 @@ exports.keys = 'foo'; exports.proxy = true; -exports.buffer = Buffer.from('test'); \ No newline at end of file +exports.buffer = Buffer.from('test'); + +exports.pass = 'this is pass'; +exports.pwd = 'this is pwd'; +exports.password = 'this is password'; +exports.passwordNew = 'this is passwordNew'; +exports.mysql = { + passd: 'this is passd', + passwd: 'this is passwd', + secret: 'secret 123', + secretNumber: 123, + secretBoolean: true, + masterKey: 'this is masterKey', + accessKey: 'this is accessKey', + accessId: 'this is accessId', + consumerSecret: 'this is consumerSecret', + someSecret: null, +}; diff --git a/test/lib/egg.test.js b/test/lib/egg.test.js index 4e7416043b..6fecf52206 100644 --- a/test/lib/egg.test.js +++ b/test/lib/egg.test.js @@ -40,11 +40,26 @@ describe('test/lib/egg.test.js', () => { it('should ignore some type', () => { const json = require(path.join(baseDir, 'run/application_config.json')); + assert(json.config.mysql.accessId === 'this is accessId'); + assert(json.config.name === 'demo'); assert(json.config.keys === ''); assert(json.config.buffer === ''); assert(json.config.siteFile['/favicon.ico'] === ''); + assert(json.config.pass === ''); + assert(json.config.pwd === ''); + assert(json.config.password === ''); + assert(json.config.passwordNew === 'this is passwordNew'); + assert(json.config.mysql.passd === ''); + assert(json.config.mysql.passwd === ''); + assert(json.config.mysql.secret === ''); + assert(json.config.mysql.secretNumber === ''); + assert(json.config.mysql.masterKey === ''); + assert(json.config.mysql.accessKey === ''); + assert(json.config.mysql.consumerSecret === ''); + assert(json.config.mysql.someSecret === null); + // don't change config assert(app.config.keys === 'foo'); });