mirror of
https://github.com/bitwarden/server
synced 2025-12-28 22:23:30 +00:00
[PM-336] Nullable Platform & Unowned Services (#5646)
* Nullable Platform & Unowned Services * Fix build errors * Format
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using System.Globalization;
|
||||
#nullable enable
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Security.Cryptography;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
@@ -116,11 +119,11 @@ public static class CoreHelpers
|
||||
return Regex.Replace(thumbprint, @"[^\da-fA-F]", string.Empty).ToUpper();
|
||||
}
|
||||
|
||||
public static X509Certificate2 GetCertificate(string thumbprint)
|
||||
public static X509Certificate2? GetCertificate(string thumbprint)
|
||||
{
|
||||
thumbprint = CleanCertificateThumbprint(thumbprint);
|
||||
|
||||
X509Certificate2 cert = null;
|
||||
X509Certificate2? cert = null;
|
||||
var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
|
||||
certStore.Open(OpenFlags.ReadOnly);
|
||||
var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
|
||||
@@ -141,7 +144,7 @@ public static class CoreHelpers
|
||||
public async static Task<X509Certificate2> GetEmbeddedCertificateAsync(string file, string password)
|
||||
{
|
||||
var assembly = typeof(CoreHelpers).GetTypeInfo().Assembly;
|
||||
using (var s = assembly.GetManifestResourceStream($"Bit.Core.{file}"))
|
||||
using (var s = assembly.GetManifestResourceStream($"Bit.Core.{file}")!)
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
await s.CopyToAsync(ms);
|
||||
@@ -153,14 +156,14 @@ public static class CoreHelpers
|
||||
{
|
||||
var assembly = Assembly.GetCallingAssembly();
|
||||
var resourceName = assembly.GetManifestResourceNames().Single(n => n.EndsWith(file));
|
||||
using (var stream = assembly.GetManifestResourceStream(resourceName))
|
||||
using (var stream = assembly.GetManifestResourceStream(resourceName)!)
|
||||
using (var reader = new StreamReader(stream))
|
||||
{
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
public async static Task<X509Certificate2> GetBlobCertificateAsync(string connectionString, string container, string file, string password)
|
||||
public async static Task<X509Certificate2?> GetBlobCertificateAsync(string connectionString, string container, string file, string password)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -233,7 +236,7 @@ public static class CoreHelpers
|
||||
throw new ArgumentOutOfRangeException(nameof(length), "length cannot be less than zero.");
|
||||
}
|
||||
|
||||
if ((characters?.Length ?? 0) == 0)
|
||||
if (string.IsNullOrEmpty(characters))
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(characters), "characters invalid.");
|
||||
}
|
||||
@@ -346,10 +349,10 @@ public static class CoreHelpers
|
||||
/// </summary>
|
||||
public static T CloneObject<T>(T obj)
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(obj));
|
||||
return JsonSerializer.Deserialize<T>(JsonSerializer.Serialize(obj))!;
|
||||
}
|
||||
|
||||
public static bool SettingHasValue(string setting)
|
||||
public static bool SettingHasValue([NotNullWhen(true)] string? setting)
|
||||
{
|
||||
var normalizedSetting = setting?.ToLowerInvariant();
|
||||
return !string.IsNullOrWhiteSpace(normalizedSetting) && !normalizedSetting.Equals("secret") &&
|
||||
@@ -448,7 +451,8 @@ public static class CoreHelpers
|
||||
return output;
|
||||
}
|
||||
|
||||
public static string PunyEncode(string text)
|
||||
[return: NotNullIfNotNull(nameof(text))]
|
||||
public static string? PunyEncode(string? text)
|
||||
{
|
||||
if (text == "")
|
||||
{
|
||||
@@ -473,21 +477,21 @@ public static class CoreHelpers
|
||||
}
|
||||
}
|
||||
|
||||
public static string FormatLicenseSignatureValue(object val)
|
||||
public static string? FormatLicenseSignatureValue(object val)
|
||||
{
|
||||
if (val == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (val.GetType() == typeof(DateTime))
|
||||
if (val is DateTime dateTimeVal)
|
||||
{
|
||||
return ToEpocSeconds((DateTime)val).ToString();
|
||||
return ToEpocSeconds(dateTimeVal).ToString();
|
||||
}
|
||||
|
||||
if (val.GetType() == typeof(bool))
|
||||
if (val is bool boolVal)
|
||||
{
|
||||
return val.ToString().ToLowerInvariant();
|
||||
return boolVal.ToString().ToLowerInvariant();
|
||||
}
|
||||
|
||||
if (val is PlanType planType)
|
||||
@@ -625,7 +629,7 @@ public static class CoreHelpers
|
||||
return subName;
|
||||
}
|
||||
|
||||
public static string GetIpAddress(this Microsoft.AspNetCore.Http.HttpContext httpContext,
|
||||
public static string? GetIpAddress(this Microsoft.AspNetCore.Http.HttpContext httpContext,
|
||||
GlobalSettings globalSettings)
|
||||
{
|
||||
if (httpContext == null)
|
||||
@@ -652,7 +656,7 @@ public static class CoreHelpers
|
||||
(!globalSettings.SelfHosted && origin == "https://bitwarden.com");
|
||||
}
|
||||
|
||||
public static X509Certificate2 GetIdentityServerCertificate(GlobalSettings globalSettings)
|
||||
public static X509Certificate2? GetIdentityServerCertificate(GlobalSettings globalSettings)
|
||||
{
|
||||
if (globalSettings.SelfHosted &&
|
||||
SettingHasValue(globalSettings.IdentityServer.CertificatePassword)
|
||||
@@ -804,14 +808,16 @@ public static class CoreHelpers
|
||||
/// <param name="jsonData">The JSON data</param>
|
||||
/// <typeparam name="T">The type to deserialize into</typeparam>
|
||||
/// <returns></returns>
|
||||
public static T LoadClassFromJsonData<T>(string jsonData) where T : new()
|
||||
public static T LoadClassFromJsonData<T>(string? jsonData) where T : new()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(jsonData))
|
||||
{
|
||||
return new T();
|
||||
}
|
||||
|
||||
#nullable disable // TODO: Remove this and fix any callee warnings.
|
||||
return System.Text.Json.JsonSerializer.Deserialize<T>(jsonData, _jsonSerializerOptions);
|
||||
#nullable enable
|
||||
}
|
||||
|
||||
public static string ClassToJsonData<T>(T data)
|
||||
@@ -829,7 +835,7 @@ public static class CoreHelpers
|
||||
return list;
|
||||
}
|
||||
|
||||
public static string DecodeMessageText(this QueueMessage message)
|
||||
public static string? DecodeMessageText(this QueueMessage message)
|
||||
{
|
||||
var text = message?.MessageText;
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
@@ -852,7 +858,7 @@ public static class CoreHelpers
|
||||
Encoding.UTF8.GetBytes(input1), Encoding.UTF8.GetBytes(input2));
|
||||
}
|
||||
|
||||
public static string ObfuscateEmail(string email)
|
||||
public static string? ObfuscateEmail(string email)
|
||||
{
|
||||
if (email == null)
|
||||
{
|
||||
@@ -886,7 +892,7 @@ public static class CoreHelpers
|
||||
|
||||
}
|
||||
|
||||
public static string GetEmailDomain(string email)
|
||||
public static string? GetEmailDomain(string email)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(email))
|
||||
{
|
||||
@@ -906,7 +912,7 @@ public static class CoreHelpers
|
||||
return _whiteSpaceRegex.Replace(input, newValue);
|
||||
}
|
||||
|
||||
public static string RedactEmailAddress(string email)
|
||||
public static string? RedactEmailAddress(string email)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(email))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user