1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-24 17:17:40 +01:00

[PS-1909] Make LicenseKey check null safe (#2444)

* Make LicenseKey check null safe

* Catch Exception during Organization Validation

* Use null-safe check in UpdateLicense

* Formatting
This commit is contained in:
Justin Baur 2022-11-30 08:40:12 -05:00 committed by GitHub
parent 297f0c8b38
commit 8718f22ab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 23 deletions

View File

@ -81,35 +81,49 @@ public class LicensingService : ILicensingService
var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync();
_logger.LogInformation(Constants.BypassFiltersEventId, null,
"Validating licenses for {0} organizations.", enabledOrgs.Count);
"Validating licenses for {NumberOfOrganizations} organizations.", enabledOrgs.Count);
var exceptions = new List<Exception>();
foreach (var org in enabledOrgs)
{
var license = await ReadOrganizationLicenseAsync(org);
if (license == null)
try
{
await DisableOrganizationAsync(org, null, "No license file.");
continue;
}
var license = await ReadOrganizationLicenseAsync(org);
if (license == null)
{
await DisableOrganizationAsync(org, null, "No license file.");
continue;
}
var totalLicensedOrgs = enabledOrgs.Count(o => o.LicenseKey.Equals(license.LicenseKey));
if (totalLicensedOrgs > 1)
{
await DisableOrganizationAsync(org, license, "Multiple organizations.");
continue;
}
var totalLicensedOrgs = enabledOrgs.Count(o => string.Equals(o.LicenseKey, license.LicenseKey));
if (totalLicensedOrgs > 1)
{
await DisableOrganizationAsync(org, license, "Multiple organizations.");
continue;
}
if (!license.VerifyData(org, _globalSettings))
{
await DisableOrganizationAsync(org, license, "Invalid data.");
continue;
}
if (!license.VerifyData(org, _globalSettings))
{
await DisableOrganizationAsync(org, license, "Invalid data.");
continue;
}
if (!license.VerifySignature(_certificate))
{
await DisableOrganizationAsync(org, license, "Invalid signature.");
continue;
if (!license.VerifySignature(_certificate))
{
await DisableOrganizationAsync(org, license, "Invalid signature.");
continue;
}
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
if (exceptions.Any())
{
throw new AggregateException("There were one or more exceptions while validating organizations.", exceptions);
}
}

View File

@ -700,7 +700,7 @@ public class OrganizationService : IOrganizationService
}
var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync();
if (enabledOrgs.Any(o => o.LicenseKey.Equals(license.LicenseKey)))
if (enabledOrgs.Any(o => string.Equals(o.LicenseKey, license.LicenseKey)))
{
throw new BadRequestException("License is already in use by another organization.");
}
@ -852,7 +852,7 @@ public class OrganizationService : IOrganizationService
}
var enabledOrgs = await _organizationRepository.GetManyByEnabledAsync();
if (enabledOrgs.Any(o => o.LicenseKey.Equals(license.LicenseKey) && o.Id != organizationId))
if (enabledOrgs.Any(o => string.Equals(o.LicenseKey, license.LicenseKey) && o.Id != organizationId))
{
throw new BadRequestException("License is already in use by another organization.");
}