fix(NetworkConfigManager, server): enhance IPSec password encryption and decryption error handling; add validation for encrypted password format and improve logging for better debugging
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 1m21s
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 1m21s
This commit is contained in:
@@ -53,30 +53,61 @@ function encrypt(text) {
|
||||
* @returns {string} - Расшифрованный текст
|
||||
*/
|
||||
function decrypt(encryptedText) {
|
||||
if (!encryptedText) return '';
|
||||
if (!encryptedText) {
|
||||
throw new Error('Encrypted text is empty');
|
||||
}
|
||||
|
||||
if (typeof encryptedText !== 'string') {
|
||||
throw new Error('Encrypted text must be a string');
|
||||
}
|
||||
|
||||
try {
|
||||
const parts = encryptedText.split(':');
|
||||
if (parts.length !== 3) {
|
||||
throw new Error('Invalid encrypted format');
|
||||
throw new Error(`Invalid encrypted format: expected 3 parts separated by ':', got ${parts.length}`);
|
||||
}
|
||||
|
||||
const [ivBase64, authTagBase64, encryptedBase64] = parts;
|
||||
const iv = Buffer.from(ivBase64, 'base64');
|
||||
const authTag = Buffer.from(authTagBase64, 'base64');
|
||||
const encrypted = encryptedBase64;
|
||||
|
||||
if (!ivBase64 || !authTagBase64 || !encryptedBase64) {
|
||||
throw new Error('Invalid encrypted format: one or more parts are empty');
|
||||
}
|
||||
|
||||
let iv, authTag;
|
||||
try {
|
||||
iv = Buffer.from(ivBase64, 'base64');
|
||||
authTag = Buffer.from(authTagBase64, 'base64');
|
||||
} catch (bufferError) {
|
||||
throw new Error(`Failed to decode base64: ${bufferError.message}`);
|
||||
}
|
||||
|
||||
if (iv.length !== 16) {
|
||||
throw new Error(`Invalid IV length: expected 16 bytes, got ${iv.length}`);
|
||||
}
|
||||
|
||||
if (authTag.length !== 16) {
|
||||
throw new Error(`Invalid auth tag length: expected 16 bytes, got ${authTag.length}`);
|
||||
}
|
||||
|
||||
const key = getEncryptionKey();
|
||||
const decipher = crypto.createDecipheriv(ALGORITHM, key, iv);
|
||||
decipher.setAuthTag(authTag);
|
||||
|
||||
let decrypted = decipher.update(encrypted, 'base64', 'utf8');
|
||||
let decrypted = decipher.update(encryptedBase64, 'base64', 'utf8');
|
||||
decrypted += decipher.final('utf8');
|
||||
|
||||
return decrypted;
|
||||
} catch (error) {
|
||||
console.error('Decryption error:', error);
|
||||
throw new Error('Failed to decrypt data');
|
||||
if (error.message.includes('Unsupported state') || error.message.includes('bad decrypt')) {
|
||||
throw new Error(`Decryption failed: possibly wrong encryption key or corrupted data. Original error: ${error.message}`);
|
||||
}
|
||||
console.error('Decryption error details:', {
|
||||
error: error.message,
|
||||
stack: error.stack,
|
||||
encryptedTextLength: encryptedText.length,
|
||||
encryptedTextPreview: encryptedText.substring(0, 100)
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user