Skip to content

Commit

Permalink
Fix merging bug's
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallimathias committed Feb 28, 2018
1 parent cae598c commit fd181eb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 82 deletions.
129 changes: 50 additions & 79 deletions KeePassHttp/Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,16 @@ private IEnumerable<PwEntryDatabase> FindMatchingEntries(Request request, Aes ae
string formHost, searchHost, submitUrl;
formHost = searchHost = GetHost(url);
string hostScheme = GetScheme(url);

if (request.SubmitUrl != null)
submitHost = GetHost(CryptoTransform(request.SubmitUrl, true, false, aes, CMode.DECRYPT));
else
{
submitUrl = request.SubmitUrl;
submitHost = GetHost(CryptoTransform(submitUrl, true, false, aes, CMode.DECRYPT));
}
else
{
submitUrl = url;
}

if (request.Realm != null)
realm = CryptoTransform(request.Realm, true, false, aes, CMode.DECRYPT);
Expand Down Expand Up @@ -172,65 +177,14 @@ private IEnumerable<PwEntryDatabase> FindMatchingEntries(Request request, Aes ae
searchHost = searchHost.Substring(searchHost.IndexOf(".") + 1);
} while (searchHost.IndexOf(".") != -1);

bool hideExpired(PwEntry e)
{
var title = e.Strings.ReadSafe(PwDefs.TitleField);
var entryUrl = e.Strings.ReadSafe(PwDefs.UrlField);
var c = GetEntryConfig(e);
if (c != null && c.RegExp != null)
{
try
{
return Regex.IsMatch(submitUrl, c.RegExp);
}
catch (Exception)
{
//ignore invalid pattern
}
}
else
{
bool found = false;
foreach (string hostNameRegExp in hostNameRegExps)
{
if (Regex.IsMatch(e.Strings.ReadSafe("URL"), hostNameRegExp) || Regex.IsMatch(e.Strings.ReadSafe("Title"), hostNameRegExp) || Regex.IsMatch(e.Strings.ReadSafe("Notes"), hostNameRegExp))
{
found = true;
break;
}
}

if(!found)
return false;
}

if (c != null)
{
if (c.Allow.Contains(formHost) && (submitHost == null || c.Allow.Contains(submitHost)))
return true;
if (c.Deny.Contains(formHost) || (submitHost != null && c.Deny.Contains(submitHost)))
return false;
if (realm != null && c.Realm != realm)
return false;
}

if (e.Expires && (e.ExpiryTime <= dtNow))
return false;

if (title.StartsWith("http://") || title.StartsWith("https://") || title.StartsWith("ftp://") || title.StartsWith("sftp://"))
{
if (formHost.EndsWith( GetHost(title)))
return true;
}
return formHost.Contains(title) || (entryUrl != null && entryUrl != "" && formHost.Contains(entryUrl));
};
bool hideExpired(PwEntry e) => e.Expires && e.ExpiryTime <= DateTime.UtcNow;

if (configOpt.MatchSchemes)
return listResult.Where(e => GetFilterShemes(e.Entry, hostScheme));
else if (configOpt.HideExpired)
return listResult.Where(e => hideExpired(e.Entry));
else
return listResult.Where(e => GetFilter(e.Entry, submitHost, realm, formHost));
return listResult.Where(e => GetFilter(e.Entry, submitHost, realm, formHost, submitUrl, hostNameRegExps));
}

private bool GetFilterShemes(PwEntry e, string hostScheme)
Expand All @@ -249,45 +203,62 @@ private bool GetFilterShemes(PwEntry e, string hostScheme)
return GetScheme(title) == hostScheme;
}

private bool GetFilter(PwEntry e, string submitHost, string realm, string formHost)
private bool GetFilter(PwEntry e, string submitHost, string realm, string formHost, string submitUrl, List<string> hostNameRegExps)
{
var title = e.Strings.ReadSafe(PwDefs.TitleField);
var entryUrl = e.Strings.ReadSafe(PwDefs.UrlField);

var c = GetEntryConfig(e);

if (c != null && c.RegExp != null)
{
try
{
return Regex.IsMatch(submitUrl, c.RegExp);
}
catch (Exception)
{
//ignore invalid pattern
}
}
else
{
bool found = false;
foreach (string hostNameRegExp in hostNameRegExps)
{
if (Regex.IsMatch(e.Strings.ReadSafe("URL"), hostNameRegExp) || Regex.IsMatch(e.Strings.ReadSafe("Title"), hostNameRegExp) || Regex.IsMatch(e.Strings.ReadSafe("Notes"), hostNameRegExp))
{
found = true;
break;
}
}
if (!found)
{
return false;
}
}
if (c != null)
{
if (c.Allow.Contains(formHost) && (submitHost == null || c.Allow.Contains(submitHost)))
return true;

if (c.Deny.Contains(formHost) || (submitHost != null && c.Deny.Contains(submitHost)))
return false;

if (realm != null && c.Realm != realm)
return false;
}

if (entryUrl != null &&
(entryUrl.StartsWith("http://") ||
entryUrl.StartsWith("https://") ||
title.StartsWith("ftp://") ||
title.StartsWith("sftp://")))
if (entryUrl != null && (entryUrl.StartsWith("http://") || entryUrl.StartsWith("https://") || title.StartsWith("ftp://") || title.StartsWith("sftp://")))
{
if (formHost.EndsWith(GetHost(entryUrl)))
var uHost = GetHost(entryUrl);
if (formHost.EndsWith(uHost))
return true;
}

if (title.StartsWith("http://") ||
title.StartsWith("https://") ||
title.StartsWith("ftp://") ||
title.StartsWith("sftp://"))
if (title.StartsWith("http://") || title.StartsWith("https://") || title.StartsWith("ftp://") || title.StartsWith("sftp://"))
{
if (formHost.EndsWith(GetHost(title)))
var uHost = GetHost(title);
if (formHost.EndsWith(uHost))
return true;
}

return formHost.Contains(title) || (entryUrl != null && formHost.Contains(entryUrl));
return formHost.Contains(title) || (entryUrl != null && entryUrl != "" && formHost.Contains(entryUrl));
}

private void GetLoginsCountHandler(Request request, Response response, Aes aes)
Expand Down Expand Up @@ -399,19 +370,19 @@ bool filter(PwEntry e)
entryUrl = entryDatabase.Entry.Strings.ReadSafe(PwDefs.TitleField);

entryUrl = entryUrl.ToLower();
var c = GetEntryConfig(entryDatabase.entry);
var c = GetEntryConfig(entryDatabase.Entry);
ulong lDistance = (ulong)LevenshteinDistance(compareToUrl, entryUrl);

//if the entry contains a matching RegExp get the matching part and calculate the minimal LevenshteinDistance metween the matches
if (c != null && c.RegExp != null)
{
try
{
foreach(Match match in Regex.Matches(compareToUrl, c.RegExp))
foreach (Match match in Regex.Matches(compareToUrl, c.RegExp))
{
ulong matchDistance = (ulong)LevenshteinDistance(compareToUrl, match.Value);
ulong matchDistance = (ulong)LevenshteinDistance(compareToUrl, match.Value);

if(matchDistance < lDistance)
if (matchDistance < lDistance)
lDistance = matchDistance;
}
}
Expand All @@ -420,7 +391,7 @@ bool filter(PwEntry e)
//ignore invalid pattern and fall back to the distance to entryUrl
}
}
entryDatabase.entry.UsageCount = lDistance;
entryDatabase.Entry.UsageCount = lDistance;

}

Expand Down
9 changes: 6 additions & 3 deletions KeePassHttp/Protocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,18 @@ public ResponseStringField(string key, string value)

public class KeePassHttpEntryConfig
{
public HashSet<string> Allow;
public HashSet<string> Deny;
public string Realm;
public HashSet<string> Allow { get; internal set; }
public HashSet<string> Deny { get; internal set; }
public string RegExp { get; internal set; }
public string Realm { get; internal set; }

public KeePassHttpEntryConfig()
{
Allow = new HashSet<string>();
Deny = new HashSet<string>();
Realm = null;
RegExp = null;
}

}
}

1 comment on commit fd181eb

@Gallimathias
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implement PullRequest pfn#340

Please sign in to comment.