Skip to content
Closed
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
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ChangeLog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/).

## [Unreleased]
...

## [1.8.4.2] - 2017-03-29
### Added
- Add feature to return only non-expired entries (#304 @schrauger)

### Fixed
- Fixed exception System.ArgumentOutOfRangeException: Index was out of range (#273 @VladimirSerykh)

## [1.8.4.1] - 2016-03-22
### Added
- Make the limitation that only advanced string fields starting with "KPH: " are returned optional (#232 @berrnd)
- Ability to change listener host (#217 @frankhommers)

## [1.8.4.0 and earlier releases]
See: https://github.com/pfn/keepasshttp/commits/master
Binary file modified KeePassHttp.plgx
Binary file not shown.
251 changes: 188 additions & 63 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 @@ -86,15 +87,10 @@ private void GetAllLoginsHandler(Request r, Response resp, Aes aes)
if (!VerifyRequest(r, aes))
return;

var list = new PwObjectList<PwEntry>();

var root = host.Database.RootGroup;

var parms = MakeSearchParameters();

parms.SearchString = @"^[A-Za-z0-9:/-]+\.[A-Za-z0-9:/-]+$"; // match anything looking like a domain or url
var list = root.GetEntries(true);

root.SearchEntries(parms, list);
foreach (var entry in list)
{
var name = entry.Strings.ReadSafe(PwDefs.TitleField);
Expand All @@ -120,11 +116,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 +153,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 +227,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 @@ -310,6 +335,7 @@ private void GetLoginsHandler(Request r, Response resp, Aes aes)
{
f.Icon = win.Icon;
f.Plugin = this;
f.StartPosition = win.Visible ? FormStartPosition.CenterParent : FormStartPosition.CenterScreen;
f.Entries = (from e in items where filter(e.entry) select e.entry).ToList();
//f.Entries = needPrompting.ToList();
f.Host = submithost != null ? submithost : host;
Expand Down Expand Up @@ -355,8 +381,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 Expand Up @@ -390,43 +438,7 @@ orderby e.entry.UsageCount
itemsList = items2.ToList();
}

foreach (var entryDatabase in itemsList)
{
var e = PrepareElementForResponseEntries(configOpt, entryDatabase);
resp.Entries.Add(e);
}

if (itemsList.Count > 0)
{
var names = (from e in resp.Entries select e.Name).Distinct<string>();
var n = String.Join("\n ", names.ToArray<string>());

if (configOpt.ReceiveCredentialNotification)
ShowNotification(String.Format("{0}: {1} is receiving credentials for:\n {2}", r.Id, host, n));
}

resp.Success = true;
resp.Id = r.Id;
SetResponseVerifier(resp, aes);

foreach (var entry in resp.Entries)
{
entry.Name = CryptoTransform(entry.Name, false, true, aes, CMode.ENCRYPT);
entry.Login = CryptoTransform(entry.Login, false, true, aes, CMode.ENCRYPT);
entry.Uuid = CryptoTransform(entry.Uuid, false, true, aes, CMode.ENCRYPT);
entry.Password = CryptoTransform(entry.Password, false, true, aes, CMode.ENCRYPT);

if (entry.StringFields != null)
{
foreach (var sf in entry.StringFields)
{
sf.Key = CryptoTransform(sf.Key, false, true, aes, CMode.ENCRYPT);
sf.Value = CryptoTransform(sf.Value, false, true, aes, CMode.ENCRYPT);
}
}
}

resp.Count = resp.Entries.Count;
CompleteGetLoginsResult(itemsList,configOpt,resp,r.Id,host,aes);
}
else
{
Expand Down Expand Up @@ -476,6 +488,119 @@ private int LevenshteinDistance(string source, string target)
return distance[currentRow, m];
}

private void CompleteGetLoginsResult(List<PwEntryDatabase> itemsList, ConfigOpt configOpt, Response resp, String rId, String host, Aes aes)
{
foreach (var entryDatabase in itemsList)
{
var e = PrepareElementForResponseEntries(configOpt, entryDatabase);
resp.Entries.Add(e);
}

if (itemsList.Count > 0)
{
var names = (from e in resp.Entries select e.Name).Distinct<string>();
var n = String.Join("\n ", names.ToArray<string>());

if (configOpt.ReceiveCredentialNotification)
{
String notificationMessage;
if (host == null)
{
notificationMessage = rId;
}
else
{
notificationMessage = String.Format("{0}: {1}", rId, host);
}
notificationMessage = String.Format("{0} is receiving credentials for:\n {1}", notificationMessage, n);
ShowNotification(notificationMessage);
}
}

resp.Success = true;
resp.Id = rId;
SetResponseVerifier(resp, aes);

foreach (var entry in resp.Entries)
{
entry.Name = CryptoTransform(entry.Name, false, true, aes, CMode.ENCRYPT);
entry.Login = CryptoTransform(entry.Login, false, true, aes, CMode.ENCRYPT);
entry.Uuid = CryptoTransform(entry.Uuid, false, true, aes, CMode.ENCRYPT);
entry.Password = CryptoTransform(entry.Password, false, true, aes, CMode.ENCRYPT);

if (entry.StringFields != null)
{
foreach (var sf in entry.StringFields)
{
sf.Key = CryptoTransform(sf.Key, false, true, aes, CMode.ENCRYPT);
sf.Value = CryptoTransform(sf.Value, false, true, aes, CMode.ENCRYPT);
}
}
}

resp.Count = resp.Entries.Count;
}

private void GetLoginsByNamesHandler(Request r, Response resp, Aes aes)
{
if (!VerifyRequest(r, aes))
return;

if (r.Names == null)
{
return;
}
List<String> decryptedNames = new List<String>();
foreach (String name in r.Names) {
if (name != null) {
decryptedNames.Add(CryptoTransform(name, true, false, aes, CMode.DECRYPT));
}
}

List<PwDatabase> listDatabases = new List<PwDatabase>();

var configOpt = new ConfigOpt(this.host.CustomConfig);
if (configOpt.SearchInAllOpenedDatabases)
{
foreach (PwDocument doc in host.MainWindow.DocumentManager.Documents)
{
if (doc.Database.IsOpen)
{
listDatabases.Add(doc.Database);
}
}
}
else
{
listDatabases.Add(host.Database);
}

var listEntries = new List<PwEntryDatabase>();
foreach (PwDatabase db in listDatabases)
{
foreach (var le in db.RootGroup.GetEntries(true)) {
var title = le.Strings.ReadSafe(PwDefs.TitleField);
bool titleMatched = false;
if (title != null) {
foreach (String name in decryptedNames)
{
if (name.Equals(title))
{
titleMatched = true;
break;
}
}
}
if (titleMatched)
{
listEntries.Add(new PwEntryDatabase(le, db));
}
}
}

CompleteGetLoginsResult(listEntries, configOpt, resp, r.Id, null, aes);
}

private ResponseEntry PrepareElementForResponseEntries(ConfigOpt configOpt, PwEntryDatabase entryDatabase)
{
SprContext ctx = new SprContext(entryDatabase.entry, entryDatabase.database, SprCompileFlags.All, false, false);
Expand Down
1 change: 1 addition & 0 deletions KeePassHttp/KeePassHttp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public override bool Initialize(IPluginHost host)
handlers.Add(Request.TEST_ASSOCIATE, TestAssociateHandler);
handlers.Add(Request.ASSOCIATE, AssociateHandler);
handlers.Add(Request.GET_LOGINS, GetLoginsHandler);
handlers.Add(Request.GET_LOGINS_BY_NAMES, GetLoginsByNamesHandler);
handlers.Add(Request.GET_LOGINS_COUNT, GetLoginsCountHandler);
handlers.Add(Request.GET_ALL_LOGINS, GetAllLoginsHandler);
handlers.Add(Request.SET_LOGIN, SetLoginHandler);
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")]
Loading