Skip to content

Commit

Permalink
Fixed issue where authorization results were cached. (#5819)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Feb 9, 2023
1 parent b3dfeab commit de82aca
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
7 changes: 6 additions & 1 deletion cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
"Swashbuckle",
"traversion",
"Websockets",
"Newtonsoft"
"Newtonsoft",
"cachable",
"fricking",
"runtimes",
"NATS"
],
"ignoreWords": [
"Specwise",
Expand All @@ -53,6 +57,7 @@
"Staib",
"shoooe",
"Senn",
"Rafi",
"Snapshooter",
"relayjs",
"Rgba",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async ValueTask AggregateAsync(
}
}

private IError CreateError(AuthorizeResult result)
private static IError CreateError(AuthorizeResult result)
{
return result switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public AuthorizeValidationRule(AuthorizationCache cache)
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
}

public bool IsCacheable => true;
public bool IsCacheable => false;

public void Validate(IDocumentValidatorContext context, DocumentNode document)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,56 @@ public async Task Authorize_Person_NoAccess()
""");
}

[Fact]
public async Task Authorize_Person_NoAccess_EnsureNotCached()
{
// arrange
var results = new Stack<AuthorizeResult>();
results.Push(AuthorizeResult.NotAllowed);
results.Push(AuthorizeResult.Allowed);

var handler = new AuthHandler2(results);
var services = CreateServices(handler);
var executor = await services.GetRequestExecutorAsync();

// act
await executor.ExecuteAsync(
"""
{
person(id: "UGVyc29uCmRhYmM=") {
name
}
}
""");

var result = await executor.ExecuteAsync(
"""
{
person(id: "UGVyc29uCmRhYmM=") {
name
}
}
""");

// assert
Snapshot
.Create()
.Add(result)
.MatchInline(
"""
{
"errors": [
{
"message": "The current user is not authorized to access this resource.",
"extensions": {
"code": "AUTH_NOT_AUTHORIZED"
}
}
]
}
""");
}

[Fact]
public async Task Authorize_Query_NoAccess()
{
Expand Down Expand Up @@ -561,7 +611,7 @@ public async Task Skip_Authorize_On_Node_Field()
}

private static IServiceProvider CreateServices(
AuthHandler handler,
IAuthorizationHandler handler,
Action<AuthorizationOptions>? configure = null)
=> new ServiceCollection()
.AddGraphQLServer()
Expand Down Expand Up @@ -661,6 +711,27 @@ public ValueTask<AuthorizeResult> AuthorizeAsync(
}
}

private sealed class AuthHandler2 : IAuthorizationHandler
{
private readonly Stack<AuthorizeResult> _results;
public AuthHandler2(Stack<AuthorizeResult> results)
{
_results = results;
}

public ValueTask<AuthorizeResult> AuthorizeAsync(
IMiddlewareContext context,
AuthorizeDirective directive,
CancellationToken cancellationToken = default)
=> new(AuthorizeResult.Allowed);

public ValueTask<AuthorizeResult> AuthorizeAsync(
AuthorizationContext context,
IReadOnlyList<AuthorizeDirective> directives,
CancellationToken cancellationToken = default)
=> new(_results.Pop());
}

[DirectiveType(DirectiveLocation.Object)]
public sealed class FooDirective { }

Expand Down

0 comments on commit de82aca

Please sign in to comment.