123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- const crypto = require('crypto');
- require('dotenv').config();
- const SECRET_KEY = process.env.SECRET_KEY;
- const encryptedValues = {
- dbHost: process.env.ENCRYPTED_DB_HOST,
- dbName: process.env.ENCRYPTED_DB_NAME,
- dbTable: process.env.ENCRYPTED_DB_TABLE,
- dbUsername: process.env.ENCRYPTED_DB_USERNAME,
- dbPassword: process.env.ENCRYPTED_DB_PASSWORD
- };
- function decryptValue(encryptedText) {
- try {
- const parts = encryptedText.split(':');
- if (parts.length !== 3) return null;
-
- const iv = Buffer.from(parts[0], 'hex');
- const authTag = Buffer.from(parts[1], 'hex');
- const encryptedData = parts[2];
-
- const decipher = crypto.createDecipheriv('aes-256-gcm', Buffer.from(SECRET_KEY, 'hex'), iv);
- decipher.setAuthTag(authTag);
-
- let decrypted = decipher.update(encryptedData, 'hex', 'utf8');
- decrypted += decipher.final('utf8');
-
- return decrypted;
- } catch (error) {
-
- return null;
- }
- }
- function processEncryptedCredentials(functionBody) {
- if (!functionBody) return functionBody;
-
- let processedBody = functionBody;
-
-
- const encryptionPattern = /['"]([0-9a-f]+:[0-9a-f]+:[0-9a-f]+)['"]/g;
- let match;
-
- while ((match = encryptionPattern.exec(functionBody)) !== null) {
- const potentialEncrypted = match[1];
- const decrypted = decryptValue(potentialEncrypted);
-
- if (decrypted) {
-
- const fullMatch = match[0];
- const replacement = fullMatch[0] + decrypted + fullMatch[fullMatch.length - 1];
- processedBody = processedBody.replace(fullMatch, replacement);
- }
- }
-
-
- Object.entries(encryptedValues).forEach(([key, encryptedValue]) => {
- if (processedBody.includes(encryptedValue)) {
- const decrypted = decryptValue(encryptedValue);
- if (decrypted) {
- processedBody = processedBody.replace(
- new RegExp(`['"]${encryptedValue}['"]`, 'g'),
- `"${decrypted}"`
- );
- }
- }
- });
-
- return processedBody;
- }
- function processEncryptedArguments(args) {
- if (!Array.isArray(args)) return args;
-
- return args.map(arg => {
-
- if (typeof arg === 'string' && arg.includes(':') && arg.length > 40) {
- const decrypted = decryptValue(arg);
- if (decrypted) {
- console.log(`[SECURITY-HELPERS] Successfully decrypted argument: ${arg.substring(0, 20)}...`);
- return decrypted;
- } else {
- console.log(`[SECURITY-HELPERS] Failed to decrypt argument: ${arg.substring(0, 20)}...`);
- }
- }
- return arg;
- });
- }
- function processAllEncryptedData(functionBody, args = []) {
- const processedBody = processEncryptedCredentials(functionBody);
- const processedArgs = processEncryptedArguments(args);
-
- return {
- processedBody,
- processedArgs
- };
- }
- module.exports = {
- decryptValue,
- processEncryptedCredentials,
- processEncryptedArguments,
- processAllEncryptedData,
- encryptedValues
- };
|