updated fill script to support multiple forms on the same page and no form at all (only fields)

This commit is contained in:
Kyle Spearrin 2016-10-04 00:26:05 -04:00
parent 23274ef914
commit b60be97b1f

View file

@ -16,109 +16,70 @@ namespace Bit.iOS.Extension.Models
DocumentUUID = pageDetails.DocumentUUID; DocumentUUID = pageDetails.DocumentUUID;
List<PageDetails.Field> usernames = new List<PageDetails.Field>();
List<PageDetails.Field> passwords = new List<PageDetails.Field>();
var passwordFields = pageDetails.Fields.Where(f => f.Type == "password").ToArray(); var passwordFields = pageDetails.Fields.Where(f => f.Type == "password").ToArray();
var passwordForms = pageDetails.Forms.Where(form => passwordFields.Any(f => f.Form == form.Key)).ToArray(); foreach(var form in pageDetails.Forms)
PageDetails.Form loginForm = null;
PageDetails.Field username = null, password = null;
if(passwordForms.Any())
{ {
if(passwordForms.Count() > 1) var passwordFieldsForForm = passwordFields.Where(f => f.Form == form.Key).ToArray();
{ passwords.AddRange(passwordFieldsForForm);
// More than one form with a password field is on the page.
// This usually occurs when a website has a login and signup form on the same page.
// Let's try to guess which one is the login form.
// First let's try to guess the correct login form by examining the form attribute strings if(string.IsNullOrWhiteSpace(fillUsername))
// for common login form attribute.
foreach(var form in passwordForms)
{ {
var formDescriptor = string.Format("{0}~{1}~{2}", continue;
form.Value?.HtmlName, form.Value?.HtmlId, form.Value?.HtmlAction)
?.ToLowerInvariant()?.Replace('_', '-');
if(formDescriptor.Contains("login") || formDescriptor.Contains("log-in")
|| formDescriptor.Contains("signin") || formDescriptor.Contains("sign-in")
|| formDescriptor.Contains("logon") || formDescriptor.Contains("log-on"))
{
loginForm = form.Value;
break;
}
} }
if(loginForm == null) foreach(var pf in passwordFieldsForForm)
{ {
// Next we can try to find the login form that only has one password field. Typically var username = pageDetails.Fields.LastOrDefault(f => f.Form == pf.Form
// a registration form may have two password fields for password confirmation. && f.ElementNumber < pf.ElementNumber && (f.Type == "text" || f.Type == "email" || f.Type == "tel"));
var fieldGroups = passwordFields.GroupBy(f => f.Form); if(username != null)
var singleFields = fieldGroups.FirstOrDefault(f => f.Count() == 1);
if(singleFields.Any())
{ {
var singlePasswordForms = passwordForms.Where(f => f.Key == singleFields.Key); usernames.Add(username);
if(singlePasswordForms.Any())
{
loginForm = singlePasswordForms.First().Value;
}
} }
} }
} }
if(loginForm == null) if(passwordFields.Any() && !passwords.Any())
{ {
loginForm = passwordForms.FirstOrDefault().Value; // The page does not have any forms with password fields. Use the first password field on the page and the
}
password = pageDetails.Fields.FirstOrDefault(f =>
f.Form == loginForm.OpId
&& f.Type == "password");
username = pageDetails.Fields.LastOrDefault(f =>
f.Form == loginForm.OpId
&& (f.Type == "text" || f.Type == "email")
&& f.ElementNumber < password.ElementNumber);
if(loginForm.HtmlAction != null)
{
AutoSubmit = new Submit { FocusOpId = password.OpId };
}
}
else if(passwordFields.Count() == 1)
{
// The page does not have any forms with password fields. Use the one password field on the page and the
// input field just before it as the username. // input field just before it as the username.
password = passwordFields.First(); var pf = passwordFields.First();
if(password.ElementNumber > 0) passwords.Add(pf);
if(!string.IsNullOrWhiteSpace(fillUsername) && pf.ElementNumber > 0)
{ {
username = pageDetails.Fields.LastOrDefault(f => var username = pageDetails.Fields.LastOrDefault(f => f.ElementNumber < pf.ElementNumber
(f.Type == "text" || f.Type == "email") && (f.Type == "text" || f.Type == "email" || f.Type == "tel"));
&& f.ElementNumber < password.ElementNumber);
if(username == null)
{
username = pageDetails.Fields[password.ElementNumber - 1];
}
}
}
Script = new List<List<string>>();
if(username != null) if(username != null)
{
usernames.Add(username);
}
}
}
foreach(var username in usernames)
{ {
Script.Add(new List<string> { "click_on_opid", username.OpId }); Script.Add(new List<string> { "click_on_opid", username.OpId });
Script.Add(new List<string> { "fill_by_opid", username.OpId, fillUsername }); Script.Add(new List<string> { "fill_by_opid", username.OpId, fillUsername });
} }
if(password != null) foreach(var password in passwords)
{ {
Script.Add(new List<string> { "click_on_opid", password.OpId }); Script.Add(new List<string> { "click_on_opid", password.OpId });
Script.Add(new List<string> { "fill_by_opid", password.OpId, fillPassword }); Script.Add(new List<string> { "fill_by_opid", password.OpId, fillPassword });
} }
if(passwords.Any())
{
AutoSubmit = new Submit { FocusOpId = passwords.First().OpId };
}
} }
[JsonProperty(PropertyName = "script")] [JsonProperty(PropertyName = "script")]
public List<List<string>> Script { get; set; } public List<List<string>> Script { get; set; } = new List<List<string>>();
[JsonProperty(PropertyName = "autosubmit")] [JsonProperty(PropertyName = "autosubmit")]
public Submit AutoSubmit { get; set; } public Submit AutoSubmit { get; set; }
[JsonProperty(PropertyName = "documentUUID")] [JsonProperty(PropertyName = "documentUUID")]