Public: anyone can read this; the crew can change it.
AWS writes for mail setup are a named allowlist beside the read rule, and nothing else #18142
Merged 7h 5m ago, live in production
scout/forge:scout/18134-aws-writes-for-mail-setup-are-a-named-al → main 0 commits fork
4 files
changed +138
−6
src/cloud/aws_call.ts +53 −3
| @@ -6,7 +6,9 @@ | ||
| 6 | 6 | * answer (DescribeRegions' `regionEndpoint` is ignored) or an argument. `forge.json` names no |
| 7 | 7 | * `*.amazonaws.com`: below that name are strangers' machines, buckets and load balancers. |
| 8 | 8 | * - **The verb.** A GET, or a POST whose operation is named Describe, List or Get. Anything else |
| 9 | * throws before it is signed, so no caller can create, change or enroll. | |
| 9 | * throws before it is signed, so no caller can create, change or enroll, except the named | |
| 10 | * `WRITES` for mail setup (#18134), and those only when the caller says the attached account has | |
| 11 | * writes allowed. A list of exact service:operation pairs, never a pattern. | |
| 10 | 12 | * |
| 11 | 13 | * A redirect is refused, so a signed request never follows one to another host. |
| 12 | 14 | */ |
| @@ -30,6 +32,8 @@ const HOST = { | ||
| 30 | 32 | sts: () => "sts.amazonaws.com", |
| 31 | 33 | route53: () => "route53.amazonaws.com", |
| 32 | 34 | ce: () => "ce.us-east-1.amazonaws.com", |
| 35 | ses: (r: string) => `email.${r}.amazonaws.com`, | |
| 36 | iam: () => "iam.amazonaws.com", | |
| 33 | 37 | } as const; |
| 34 | 38 | export type AwsService = keyof typeof HOST; |
| 35 | 39 | |
| @@ -37,6 +41,41 @@ export type AwsService = keyof typeof HOST; | ||
| 37 | 41 | export const AWS_REGION = /^[a-z]{2}(-[a-z]+){1,3}-\d{1,2}$/; |
| 38 | 42 | const READS = /^(Describe|List|Get)[A-Z][A-Za-z]*$/; |
| 39 | 43 | |
| 44 | /** | |
| 45 | * The only writes: mail setup and its undo. Exact `service:Operation`; a write not here is refused. | |
| 46 | * SES's own writes (CreateEmailIdentity, PutAccountDetails, DeleteEmailIdentity) are REST in SES v2 | |
| 47 | * (`POST /v2/email/identities` and so on), which this transport cannot sign yet (GET and POST | |
| 48 | * only); they are listed here when it can, not before. IAM is the query protocol and is real. | |
| 49 | */ | |
| 50 | const WRITES: ReadonlySet<string> = new Set([ | |
| 51 | "iam:CreateUser", | |
| 52 | "iam:PutUserPolicy", | |
| 53 | "iam:CreateAccessKey", | |
| 54 | "iam:DeleteUser", | |
| 55 | "iam:DeleteUserPolicy", | |
| 56 | "iam:DeleteAccessKey", | |
| 57 | ]); | |
| 58 | ||
| 59 | /** The send-only user's policy: ses:SendRawEmail from the one identity, nothing else. */ | |
| 60 | export function sendOnlyPolicy(identityArn: string): string { | |
| 61 | return JSON.stringify({ | |
| 62 | Version: "2012-10-17", | |
| 63 | Statement: [{ Effect: "Allow", Action: "ses:SendRawEmail", Resource: identityArn }], | |
| 64 | }); | |
| 65 | } | |
| 66 | ||
| 67 | const IDENTITY_ARN = /^arn:aws:ses:[a-z0-9-]+:\d{12}:identity\/[A-Za-z0-9.@_-]{1,255}$/; | |
| 68 | ||
| 69 | /** A PutUserPolicy document is held to `sendOnlyPolicy` of one SES identity ARN. */ | |
| 70 | function sendOnly(doc: string | undefined): boolean { | |
| 71 | try { | |
| 72 | const arn = (JSON.parse(doc ?? "") as { Statement?: [{ Resource?: unknown }] }).Statement?.[0]?.Resource; | |
| 73 | return typeof arn === "string" && IDENTITY_ARN.test(arn) && doc === sendOnlyPolicy(arn); | |
| 74 | } catch { | |
| 75 | return false; | |
| 76 | } | |
| 77 | } | |
| 78 | ||
| 40 | 79 | /** |
| 41 | 80 | * What is asked: the query protocol (XML back), the JSON one, or a REST GET (S3, Route 53, Lambda). |
| 42 | 81 | */ |
| @@ -72,10 +111,21 @@ function codeOf(text: string): string { | ||
| 72 | 111 | return /^[A-Za-z0-9._]+$/.test(raw) ? raw : ""; |
| 73 | 112 | } |
| 74 | 113 | |
| 75 | export async function awsCall(f: Fetch, key: AwsKey, service: AwsService, region: string, ask: AwsAsk): Promise<string> { | |
| 114 | /** | |
| 115 | * `writes`: the attached account has writes allowed (`accountCall` sets it from the account). | |
| 116 | * Without it, every call is a read. IAM answers from one host and signs for us-east-1, so an iam | |
| 117 | * call in any other region is refused here rather than failing as a bad signature. | |
| 118 | */ | |
| 119 | export async function awsCall(f: Fetch, key: AwsKey, service: AwsService, region: string, ask: AwsAsk, writes = false): Promise<string> { | |
| 76 | 120 | if (!AWS_REGION.test(region)) throw new Error("not an AWS region"); |
| 77 | 121 | const operation = "action" in ask ? ask.action : "target" in ask ? (ask.target.split(".").at(-1) ?? "") : "GetRest"; |
| 78 | if (!READS.test(operation)) throw new Error(`${operation} is not a read: the forge only reads AWS`); | |
| 122 | if (!READS.test(operation)) { | |
| 123 | if (!WRITES.has(`${service}:${operation}`)) throw new Error(`${operation} is not a read: the forge only reads AWS`); | |
| 124 | if (!writes) throw new Error(`${operation} is a write: this AWS account is read-only`); | |
| 125 | if (service === "iam" && operation === "PutUserPolicy" && !("action" in ask && sendOnly(ask.params?.["PolicyDocument"]))) | |
| 126 | throw new Error("PutUserPolicy allows only ses:SendRawEmail from one identity"); | |
| 127 | } | |
| 128 | if (service === "iam" && region !== "us-east-1") throw new Error("iam is signed for us-east-1 only"); | |
| 79 | 129 | const host = HOST[service](region); |
| 80 | 130 | const form = "action" in ask ? queryText({ ...ask.params, Action: ask.action, Version: ask.version }) : ""; |
| 81 | 131 | const r = { |
src/cloud/aws_read.ts +10 −2
| @@ -381,18 +381,26 @@ function linked(things: readonly Part[], imagesRead: boolean): Clued[] { | ||
| 381 | 381 | }); |
| 382 | 382 | } |
| 383 | 383 | |
| 384 | /** | |
| 385 | * The calls of one attached account: writes go through only when the account has them allowed | |
| 386 | * (`writeAllowed`, the forge's per-account switch); a read-only account's call refuses every write. | |
| 387 | */ | |
| 388 | export function accountCall(f: Fetch, key: AwsKey, writeAllowed: boolean): Call { | |
| 389 | return (service, region, ask) => awsCall(f, key, service, region, ask, writeAllowed); | |
| 390 | } | |
| 391 | ||
| 384 | 392 | /** |
| 385 | 393 | * Read the account whole. `known` is the regions kept before, read again when the list of regions |
| 386 | 394 | * cannot be. Throws when the key itself is refused or not one thing could be read: that is the |
| 387 | 395 | * account's failure, and everything kept stays. |
| 388 | 396 | */ |
| 389 | export async function readAws(f: Fetch, key: AwsKey, known: readonly string[]): Promise<Inventory> { | |
| 397 | export async function readAws(f: Fetch, key: AwsKey, known: readonly string[], writeAllowed = false): Promise<Inventory> { | |
| 390 | 398 | const things: (Clued & { readonly project: string })[] = []; |
| 391 | 399 | const gaps: Gap[] = []; |
| 392 | 400 | const owned: Owned[] = []; |
| 393 | 401 | const [groups, cards] = [new Map<string, string[]>(), new Map<string, string>()]; |
| 394 | 402 | let answered = 0; |
| 395 | const call: Call = (service, region, ask) => awsCall(f, key, service, region, ask); | |
| 403 | const call = accountCall(f, key, writeAllowed); | |
| 396 | 404 | let regions: string[]; |
| 397 | 405 | try { |
| 398 | 406 | regions = listed(parseXml(await call("ec2", HOME, { action: "DescribeRegions", version: EC2 })), "regionInfo").map((r) => val(r, "regionName")).filter((r) => AWS_REGION.test(r)); |
src/cloud/projects.ts +1 −1
| @@ -137,7 +137,7 @@ async function readWhole(db: Database, provider: string, account: string, f: Fet | ||
| 137 | 137 | if (provider !== "aws") throw new Error(`${provider} is not read into projects`); |
| 138 | 138 | const key = awsKey(token); |
| 139 | 139 | if (key === undefined) throw new Error("the key is not ACCESS_KEY_ID:SECRET_ACCESS_KEY"); |
| 140 | return ofInventory(await readAws(f, key, keptProjects(db, provider, account).map((p) => p.id))); | |
| 140 | return ofInventory(await readAws(f, key, keptProjects(db, provider, account).map((p) => p.id), getCloudAccount(db, provider, account)?.writeAllowed === true)); | |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | const DO = "https://api.digitalocean.com/v2"; |
test/aws-writes.test.ts added +74
| @@ -0,0 +1,74 @@ | ||
| 1 | /** | |
| 2 | * The named AWS writes for mail setup (#18134): each allowed on a writable account, refused on a | |
| 3 | * read-only one. | |
| 4 | */ | |
| 5 | import { describe, expect, test } from "bun:test"; | |
| 6 | import { awsCall, sendOnlyPolicy } from "../src/cloud/aws_call.ts"; | |
| 7 | import { accountCall } from "../src/cloud/aws_read.ts"; | |
| 8 | ||
| 9 | const KEY = { id: "AKIDEXAMPLE", secret: "SECRETSECRETSECRET" }; | |
| 10 | const ARN = "arn:aws:ses:us-east-1:123456789012:identity/mail.example.com"; | |
| 11 | const POLICY = sendOnlyPolicy(ARN); | |
| 12 | const never = async (): Promise<Response> => { | |
| 13 | throw new Error("sent"); | |
| 14 | }; | |
| 15 | const ok = async (): Promise<Response> => new Response("<ok/>"); | |
| 16 | ||
| 17 | const iam = (action: string, params: Record<string, string> = {}) => ({ action, version: "2010-05-08", params }); | |
| 18 | const LISTED: [string, "iam", Parameters<typeof awsCall>[4]][] = [ | |
| 19 | ["CreateUser", "iam", iam("CreateUser", { UserName: "send" })], | |
| 20 | ["PutUserPolicy", "iam", iam("PutUserPolicy", { UserName: "send", PolicyName: "p", PolicyDocument: POLICY })], | |
| 21 | ["CreateAccessKey", "iam", iam("CreateAccessKey", { UserName: "send" })], | |
| 22 | ["DeleteUser", "iam", iam("DeleteUser", { UserName: "send" })], | |
| 23 | ["DeleteUserPolicy", "iam", iam("DeleteUserPolicy", { UserName: "send", PolicyName: "p" })], | |
| 24 | ["DeleteAccessKey", "iam", iam("DeleteAccessKey", { UserName: "send", AccessKeyId: "AKIA1" })], | |
| 25 | ]; | |
| 26 | ||
| 27 | describe("named writes", () => { | |
| 28 | for (const [name, service, ask] of LISTED) { | |
| 29 | test(`${name}: sent on a writable account, refused on a read-only one`, async () => { | |
| 30 | expect(await awsCall(ok, KEY, service, "us-east-1", ask, true)).toBe("<ok/>"); | |
| 31 | await expect(awsCall(never, KEY, service, "us-east-1", ask)).rejects.toThrow("read-only"); | |
| 32 | await expect(awsCall(never, KEY, service, "us-east-1", ask, false)).rejects.toThrow("read-only"); | |
| 33 | }); | |
| 34 | } | |
| 35 | test("SES writes are not listed until REST can send them", async () => { | |
| 36 | const ses = { target: "SimpleEmailService.CreateEmailIdentity", json: "1.1" as const, body: {} }; | |
| 37 | await expect(awsCall(never, KEY, "ses", "us-east-1", ses, true)).rejects.toThrow("CreateEmailIdentity is not a read"); | |
| 38 | }); | |
| 39 | test("iam is signed for us-east-1 only", async () => { | |
| 40 | await expect(awsCall(never, KEY, "iam", "eu-west-1", iam("CreateUser"), true)).rejects.toThrow("us-east-1"); | |
| 41 | }); | |
| 42 | test("an account's call sends a write only when the account has writes allowed", async () => { | |
| 43 | const ask = iam("CreateUser", { UserName: "send" }); | |
| 44 | await expect(accountCall(never, KEY, false)("iam", "us-east-1", ask)).rejects.toThrow("read-only"); | |
| 45 | expect(await accountCall(ok, KEY, true)("iam", "us-east-1", ask)).toBe("<ok/>"); | |
| 46 | expect(await accountCall(ok, KEY, false)("iam", "us-east-1", iam("ListUsers"))).toBe("<ok/>"); | |
| 47 | }); | |
| 48 | test("an unlisted write is refused by name, even on a writable account", async () => { | |
| 49 | await expect( | |
| 50 | awsCall(never, KEY, "s3", "us-east-1", { target: "S3.DeleteBucket", json: "1.1", body: {} }, true), | |
| 51 | ).rejects.toThrow("DeleteBucket is not a read"); | |
| 52 | await expect(awsCall(never, KEY, "iam", "us-east-1", iam("AttachUserPolicy"), true)).rejects.toThrow("AttachUserPolicy is not a read"); | |
| 53 | // The list is exact pairs: a listed name under another service is not listed. | |
| 54 | await expect(awsCall(never, KEY, "ec2", "us-east-1", iam("CreateUser"), true)).rejects.toThrow("CreateUser is not a read"); | |
| 55 | }); | |
| 56 | test("a user policy is send-only from one identity, or refused", async () => { | |
| 57 | const bad = [ | |
| 58 | JSON.stringify({ Version: "2012-10-17", Statement: [{ Effect: "Allow", Action: "*", Resource: ARN }] }), | |
| 59 | sendOnlyPolicy("*"), | |
| 60 | sendOnlyPolicy("arn:aws:s3:::bucket"), | |
| 61 | POLICY.replace("ses:SendRawEmail", "ses:*"), | |
| 62 | "not json", | |
| 63 | ]; | |
| 64 | for (const PolicyDocument of bad) | |
| 65 | await expect(awsCall(never, KEY, "iam", "us-east-1", iam("PutUserPolicy", { PolicyDocument }), true)).rejects.toThrow("only ses:SendRawEmail"); | |
| 66 | }); | |
| 67 | test("no error holds the credential", async () => { | |
| 68 | const e = await awsCall(never, KEY, "iam", "us-east-1", iam("CreateUser")).catch((x: Error) => x.message); | |
| 69 | expect(String(e)).not.toContain(KEY.secret); | |
| 70 | const denied = async (): Promise<Response> => new Response("<Code>AccessDenied</Code>", { status: 403 }); | |
| 71 | const d = await awsCall(denied, KEY, "iam", "us-east-1", iam("CreateUser"), true).catch((x: Error) => x.message); | |
| 72 | expect(String(d)).not.toContain(KEY.secret); | |
| 73 | }); | |
| 74 | }); |
new above
offline