This commit is contained in:
Leendert de Borst
2025-08-26 17:09:29 +02:00
committed by Leendert de Borst
parent 2bb7f0a742
commit bf1a235dd2
2 changed files with 9 additions and 11 deletions

View File

@@ -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
/// <param name="customDecoder">Optional custom decoder function for importer-specific decoding.</param>
private static void DecodeFields<T>(T record, Func<string, string>? customDecoder = null)
{
if (record == null) return;
if (record.Equals(default(T))) {
return;
}
var type = typeof(T);
var properties = type.GetProperties();

View File

@@ -21,7 +21,7 @@ public static class KeePassImporter
{
/// <summary>
/// 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).
/// </summary>
/// <param name="value">The field value to decode.</param>
@@ -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;