fix(inbound,security): bound the sync prefix of inbound scripts by the method timeout; persist a role refresh done on a passive request (INB-1, SEC-1, WP7 task 7.6)

This commit is contained in:
Joseph Doherty
2026-12-03 09:49:51 -05:00
parent 76c50a29c2
commit f7d3c69071
6 changed files with 84 additions and 7 deletions
@@ -1106,6 +1106,40 @@ public class InboundScriptExecutorTests
Assert.Equal(0, _executor.AbandonedExecutionCount); // ...and DECREMENTED
}
/// <summary>
/// INB-1: a script that blocks BEFORE its first await (CPU loop, <c>.Result</c> on a helper) used to
/// run inline on the request thread, so the method timeout could not fire until it returned. The
/// request must come back with TIMEOUT at the deadline and the orphan must be counted.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ScriptBlockingBeforeFirstAwait_StillTimesOutAtTheDeadline()
{
var release = new ManualResetEventSlim();
_executor.RegisterHandler("spin", _ =>
{
release.Wait(TimeSpan.FromSeconds(30)); // synchronous block: no await has happened yet
return Task.FromResult<object?>(1);
});
var method = new ApiMethod("spin", "unused") { Id = 10, TimeoutSeconds = 1 };
var started = System.Diagnostics.Stopwatch.StartNew();
var execution = _executor.ExecuteAsync(
method, new Dictionary<string, object?>(), _route, TimeSpan.FromMilliseconds(200));
var finished = await Task.WhenAny(execution, Task.Delay(TimeSpan.FromSeconds(10)));
Assert.Same(execution, finished); // pre-fix: the call itself never returned until released
var result = await execution;
Assert.False(result.Success);
Assert.Equal("TIMEOUT", result.ErrorCode);
Assert.True(started.Elapsed < TimeSpan.FromSeconds(5), $"took {started.Elapsed}");
Assert.Equal(1, _executor.AbandonedExecutionCount);
release.Set(); // the orphan finishes; its continuation cleans up
await Task.Delay(300);
Assert.Equal(0, _executor.AbandonedExecutionCount);
}
// --- Flow Debugger step logs (design 2026-09-09 D4/D5/D7): the Log global ---
/// <summary>Verifies that a traced run emits one ScriptLog row on the trace sink.</summary>
@@ -586,7 +586,7 @@ public class OnValidatePrincipalAdapterTests
}
private static (CookieValidatePrincipalContext Context, StubAuthenticationService AuthService)
BuildContext(ClaimsPrincipal principal)
BuildContext(ClaimsPrincipal principal, string path = "/")
{
var authService = new StubAuthenticationService();
@@ -596,6 +596,7 @@ public class OnValidatePrincipalAdapterTests
var serviceProvider = services.BuildServiceProvider();
var httpContext = new DefaultHttpContext { RequestServices = serviceProvider };
httpContext.Request.Path = path;
var ticket = new AuthenticationTicket(
principal,
new AuthenticationProperties(),
@@ -663,6 +664,33 @@ public class OnValidatePrincipalAdapterTests
Assert.Equal(0, authService.SignOutCallCount);
}
/// <summary>
/// SEC-1: a role refresh done on a passive request (keep-alive ping, Blazor transport) must be
/// persisted, otherwise the old anchors come back on the next ping and the refresh repeats on every
/// one. The principal built for a passive request keeps the OLD LastActivity claim (recordActivity
/// false), so re-issuing the ticket does not extend the idle window.
/// </summary>
/// <param name="path">A passive session request path.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
[Theory]
[InlineData("/auth/ping")]
[InlineData("/auth/activity")]
[InlineData("/_blazor")]
public async Task ApplyResult_Replace_OnPassiveRequest_StillPersistsTheRefreshedTicket(string path)
{
var newPrincipal = SessionClaimBuilder.Build(
"alice", "Alice",
["SCADA-Admins"],
new RoleMappingResult([Roles.Viewer], [], false),
AdapterStart.AddMinutes(16));
var (ctx, _) = BuildContext(AuthenticatedPrincipal(), path);
await ServiceCollectionExtensions.ApplyValidationResultAsync(ctx, SessionValidationResult.Replace(newPrincipal));
Assert.Same(newPrincipal, ctx.Principal);
Assert.True(ctx.ShouldRenew);
}
/// <summary>Verifies that applying a keep result leaves the context unchanged.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]