Skip to content

Commit fd181eb

Browse files
committed
Fix merging bug's
1 parent cae598c commit fd181eb

2 files changed

Lines changed: 56 additions & 82 deletions

File tree

KeePassHttp/Handlers.cs

Lines changed: 50 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,16 @@ private IEnumerable<PwEntryDatabase> FindMatchingEntries(Request request, Aes ae
120120
string formHost, searchHost, submitUrl;
121121
formHost = searchHost = GetHost(url);
122122
string hostScheme = GetScheme(url);
123-
123+
124124
if (request.SubmitUrl != null)
125-
submitHost = GetHost(CryptoTransform(request.SubmitUrl, true, false, aes, CMode.DECRYPT));
126-
else
125+
{
126+
submitUrl = request.SubmitUrl;
127+
submitHost = GetHost(CryptoTransform(submitUrl, true, false, aes, CMode.DECRYPT));
128+
}
129+
else
130+
{
127131
submitUrl = url;
132+
}
128133

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

175-
bool hideExpired(PwEntry e)
176-
{
177-
var title = e.Strings.ReadSafe(PwDefs.TitleField);
178-
var entryUrl = e.Strings.ReadSafe(PwDefs.UrlField);
179-
var c = GetEntryConfig(e);
180-
if (c != null && c.RegExp != null)
181-
{
182-
try
183-
{
184-
return Regex.IsMatch(submitUrl, c.RegExp);
185-
}
186-
catch (Exception)
187-
{
188-
//ignore invalid pattern
189-
}
190-
}
191-
else
192-
{
193-
bool found = false;
194-
foreach (string hostNameRegExp in hostNameRegExps)
195-
{
196-
if (Regex.IsMatch(e.Strings.ReadSafe("URL"), hostNameRegExp) || Regex.IsMatch(e.Strings.ReadSafe("Title"), hostNameRegExp) || Regex.IsMatch(e.Strings.ReadSafe("Notes"), hostNameRegExp))
197-
{
198-
found = true;
199-
break;
200-
}
201-
}
202-
203-
if(!found)
204-
return false;
205-
}
206-
207-
if (c != null)
208-
{
209-
if (c.Allow.Contains(formHost) && (submitHost == null || c.Allow.Contains(submitHost)))
210-
return true;
211-
if (c.Deny.Contains(formHost) || (submitHost != null && c.Deny.Contains(submitHost)))
212-
return false;
213-
if (realm != null && c.Realm != realm)
214-
return false;
215-
}
216-
217-
if (e.Expires && (e.ExpiryTime <= dtNow))
218-
return false;
219-
220-
if (title.StartsWith("http://") || title.StartsWith("https://") || title.StartsWith("ftp://") || title.StartsWith("sftp://"))
221-
{
222-
if (formHost.EndsWith( GetHost(title)))
223-
return true;
224-
}
225-
return formHost.Contains(title) || (entryUrl != null && entryUrl != "" && formHost.Contains(entryUrl));
226-
};
180+
bool hideExpired(PwEntry e) => e.Expires && e.ExpiryTime <= DateTime.UtcNow;
227181

228182
if (configOpt.MatchSchemes)
229183
return listResult.Where(e => GetFilterShemes(e.Entry, hostScheme));
230184
else if (configOpt.HideExpired)
231185
return listResult.Where(e => hideExpired(e.Entry));
232186
else
233-
return listResult.Where(e => GetFilter(e.Entry, submitHost, realm, formHost));
187+
return listResult.Where(e => GetFilter(e.Entry, submitHost, realm, formHost, submitUrl, hostNameRegExps));
234188
}
235189

236190
private bool GetFilterShemes(PwEntry e, string hostScheme)
@@ -249,45 +203,62 @@ private bool GetFilterShemes(PwEntry e, string hostScheme)
249203
return GetScheme(title) == hostScheme;
250204
}
251205

252-
private bool GetFilter(PwEntry e, string submitHost, string realm, string formHost)
206+
private bool GetFilter(PwEntry e, string submitHost, string realm, string formHost, string submitUrl, List<string> hostNameRegExps)
253207
{
254208
var title = e.Strings.ReadSafe(PwDefs.TitleField);
255209
var entryUrl = e.Strings.ReadSafe(PwDefs.UrlField);
256-
257210
var c = GetEntryConfig(e);
258-
211+
if (c != null && c.RegExp != null)
212+
{
213+
try
214+
{
215+
return Regex.IsMatch(submitUrl, c.RegExp);
216+
}
217+
catch (Exception)
218+
{
219+
//ignore invalid pattern
220+
}
221+
}
222+
else
223+
{
224+
bool found = false;
225+
foreach (string hostNameRegExp in hostNameRegExps)
226+
{
227+
if (Regex.IsMatch(e.Strings.ReadSafe("URL"), hostNameRegExp) || Regex.IsMatch(e.Strings.ReadSafe("Title"), hostNameRegExp) || Regex.IsMatch(e.Strings.ReadSafe("Notes"), hostNameRegExp))
228+
{
229+
found = true;
230+
break;
231+
}
232+
}
233+
if (!found)
234+
{
235+
return false;
236+
}
237+
}
259238
if (c != null)
260239
{
261240
if (c.Allow.Contains(formHost) && (submitHost == null || c.Allow.Contains(submitHost)))
262241
return true;
263-
264242
if (c.Deny.Contains(formHost) || (submitHost != null && c.Deny.Contains(submitHost)))
265243
return false;
266-
267244
if (realm != null && c.Realm != realm)
268245
return false;
269246
}
270247

271-
if (entryUrl != null &&
272-
(entryUrl.StartsWith("http://") ||
273-
entryUrl.StartsWith("https://") ||
274-
title.StartsWith("ftp://") ||
275-
title.StartsWith("sftp://")))
248+
if (entryUrl != null && (entryUrl.StartsWith("http://") || entryUrl.StartsWith("https://") || title.StartsWith("ftp://") || title.StartsWith("sftp://")))
276249
{
277-
if (formHost.EndsWith(GetHost(entryUrl)))
250+
var uHost = GetHost(entryUrl);
251+
if (formHost.EndsWith(uHost))
278252
return true;
279253
}
280254

281-
if (title.StartsWith("http://") ||
282-
title.StartsWith("https://") ||
283-
title.StartsWith("ftp://") ||
284-
title.StartsWith("sftp://"))
255+
if (title.StartsWith("http://") || title.StartsWith("https://") || title.StartsWith("ftp://") || title.StartsWith("sftp://"))
285256
{
286-
if (formHost.EndsWith(GetHost(title)))
257+
var uHost = GetHost(title);
258+
if (formHost.EndsWith(uHost))
287259
return true;
288260
}
289-
290-
return formHost.Contains(title) || (entryUrl != null && formHost.Contains(entryUrl));
261+
return formHost.Contains(title) || (entryUrl != null && entryUrl != "" && formHost.Contains(entryUrl));
291262
}
292263

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

401372
entryUrl = entryUrl.ToLower();
402-
var c = GetEntryConfig(entryDatabase.entry);
373+
var c = GetEntryConfig(entryDatabase.Entry);
403374
ulong lDistance = (ulong)LevenshteinDistance(compareToUrl, entryUrl);
404375

405376
//if the entry contains a matching RegExp get the matching part and calculate the minimal LevenshteinDistance metween the matches
406377
if (c != null && c.RegExp != null)
407378
{
408379
try
409380
{
410-
foreach(Match match in Regex.Matches(compareToUrl, c.RegExp))
381+
foreach (Match match in Regex.Matches(compareToUrl, c.RegExp))
411382
{
412-
ulong matchDistance = (ulong)LevenshteinDistance(compareToUrl, match.Value);
383+
ulong matchDistance = (ulong)LevenshteinDistance(compareToUrl, match.Value);
413384

414-
if(matchDistance < lDistance)
385+
if (matchDistance < lDistance)
415386
lDistance = matchDistance;
416387
}
417388
}
@@ -420,7 +391,7 @@ bool filter(PwEntry e)
420391
//ignore invalid pattern and fall back to the distance to entryUrl
421392
}
422393
}
423-
entryDatabase.entry.UsageCount = lDistance;
394+
entryDatabase.Entry.UsageCount = lDistance;
424395

425396
}
426397

KeePassHttp/Protocol.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,18 @@ public ResponseStringField(string key, string value)
222222

223223
public class KeePassHttpEntryConfig
224224
{
225-
public HashSet<string> Allow;
226-
public HashSet<string> Deny;
227-
public string Realm;
225+
public HashSet<string> Allow { get; internal set; }
226+
public HashSet<string> Deny { get; internal set; }
227+
public string RegExp { get; internal set; }
228+
public string Realm { get; internal set; }
228229

229230
public KeePassHttpEntryConfig()
230231
{
231232
Allow = new HashSet<string>();
232233
Deny = new HashSet<string>();
233234
Realm = null;
235+
RegExp = null;
234236
}
237+
235238
}
236239
}

0 commit comments

Comments
 (0)