Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated crypto.createDecipher() and crypto.createCipher() Node.js methods #76

Merged
merged 9 commits into from Jun 20, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions index.js
Expand Up @@ -229,8 +229,16 @@ class Conf {

if (this.encryptionKey) {
try {
const decipher = crypto.createDecipher(encryptionAlgorithm, this.encryptionKey);
data = Buffer.concat([decipher.update(data), decipher.final()]);
// Check if an IV has been used to encrypt the data
if (data.slice(16, 17).toString() === ':') {
const iv = data.slice(0, 16);
const pass = crypto.pbkdf2Sync(this.encryptionKey, iv.toString(), 10000, 32, 'sha512');
popod marked this conversation as resolved.
Show resolved Hide resolved
const decipher = crypto.createDecipheriv(encryptionAlgorithm, pass, iv);
data = Buffer.concat([decipher.update(data.slice(17)), decipher.final()]);
} else {
const decipher = crypto.createDecipher(encryptionAlgorithm, this.encryptionKey);
data = Buffer.concat([decipher.update(data), decipher.final()]);
}
} catch (_) {}
}

Expand Down Expand Up @@ -260,8 +268,10 @@ class Conf {
let data = this.serialize(value);

if (this.encryptionKey) {
const cipher = crypto.createCipher(encryptionAlgorithm, this.encryptionKey);
data = Buffer.concat([cipher.update(Buffer.from(data)), cipher.final()]);
const iv = crypto.randomBytes(16);
const pass = crypto.pbkdf2Sync(this.encryptionKey, iv.toString(), 10000, 32, 'sha512');
const cipher = crypto.createCipheriv(encryptionAlgorithm, pass, iv);
data = Buffer.concat([iv, Buffer.from(':'), cipher.update(Buffer.from(data)), cipher.final()]);
}

writeFileAtomic.sync(this.path, data);
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -129,9 +129,9 @@ The only use-case I can think of is having the config located in the app directo
Type: `string` `Buffer` `TypedArray` `DataView`<br>
Default: `undefined`

Note that this is **not intended for security purposes**, since the encryption key would be easily found inside a plain-text Node.js app.
This can be used to secure sensitive data if the encryption key is stored in a secure manner (not plain-text) in the Node.js app side.

Its main use is for obscurity. If a user looks through the config directory and finds the config file, since it's just a JSON file, they may be tempted to modify it. By providing an encryption key, the file will be obfuscated, which should hopefully deter any users from doing so.
In addition to security, this could be used for obscurity. If a user looks through the config directory and finds the config file, since it's just a JSON file, they may be tempted to modify it. By providing an encryption key, the file will be obfuscated, which should hopefully deter any users from doing so.

It also has the added bonus of ensuring the config file's integrity. If the file is changed in any way, the decryption will not work, in which case the store will just reset back to its default state.

Expand Down
6 changes: 6 additions & 0 deletions test.js
Expand Up @@ -360,6 +360,12 @@ test('encryption - corrupt file', t => {
t.is(after.get('foo'), undefined);
});

test('decription migration to IV', t => {
popod marked this conversation as resolved.
Show resolved Hide resolved
// The encrypted_config.json contain '{'unicorn', '🦄'}' encrypted with conf@4.1.0 and password 'abcd1234'
popod marked this conversation as resolved.
Show resolved Hide resolved
const config = new Conf({cwd: './test', encryptionKey: 'abcd1234', configName: 'encrypted_config'});
t.deepEqual(config.store, {unicorn: '🦄'});
});

test('onDidChange()', t => {
const {config} = t.context;

Expand Down
1 change: 1 addition & 0 deletions test/encrypted_config.json
@@ -0,0 +1 @@
��/���㵪�M�z^��^m����9!�W?�