Skip to content

Commit f08dfec

Browse files
thomtrpThomas Trompette
andauthored
Fix encryption logic (twentyhq#4672)
Co-authored-by: Thomas Trompette <[email protected]>
1 parent d4eb75a commit f08dfec

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed
Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { createCipheriv, createDecipheriv, createHash } from 'crypto';
1+
import {
2+
createCipheriv,
3+
createDecipheriv,
4+
createHash,
5+
randomBytes,
6+
} from 'crypto';
27

38
import * as bcrypt from 'bcrypt';
49

@@ -16,41 +21,34 @@ export const compareHash = async (password: string, passwordHash: string) => {
1621
return bcrypt.compare(password, passwordHash);
1722
};
1823

19-
export const encryptText = (
20-
textToEncrypt: string,
21-
key: string,
22-
iv: string,
23-
): string => {
24+
export const encryptText = (textToEncrypt: string, key: string): string => {
2425
const keyHash = createHash('sha512')
2526
.update(key)
2627
.digest('hex')
2728
.substring(0, 32);
2829

29-
const ivHash = createHash('sha512').update(iv).digest('hex').substring(0, 16);
30+
const iv = randomBytes(16);
3031

31-
const cipher = createCipheriv('aes-256-ctr', keyHash, ivHash);
32+
const cipher = createCipheriv('aes-256-ctr', keyHash, iv);
3233

33-
return Buffer.concat([cipher.update(textToEncrypt), cipher.final()]).toString(
34-
'base64',
35-
);
34+
return Buffer.concat([
35+
iv,
36+
cipher.update(textToEncrypt),
37+
cipher.final(),
38+
]).toString('base64');
3639
};
3740

38-
export const decryptText = (
39-
textToDecrypt: string,
40-
key: string,
41-
iv: string,
42-
): string => {
41+
export const decryptText = (textToDecrypt: string, key: string): string => {
42+
const textBuffer = Buffer.from(textToDecrypt, 'base64');
43+
const iv = textBuffer.subarray(0, 16);
44+
const text = textBuffer.subarray(16);
45+
4346
const keyHash = createHash('sha512')
4447
.update(key)
4548
.digest('hex')
4649
.substring(0, 32);
4750

48-
const ivHash = createHash('sha512').update(iv).digest('hex').substring(0, 16);
51+
const decipher = createDecipheriv('aes-256-ctr', keyHash, iv);
4952

50-
const decipher = createDecipheriv('aes-256-ctr', keyHash, ivHash);
51-
52-
return Buffer.concat([
53-
decipher.update(Buffer.from(textToDecrypt, 'base64')),
54-
decipher.final(),
55-
]).toString();
53+
return Buffer.concat([decipher.update(text), decipher.final()]).toString();
5654
};

packages/twenty-server/src/engine/metadata-modules/remote-server/remote-server.service.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ export class RemoteServerService<T extends RemoteServerType> {
5353
const encryptedPassword = await encryptText(
5454
remoteServerInput.userMappingOptions.password,
5555
key,
56-
// TODO: check if we should use a separated IV
57-
key,
5856
);
5957

6058
remoteServerToCreate = {

packages/twenty-server/src/engine/metadata-modules/remote-server/remote-table/utils/remote-table-postgres.util.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ export const buildPostgresUrl = (
1717
const foreignDataWrapperOptions = remoteServer.foreignDataWrapperOptions;
1818
const userMappingOptions = remoteServer.userMappingOptions;
1919

20-
const password = decryptText(
21-
userMappingOptions.password,
22-
secretKey,
23-
secretKey,
24-
);
20+
const password = decryptText(userMappingOptions.password, secretKey);
2521

2622
const url = `postgres://${userMappingOptions.username}:${password}@${foreignDataWrapperOptions.host}:${foreignDataWrapperOptions.port}/${foreignDataWrapperOptions.dbname}`;
2723

0 commit comments

Comments
 (0)