StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 5 · Middleware

Pipeline Order Repair

easy—No data yet

Problem statement

Exception handling, routing, authentication and endpoint middleware are registered in a broken order, so errors leak raw and protected endpoints are reachable. Re-order the pipeline in Program.cs so exception handling wraps everything, and authentication and authorization run between routing and endpoint execution.

Sample Data

The warehouse control panel is guarded by a single operator API key, control-room-key, supplied in the X-Api-Key request header.

Every stage records its own entry in IExecutionLog as a request passes through it. Each panel and health endpoint takes a tag query value, and GET /trace/{tag} hands back the entries recorded under that tag, in the order they were written. Here is an example of a trace:

[
  "error-middleware:entered",
  "endpoint:health-ping:started",
  "endpoint:health-ping:completed"
]

Service

The Pipeline

Program.cs builds the request pipeline from five calls, registered in the wrong order:

  • UseErrorHandling records error-middleware:entered as it hands the request on, converts a failure raised anywhere further down into the error answer below, and records error-middleware:converted when it does.
  • UseRouting resolves the request to an endpoint.
  • UseAuthentication runs the API key scheme, which reads X-Api-Key and settles who the caller is for the endpoint routing resolved.
  • UseApiKeyAuthorization reads the RequireApiKey marker from the resolved endpoint, records authorization:guarding when it finds one, and challenges an unauthenticated caller before that endpoint runs.
  • UseEndpoints runs the endpoint, which records that it started and records that it completed unless it fails first.

The four panel endpoints /panel/status, /panel/report, /panel/scan and /panel/calibrate carry the RequireApiKey marker. /health/ping and /trace/{tag} carry none, so they answer callers who supply no key at all. /panel/scan and /panel/calibrate record that they started and then fail, which is what the error stage is there to convert.

The API Key Scheme

  • A request carrying the operator key is authenticated and the scheme records auth-handler:authenticated, whether or not the endpoint it is bound for carries the marker.
  • A request carrying no X-Api-Key header, or one holding a key the scheme does not know, is not authenticated. When it is bound for a marked endpoint the authorization stage challenges it: the scheme records auth-handler:challenged, and the answer is 401 with the response header WWW-Authenticate: ApiKey. That endpoint never runs, so it records nothing at all.
  • A request carrying more than one X-Api-Key value is ambiguous, and the scheme fails instead of settling the caller. The error stage converts that failure like any other, and no endpoint entry is recorded for it.

The Error Answer

A converted failure is answered 500 as application/json, with a body holding error, always the text Unhandled server error, and path, the path of the request that failed.

Program

Re-order the five calls in Program.cs. Nothing else changes, no call is added and none is removed. The order is right when the error stage is the first of the five every request meets, and authentication and authorization both run after routing has resolved the endpoint and before that endpoint runs.

Sample Requests & Responses

Each call below is followed by the trace GET /trace/{tag} reports for it, which always answers 200.

GET /panel/status?tag=t1 with the request header X-Api-Key: control-room-key

Response code: 200

{ "panel": "status", "state": "ready" }

Trace: error-middleware:entered, auth-handler:authenticated, authorization:guarding, endpoint:panel-status:started, endpoint:panel-status:completed

GET /panel/report?tag=t6 with the same header is answered the same way, recording endpoint:panel-report entries in place of the endpoint:panel-status ones, with the body:

{ "panel": "report", "state": "generated" }

GET /panel/status?tag=t2 with no X-Api-Key request header

Response code: 401, with the response header WWW-Authenticate: ApiKey and no body.

Trace: error-middleware:entered, authorization:guarding, auth-handler:challenged

An unknown key is refused the same way, and so are /panel/scan and /panel/calibrate, which never reach the failure they would otherwise raise.

GET /panel/scan?tag=t3 with the request header X-Api-Key: control-room-key

Response code: 500

{ "error": "Unhandled server error", "path": "/panel/scan" }

Trace: error-middleware:entered, auth-handler:authenticated, authorization:guarding, endpoint:panel-scan:started, error-middleware:converted

/panel/calibrate is answered the same way, reporting its own path and recording endpoint:panel-calibrate:started.

GET /panel/status?tag=t4 with the request header X-Api-Key supplied twice

The scheme itself fails before the request is routed, so nothing between the error stage and the failure records anything.

Response code: 500

{ "error": "Unhandled server error", "path": "/panel/status" }

Trace: error-middleware:entered, error-middleware:converted

GET /health/ping?tag=t5 with no X-Api-Key request header

Response code: 200

{ "panel": "ping", "state": "awake" }

Trace: error-middleware:entered, endpoint:health-ping:started, endpoint:health-ping:completed

GET /health/ping?tag=t8 with the request header X-Api-Key: control-room-key answers the same body, but the scheme settles the caller even though the endpoint carries no marker, so the trace gains auth-handler:authenticated after error-middleware:entered.

More in Middleware

See the module
Request Timing LoggerThis problem covers timing every request as it passes through the application and leaving one log entry for it, recording the method, the path, the status it ended with and how long it took, without changing the answer itself.0%0 of 1 solvedeasyMaintenance Mode SwitchThis problem covers putting a switch in front of the whole application, so that while it is on every request is turned away with the same notice and told when to come back, and while it is off nothing about the normal answers changes.—No data yeteasyCorrelation ID PipelineThis problem covers giving every request one id that follows it all the way through the application, so each stage and each log line reports the same one, and it comes back on the response whatever happened to the request.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.