Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable use of regexp for matching sites #340

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified KeePassHttp.plgx
Binary file not shown.
92 changes: 72 additions & 20 deletions KeePassHttp/Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System;
using System.Threading;
using System.Text.RegularExpressions;

using KeePass.Plugins;
using KeePassLib.Collections;
Expand Down Expand Up @@ -120,11 +121,15 @@ private IEnumerable<PwEntryDatabase> FindMatchingEntries(Request r, Aes aes)
string realm = null;
var listResult = new List<PwEntryDatabase>();
var url = CryptoTransform(r.Url, true, false, aes, CMode.DECRYPT);
string formHost, searchHost;
string formHost, searchHost, submitUrl;
formHost = searchHost = GetHost(url);
string hostScheme = GetScheme(url);
if (r.SubmitUrl != null) {
submitHost = GetHost(CryptoTransform(r.SubmitUrl, true, false, aes, CMode.DECRYPT));
submitUrl = CryptoTransform(r.SubmitUrl, true, false, aes, CMode.DECRYPT);
submitHost = GetHost(submitUrl);
} else
{
submitUrl = url;
}
if (r.Realm != null)
realm = CryptoTransform(r.Realm, true, false, aes, CMode.DECRYPT);
Expand Down Expand Up @@ -153,32 +158,57 @@ private IEnumerable<PwEntryDatabase> FindMatchingEntries(Request r, Aes aes)
int listCount = 0;
foreach (PwDatabase db in listDatabases)
{
searchHost = origSearchHost;
//get all possible entries for given host-name
while (listResult.Count == listCount && (origSearchHost == searchHost || searchHost.IndexOf(".") != -1))
parms.SearchString = ".*";
var listEntries = new PwObjectList<PwEntry>();
db.RootGroup.SearchEntries(parms, listEntries);
foreach (var le in listEntries)
{
parms.SearchString = String.Format("^{0}$|/{0}/?", searchHost);
var listEntries = new PwObjectList<PwEntry>();
db.RootGroup.SearchEntries(parms, listEntries);
foreach (var le in listEntries)
{
listResult.Add(new PwEntryDatabase(le, db));
}
searchHost = searchHost.Substring(searchHost.IndexOf(".") + 1);

//searchHost contains no dot --> prevent possible infinite loop
if (searchHost == origSearchHost)
break;
listResult.Add(new PwEntryDatabase(le, db));
}
listCount = listResult.Count;
}


searchHost = origSearchHost;
List<string> hostNameRegExps = new List<string>();

do
{
hostNameRegExps.Add(String.Format("^{0}$|/{0}/?", searchHost));
searchHost = searchHost.Substring(searchHost.IndexOf(".") + 1);
} while (searchHost.IndexOf(".") != -1);

Func<PwEntry, bool> filter = delegate(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)))
Expand All @@ -202,7 +232,7 @@ private IEnumerable<PwEntryDatabase> FindMatchingEntries(Request r, Aes aes)
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));
};

Func<PwEntry, bool> filterSchemes = delegate(PwEntry e)
Expand Down Expand Up @@ -355,8 +385,30 @@ private void GetLoginsHandler(Request r, Response resp, Aes aes)
entryUrl = entryDatabase.entry.Strings.ReadSafe(PwDefs.TitleField);

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

entryDatabase.entry.UsageCount = (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
{
MatchCollection matches = Regex.Matches(compareToUrl, c.RegExp);
foreach(Match match in matches)
{
ulong matchDistance = (ulong)LevenshteinDistance(compareToUrl, match.Value);
if(matchDistance < lDistance)
{
lDistance = matchDistance;
}
}
}
catch (Exception)
{
//ignore invalid pattern and fall back to the distance to entryUrl
}
}
entryDatabase.entry.UsageCount = lDistance;

}

Expand Down
2 changes: 1 addition & 1 deletion KeePassHttp/KeePassHttp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
2 changes: 1 addition & 1 deletion KeePassHttp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.34.0.0")]
[assembly: AssemblyVersion("2.37.0.0")]
[assembly: AssemblyFileVersion("1.8.4.2")]
1 change: 1 addition & 0 deletions KeePassHttp/Protocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public class KeePassHttpEntryConfig
{
public HashSet<string> Allow = new HashSet<string>();
public HashSet<string> Deny = new HashSet<string>();
public string RegExp = null;
public string Realm = null;
}
}