Skip to content

Commit 6edaee8

Browse files
ReubenBondCopilot
andcommitted
fix(security): dispose authentication resources once
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent ebbe659 commit 6edaee8

1 file changed

Lines changed: 40 additions & 49 deletions

File tree

src/Orleans.Connections.Security/Authentication/SiloConnectionAuthenticationMiddleware.cs

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,9 @@ public async Task OnConnectionAsync(ConnectionContext context, ConnectionDelegat
408408
var state = new SiloConnectionAuthenticationStateMachine();
409409
state.Move(SiloConnectionAuthenticationState.Created, SiloConnectionAuthenticationState.ProtocolSelected);
410410

411-
using var linked = CreateExchangeCancellation(context, out var timeout);
411+
SiloConnectionAuthenticationFeature? feature = null;
412+
var resultCategory = AuthenticationResultCategory.Authenticated;
413+
using (var linked = CreateExchangeCancellation(context, out var timeout))
412414
using (timeout)
413415
{
414416
IDisposable? admission = null;
@@ -469,23 +471,16 @@ await ConnectionFrameHelper.WriteFrameAsync(
469471
}
470472

471473
state.Move(SiloConnectionAuthenticationState.ResultTransferred, SiloConnectionAuthenticationState.Accepted);
472-
var feature = new SiloConnectionAuthenticationFeature(
474+
feature = new SiloConnectionAuthenticationFeature(
473475
true,
474476
isAuthenticated,
475477
principal,
476478
expiresAt,
477479
failure,
478480
SiloConnectionAuthenticationProtocol.Version2);
479-
admission.Dispose();
480-
linked.Dispose();
481-
timeout.Dispose();
482-
await RunAcceptedAsync(
483-
context,
484-
next,
485-
direction,
486-
feature,
487-
started,
488-
isAuthenticated ? AuthenticationResultCategory.Authenticated : AuthenticationResultCategory.AcceptedUnauthenticated);
481+
resultCategory = isAuthenticated
482+
? AuthenticationResultCategory.Authenticated
483+
: AuthenticationResultCategory.AcceptedUnauthenticated;
489484
}
490485
}
491486
catch (OperationCanceledException) when (state.State != SiloConnectionAuthenticationState.Accepted)
@@ -501,6 +496,11 @@ await RunAcceptedAsync(
501496
Abort(context, direction, AuthenticationResultCategory.ProtocolError, started);
502497
}
503498
}
499+
500+
if (feature is not null)
501+
{
502+
await RunAcceptedAsync(context, next, direction, feature, started, resultCategory);
503+
}
504504
}
505505

506506
private async ValueTask<SiloConnectionTokenValidationResult> ValidateAsync(
@@ -690,7 +690,9 @@ public async Task OnConnectionAsync(ConnectionContext context, ConnectionDelegat
690690
var state = new SiloConnectionAuthenticationStateMachine();
691691
state.Move(SiloConnectionAuthenticationState.Created, SiloConnectionAuthenticationState.ProtocolSelected);
692692

693-
using var linked = CreateExchangeCancellation(context, out var timeout);
693+
SiloConnectionAuthenticationFeature? feature = null;
694+
var resultCategory = AuthenticationResultCategory.Authenticated;
695+
using (var linked = CreateExchangeCancellation(context, out var timeout))
694696
using (timeout)
695697
{
696698
try
@@ -727,44 +729,28 @@ public async Task OnConnectionAsync(ConnectionContext context, ConnectionDelegat
727729
{
728730
case AuthenticatedResult:
729731
state.Move(SiloConnectionAuthenticationState.ResultTransferred, SiloConnectionAuthenticationState.Accepted);
730-
admission.Dispose();
731-
linked.Dispose();
732-
timeout.Dispose();
733-
await RunAcceptedAsync(
734-
context,
735-
next,
736-
direction,
737-
new SiloConnectionAuthenticationFeature(
738-
true,
739-
true,
740-
null,
741-
expiresAt,
742-
SiloConnectionAuthenticationFailure.None,
743-
SiloConnectionAuthenticationProtocol.Version2),
744-
started,
745-
AuthenticationResultCategory.Authenticated);
746-
return;
732+
feature = new SiloConnectionAuthenticationFeature(
733+
true,
734+
true,
735+
null,
736+
expiresAt,
737+
SiloConnectionAuthenticationFailure.None,
738+
SiloConnectionAuthenticationProtocol.Version2);
739+
resultCategory = AuthenticationResultCategory.Authenticated;
740+
break;
747741
case AcceptedUnauthenticatedResult when Options.Mode == SiloConnectionAuthenticationMode.Audit:
748742
state.Move(SiloConnectionAuthenticationState.ResultTransferred, SiloConnectionAuthenticationState.Accepted);
749-
admission.Dispose();
750-
linked.Dispose();
751-
timeout.Dispose();
752-
await RunAcceptedAsync(
753-
context,
754-
next,
755-
direction,
756-
new SiloConnectionAuthenticationFeature(
757-
true,
758-
false,
759-
null,
760-
null,
761-
localFailure == SiloConnectionAuthenticationFailure.None
762-
? SiloConnectionAuthenticationFailure.InvalidToken
763-
: localFailure,
764-
SiloConnectionAuthenticationProtocol.Version2),
765-
started,
766-
AuthenticationResultCategory.AcceptedUnauthenticated);
767-
return;
743+
feature = new SiloConnectionAuthenticationFeature(
744+
true,
745+
false,
746+
null,
747+
null,
748+
localFailure == SiloConnectionAuthenticationFailure.None
749+
? SiloConnectionAuthenticationFailure.InvalidToken
750+
: localFailure,
751+
SiloConnectionAuthenticationProtocol.Version2);
752+
resultCategory = AuthenticationResultCategory.AcceptedUnauthenticated;
753+
break;
768754
case RejectedResult:
769755
case AcceptedUnauthenticatedResult:
770756
state.Move(SiloConnectionAuthenticationState.ResultTransferred, SiloConnectionAuthenticationState.Rejected);
@@ -789,6 +775,11 @@ await RunAcceptedAsync(
789775
Abort(context, direction, AuthenticationResultCategory.ProtocolError, started);
790776
}
791777
}
778+
779+
if (feature is not null)
780+
{
781+
await RunAcceptedAsync(context, next, direction, feature, started, resultCategory);
782+
}
792783
}
793784

794785
private async ValueTask<(byte[]? Payload, DateTimeOffset? ExpiresAt, SiloConnectionAuthenticationFailure Failure)> GetTokenPayloadAsync(

0 commit comments

Comments
 (0)