English
Checkpoint
What must work
powershell
dotnet buildpowershell
dotnet test --solution Chatex.slnxCommit:
powershell
git add .powershell
git commit -m "Add file endpoints, rate limiting, health checks and maintenance to the API"Common mistakes
Table 13.1. Common mistakes in files, limits and operations
| Symptom | Cause | Fix |
|---|---|---|
| uploads fail with an error about antiforgery | form-bound endpoints require antiforgery by default | DisableAntiforgery() in AcceptsFileUpTo |
a 3 MB avatar is rejected with 413 Payload Too Large | the global 1 MB body limit applies to the endpoint | add .AcceptsFileUpTo(AvatarLimits.MaxBytes) |
requests to /api/auth fail with an exception about a missing rate limiting policy | the group requires the policy, but AddChatexRateLimiting is not called | register it in AddApi and add UseRateLimiter() |
tests fail with 429 in unrelated classes | the test limit is the real 20 per minute | set RateLimiting:AuthPermitLimit high in ApiTestBase |
| on the server all users are throttled together | the forwarded headers middleware is missing or not first | UseForwardedHeaders() before everything else |
/health takes half a minute to fail | the check went through EF Core's retries | keep the plain-connection check of topic 5 |
the cleanup logs Cannot resolve scoped service | a scoped service is taken from the root provider in the hosted service | create a scope per pass |
Self-check questions
- Which two limits protect the server from large uploads, and why are both needed?
- Why does the API sniff the type of an avatar instead of storing the client's media type?
- What do
X-Content-Type-Options: nosniffandContent-Disposition: attachmentprotect against? - Why are uploads exempt from antiforgery checks in Chatex?
- What attacks does rate limiting of the auth endpoints stop that the account lockout does not?
- Why must the forwarded headers middleware run first behind Nginx?
- What does
/healthcheck, who calls it, and why is it anonymous? - Why does a hosted service create its own scope for each pass?
- Why are the Data Protection keys stored in a folder on the server?
- How does a test class change one setting of the application under test?
Exercises
- A second policy. Add a policy
uploadsthat allows 30 uploads per user (partition by thesubclaim) per minute and apply it toPOST /api/attachments. Write a test with a lower limit. - More image types. Teach
ImageSnifferto recognise GIF (47 49 46 38). Which other file would you have to change so that GIF avatars are accepted at all? - Liveness and readiness. Kubernetes distinguishes "the process is alive" from "it can serve". Map
/health/livethat never touches the database next to the existing/health. Why might a deployment want both?