Skip to content

Commit

Permalink
add option to use legacy host matching
Browse files Browse the repository at this point in the history
with the option on, it doesn't prompt if the host matches the title or
url. fixes #84, #109
  • Loading branch information
smorks committed Mar 15, 2023
1 parent 6cbad44 commit bcf4cee
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 41 deletions.
7 changes: 7 additions & 0 deletions KeePassNatMsg/ConfigOpt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ConfigOpt
const string ConnectionDatabaseHashKey = "KeePassHttp_ConnectionDatabaseHash";
const string SearchUrlsKey = "KeePassHttp_SearchUrls";
const string UseKeePassXcSettingsKey = "KeePassNatMsg_UseKpxcSettings";
private const string UseLegacyHostMatchingKey = "KeePassNatMsg_UseLegacyHostMatching";

public ConfigOpt(AceCustomConfig config)
{
Expand Down Expand Up @@ -132,5 +133,11 @@ public bool UseKeePassXcSettings
_config.SetBool(UseKeePassXcSettingsKey, value);
}
}

public bool UseLegacyHostMatching
{
get { return _config.GetBool(UseLegacyHostMatchingKey, false); }
set { _config.SetBool(UseLegacyHostMatchingKey, value); }
}
}
}
20 changes: 19 additions & 1 deletion KeePassNatMsg/Entry/EntrySearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ internal Response GetLoginsHandler(Request req)

Uri hostUri;
Uri submitUri = null;
var uris = new List<Uri>();

if (!string.IsNullOrEmpty(url))
{
hostUri = new Uri(url);
uris.Add(hostUri);
}
else
{
Expand All @@ -55,6 +57,7 @@ internal Response GetLoginsHandler(Request req)
if (!string.IsNullOrEmpty(submitUrl))
{
submitUri = new Uri(submitUrl);
uris.Add(submitUri);
}

var resp = req.GetResponse();
Expand All @@ -63,14 +66,29 @@ internal Response GetLoginsHandler(Request req)
var items = FindMatchingEntries(url, null);
if (items.ToList().Count > 0)
{
var configOpt = new ConfigOpt(_host.CustomConfig);

var filter = new GFunc<PwEntry, bool>((PwEntry e) =>
{
var c = _ext.GetEntryConfig(e);

if (configOpt.UseLegacyHostMatching)
{
var fields = new[] {PwDefs.TitleField, PwDefs.UrlField};
var entryUrls = e.Strings
.Where(s => fields.Contains(s.Key) && s.Value != null && !s.Value.IsEmpty)
.Select(s => s.Value.ReadString())
.ToList();

var isAllowed = c != null && uris.Select(u => u.Host).Any(u => c.Allow.Contains(u));
var hostMatch = uris.Select(u => u.Host).Any(u => entryUrls.Contains(u));

return (c == null && !hostMatch) || (c != null && !hostMatch && !isAllowed);
}

return c == null || (!c.Allow.Contains(hostUri.Authority)) || (submitUri != null && submitUri.Authority != null && !c.Allow.Contains(submitUri.Authority));
});

var configOpt = new ConfigOpt(_host.CustomConfig);
var needPrompting = items.Where(e => filter(e.entry)).ToList();

if (needPrompting.Count > 0 && !configOpt.AlwaysAllowAccess)
Expand Down
80 changes: 40 additions & 40 deletions KeePassNatMsg/Options/OptionsForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bcf4cee

Please sign in to comment.