Skip to content

Commit

Permalink
feat: ignore any key contains "secret" (#1156)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Jul 5, 2017
1 parent 74c8a54 commit 5dc56fa
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
6 changes: 5 additions & 1 deletion config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]),
},

/**
Expand Down
10 changes: 9 additions & 1 deletion lib/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 18 additions & 1 deletion test/fixtures/apps/demo/config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
exports.keys = 'foo';
exports.proxy = true;
exports.buffer = Buffer.from('test');
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,
};
15 changes: 15 additions & 0 deletions test/lib/egg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '<String len: 3>');
assert(json.config.buffer === '<Buffer len: 4>');
assert(json.config.siteFile['/favicon.ico'] === '<Buffer len: 14191>');

assert(json.config.pass === '<String len: 12>');
assert(json.config.pwd === '<String len: 11>');
assert(json.config.password === '<String len: 16>');
assert(json.config.passwordNew === 'this is passwordNew');
assert(json.config.mysql.passd === '<String len: 13>');
assert(json.config.mysql.passwd === '<String len: 14>');
assert(json.config.mysql.secret === '<String len: 10>');
assert(json.config.mysql.secretNumber === '<Number>');
assert(json.config.mysql.masterKey === '<String len: 17>');
assert(json.config.mysql.accessKey === '<String len: 17>');
assert(json.config.mysql.consumerSecret === '<String len: 22>');
assert(json.config.mysql.someSecret === null);

// don't change config
assert(app.config.keys === 'foo');
});
Expand Down

0 comments on commit 5dc56fa

Please sign in to comment.