Fixed potential broadcast leak & policy value parsing (#756)

This commit is contained in:
Matt Portune 2020-03-05 12:44:01 -05:00 committed by GitHub
parent b9c134654f
commit b65b01fe3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 21 deletions

View file

@ -24,7 +24,7 @@ namespace Bit.App.Pages
{ {
base.OnAppearing(); base.OnAppearing();
await _vm.InitAsync(); await _vm.InitAsync();
_broadcasterService.Subscribe(nameof(AttachmentsPage), (message) => _broadcasterService.Subscribe(nameof(ExportVaultPage), (message) =>
{ {
if(message.Command == "selectSaveFileResult") if(message.Command == "selectSaveFileResult")
{ {
@ -45,6 +45,7 @@ namespace Bit.App.Pages
protected async override void OnDisappearing() protected async override void OnDisappearing()
{ {
base.OnDisappearing(); base.OnDisappearing();
_broadcasterService.Unsubscribe(nameof(ExportVaultPage));
} }
public Entry MasterPasswordEntry { get; set; } public Entry MasterPasswordEntry { get; set; }

View file

@ -344,55 +344,78 @@ namespace Bit.Core.Services
enforcedOptions = new PasswordGeneratorPolicyOptions(); enforcedOptions = new PasswordGeneratorPolicyOptions();
} }
var currentPolicyMinLength = currentPolicy.Data["minLength"]; var minLength = GetPolicyInt(currentPolicy, "minLength");
if(currentPolicyMinLength != null && if(minLength != null && (int)(long)minLength > enforcedOptions.MinLength)
(int)(long)currentPolicyMinLength > enforcedOptions.MinLength)
{ {
enforcedOptions.MinLength = (int)(long)currentPolicyMinLength; enforcedOptions.MinLength = (int)(long)minLength;
} }
var currentPolicyUseUpper = currentPolicy.Data["useUpper"]; var useUpper = GetPolicyBool(currentPolicy, "useUpper");
if(currentPolicyUseUpper != null && (bool)currentPolicyUseUpper) if(useUpper != null && (bool)useUpper)
{ {
enforcedOptions.UseUppercase = true; enforcedOptions.UseUppercase = true;
} }
var currentPolicyUseLower = currentPolicy.Data["useLower"]; var useLower = GetPolicyBool(currentPolicy, "useLower");
if(currentPolicyUseLower != null && (bool)currentPolicyUseLower) if(useLower != null && (bool)useLower)
{ {
enforcedOptions.UseLowercase = true; enforcedOptions.UseLowercase = true;
} }
var currentPolicyUseNumbers = currentPolicy.Data["useNumbers"]; var useNumbers = GetPolicyBool(currentPolicy, "useNumbers");
if(currentPolicyUseNumbers != null && (bool)currentPolicyUseNumbers) if(useNumbers != null && (bool)useNumbers)
{ {
enforcedOptions.UseNumbers = true; enforcedOptions.UseNumbers = true;
} }
var currentPolicyMinNumbers = currentPolicy.Data["minNumbers"]; var minNumbers = GetPolicyInt(currentPolicy, "minNumbers");
if(currentPolicyMinNumbers != null && if(minNumbers != null && (int)(long)minNumbers > enforcedOptions.NumberCount)
(int)(long)currentPolicyMinNumbers > enforcedOptions.NumberCount)
{ {
enforcedOptions.NumberCount = (int)(long)currentPolicyMinNumbers; enforcedOptions.NumberCount = (int)(long)minNumbers;
} }
var currentPolicyUseSpecial = currentPolicy.Data["useSpecial"]; var useSpecial = GetPolicyBool(currentPolicy, "useSpecial");
if(currentPolicyUseSpecial != null && (bool)currentPolicyUseSpecial) if(useSpecial != null && (bool)useSpecial)
{ {
enforcedOptions.UseSpecial = true; enforcedOptions.UseSpecial = true;
} }
var currentPolicyMinSpecial = currentPolicy.Data["minSpecial"]; var minSpecial = GetPolicyInt(currentPolicy, "minSpecial");
if(currentPolicyMinSpecial != null && if(minSpecial != null && (int)(long)minSpecial > enforcedOptions.SpecialCount)
(int)(long)currentPolicyMinSpecial > enforcedOptions.SpecialCount)
{ {
enforcedOptions.SpecialCount = (int)(long)currentPolicyMinSpecial; enforcedOptions.SpecialCount = (int)(long)minSpecial;
} }
} }
return enforcedOptions; return enforcedOptions;
} }
private int? GetPolicyInt(Policy policy, string key)
{
if(policy.Data.ContainsKey(key))
{
var value = policy.Data[key];
if(value != null)
{
return (int)(long)value;
}
}
return null;
}
private bool? GetPolicyBool(Policy policy, string key)
{
if(policy.Data.ContainsKey(key))
{
var value = policy.Data[key];
if(value != null)
{
return (bool)value;
}
}
return null;
}
public async Task SaveOptionsAsync(PasswordGenerationOptions options) public async Task SaveOptionsAsync(PasswordGenerationOptions options)
{ {
await _storageService.SaveAsync(Keys_Options, options); await _storageService.SaveAsync(Keys_Options, options);