diff --git a/engine/engine/utils/parseDbResults.js b/engine/engine/utils/parseDbResults.js index 1b31714..e30482b 100644 --- a/engine/engine/utils/parseDbResults.js +++ b/engine/engine/utils/parseDbResults.js @@ -24,6 +24,9 @@ module.exports = async function parseDbResults({ unparsedResults, tableSchema }) */ let parsedResults = []; + const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || ""; + const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || ""; + try { /** * Declare variables @@ -52,7 +55,7 @@ module.exports = async function parseDbResults({ unparsedResults, tableSchema }) if (resultFieldSchema?.encrypted) { if (value?.match(/./)) { - result[resultFieldName] = decrypt(value); + result[resultFieldName] = decrypt({ encryptedString: value, encryptionKey, encryptionSalt }); } } } diff --git a/engine/query/utils/addDbEntry.js b/engine/query/utils/addDbEntry.js index 5b166ea..63fee79 100644 --- a/engine/query/utils/addDbEntry.js +++ b/engine/query/utils/addDbEntry.js @@ -26,8 +26,8 @@ const updateDbEntry = require("./updateDbEntry"); * @param {string} [params.duplicateColumnName] - Duplicate column name * @param {string} [params.duplicateColumnValue] - Duplicate column value * @param {boolean} [params.update] - Update this row if it exists - * @param {string} [params.encryptionKey] - Update this row if it exists - * @param {string} [params.encryptionSalt] - Update this row if it exists + * @param {string} params.encryptionKey - Update this row if it exists + * @param {string} params.encryptionSalt - Update this row if it exists * * @returns {Promise} */ @@ -63,6 +63,8 @@ async function addDbEntry({ dbContext, paradigm, dbFullName, tableName, data, ta tableSchema, identifierColumnName: duplicateColumnName, identifierValue: duplicateColumnValue || "", + encryptionKey, + encryptionSalt, }); } } diff --git a/engine/query/utils/runQuery.js b/engine/query/utils/runQuery.js index 92506c8..52538de 100644 --- a/engine/query/utils/runQuery.js +++ b/engine/query/utils/runQuery.js @@ -33,6 +33,9 @@ async function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArra * * @description Declare "results" variable */ + const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || ""; + const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || ""; + let result, error, tableSchema; if (dbSchema) { @@ -78,6 +81,8 @@ async function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArra duplicateColumnName, duplicateColumnValue, tableSchema, + encryptionKey, + encryptionSalt, }); if (!result?.insertId) { @@ -96,6 +101,8 @@ async function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArra identifierColumnName, identifierValue, tableSchema, + encryptionKey, + encryptionSalt, }); break; diff --git a/engine/query/utils/updateDbEntry.js b/engine/query/utils/updateDbEntry.js index b9687d6..1f1f977 100644 --- a/engine/query/utils/updateDbEntry.js +++ b/engine/query/utils/updateDbEntry.js @@ -24,10 +24,12 @@ const dbHandler = require("../../engine/utils/dbHandler"); * @param {import("../../../types/database-schema.td").DSQL_TableSchemaType} [params.tableSchema] - Table schema * @param {string} params.identifierColumnName - Update row identifier column name * @param {string | number} params.identifierValue - Update row identifier column value + * @param {string} params.encryptionKey - Encryption key + * @param {string} params.encryptionSalt - Encryption salt * * @returns {Promise} */ -async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue }) { +async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, encryptionKey, encryptionSalt }) { /** * Check if data is valid */ @@ -47,9 +49,6 @@ async function updateDbEntry({ dbContext, paradigm, dbFullName, tableName, data, let updateKeyValueArray = []; let updateValues = []; - const encryptionKey = process.env.DSQL_ENCRYPTION_KEY; - const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT; - /** * Declare variables * diff --git a/engine/user/add-user.js b/engine/user/add-user.js index 2fa7098..3e70a02 100644 --- a/engine/user/add-user.js +++ b/engine/user/add-user.js @@ -31,6 +31,10 @@ async function localAddUser({ payload, dbSchema }) { * Initialize Variables */ const dbFullName = process.env.DSQL_DB_NAME || ""; + + const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || ""; + const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || ""; + /** * Hash Password * @@ -105,6 +109,8 @@ async function localAddUser({ payload, dbSchema }) { image: "/images/user_images/user-preset.png", image_thumbnail: "/images/user_images/user-preset-thumbnail.png", }, + encryptionKey, + encryptionSalt, }); if (addUser?.insertId) { diff --git a/functions/decrypt.js b/functions/decrypt.js index d121455..369958a 100644 --- a/functions/decrypt.js +++ b/functions/decrypt.js @@ -5,6 +5,15 @@ const decrypt = ({ encryptedString, encryptionKey, encryptionSalt }) => { const algorithm = "aes-192-cbc"; const password = encryptionKey; + if (!encryptionKey?.match(/.{8,}/)) { + console.log("Decrption key is invalid"); + return data; + } + if (!encryptionSalt?.match(/.{8,}/)) { + console.log("Decrption salt is invalid"); + return data; + } + let key = scryptSync(password, encryptionSalt, 24); let iv = Buffer.alloc(16, 0); const decipher = createDecipheriv(algorithm, key, iv); diff --git a/package.json b/package.json index d802de6..26d0e82 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "1.6.1", + "version": "1.6.2", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": {