Skip to content

Commit 2077081

Browse files
authored
Add nullability to antiforgery (#22279)
* Add nullability to antiforgery Addresses #5680 * Rebase
1 parent 4c4351e commit 2077081

28 files changed

+154
-141
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project>
2+
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory)..\, Directory.Build.props))\Directory.Build.props" />
3+
4+
<PropertyGroup>
5+
<Nullable>annotations</Nullable>
6+
</PropertyGroup>
7+
</Project>

src/Antiforgery/ref/Microsoft.AspNetCore.Antiforgery.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
5+
<Nullable>annotations</Nullable>
56
</PropertyGroup>
67
<ItemGroup Condition="'$(TargetFramework)' == '$(DefaultNetCoreTargetFramework)'">
78
<Compile Include="Microsoft.AspNetCore.Antiforgery.netcoreapp.cs" />

src/Antiforgery/ref/Microsoft.AspNetCore.Antiforgery.netcoreapp.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ public partial class AntiforgeryOptions
99
public AntiforgeryOptions() { }
1010
public Microsoft.AspNetCore.Http.CookieBuilder Cookie { get { throw null; } set { } }
1111
public string FormFieldName { get { throw null; } set { } }
12-
public string HeaderName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
12+
public string? HeaderName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
1313
public bool SuppressXFrameOptionsHeader { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute] set { } }
1414
}
1515
public partial class AntiforgeryTokenSet
1616
{
17-
public AntiforgeryTokenSet(string requestToken, string cookieToken, string formFieldName, string headerName) { }
18-
public string CookieToken { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
17+
public AntiforgeryTokenSet(string? requestToken, string? cookieToken, string formFieldName, string? headerName) { }
18+
public string? CookieToken { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
1919
public string FormFieldName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
20-
public string HeaderName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
21-
public string RequestToken { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
20+
public string? HeaderName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
21+
public string? RequestToken { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
2222
}
2323
public partial class AntiforgeryValidationException : System.Exception
2424
{
2525
public AntiforgeryValidationException(string message) { }
26-
public AntiforgeryValidationException(string message, System.Exception innerException) { }
26+
public AntiforgeryValidationException(string message, System.Exception? innerException) { }
2727
}
2828
public partial interface IAntiforgery
2929
{

src/Antiforgery/src/AntiforgeryOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public string FormFieldName
7373
/// Specifies the name of the header value that is used by the antiforgery system. If <c>null</c> then
7474
/// antiforgery validation will only consider form data.
7575
/// </summary>
76-
public string HeaderName { get; set; } = AntiforgeryTokenHeaderName;
76+
public string? HeaderName { get; set; } = AntiforgeryTokenHeaderName;
7777

7878
/// <summary>
7979
/// Specifies whether to suppress the generation of X-Frame-Options header

src/Antiforgery/src/AntiforgeryTokenSet.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public class AntiforgeryTokenSet
1818
/// <param name="formFieldName">The name of the form field used for the request token.</param>
1919
/// <param name="headerName">The name of the header used for the request token.</param>
2020
public AntiforgeryTokenSet(
21-
string requestToken,
22-
string cookieToken,
21+
string? requestToken,
22+
string? cookieToken,
2323
string formFieldName,
24-
string headerName)
24+
string? headerName)
2525
{
2626
if (formFieldName == null)
2727
{
@@ -37,7 +37,7 @@ public AntiforgeryTokenSet(
3737
/// <summary>
3838
/// Gets the request token.
3939
/// </summary>
40-
public string RequestToken { get; }
40+
public string? RequestToken { get; }
4141

4242
/// <summary>
4343
/// Gets the name of the form field used for the request token.
@@ -47,11 +47,11 @@ public AntiforgeryTokenSet(
4747
/// <summary>
4848
/// Gets the name of the header used for the request token.
4949
/// </summary>
50-
public string HeaderName { get; }
50+
public string? HeaderName { get; }
5151

5252
/// <summary>
5353
/// Gets the cookie token.
5454
/// </summary>
55-
public string CookieToken { get; }
55+
public string? CookieToken { get; }
5656
}
57-
}
57+
}

src/Antiforgery/src/AntiforgeryValidationException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public AntiforgeryValidationException(string message)
2626
/// </summary>
2727
/// <param name="message">The message that describes the error.</param>
2828
/// <param name="innerException">The inner <see cref="Exception"/>.</param>
29-
public AntiforgeryValidationException(string message, Exception innerException)
29+
public AntiforgeryValidationException(string message, Exception? innerException)
3030
: base(message, innerException)
3131
{
3232
}

src/Antiforgery/src/Internal/AntiforgeryFeature.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ internal class AntiforgeryFeature : IAntiforgeryFeature
1010
{
1111
public bool HaveDeserializedCookieToken { get; set; }
1212

13-
public AntiforgeryToken CookieToken { get; set; }
13+
public AntiforgeryToken? CookieToken { get; set; }
1414

1515
public bool HaveDeserializedRequestToken { get; set; }
1616

17-
public AntiforgeryToken RequestToken { get; set; }
17+
public AntiforgeryToken? RequestToken { get; set; }
1818

1919
public bool HaveGeneratedNewCookieToken { get; set; }
2020

2121
// After HaveGeneratedNewCookieToken is true, remains null if CookieToken is valid.
22-
public AntiforgeryToken NewCookieToken { get; set; }
22+
public AntiforgeryToken? NewCookieToken { get; set; }
2323

2424
// After HaveGeneratedNewCookieToken is true, remains null if CookieToken is valid.
25-
public string NewCookieTokenString { get; set; }
25+
public string? NewCookieTokenString { get; set; }
2626

27-
public AntiforgeryToken NewRequestToken { get; set; }
27+
public AntiforgeryToken? NewRequestToken { get; set; }
2828

29-
public string NewRequestTokenString { get; set; }
29+
public string? NewRequestTokenString { get; set; }
3030

3131
// Always false if NewCookieToken is null. Never store null cookie token or re-store cookie token from request.
3232
public bool HaveStoredNewCookieToken { get; set; }

src/Antiforgery/src/Internal/AntiforgeryLoggerExtensions.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ namespace Microsoft.AspNetCore.Antiforgery
88
{
99
internal static class AntiforgeryLoggerExtensions
1010
{
11-
private static readonly Action<ILogger, Exception> _failedToDeserialzeTokens;
12-
private static readonly Action<ILogger, string, Exception> _validationFailed;
13-
private static readonly Action<ILogger, Exception> _validated;
14-
private static readonly Action<ILogger, string, Exception> _missingCookieToken;
15-
private static readonly Action<ILogger, string, string, Exception> _missingRequestToken;
16-
private static readonly Action<ILogger, Exception> _newCookieToken;
17-
private static readonly Action<ILogger, Exception> _reusedCookieToken;
18-
private static readonly Action<ILogger, Exception> _tokenDeserializeException;
19-
private static readonly Action<ILogger, Exception> _responseCacheHeadersOverridenToNoCache;
11+
private static readonly Action<ILogger, Exception?> _failedToDeserialzeTokens;
12+
private static readonly Action<ILogger, string, Exception?> _validationFailed;
13+
private static readonly Action<ILogger, Exception?> _validated;
14+
private static readonly Action<ILogger, string?, Exception?> _missingCookieToken;
15+
private static readonly Action<ILogger, string, string?, Exception?> _missingRequestToken;
16+
private static readonly Action<ILogger, Exception?> _newCookieToken;
17+
private static readonly Action<ILogger, Exception?> _reusedCookieToken;
18+
private static readonly Action<ILogger, Exception?> _tokenDeserializeException;
19+
private static readonly Action<ILogger, Exception?> _responseCacheHeadersOverridenToNoCache;
2020

2121
static AntiforgeryLoggerExtensions()
2222
{
@@ -28,11 +28,11 @@ static AntiforgeryLoggerExtensions()
2828
LogLevel.Debug,
2929
new EventId(2, "Validated"),
3030
"Antiforgery successfully validated a request.");
31-
_missingCookieToken = LoggerMessage.Define<string>(
31+
_missingCookieToken = LoggerMessage.Define<string?>(
3232
LogLevel.Warning,
3333
new EventId(3, "MissingCookieToken"),
3434
"The required antiforgery cookie '{CookieName}' is not present.");
35-
_missingRequestToken = LoggerMessage.Define<string, string>(
35+
_missingRequestToken = LoggerMessage.Define<string, string?>(
3636
LogLevel.Warning,
3737
new EventId(4, "MissingRequestToken"),
3838
"The required antiforgery request token was not provided in either form field '{FormFieldName}' "
@@ -71,12 +71,12 @@ public static void ValidatedAntiforgeryToken(this ILogger logger)
7171
_validated(logger, null);
7272
}
7373

74-
public static void MissingCookieToken(this ILogger logger, string cookieName)
74+
public static void MissingCookieToken(this ILogger logger, string? cookieName)
7575
{
7676
_missingCookieToken(logger, cookieName, null);
7777
}
7878

79-
public static void MissingRequestToken(this ILogger logger, string formFieldName, string headerName)
79+
public static void MissingRequestToken(this ILogger logger, string formFieldName, string? headerName)
8080
{
8181
_missingRequestToken(logger, formFieldName, headerName, null);
8282
}

src/Antiforgery/src/Internal/AntiforgerySerializationContext.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ internal class AntiforgerySerializationContext
2323
// Don't let _chars grow beyond 512k characters.
2424
private const int MaximumCharsLength = 0x80000;
2525

26-
private char[] _chars;
27-
private MemoryStream _stream;
28-
private BinaryReader _reader;
29-
private BinaryWriter _writer;
30-
private SHA256 _sha256;
26+
private char[]? _chars;
27+
private MemoryStream? _stream;
28+
private BinaryReader? _reader;
29+
private BinaryWriter? _writer;
30+
private SHA256? _sha256;
3131

3232
public MemoryStream Stream
3333
{
@@ -126,9 +126,9 @@ public void Reset()
126126
{
127127
if (Stream.Capacity > MaximumStreamSize)
128128
{
129-
Stream = null;
130-
Reader = null;
131-
Writer = null;
129+
_stream = null;
130+
_reader = null;
131+
_writer = null;
132132
}
133133
else
134134
{

src/Antiforgery/src/Internal/AntiforgeryToken.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal sealed class AntiforgeryToken
1010

1111
private string _additionalData = string.Empty;
1212
private string _username = string.Empty;
13-
private BinaryBlob _securityToken;
13+
private BinaryBlob? _securityToken;
1414

1515
public string AdditionalData
1616
{
@@ -21,11 +21,11 @@ public string AdditionalData
2121
}
2222
}
2323

24-
public BinaryBlob ClaimUid { get; set; }
24+
public BinaryBlob? ClaimUid { get; set; }
2525

2626
public bool IsCookieToken { get; set; }
2727

28-
public BinaryBlob SecurityToken
28+
public BinaryBlob? SecurityToken
2929
{
3030
get
3131
{
@@ -41,7 +41,7 @@ public BinaryBlob SecurityToken
4141
}
4242
}
4343

44-
public string Username
44+
public string? Username
4545
{
4646
get { return _username; }
4747
set

0 commit comments

Comments
 (0)