Skip to content

Authorization standard

This page sets the rules for authorization in ConnectSoft services: which endpoints are gated, how, which status a refusal returns, and how a service limits the records a caller can read. It is written for engineers who add or review endpoints, queries and service-to-service calls. Token validation and trust in gateway-propagated identity are covered by CS-SEC-002 and CS-SEC-003; the concepts are explained in Authorization.

Two levels of authorization

  • Capability: may this caller perform this kind of operation at all? A policy on the endpoint answers it, from the scopes, roles or permissions in the token.
  • Record scope: which records may this caller read or change? The service answers it inside the operation, from the caller's identity and grants, for every record the operation touches.

A capability gate without a record scope lets any caller with the capability read every record. Both are needed.

Rules

CS-AUZ-001: Gate every action with a named policy above an authenticated floor

Severity: blocker · Applies to: service, template · Enforced by: manual (target: ENF-038) · Status: active

Why

An action without a policy is open to every authenticated caller, and an action without any attribute on an unprotected controller is open to anonymous callers. A floor on the class makes a forgotten attribute fail safe, and a policy on each action states what the action needs. A sweep test is needed because the floor makes an omission silent.

Do

  • Put a bare [Authorize] on every controller class, or configure a fallback policy that requires an authenticated user.
  • Put [Authorize(Policy = ...)] on every routed action.
  • Add a test that enumerates every routed action and fails when one has no policy. Opened routes are listed in the test by name, each with its reason.
  • Express role requirements inside a policy.

Don't

  • Use [Authorize(Roles = ...)] in new code.
  • Leave an action covered only by the class floor.

Example

[Authorize]
[Route("api/orders")]
public class OrdersController : ControllerBase
{
    [Authorize(Policy = OrdersPolicies.ReadOrders)]
    [HttpGet("{orderId}")]
    public Task<ActionResult<OrderResponse>> GetOrder(Guid orderId, CancellationToken token) => ...;

    [Authorize(Policy = OrdersPolicies.ManageOrders)]
    [HttpPost]
    public Task<ActionResult<OrderResponse>> CreateOrder(CreateOrderRequest request, CancellationToken token) => ...;
}

How to check

  • Every routed action in the change carries a policy.
  • The service has a sweep test over all routed actions, and the change keeps it green.
  • Search the change for Roles = in [Authorize] attributes. New code has none.

CS-AUZ-002: Remember that authorize attributes add up and only [AllowAnonymous] overrides them

Severity: blocker · Applies to: service, template · Enforced by: manual · Status: active

Why

ASP.NET Core combines every [Authorize] in an endpoint's metadata, from the controller and from the action, and requires all of them. An action attribute never replaces or loosens a class attribute. [AllowAnonymous] is the only override, and it removes authentication too. A test that only reads the attributes of an action cannot see the effective policy, so an attempt to loosen a class policy passes such a test and still refuses the caller.

Do

  • To open one action on a controller whose class carries a policy, move the policy from the class onto every other action, and leave a bare [Authorize] floor on the class.
  • When an action already has its own policy and the class policy moves onto it, keep both, so the requirement stays the same.
  • Test an opened or changed route by calling it with a real token of the caller it is meant for.
  • Read attributes in tests with the plural accessor (GetCustomAttributes<AuthorizeAttribute>()), because an action may carry more than one.

Don't

  • Add a bare [Authorize] to an action to "open" it under a class policy.
  • Use [AllowAnonymous] to mean "authenticated, without the policy".

How to check

  • For each changed action, list the attributes of the class and the action together; the effective requirement is all of them.
  • A route that was opened has a test that calls it with a token that lacks the class policy.

CS-AUZ-003: Name policies with constants

Severity: minor · Applies to: service, template · Enforced by: manual (target: ENF-039) · Status: active

Why

A policy name typed as a string literal is checked only at run time. A misspelt name fails every request to the action with an exception, and a renamed policy leaves stale literals behind.

Do

  • Declare each policy name once as a public const string in a policies class, for example OrdersPolicies.ManageOrders.
  • Use the constant in [Authorize(Policy = ...)] and in the policy registration.

Don't

  • Write [Authorize(Policy = "ManageOrders")] in new code.

How to check

  • Search the change for Policy = ".
  • Search policy registrations in the change, such as AddPolicy(", for policy names written as string literals.

CS-AUZ-004: Return 404 for an out-of-scope record and 403 for a missing capability, never 500

Severity: blocker · Applies to: service, template · Enforced by: manual (target: ENF-040) · Status: active

Why

A refusal that reaches the caller as a 500 looks like an outage: clients retry it, alerts fire on it, and a user is told the service is down. A 403 for a record outside the caller's scope confirms that the record exists. A 404 tells the caller nothing about records it may not see.

Do

  • Return 404 when the caller has the capability but the requested record is outside their scope.
  • Return 403 when the caller lacks the capability or policy for the operation.
  • Throw a dedicated refusal exception from the domain service and map it to 403 or 404 in the service's ProblemDetails mappings.
  • Test the status the caller receives by calling the endpoint through a test host, not by asserting that a use case throws.

Don't

  • Let a refusal exception fall through to the catch-all 500 mapping.
  • Return 403 for a record the caller is not allowed to know about.

How to check

  • Every refusal exception type in the change has a ProblemDetails mapping to 403 or 404.
  • A test calls the endpoint as an out-of-scope caller and asserts the status code.

CS-AUZ-005: Enforce record scope on the server, from the token

Severity: blocker · Applies to: service, template · Enforced by: manual · Status: active

Why

A scope that the client sends, in a header, a query parameter or the body, is a filter the client can leave out. An API that relies on its own front end to send the right scope is not protected; any other caller reads everything. CS-SEC-002 requires record-level authorization in the backend; this rule says how.

Do

  • Resolve the caller's record scope once per request, on the server, from the validated token and the grants stored for that identity.
  • Pass the resolved scope into every query and command that reads or changes records.
  • Treat a scope value the client sends, such as a selected region or tenant, only as a narrowing: intersect it with the caller's grants.

Don't

  • Accept a scope, a tenant or an owner id from the request in place of the token.
  • Skip the scope when the request carries no selection.

How to check

  • Read the query path of each changed endpoint and find where the scope comes from. It must come from the token and the stored grants.
  • A test calls the endpoint without the optional selection and asserts that the result is still limited to the caller's grants.

CS-AUZ-006: Treat an empty grant set as no access, and hide unscoped rows

Severity: blocker · Applies to: service, template · Enforced by: manual · Status: active

Why

In a filter, an empty set of allowed values often reads as "no filter". A bounded caller who has no grants then reads every record. A row whose scope column is empty matches no grant, and a filter that lets such rows through discloses them to every caller.

Do

  • Represent "this caller may see nothing" explicitly, for example with a sentinel value or a separate flag, so the query returns no rows.
  • Keep the distinction between an unbounded caller, who has no limit, and a bounded caller with an empty grant set, who has no access.
  • Hide rows with an empty scope value from bounded callers.
  • Where a row with an empty scope value must be visible to every caller, state the decision in a code comment on the filter and cover it with a test.

Don't

  • Pass an empty collection to a filter that treats empty as unrestricted.
  • Write x.Scope == null || grants.Contains(x.Scope) without a documented decision and a test.

How to check

  • A test calls each scoped list as a bounded caller with no grants and asserts that it is empty.
  • Search the change for == null || in scope filters.

CS-AUZ-007: Call other services from background jobs with a service token

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Why

A background job, a timer or a grain reminder runs outside an HTTP request, so it has no user token to forward. An HttpClient that forwards the caller's token or propagates request headers throws before sending, or the call is refused with 401. Unit tests replace the client and do not see this.

Do

  • Give the client a job uses its own service-to-service token handler and its own client registration and scope at the authorization server.
  • Require that scope on the target route.
  • Pin the client's handler chain with a unit test.

Don't

  • Reuse a client that propagates the user's token or request headers from code that runs outside a request.

How to check

Every named HttpClient used by a job, timer or grain has a service-token handler and no header propagation.

CS-AUZ-008: Compare scope in the id space the record stores

Severity: major · Applies to: service · Enforced by: manual · Status: active

Why

A person can have more than one identifier, for example an identity user id and an employee id. A "mine" filter that compares the caller's employee id with a column that stores user ids is always empty for real users. Administrators usually skip such filters, so testing only as an administrator never shows it. A screen that resolves the stored id to a name proves only that the id belongs to someone, not that it is in the right id space.

Do

  • Check which identifier each scoped column stores, and compare it with the same identifier of the caller.
  • Resolve the caller's identifier in one helper and reuse it in every self-scoped filter.

Don't

  • Use a displayed name as proof that a scope comparison is right.

How to check

Each changed self-scoped filter is tested as a non-administrator caller whose records exist, and the result is not empty.

CS-AUZ-009: Carry the scope into every query an operation builds

Severity: major · Applies to: service · Enforced by: manual · Status: active

Why

An operation that builds its own inner queries, such as an export that pages through a report, often copies some of the input fields and drops a newly added scope field. A filter that only ANDs an operator's selection with a person-level scope can still return rows from outside the caller's grants through people who also have records inside them.

Do

  • When adding a scope field to an input, find every place that constructs that input, not only the places that read it.
  • Intersect an operator's selection with the caller's grants before the query.
  • When a person is in scope through one of several child records, test each child record separately when walking them.

Don't

  • Assume that an export, a paging loop or a follow-up query inherits the scope of the first query.

How to check

  • Test each scoped list and export as two callers at the same level whose grants do not overlap. Each must see none of the other's records.
  • Run the same test with and without the optional selection the client normally sends.

CS-AUZ-010: Give every issued claim a token destination, and assert it in a decoded token

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Why

The authorization server drops a claim that has no destination when it issues the token. The code compiles and the server starts, so the first sign is an API that refuses a user who has the role.

Do

  • List every claim the server issues in one destinations map: access token, identity token or both.
  • Cover each issued claim with an acceptance test that requests a real token, decodes it and asserts the claim.

Don't

  • Add a claim without adding its destination.
  • Pass a destination as a positional argument without checking the overload; AddClaim(type, value, issuer) takes an issuer.

How to check

Every claim added in the change appears in the destinations map and in a decoded-token test.

CS-AUZ-011: Shape an open route's response to the reason it is open

Severity: major · Applies to: service, template · Enforced by: manual · Status: active

Why

A route is sometimes left open to every authenticated caller for a narrow reason, such as letting a picker resolve names. If it returns the full record, every caller reads fields that the gated routes protect.

Do

  • State next to the route why it is open, in a code comment or the policy name.
  • Return only the fields that reason needs, through a dedicated response type.

Don't

  • Reuse the full detail response on a route opened for lookup.

How to check

For each open read route in the change, compare the response fields with the stated reason; a test as a low-privilege caller asserts that the other fields are absent.

CS-AUZ-012: Test every gated feature as the least-privileged role that uses it

Severity: major · Applies to: service, blazor, template · Enforced by: manual · Status: active

Why

Administrators pass every gate and skip every scope filter. A feature tested only as an administrator hides refusals that are swallowed, filters in the wrong id space and enrichments that silently degrade for the roles that use them.

Do

  • Name the least-privileged role that uses the feature in its acceptance criteria.
  • Run at least one acceptance test of the feature as that role and assert the full result, not only a 200.

Don't

  • Accept a feature on the evidence of an administrator session alone.

How to check

The change's tests include a run as the least-privileged role that uses the feature.

CS-AUZ-013: Write tool policies against the exposed tool name

Severity: minor · Applies to: service · Enforced by: manual · Status: active

Why

The MCP SDK exposes a tool under a snake_case name unless the name is set. A policy written against the C# method name never matches, and the default-deny rule blocks the tool.

Do

  • Set the tool name explicitly, or write the policy against the name the server lists.
  • Test each allowed tool through the governed server.

Don't

  • Name a tool in a policy by its C# method name.

How to check

Every tool name in the changed policy appears in the server's tool list, and each allowed tool has a call test.

CS-AUZ-014: Compare ownership through one helper that rejects empty ids

Severity: blocker · Applies to: service, template · Enforced by: manual · Status: active

Why

A missing caller id coalesced to Guid.Empty equals an empty stored owner id, so an ownership check written inline fails open. Each inline copy has to get this right again.

Do

  • Compare ownership through one helper, such as IsOwnedBy(record, caller), that returns false when either id is missing or empty.
  • Cover the helper with tests for a missing caller id, an empty owner id and both.

Don't

  • Write caller.UserId ?? Guid.Empty in an ownership or self-scope comparison.

How to check

Search the change for ?? Guid.Empty near ownership or scope comparisons; each comparison calls the shared helper.