Files
UA-.NETStandard/tests/Opc.Ua.Server.Tests/CreateSessionSpecRegressionTests.cs
T
romanett d98eb6b28c Fix server session defects found by a spec-checked review of CreateSession, ActivateSession, session lifecycle and continuation points (#4632)
# Description

Fixes defects found by a spec-checked review of the server session
implementation: CreateSession, ActivateSession, the session lifecycle
(per-request validation, CloseSession, Cancel, timeout, shutdown) and
session continuation points. Each finding was checked against OPC
10000-4 (§5.7, §7.9, §7.32), OPC 10000-5 (session diagnostics and audit
events) and OPC 10000-6 §6.8.2.

There is one commit per finding, and each commit message cites the spec
section. A max-effort code review of the fixes then found 15 more
issues, mostly regressions the first round introduced; they are fixed in
the `revfix` merges.

## What changed

**CreateSession**
- At MaxSessionCount the oldest session that was never activated is
closed to make room (§5.7.2.1). Sessions that are activated, already
closing, or have an activation in flight are never evicted. The victim
is claimed atomically, so a late activation commit on it is rejected.
- `requestedSessionTimeout` NaN is revised. A malformed `endpointUrl` no
longer fails the call with Bad_UnexpectedError. `serverUri` is ignored,
as §5.7.2.2 requires.
- Unnamed sessions get distinct names.
- Session diagnostics now report `MaxResponseMessageSize` and
`ServerUri`, and `SessionSecurityDiagnostics.Encoding` uses the
specified names ("UA Binary", "XML", "JSON").
- AuditCreateSessionEvent carries SecureChannelId and the request
certificate, also on failure.
- Every rejected CreateSession is counted, and only once. A rejection
during server startup no longer turns into Bad_UnexpectedError.
- If an `OnApplicationCertificateError` override accepts an error, the
certificate is now kept and the remaining checks still run. A
certificate accepted this way does not grant TrustedApplication or
application-based roles.

**ActivateSession**
- EphemeralKey handling follows OPC 10000-6 §6.8.2:
- A key used to decrypt a UserName or IssuedToken secret is retired and
replaced in the same commit.
  - A key that was not used is kept, and nothing is returned.
- ECDHPolicyUri is honoured; an unsupported policy gets
Bad_SecurityPolicyRejected.
  - Request parameters are no longer echoed back.
- After the activation commits, a failure or cancellation no longer
faults the response, which would hide the new server nonce. Post-commit
callbacks run uncancellably.
- An activation that overlaps a session close or timeout fails with
Bad_SessionClosed instead of returning Good.
- The PolicyId of an AnonymousIdentityToken is validated.
- Status codes now follow §5.7.3.3:
- Bad_ApplicationSignatureInvalid for a mismatched client-signature
algorithm
- Bad_UserSignatureInvalid for a mismatched X509 user-token signature
algorithm
- Bad_IdentityTokenInvalid for an undecodable token or an unknown policy
- `ClientUserIdOfSession` keeps the user of the first activation.

**Lifecycle and requests**
- Requests on an expired or closing session are rejected with
Bad_SessionClosed (§5.7.2.1). A CloseSession on an expired session that
is not yet closing is still processed as the client's close and honours
`deleteSubscriptions`.
- Closing a session aborts its outstanding requests with
Bad_SessionClosed. A request that registers after that abort pass is
rejected too.
- Cancel completes the matching requests with
Bad_RequestCancelledByClient (§5.7.5.1). It also reaches requests still
waiting in the request queue, but only when both timestamps prove the
request was sent earlier.
- There is one AuditCancelEvent per Cancel call, carrying the client's
audit entry id and user.
- Every close path goes through a single close claim, so
SessionAbortCount, SessionTimeoutCount and the "Session/Terminated" and
"Session/Timeout" audits are each counted once.
- Teardown that started cannot be cancelled. Requests rejected by
session validation are counted.

**Continuation points**
- HistoryRead returns Bad_NoContinuationPoints instead of evicting
points of the same request (§7.9). Only the request that saved a point
pins it, so points from prior requests can still be freed.
- Points saved by a HistoryRead that faults are released.
- Session-less Browse and BrowseNext no longer dereference a null
session.

## Behaviour changes reviewers should know about
- At the session cap, CreateSession evicts a non-activated session
instead of returning Bad_TooManySessions. The eviction is counted in
SessionAbortCount.
- Clients see Bad_SessionClosed for requests on expired or closing
sessions, and Bad_RequestCancelledByClient for cancelled requests.
- `ActivateSessionProcessAdditionalParameters` is now called on every
successful activation, with or without an AdditionalHeader.
- If an `OnApplicationCertificateError` override returns instead of
throwing, CreateSession now accepts the certificate.
- Timeout and termination audits are now raised after the session
teardown.
- The only public API changes are additive overloads in `AuditEvents`
and `RequestManager`.

**Known partial fixes:**
- The session's `maxResponseMessageSize` is reported but not enforced;
only the channel limit applies.
- With overlapping ActivateSession calls on one session, an earlier
response can still carry a key that a later activation has already
replaced. Closing that fully needs a new `ISession` member.

## Related Issues

- The Redundancy.Server session-mirroring findings from the same review
are tracked separately in #4627.

## Tests
- Regression tests cover each finding. The new files are:
  - `CreateSessionSpecRegressionTests`
  - `ActivateSessionValidationRegressionTests`
  - `StandardServerActivateSessionRegressionTests`
  - `SessionLifecycleRegressionTests`
  - `SessionRequestAdmissionRegressionTests`
  - `HistoryReadContinuationDispatchTests`
  
  Existing test classes were extended too.
- Local results on net10.0:
  - `Opc.Ua.Server.Tests`: 7883 passed / 0 failed / 5 skipped
- `Opc.Ua.Sessions.Tests`: 790 passed / 0 failed / 379 skipped (same as
master)
- net48 Release build of `Opc.Ua.Server.Tests`: 0 errors / 0 warnings.

## Checklist

- [x] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [x] I have added tests that prove my fix is effective or that my
feature works and increased code coverage.
- [ ] I have added all necessary documentation.
- [x] I have verified that my changes do not introduce (new) build or
analyzer warnings.
- [ ] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed.
- [ ] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [ ] I have addressed **all** PR feedback received.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 05:42:01 +02:00

647 lines
30 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2026 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.Security.Certificates;
using Opc.Ua.Server.TestFramework;
namespace Opc.Ua.Server.Tests
{
/// <summary>
/// Regression tests for CreateSession parameter handling against Part 4 5.7.2
/// and the session diagnostics of Part 5 12.
/// </summary>
[TestFixture]
[Category("Session")]
public sealed class CreateSessionSpecRegressionTests
{
/// <summary>
/// Part 4 5.7.2.1: at the session cap the oldest Session that was never
/// activated is closed, so abandoned Sessions cannot lock Clients out.
/// </summary>
[Test]
public async Task OldestNonActivatedSessionIsClosedAtSessionCapAsync()
{
ServerFixture<StandardServer> fixture = await StartAsync(maxSessionCount: 2).ConfigureAwait(false);
try
{
StandardServer server = fixture.Server;
CreateSessionResponse first = await CreateAsync(server, "cap-1").ConfigureAwait(false);
CreateSessionResponse second = await CreateAsync(server, "cap-2").ConfigureAwait(false);
uint abortsBefore = ((ServerInternalData)server.CurrentInstance).ServerDiagnostics.SessionAbortCount;
CreateSessionResponse third = await CreateAsync(server, "cap-3").ConfigureAwait(false);
List<NodeId> ids = [.. server.CurrentInstance.SessionManager.GetSessions().Select(s => s.Id)];
Assert.That(ids, Has.Count.EqualTo(2));
Assert.That(ids, Does.Not.Contain(first.SessionId));
Assert.That(ids, Does.Contain(second.SessionId));
Assert.That(ids, Does.Contain(third.SessionId));
// K4: the eviction is a termination by the server (OPC 10000-5 12.9).
Assert.That(
((ServerInternalData)server.CurrentInstance).ServerDiagnostics.SessionAbortCount,
Is.EqualTo(abortsBefore + 1));
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
}
}
/// <summary>
/// PR review 4166517725: cap eviction and activation admission are decided under
/// the same lock. A session whose activation registered itself in flight is not
/// evicted even when its activation gate reads as free, and an activation that
/// is admitted after the session was claimed as a victim fails.
/// </summary>
[Test]
public async Task CapEvictionAndActivationAdmissionAreMutuallyExclusiveAsync()
{
ServerFixture<StandardServer> fixture = await StartAsync(maxSessionCount: 1).ConfigureAwait(false);
try
{
StandardServer server = fixture.Server;
SecureChannelContext channel = CreateNoneChannel(server, "cap-admission");
CreateSessionResponse first = await server.CreateSessionAsync(
channel, new RequestHeader(), null, null, null, "cap-admission-1",
default, default, 60000, 0, RequestLifetime.None).ConfigureAwait(false);
object sessionManager = server.CurrentInstance.SessionManager;
ISession session = server.CurrentInstance.SessionManager.GetSession(first.AuthenticationToken);
object state = GetActivationState(sessionManager, session);
PropertyInfo inFlight = state.GetType().GetProperty("ActivationInFlight");
PropertyInfo claimed = state.GetType().GetProperty("CapEvictionClaimed");
// an activation admitted between the eviction's candidate check and its claim.
inFlight.SetValue(state, true);
ServiceResultException ex = Assert.ThrowsAsync<ServiceResultException>(
async () => await CreateAsync(server, "cap-admission-2").ConfigureAwait(false));
Assert.That(ex.StatusCode, Is.EqualTo(StatusCodes.BadTooManySessions));
Assert.That(session.IsClosing, Is.False);
Assert.That(
server.CurrentInstance.SessionManager.GetSession(first.AuthenticationToken),
Is.SameAs(session));
// an activation that acquires the gate after the eviction claim fails.
inFlight.SetValue(state, false);
claimed.SetValue(state, true);
ex = Assert.ThrowsAsync<ServiceResultException>(async () =>
await server.ActivateSessionAsync(
channel, new RequestHeader { AuthenticationToken = first.AuthenticationToken },
null, default, default, default, null, RequestLifetime.None).ConfigureAwait(false));
Assert.That(ex.StatusCode, Is.EqualTo(StatusCodes.BadSessionClosed));
Assert.That(session.Activated, Is.False);
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
}
}
private static object GetActivationState(object sessionManager, ISession session)
{
object table = typeof(SessionManager)
.GetField("m_sessionActivationStates", BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(sessionManager);
object[] args = [session, null];
bool found = (bool)table.GetType().GetMethod("TryGetValue").Invoke(table, args);
Assert.That(found, Is.True);
return args[1];
}
/// <summary>
/// A cap filled with activated Sessions still rejects CreateSession with
/// Bad_TooManySessions; activated Sessions are never evicted.
/// </summary>
[Test]
public async Task ActivatedSessionsAtCapAreNotEvictedAsync()
{
ServerFixture<StandardServer> fixture = await StartAsync(maxSessionCount: 2).ConfigureAwait(false);
try
{
StandardServer server = fixture.Server;
await server.CreateAndActivateSessionAsync("activated-1").ConfigureAwait(false);
await server.CreateAndActivateSessionAsync("activated-2").ConfigureAwait(false);
ServiceResultException ex = Assert.ThrowsAsync<ServiceResultException>(
async () => await CreateAsync(server, "activated-3").ConfigureAwait(false));
Assert.That(ex.StatusCode, Is.EqualTo(StatusCodes.BadTooManySessions));
Assert.That(server.CurrentInstance.SessionManager.GetSessions(), Has.Count.EqualTo(2));
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
}
}
/// <summary>
/// Part 4 5.7.2.2 defines no status code for a malformed endpointUrl: an
/// empty, relative or garbage value must not fail CreateSession.
/// </summary>
[TestCase("")]
[TestCase("myhost:4840/x")]
[TestCase("/relative/path")]
[TestCase("not a url")]
public async Task MalformedEndpointUrlDoesNotFailCreateSessionAsync(string endpointUrl)
{
ServerFixture<StandardServer> fixture = await StartAsync().ConfigureAwait(false);
try
{
CreateSessionResponse response = await CreateAsync(
fixture.Server, "endpoint-url", endpointUrl: endpointUrl).ConfigureAwait(false);
Assert.That(response.SessionId.IsNull, Is.False);
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
}
}
/// <summary>
/// Part 4 5.7.2.2: the Server shall ignore any serverUri. Part 5 12.11
/// reports the requested value in SessionDiagnostics.ServerUri.
/// </summary>
[Test]
public async Task ForeignServerUriIsIgnoredAndReportedInDiagnosticsAsync()
{
ServerFixture<StandardServer> fixture = await StartAsync().ConfigureAwait(false);
try
{
const string foreignUri = "urn:gateway:underlying-server";
CreateSessionResponse response = await CreateAsync(
fixture.Server, "server-uri", serverUri: foreignUri).ConfigureAwait(false);
ISession session = fixture.Server.CurrentInstance.SessionManager.GetSession(
response.AuthenticationToken);
Assert.That(session.ReadDiagnostics(d => d.ServerUri), Is.EqualTo(foreignUri));
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
}
}
/// <summary>
/// A NaN requestedSessionTimeout is revised to a legal value instead of
/// creating a Session that is expired from the start.
/// </summary>
[Test]
public async Task NaNSessionTimeoutIsRevisedToTheMinimumAsync()
{
ServerFixture<StandardServer> fixture = await StartAsync().ConfigureAwait(false);
try
{
CreateSessionResponse response = await CreateAsync(
fixture.Server, "nan-timeout", sessionTimeout: double.NaN).ConfigureAwait(false);
Assert.That(double.IsNaN(response.RevisedSessionTimeout), Is.False);
Assert.That(
response.RevisedSessionTimeout,
Is.EqualTo(fixture.Config.ServerConfiguration.MinSessionTimeout));
ISession session = fixture.Server.CurrentInstance.SessionManager.GetSession(
response.AuthenticationToken);
Assert.That(session.HasExpired, Is.False);
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
}
}
/// <summary>
/// Part 5 12.11: SessionDiagnostics.MaxResponseMessageSize is mandatory and
/// reports the maxResponseMessageSize of the CreateSession request.
/// </summary>
[Test]
public async Task MaxResponseMessageSizeIsReportedInDiagnosticsAsync()
{
ServerFixture<StandardServer> fixture = await StartAsync().ConfigureAwait(false);
try
{
CreateSessionResponse response = await CreateAsync(
fixture.Server, "max-response", maxResponseMessageSize: 65536).ConfigureAwait(false);
ISession session = fixture.Server.CurrentInstance.SessionManager.GetSession(
response.AuthenticationToken);
Assert.That(session.ReadDiagnostics(d => d.MaxResponseMessageSize), Is.EqualTo(65536u));
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
}
}
/// <summary>
/// Part 5 12.12: the SessionSecurityDiagnostics encoding is "UA Binary",
/// "XML" or "JSON".
/// </summary>
[TestCase(RequestEncoding.Binary, "UA Binary")]
[TestCase(RequestEncoding.Xml, "XML")]
[TestCase(RequestEncoding.Json, "JSON")]
public void SecurityDiagnosticsEncodingUsesTheSpecifiedNames(RequestEncoding encoding, string expected)
{
Assert.That(Opc.Ua.Server.Session.GetEncodingName(encoding), Is.EqualTo(expected));
}
/// <summary>
/// Part 5 12.9: a CreateSession rejected by request validation, before the
/// service body runs, is still counted as a rejected session request.
/// </summary>
[Test]
public async Task CreateSessionRejectedByValidationIsCountedAsync()
{
ServerFixture<StandardServer> fixture = await StartAsync().ConfigureAwait(false);
try
{
StandardServer server = fixture.Server;
Assert.CatchAsync<ServiceResultException>(async () =>
await server.CreateSessionAsync(
CreateNoneChannel(server, "invalid-header"), null, null, null, null, "invalid-header",
default, default, 60000, 0, RequestLifetime.None).ConfigureAwait(false));
uint rejectedSessions = 0;
uint rejectedRequests = 0;
uint cumulated = 0;
server.CurrentInstance.UpdateServerDiagnostics(diagnostics =>
{
rejectedSessions = diagnostics.RejectedSessionCount;
rejectedRequests = diagnostics.RejectedRequestsCount;
cumulated = diagnostics.CumulatedSessionCount;
});
Assert.That(rejectedSessions, Is.EqualTo(1));
Assert.That(rejectedRequests, Is.EqualTo(1));
Assert.That(cumulated, Is.Zero);
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
}
}
/// <summary>
/// Part 4 5.7.2.2: a Server-assigned session name is distinct per Session,
/// so the SessionDiagnostics objects can be told apart.
/// </summary>
[Test]
public async Task ServerAssignedSessionNamesAreDistinctAsync()
{
ServerFixture<StandardServer> fixture = await StartAsync().ConfigureAwait(false);
try
{
CreateSessionResponse first = await CreateAsync(fixture.Server, null).ConfigureAwait(false);
CreateSessionResponse second = await CreateAsync(fixture.Server, string.Empty).ConfigureAwait(false);
ISessionManager sessions = fixture.Server.CurrentInstance.SessionManager;
string firstName = sessions.GetSession(first.AuthenticationToken)
.ReadDiagnostics(d => d.SessionName);
string secondName = sessions.GetSession(second.AuthenticationToken)
.ReadDiagnostics(d => d.SessionName);
Assert.That(firstName, Is.Not.Empty);
Assert.That(secondName, Is.Not.Empty);
Assert.That(firstName, Is.Not.EqualTo(secondName));
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
}
}
/// <summary>
/// An OnApplicationCertificateError override that accepts an untrusted client
/// certificate keeps the certificate: the Session is created with it and the
/// response carries a serverSignature (Part 4 6.1.8).
/// </summary>
[Test]
public async Task AcceptedCertificateErrorKeepsTheClientCertificateAsync()
{
using Certificate untrusted = CreateClientCertificate("CN=Accepted Untrusted Client");
(ServerFixture<AcceptingServer> fixture, string pkiRoot) =
await StartAcceptingAsync().ConfigureAwait(false);
try
{
AcceptingServer server = fixture.Server;
CreateSessionResponse response = await CreateSecuredAsync(
server, untrusted, kApplicationUri).ConfigureAwait(false);
Assert.That(server.ReportedErrors, Does.Contain((StatusCode)StatusCodes.BadCertificateUntrusted));
Assert.That(response.ServerSignature, Is.Not.Null);
Assert.That(response.ServerSignature.Signature.IsEmpty, Is.False);
ISession session = server.CurrentInstance.SessionManager.GetSession(response.AuthenticationToken);
Assert.That(session.ClientCertificate, Is.Not.Null);
Assert.That(session.ClientCertificate.Thumbprint, Is.EqualTo(untrusted.Thumbprint));
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
DeleteDirectory(pkiRoot);
}
}
/// <summary>
/// K21: a client certificate whose validation error an override accepted still
/// signs the session, but it is not a trusted application identity: the
/// session is not granted TrustedApplication (Part 3 4.9) or application-based
/// role mappings (Part 18 4.4.4) from it.
/// </summary>
[Test]
public async Task AcceptedCertificateErrorDoesNotGrantTrustedApplicationAsync()
{
using Certificate untrusted = CreateClientCertificate("CN=Accepted Untrusted Client Roles");
(ServerFixture<AcceptingServer> fixture, string pkiRoot) =
await StartAcceptingAsync().ConfigureAwait(false);
try
{
AcceptingServer server = fixture.Server;
CreateSessionResponse response = await CreateSecuredAsync(
server, untrusted, kApplicationUri).ConfigureAwait(false);
var session = (Opc.Ua.Server.Session)server.CurrentInstance.SessionManager
.GetSession(response.AuthenticationToken);
Assert.That(session.ClientCertificateValidated, Is.False);
EndpointDescription endpoint = session.EndpointDescription;
var channel = new SecureChannelContext(
"roles", endpoint, RequestEncoding.Binary,
untrusted.RawData, endpoint.ServerCertificate.ToArray());
using var context = new OperationContext(
new RequestHeader(), channel, RequestType.ActivateSession, RequestLifetime.None);
MethodInfo addMandatoryRoles = typeof(SessionManager).GetMethod(
"AddMandatoryRoles", BindingFlags.Instance | BindingFlags.NonPublic);
object sessionManager = server.CurrentInstance.SessionManager;
var untrustedIdentity = (IUserIdentity)addMandatoryRoles.Invoke(
sessionManager, [session, context, new UserIdentity()]);
Assert.That(
untrustedIdentity.GrantedRoleIds.ToArray(),
Has.No.Member(ObjectIds.WellKnownRole_TrustedApplication));
// the same session with a validated certificate is a trusted application.
session.ClientCertificateValidated = true;
var trustedIdentity = (IUserIdentity)addMandatoryRoles.Invoke(
sessionManager, [session, context, new UserIdentity()]);
Assert.That(
trustedIdentity.GrantedRoleIds.ToArray(),
Has.Member(ObjectIds.WellKnownRole_TrustedApplication));
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
DeleteDirectory(pkiRoot);
}
}
/// <summary>
/// PR review 4166517268: the accepted-certificate provenance is recorded for
/// every ISession implementation, so a session from a custom CreateSession
/// factory whose certificate error was accepted is not granted
/// TrustedApplication (Part 3 4.9) either.
/// </summary>
[Test]
public async Task AcceptedCertificateErrorDoesNotGrantTrustedApplicationToCustomSessionAsync()
{
using Certificate untrusted = CreateClientCertificate("CN=Accepted Untrusted Custom Session");
(ServerFixture<AcceptingServer> fixture, string pkiRoot) =
await StartAcceptingAsync().ConfigureAwait(false);
try
{
AcceptingServer server = fixture.Server;
EndpointDescription endpoint = server.GetEndpoints().Find(
candidate => candidate.SecurityMode == MessageSecurityMode.SignAndEncrypt);
Assert.That(endpoint, Is.Not.Null);
var customSession = new Moq.Mock<ISession>();
customSession.SetupGet(s => s.ClientCertificate).Returns(untrusted);
customSession.SetupGet(s => s.EndpointDescription).Returns(endpoint);
ClientCertificateProvenance.SetValidated(customSession.Object, false);
var channel = new SecureChannelContext(
"roles-custom", endpoint, RequestEncoding.Binary,
untrusted.RawData, endpoint.ServerCertificate.ToArray());
using var context = new OperationContext(
new RequestHeader(), channel, RequestType.ActivateSession, RequestLifetime.None);
MethodInfo addMandatoryRoles = typeof(SessionManager).GetMethod(
"AddMandatoryRoles", BindingFlags.Instance | BindingFlags.NonPublic);
object sessionManager = server.CurrentInstance.SessionManager;
var untrustedIdentity = (IUserIdentity)addMandatoryRoles.Invoke(
sessionManager, [customSession.Object, context, new UserIdentity()]);
Assert.That(
untrustedIdentity.GrantedRoleIds.ToArray(),
Has.No.Member(ObjectIds.WellKnownRole_TrustedApplication));
ClientCertificateProvenance.SetValidated(customSession.Object, true);
var trustedIdentity = (IUserIdentity)addMandatoryRoles.Invoke(
sessionManager, [customSession.Object, context, new UserIdentity()]);
Assert.That(
trustedIdentity.GrantedRoleIds.ToArray(),
Has.Member(ObjectIds.WellKnownRole_TrustedApplication));
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
DeleteDirectory(pkiRoot);
}
}
/// <summary>
/// Accepting one certificate error does not skip the checks that follow it:
/// an ApplicationUri mismatch after an accepted trust error still fails.
/// </summary>
[Test]
public async Task AcceptedCertificateErrorDoesNotSkipTheApplicationUriCheckAsync()
{
using Certificate untrusted = CreateClientCertificate("CN=Accepted Untrusted Client Uri");
(ServerFixture<AcceptingServer> fixture, string pkiRoot) =
await StartAcceptingAsync().ConfigureAwait(false);
try
{
AcceptingServer server = fixture.Server;
ServiceResultException ex = Assert.ThrowsAsync<ServiceResultException>(async () =>
await CreateSecuredAsync(server, untrusted, "urn:wrong:application").ConfigureAwait(false));
Assert.That(ex.StatusCode, Is.EqualTo(StatusCodes.BadCertificateUriInvalid));
Assert.That(server.ReportedErrors, Does.Contain((StatusCode)StatusCodes.BadCertificateUntrusted));
Assert.That(server.CurrentInstance.SessionManager.GetSessions(), Is.Empty);
}
finally
{
await fixture.StopAsync().ConfigureAwait(false);
DeleteDirectory(pkiRoot);
}
}
private static async Task<ServerFixture<StandardServer>> StartAsync(int maxSessionCount = 0)
{
var fixture = new ServerFixture<StandardServer>(telemetry => new StandardServer(telemetry))
{
SecurityNone = true
};
await fixture.LoadConfigurationAsync().ConfigureAwait(false);
if (maxSessionCount > 0)
{
fixture.Config.ServerConfiguration.MaxSessionCount = maxSessionCount;
}
await fixture.StartAsync().ConfigureAwait(false);
return fixture;
}
private static async Task<(ServerFixture<AcceptingServer>, string)> StartAcceptingAsync()
{
string pkiRoot = Path.Combine(Path.GetTempPath(), "cs-accept-" + Guid.NewGuid().ToString("N"));
var fixture = new ServerFixture<AcceptingServer>(telemetry => new AcceptingServer(telemetry))
{
SecurityNone = true,
AutoAccept = false
};
await fixture.LoadConfigurationAsync(pkiRoot).ConfigureAwait(false);
fixture.Config.SecurityConfiguration.RejectUnknownRevocationStatus = false;
await fixture.StartAsync().ConfigureAwait(false);
return (fixture, pkiRoot);
}
private static SecureChannelContext CreateNoneChannel(StandardServer server, string channelId)
{
EndpointDescription endpoint = server.GetEndpoints().Find(
candidate => candidate.SecurityPolicyUri == SecurityPolicies.None);
Assert.That(endpoint, Is.Not.Null);
return new SecureChannelContext(channelId, endpoint, RequestEncoding.Binary, null, null);
}
private static ValueTask<CreateSessionResponse> CreateAsync(
StandardServer server,
string sessionName,
string serverUri = null,
string endpointUrl = null,
double sessionTimeout = 60000,
uint maxResponseMessageSize = 0)
{
return server.CreateSessionAsync(
CreateNoneChannel(server, Guid.NewGuid().ToString("N")),
new RequestHeader(),
null,
serverUri,
endpointUrl,
sessionName,
default,
default,
sessionTimeout,
maxResponseMessageSize,
RequestLifetime.None);
}
private static ValueTask<CreateSessionResponse> CreateSecuredAsync(
AcceptingServer server,
Certificate clientCertificate,
string applicationUri)
{
EndpointDescription template = server.GetEndpoints().Find(endpoint =>
endpoint.SecurityPolicyUri == SecurityPolicies.Basic256Sha256 &&
endpoint.SecurityMode == MessageSecurityMode.SignAndEncrypt &&
endpoint.TransportProfileUri == Profiles.UaTcpTransport)
?? throw new InvalidOperationException("The expected test endpoint was not configured.");
var channel = new SecureChannelContext(
Guid.NewGuid().ToString("N"),
template,
RequestEncoding.Binary,
clientCertificate.RawData,
template.ServerCertificate.ToArray());
return server.CreateSessionAsync(
channel,
new RequestHeader(),
new ApplicationDescription
{
ApplicationUri = applicationUri,
ApplicationName = new LocalizedText("CreateSession spec regression"),
ApplicationType = ApplicationType.Client
},
null,
template.EndpointUrl,
"accepted-certificate",
Nonce.CreateRandomNonceData(32).ToByteString(),
clientCertificate.RawData.ToByteString(),
60000,
0,
RequestLifetime.None);
}
private static Certificate CreateClientCertificate(string subject)
{
return CertificateBuilder.Create(subject)
.SetNotBefore(new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc))
.SetNotAfter(new DateTime(2099, 1, 1, 0, 0, 0, DateTimeKind.Utc))
.AddExtension(new X509SubjectAltNameExtension([kApplicationUri], ["localhost"]))
.SetRSAKeySize(2048)
.CreateForRSA();
}
private static void DeleteDirectory(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}
}
private const string kApplicationUri = "urn:servercore:createsession-spec-regression";
/// <summary>
/// A server whose OnApplicationCertificateError accepts untrusted client
/// certificates and rejects every other error.
/// </summary>
public sealed class AcceptingServer : StandardServer
{
public AcceptingServer(ITelemetryContext telemetry)
: base(telemetry)
{
}
public List<StatusCode> ReportedErrors { get; } = [];
protected override void OnApplicationCertificateError(
ByteString clientCertificate,
ServiceResult result)
{
lock (ReportedErrors)
{
ReportedErrors.Add(result.StatusCode);
}
if (result.StatusCode == StatusCodes.BadCertificateUntrusted)
{
return;
}
base.OnApplicationCertificateError(clientCertificate, result);
}
}
}
}