diff --git a/.agents/skills/security-defaults/SKILL.md b/.agents/skills/security-defaults/SKILL.md new file mode 100644 index 0000000..d789f92 --- /dev/null +++ b/.agents/skills/security-defaults/SKILL.md @@ -0,0 +1,31 @@ +--- +name: security-defaults +description: What this Lambda function does to be safe by default - IAM on direct invocations, throttling on the public HTTP API routes, and where an authorizer would go. Use when adding an HTTP route, changing throttling or access, or asked whether the function is safe to deploy. +metadata: + scope: presentation +--- + +# Security defaults (Lambda Function) + +## The rule + +The function has no authentication code of its own. AWS does the guarding, in `src/Presentation/serverless.template`, so keep it there rather than checking callers in a handler. + +| Protection | Where | Default | +| --- | --- | --- | +| Direct invocations | AWS IAM | Only callers with `lambda:InvokeFunction` | +| HTTP API routes | `Globals.HttpApi.DefaultRouteSettings` | Public, throttled to 50 requests a second, bursts of 100 | +| Compose ports on `127.0.0.1` | `docker-compose.yml` (`docker-and-compose`) | Always | + +The HTTP routes are read only and public on purpose, so a browser or `curl` can call them. The throttle is there so a busy caller can't run up the bill. It applies to every route on the stack's implicit HTTP API, so a new `[HttpApi]` function gets it without anything else. + +## Steps + +1. Change the limits in `Globals.HttpApi.DefaultRouteSettings`. Lambda Annotations rewrites the functions in `serverless.template` on build but leaves `Globals` alone. +2. To require a token, add a JWT authorizer to the HTTP API in `serverless.template` (see the AWS guide for HTTP API JWT authorizers) rather than reading headers in a handler. +3. Check the template still deploys: `cfn-lint src/Presentation/serverless.template`, if you have it, runs the same transform CloudFormation does. + +## Check + +- [ ] New HTTP routes go on the stack's HTTP API, so the throttle covers them. +- [ ] Nothing in a handler decides who may call it. diff --git a/AGENTS.md b/AGENTS.md index 76996d2..32eb00d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -130,6 +130,7 @@ These are for this Lambda function's presentation layer. The other templates hav | `api-conventions` | Name a function, shape its request or response, or decide how a failure reaches the caller | | `json-serialization` | Add a request or response type, or change how JSON looks | | `presentation-integration-tests` | Write a test that calls the handler | +| `security-defaults` | Add an HTTP route, change throttling or access, or check what the function does to stay safe | | `lambda-emulator` | Invoke the function by hand as Lambda would | diff --git a/README.md b/README.md index 3b7e4bf..5e633ca 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ When you're ready for the real thing, fill in your profile, region and stack nam The lists also come over HTTP. `HttpGetMovies`, `HttpGetAuthors` and `HttpGetReviews` sit behind an API Gateway HTTP API at `GET /movies`, `GET /authors` and `GET /reviews`, taking `query`, `offset` and `limit` (or `movieId` and `authorId`) from the query string. They answer `200` with the page, or `400` with problem details when the request doesn't add up. The deployed stack prints the URL as `ApiUrl`. -Direct invocations are guarded by AWS IAM, and the HTTP routes are open until you say otherwise. Want bearer tokens? Add a JWT authorizer to the HTTP API. Either way, the bouncer stands outside the door, not inside your code. +Direct invocations are guarded by AWS IAM. The HTTP routes are open until you say otherwise, but throttled to 50 requests a second with bursts of 100, so a keen caller can't run up your bill. Want bearer tokens? Add a [JWT authorizer](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-jwt-authorizer.html) to the HTTP API. Either way, the bouncer stands outside the door, not inside your code. ## What's inside diff --git a/docs/RUN.md b/docs/RUN.md index 158dcbd..fb4aecb 100644 --- a/docs/RUN.md +++ b/docs/RUN.md @@ -96,7 +96,7 @@ dotnet test ## Deploying to AWS -`src/Presentation/serverless.template` is a CloudFormation template with a Lambda function for each handler method, all running the same container image. Lambda Annotations writes each function into it when the project builds, so a new function appears there by itself. The three `Http*` functions also get a route on the stack's API Gateway HTTP API, and the stack's `ApiUrl` output is its address. Add any other resources you need beside them. +`src/Presentation/serverless.template` is a CloudFormation template with a Lambda function for each handler method, all running the same container image. Lambda Annotations writes each function into it when the project builds, so a new function appears there by itself. The three `Http*` functions also get a route on the stack's API Gateway HTTP API, and the stack's `ApiUrl` output is its address. The routes are public, so `Globals` throttles every one of them to 50 requests a second with bursts of 100; change `DefaultRouteSettings` to suit. To ask for a token, add a [JWT authorizer](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-jwt-authorizer.html) to the HTTP API. Add any other resources you need beside them. `src/Presentation/aws-lambda-tools-defaults.json` holds the defaults for the [Amazon.Lambda.Tools](https://github.com/aws/aws-extensions-for-dotnet-cli) CLI. Fill in your profile, region and stack name, then deploy the stack. The CLI builds the image from the Dockerfile, pushes it to Amazon ECR and creates or updates every function in one go. diff --git a/src/Presentation/serverless.template b/src/Presentation/serverless.template index 3d5bd20..8416a7c 100644 --- a/src/Presentation/serverless.template +++ b/src/Presentation/serverless.template @@ -2,6 +2,14 @@ "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Description": "The movie reviews functions: one container image, with a Lambda function for each handler method.", + "Globals": { + "HttpApi": { + "DefaultRouteSettings": { + "ThrottlingRateLimit": 50, + "ThrottlingBurstLimit": 100 + } + } + }, "Resources": { "GetMovies": { "Type": "AWS::Serverless::Function",