diff --git a/src/App/Models/DomainName.cs b/src/App/Models/DomainName.cs index 0e837b6f1..0c21f218d 100644 --- a/src/App/Models/DomainName.cs +++ b/src/App/Models/DomainName.cs @@ -4,12 +4,18 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; +using System.Text.RegularExpressions; namespace Bit.App.Models { // ref: https://github.com/danesparza/domainname-parser public class DomainName { + private const string IpRegex = "^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." + + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." + + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\." + + "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"; + private string _subDomain = string.Empty; private string _domain = string.Empty; private string _tld = string.Empty; @@ -59,6 +65,20 @@ namespace Bit.App.Models return retval; } + public static bool TryParseBaseDomain(string domainString, out string result) + { + if(Regex.IsMatch(domainString, IpRegex)) + { + result = domainString; + return true; + } + + DomainName domain; + var retval = TryParse(domainString, out domain); + result = domain?.BaseDomain; + return retval; + } + private static void ParseDomainName(string domainString, out string TLD, out string SLD, out string SubDomain, out TLDRule MatchingRule) { // Make sure domain is all lowercase diff --git a/src/App/Models/Page/VaultViewLoginPageModel.cs b/src/App/Models/Page/VaultViewLoginPageModel.cs index 6425b610b..848bde56f 100644 --- a/src/App/Models/Page/VaultViewLoginPageModel.cs +++ b/src/App/Models/Page/VaultViewLoginPageModel.cs @@ -115,10 +115,10 @@ namespace Bit.App.Models.Page return Uri; } - DomainName domain; - if(DomainName.TryParse(uri.Host, out domain)) + string domain; + if(DomainName.TryParseBaseDomain(uri.Host, out domain)) { - return domain.BaseDomain; + return domain; } return uri.Host; diff --git a/src/App/Pages/Vault/VaultAutofillListLoginsPage.cs b/src/App/Pages/Vault/VaultAutofillListLoginsPage.cs index 84fe99386..05eaccbc9 100644 --- a/src/App/Pages/Vault/VaultAutofillListLoginsPage.cs +++ b/src/App/Pages/Vault/VaultAutofillListLoginsPage.cs @@ -30,20 +30,15 @@ namespace Bit.App.Pages Uri = uriString; Uri uri; - DomainName domainName; if(uriString?.StartsWith(Constants.AndroidAppProtocol) ?? false) { _name = uriString.Substring(Constants.AndroidAppProtocol.Length); } else if(!System.Uri.TryCreate(uriString, UriKind.Absolute, out uri) || - !DomainName.TryParse(uri.Host, out domainName)) + !DomainName.TryParseBaseDomain(uri.Host, out _name)) { _name = "--"; } - else - { - _name = domainName.BaseDomain; - } _loginService = Resolver.Resolve(); _deviceInfoService = Resolver.Resolve(); diff --git a/src/App/Services/LoginService.cs b/src/App/Services/LoginService.cs index 086e38ded..42d1a6c7c 100644 --- a/src/App/Services/LoginService.cs +++ b/src/App/Services/LoginService.cs @@ -78,11 +78,12 @@ namespace Bit.App.Services } Uri uri = null; - DomainName domainName = null; + string domainName = null; var androidApp = UriIsAndroidApp(uriString); if(!androidApp && - (!Uri.TryCreate(uriString, UriKind.Absolute, out uri) || !DomainName.TryParse(uri.Host, out domainName))) + (!Uri.TryCreate(uriString, UriKind.Absolute, out uri) || + !DomainName.TryParseBaseDomain(uri.Host, out domainName))) { return null; } @@ -104,7 +105,7 @@ namespace Bit.App.Services matchingFuzzyDomains.AddRange(eqDomain.Select(d => d).ToList()); } } - else if(Array.IndexOf(eqDomain, domainName.BaseDomain) >= 0) + else if(Array.IndexOf(eqDomain, domainName) >= 0) { matchingDomains.AddRange(eqDomain.Select(d => d).ToList()); } @@ -112,7 +113,7 @@ namespace Bit.App.Services if(!matchingDomains.Any()) { - matchingDomains.Add(androidApp ? uriString : domainName.BaseDomain); + matchingDomains.Add(androidApp ? uriString : domainName); } if(androidApp && androidAppWebUriString != null && @@ -156,18 +157,18 @@ namespace Bit.App.Services } Uri loginUri; - DomainName loginDomainName; + string loginDomainName = null; if(!Uri.TryCreate(loginUriString, UriKind.Absolute, out loginUri) - || !DomainName.TryParse(loginUri.Host, out loginDomainName)) + || !DomainName.TryParseBaseDomain(loginUri.Host, out loginDomainName)) { continue; } - if(Array.IndexOf(matchingDomainsArray, loginDomainName.BaseDomain) >= 0) + if(Array.IndexOf(matchingDomainsArray, loginDomainName) >= 0) { matchingLogins.Add(new Login(login)); } - else if(androidApp && Array.IndexOf(matchingFuzzyDomainsArray, loginDomainName.BaseDomain) >= 0) + else if(androidApp && Array.IndexOf(matchingFuzzyDomainsArray, loginDomainName) >= 0) { matchingFuzzyLogins.Add(new Login(login)); }