From bf1a235dd21c5b8a539e5dc18ddd50b59d97d743 Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Tue, 26 Aug 2025 17:09:29 +0200 Subject: [PATCH] Refactor (#1146) --- .../Importers/BaseImporter.cs | 5 +++-- .../Importers/KeePassImporter.cs | 15 ++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/apps/server/Utilities/AliasVault.ImportExport/Importers/BaseImporter.cs b/apps/server/Utilities/AliasVault.ImportExport/Importers/BaseImporter.cs index a9d2a6fd5..1e7bb8d65 100644 --- a/apps/server/Utilities/AliasVault.ImportExport/Importers/BaseImporter.cs +++ b/apps/server/Utilities/AliasVault.ImportExport/Importers/BaseImporter.cs @@ -32,7 +32,6 @@ public static class BaseImporter { // Log bad data but don't throw, allowing the parser to continue // This helps with malformed CSV entries - return; }, MissingFieldFound = null, // Ignore missing fields HeaderValidated = null, // Don't validate header names @@ -100,7 +99,9 @@ public static class BaseImporter /// Optional custom decoder function for importer-specific decoding. private static void DecodeFields(T record, Func? customDecoder = null) { - if (record == null) return; + if (record.Equals(default(T))) { + return; + } var type = typeof(T); var properties = type.GetProperties(); diff --git a/apps/server/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.cs b/apps/server/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.cs index ed4436be1..13aa4b072 100644 --- a/apps/server/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.cs +++ b/apps/server/Utilities/AliasVault.ImportExport/Importers/KeePassImporter.cs @@ -21,7 +21,7 @@ public static class KeePassImporter { /// /// Decodes KeePass 1.x specific field encoding. - /// KeePass 1.x rules: Quotes (") in strings are encoded as \" (two characters). + /// KeePass 1.x rules: Quotes (") in strings are encoded as \" (two characters). /// Backslashes (\) are encoded as \\ (two characters). /// /// The field value to decode. @@ -37,7 +37,7 @@ public static class KeePassImporter decoded = decoded.Replace("\"\"", "\""); // Handle KeePass 1.x specific encoding rules - // Backslashes (\) are encoded as \\ -> \ + // Backslashes (\) are encoded as \\ -> \ decoded = decoded.Replace("\\\\", "\\"); // Quotes (") in strings are encoded as \" -> " @@ -46,23 +46,20 @@ public static class KeePassImporter // Special handling for the case where the CSV parser has already partially processed // the escaped quotes, leaving single backslashes that should be quotes // This handles the case where \with should become "with - if (decoded.Contains("\\")) + if (decoded.Contains('\\')) { // Look for standalone backslashes that should be quotes // This is a fallback for malformed or partially-parsed KeePass data - decoded = Regex.Replace(decoded, @"\\(?![\\\""])", "\""); + decoded = Regex.Replace(decoded, @"\\(?![\\\""])", "\"", RegexOptions.None, TimeSpan.FromMilliseconds(100)); } // Handle edge case where CSV parser incorrectly includes trailing quotes // This happens when the CSV parser gets confused by escaped quotes inside quoted fields - if (decoded.EndsWith("\"")) + if (decoded.EndsWith('"') && decoded.Length > 1 && !decoded.EndsWith("\"\"")) { // Check if this is likely a CSV parsing error (trailing quote that shouldn't be there) // Look for patterns like: some content" at the end - if (decoded.Length > 1 && !decoded.EndsWith("\"\"")) - { - decoded = decoded.Substring(0, decoded.Length - 1); - } + decoded = decoded.Substring(0, decoded.Length - 1); } return decoded;