@anthusai/papyrus 1.0.0-next.50 → 1.0.0-next.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,7 @@ import { emailSubmissionProcessor } from "../functions/email-submission-processo
12
12
  import { sesInboundReceive } from "../functions/ses-inbound-receive/resource.js";
13
13
  import { slackDelivery } from "../functions/slack-delivery/resource.js";
14
14
  import { slackEvents } from "../functions/slack-events/resource.js";
15
- import { schema as leanSchema } from "./schema.js";
15
+ import { schema as leanSchema, schemaWithPlugins } from "./schema.js";
16
16
  const authoringOperations = [
17
17
  "read",
18
18
  "create",
@@ -2009,13 +2009,19 @@ const schema = a.schema({
2009
2009
  allow.resource(slackEvents),
2010
2010
  allow.resource(slackDelivery),
2011
2011
  ]);
2012
- export const data = defineData({
2013
- name: "PapyrusCmsApi",
2014
- schema: leanSchema,
2015
- authorizationModes: {
2016
- defaultAuthorizationMode: "userPool",
2017
- apiKeyAuthorizationMode: {
2018
- expiresInDays: 365,
2012
+ /**
2013
+ * The site's data API. Amplify allows one `defineData` per backend and registers it when called, so this is
2014
+ * a function: a site without plugins passes none and gets exactly the base schema.
2015
+ */
2016
+ export function defineSiteData(pluginDefinitions = {}) {
2017
+ return defineData({
2018
+ name: "PapyrusCmsApi",
2019
+ schema: Object.keys(pluginDefinitions).length === 0 ? leanSchema : schemaWithPlugins(pluginDefinitions),
2020
+ authorizationModes: {
2021
+ defaultAuthorizationMode: "userPool",
2022
+ apiKeyAuthorizationMode: {
2023
+ expiresInDays: 365,
2024
+ },
2019
2025
  },
2020
- },
2021
- });
2026
+ });
2027
+ }
@@ -12,7 +12,7 @@ import { emailSubmissionProcessor } from "../functions/email-submission-processo
12
12
  import { sesInboundReceive } from "../functions/ses-inbound-receive/resource";
13
13
  import { slackDelivery } from "../functions/slack-delivery/resource";
14
14
  import { slackEvents } from "../functions/slack-events/resource";
15
- import { schema as leanSchema } from "./schema";
15
+ import { schema as leanSchema, schemaWithPlugins } from "./schema";
16
16
  const authoringOperations: ("read" | "create" | "update" | "delete")[] = [
17
17
  "read",
18
18
  "create",
@@ -2125,13 +2125,19 @@ const schema = a.schema({
2125
2125
 
2126
2126
  export type Schema = ClientSchema<typeof schema>;
2127
2127
 
2128
- export const data = defineData({
2129
- name: "PapyrusCmsApi",
2130
- schema: leanSchema,
2131
- authorizationModes: {
2132
- defaultAuthorizationMode: "userPool",
2133
- apiKeyAuthorizationMode: {
2134
- expiresInDays: 365,
2128
+ /**
2129
+ * The site's data API. Amplify allows one `defineData` per backend and registers it when called, so this is
2130
+ * a function: a site without plugins passes none and gets exactly the base schema.
2131
+ */
2132
+ export function defineSiteData(pluginDefinitions: Record<string, unknown> = {}) {
2133
+ return defineData({
2134
+ name: "PapyrusCmsApi",
2135
+ schema: Object.keys(pluginDefinitions).length === 0 ? leanSchema : schemaWithPlugins(pluginDefinitions),
2136
+ authorizationModes: {
2137
+ defaultAuthorizationMode: "userPool",
2138
+ apiKeyAuthorizationMode: {
2139
+ expiresInDays: 365,
2140
+ },
2135
2141
  },
2136
- },
2137
- });
2142
+ });
2143
+ }
@@ -20,7 +20,7 @@ const categoryWriteGroups = ["editor", "admin"];
20
20
  const categoryAppendOnlyOperations = ["read", "create"];
21
21
  const modelWithoutSubscriptions = (fields) => a.model(fields).disableOperations(["subscriptions"]);
22
22
  const realtimeModel = (fields) => a.model(fields);
23
- export const schema = a.schema({
23
+ const baseDefinitions = {
24
24
  UserProfile: modelWithoutSubscriptions({
25
25
  id: a.id().required(),
26
26
  email: a.email(),
@@ -1841,7 +1841,8 @@ export const schema = a.schema({
1841
1841
  allow.guest().to(["read"]),
1842
1842
  allow.groups(contentWriteGroups),
1843
1843
  ]),
1844
- }).authorization((allow) => [
1844
+ };
1845
+ export const schema = a.schema(baseDefinitions).authorization((allow) => [
1845
1846
  allow.resource(categoryAction),
1846
1847
  allow.resource(contentActions),
1847
1848
  allow.resource(manageUserRole),
@@ -1849,3 +1850,14 @@ export const schema = a.schema({
1849
1850
  allow.resource(newsroomSummary),
1850
1851
  allow.resource(procedureAction),
1851
1852
  ]);
1853
+ /** The deployed schema of a site that enables plugins: the base definitions plus what each plugin contributes (docs/plugins.md). */
1854
+ export function schemaWithPlugins(pluginDefinitions) {
1855
+ return a.schema({ ...baseDefinitions, ...pluginDefinitions }).authorization((allow) => [
1856
+ allow.resource(categoryAction),
1857
+ allow.resource(contentActions),
1858
+ allow.resource(manageUserRole),
1859
+ allow.resource(modelAttachmentUpload),
1860
+ allow.resource(newsroomSummary),
1861
+ allow.resource(procedureAction),
1862
+ ]);
1863
+ }
@@ -23,7 +23,7 @@ const modelWithoutSubscriptions = <Fields extends Parameters<typeof a.model>[0]>
23
23
  a.model(fields).disableOperations(["subscriptions"]);
24
24
  const realtimeModel = <Fields extends Parameters<typeof a.model>[0]>(fields: Fields) => a.model(fields);
25
25
 
26
- export const schema = a.schema({
26
+ const baseDefinitions = {
27
27
  UserProfile: modelWithoutSubscriptions({
28
28
  id: a.id().required(),
29
29
  email: a.email(),
@@ -1947,7 +1947,9 @@ export const schema = a.schema({
1947
1947
  allow.guest().to(["read"]),
1948
1948
  allow.groups(contentWriteGroups),
1949
1949
  ]),
1950
- }).authorization((allow) => [
1950
+ };
1951
+
1952
+ export const schema = a.schema(baseDefinitions).authorization((allow) => [
1951
1953
  allow.resource(categoryAction),
1952
1954
  allow.resource(contentActions),
1953
1955
  allow.resource(manageUserRole),
@@ -1956,4 +1958,16 @@ export const schema = a.schema({
1956
1958
  allow.resource(procedureAction),
1957
1959
  ]);
1958
1960
 
1961
+ /** The deployed schema of a site that enables plugins: the base definitions plus what each plugin contributes (docs/plugins.md). */
1962
+ export function schemaWithPlugins(pluginDefinitions: Record<string, any>) {
1963
+ return a.schema({ ...baseDefinitions, ...pluginDefinitions } as typeof baseDefinitions).authorization((allow) => [
1964
+ allow.resource(categoryAction),
1965
+ allow.resource(contentActions),
1966
+ allow.resource(manageUserRole),
1967
+ allow.resource(modelAttachmentUpload),
1968
+ allow.resource(newsroomSummary),
1969
+ allow.resource(procedureAction),
1970
+ ]);
1971
+ }
1972
+
1959
1973
  export type Schema = ClientSchema<typeof schema>;
@@ -0,0 +1,19 @@
1
+ import { signupPlugin } from "./signup/papyrus.plugin.js";
2
+ /** Plugins are packaged with Papyrus and pinned by its version: there is no registry to fetch from. */
3
+ export const KNOWN_PLUGINS = {
4
+ signup: signupPlugin,
5
+ };
6
+ export function resolvePlugins(requests) {
7
+ const resolved = [];
8
+ for (const request of requests ?? []) {
9
+ const name = typeof request === "string" ? request : request.name;
10
+ const plugin = KNOWN_PLUGINS[name];
11
+ if (plugin === undefined) {
12
+ throw new Error(`Unknown plugin "${name}". Known plugins: ${Object.keys(KNOWN_PLUGINS).join(", ")}`);
13
+ }
14
+ if (resolved.some((entry) => entry.plugin.manifest.name === name))
15
+ throw new Error(`Plugin "${name}" is listed twice.`);
16
+ resolved.push({ plugin, config: plugin.resolveConfig(request) });
17
+ }
18
+ return resolved;
19
+ }
@@ -0,0 +1,23 @@
1
+ import type { PapyrusPlugin, PluginRequest } from "./types";
2
+ import { signupPlugin } from "./signup/papyrus.plugin";
3
+
4
+ /** Plugins are packaged with Papyrus and pinned by its version: there is no registry to fetch from. */
5
+ export const KNOWN_PLUGINS: Record<string, PapyrusPlugin<any, any>> = {
6
+ signup: signupPlugin,
7
+ };
8
+
9
+ export type ResolvedPlugin = { plugin: PapyrusPlugin<any, any>; config: unknown };
10
+
11
+ export function resolvePlugins(requests: PluginRequest[] | undefined): ResolvedPlugin[] {
12
+ const resolved: ResolvedPlugin[] = [];
13
+ for (const request of requests ?? []) {
14
+ const name = typeof request === "string" ? request : request.name;
15
+ const plugin = KNOWN_PLUGINS[name];
16
+ if (plugin === undefined) {
17
+ throw new Error(`Unknown plugin "${name}". Known plugins: ${Object.keys(KNOWN_PLUGINS).join(", ")}`);
18
+ }
19
+ if (resolved.some((entry) => entry.plugin.manifest.name === name)) throw new Error(`Plugin "${name}" is listed twice.`);
20
+ resolved.push({ plugin, config: plugin.resolveConfig(request) });
21
+ }
22
+ return resolved;
23
+ }
@@ -0,0 +1,200 @@
1
+ """submitSignup: the only operation of the signup plugin that a guest can call.
2
+
3
+ A guest sends a form name and an email-like value. This handler throttles the
4
+ caller, validates and length-limits every argument, drops honeypot and too-fast
5
+ submissions without telling the sender, and writes one SignupEntry through the
6
+ GraphQL API under an id derived from the content, so repeating a submission
7
+ changes nothing. Guests never read entries back.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import hashlib
13
+ import json
14
+ import os
15
+ import re
16
+ import time
17
+ import urllib.error
18
+ import urllib.request
19
+ from dataclasses import dataclass
20
+ from datetime import datetime, timezone
21
+ from typing import Any, Callable
22
+
23
+ STATUS_ACCEPTED = "accepted"
24
+ STATUS_INVALID = "invalid"
25
+ STATUS_EXPIRED = "expired"
26
+ STATUS_THROTTLED = "throttled"
27
+
28
+ MAXIMUM_ARGUMENT_CHARACTERS = 1000
29
+ MAXIMUM_VALUE_CHARACTERS = 254
30
+ MINIMUM_FILL_SECONDS = 2
31
+ MAXIMUM_AGE_SECONDS = 24 * 60 * 60
32
+ RATE_WINDOW_SECONDS = 60
33
+ COUNTER_LIFETIME_SECONDS = 5 * 60
34
+ DEFAULT_ADDRESS_LIMIT_PER_WINDOW = 20
35
+ DEFAULT_IDENTITY_LIMIT_PER_WINDOW = 5
36
+ FORBIDDEN_FIRST_CHARACTERS = "=+-@"
37
+ FORM_NAME = re.compile(r"[a-z0-9][a-z0-9-]{0,39}")
38
+ RENDERED_AT = re.compile(r"[0-9]{1,16}")
39
+ LABEL = r"[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?"
40
+ EMAIL_LIKE = re.compile(rf"[A-Za-z0-9.!#$%&'*+/=?^_`{{|}}~-]{{1,64}}@{LABEL}(?:\.{LABEL})+")
41
+ CREATE_ENTRY_MUTATION = (
42
+ "mutation CreateSignupEntry($input: CreateSignupEntryInput!) { createSignupEntry(input: $input) { id } }"
43
+ )
44
+ ALREADY_STORED_MARKERS = ("ConditionalCheckFailed", "conditional request failed")
45
+
46
+
47
+ @dataclass(frozen=True)
48
+ class SignupSettings:
49
+ forms: frozenset[str]
50
+ address_limit: int = DEFAULT_ADDRESS_LIMIT_PER_WINDOW
51
+ identity_limit: int = DEFAULT_IDENTITY_LIMIT_PER_WINDOW
52
+
53
+
54
+ def settings_from_environment(environment: dict[str, str] | None = None) -> SignupSettings:
55
+ environment = os.environ if environment is None else environment
56
+ forms = frozenset(name.strip() for name in environment.get("PAPYRUS_SIGNUP_FORMS", "").split(",") if name.strip())
57
+ return SignupSettings(
58
+ forms=forms,
59
+ address_limit=int(environment.get("PAPYRUS_SIGNUP_ADDRESS_LIMIT", DEFAULT_ADDRESS_LIMIT_PER_WINDOW)),
60
+ identity_limit=int(environment.get("PAPYRUS_SIGNUP_IDENTITY_LIMIT", DEFAULT_IDENTITY_LIMIT_PER_WINDOW)),
61
+ )
62
+
63
+
64
+ def result(status: str, message: str = "") -> dict[str, str]:
65
+ return {"status": status, "message": message}
66
+
67
+
68
+ def normalized_value(value: str) -> str:
69
+ local, _, domain = value.partition("@")
70
+ return f"{local}@{domain.lower()}"
71
+
72
+
73
+ def entry_identifier(form: str, value: str) -> str:
74
+ return hashlib.sha256(f"{form}\n{normalized_value(value)}".encode("utf-8")).hexdigest()[:32]
75
+
76
+
77
+ def valid_value(value: str) -> bool:
78
+ return (
79
+ 0 < len(value) <= MAXIMUM_VALUE_CHARACTERS
80
+ and value[0] not in FORBIDDEN_FIRST_CHARACTERS
81
+ and EMAIL_LIKE.fullmatch(value) is not None
82
+ )
83
+
84
+
85
+ def caller_address(identity: dict[str, Any] | None) -> str:
86
+ """The last of AppSync's sourceIp list: the connection's address. A sender-supplied x-forwarded-for only adds earlier entries."""
87
+ addresses = [str(address).strip() for address in (identity or {}).get("sourceIp") or [] if str(address).strip()]
88
+ return addresses[-1] if addresses else "unknown"
89
+
90
+
91
+ def rate_key(kind: str, subject: str, now_seconds: float) -> str:
92
+ window = int(now_seconds // RATE_WINDOW_SECONDS)
93
+ return f"{kind}#{hashlib.sha256(subject.encode('utf-8')).hexdigest()[:24]}#{window}"
94
+
95
+
96
+ def parse_rendered_at(text: str) -> float | None:
97
+ return int(text) / 1000.0 if RENDERED_AT.fullmatch(text) else None
98
+
99
+
100
+ def submit_signup(
101
+ arguments: dict[str, Any],
102
+ *,
103
+ address: str,
104
+ identity_id: str | None,
105
+ now_seconds: float,
106
+ settings: SignupSettings,
107
+ count_hit: Callable[[str, int], int],
108
+ store_entry: Callable[[dict[str, str]], bool],
109
+ ) -> dict[str, str]:
110
+ if count_hit(rate_key("address", address, now_seconds), COUNTER_LIFETIME_SECONDS) > settings.address_limit:
111
+ return result(STATUS_THROTTLED, "Too many requests. Try again in a minute.")
112
+ if identity_id and count_hit(rate_key("identity", identity_id, now_seconds), COUNTER_LIFETIME_SECONDS) > settings.identity_limit:
113
+ return result(STATUS_THROTTLED, "Too many requests. Try again in a minute.")
114
+
115
+ form, value = arguments.get("form"), arguments.get("value")
116
+ honeypot, rendered_at = arguments.get("website"), arguments.get("renderedAt")
117
+ texts = [form, value, honeypot, rendered_at]
118
+ if any(text is not None and not isinstance(text, str) for text in texts):
119
+ return result(STATUS_INVALID, "Malformed request.")
120
+ if any(text is not None and len(text) > MAXIMUM_ARGUMENT_CHARACTERS for text in texts):
121
+ return result(STATUS_INVALID, "Request too large.")
122
+ if not isinstance(form, str) or not FORM_NAME.fullmatch(form) or form not in settings.forms:
123
+ return result(STATUS_INVALID, "Unknown form.")
124
+ if not isinstance(value, str) or not valid_value(value):
125
+ return result(STATUS_INVALID, "Enter a valid email address.")
126
+ rendered_seconds = parse_rendered_at(rendered_at) if isinstance(rendered_at, str) else None
127
+ if rendered_seconds is None:
128
+ return result(STATUS_INVALID, "Malformed request.")
129
+ age_seconds = now_seconds - rendered_seconds
130
+ if age_seconds > MAXIMUM_AGE_SECONDS:
131
+ return result(STATUS_EXPIRED, "This form has expired. Reload the page and try again.")
132
+ if honeypot or age_seconds < MINIMUM_FILL_SECONDS:
133
+ return result(STATUS_ACCEPTED)
134
+
135
+ submitted_at = datetime.fromtimestamp(now_seconds, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.000Z")
136
+ store_entry(
137
+ {
138
+ "id": entry_identifier(form, value),
139
+ "form": form,
140
+ "value": normalized_value(value),
141
+ "submittedAt": submitted_at,
142
+ }
143
+ )
144
+ return result(STATUS_ACCEPTED)
145
+
146
+
147
+ def counting_hits(table_name: str, client: Any = None) -> Callable[[str, int], int]:
148
+ import boto3
149
+
150
+ table_client = client or boto3.client("dynamodb")
151
+
152
+ def count_hit(key: str, lifetime_seconds: int) -> int:
153
+ response = table_client.update_item(
154
+ TableName=table_name,
155
+ Key={"pk": {"S": key}},
156
+ UpdateExpression="ADD hits :one SET expiresAt = :expires",
157
+ ExpressionAttributeValues={
158
+ ":one": {"N": "1"},
159
+ ":expires": {"N": str(int(time.time()) + lifetime_seconds)},
160
+ },
161
+ ReturnValues="UPDATED_NEW",
162
+ )
163
+ return int(response["Attributes"]["hits"]["N"])
164
+
165
+ return count_hit
166
+
167
+
168
+ def storing_entries(endpoint: str, region: str) -> Callable[[dict[str, str]], bool]:
169
+ import boto3
170
+ from botocore.auth import SigV4Auth
171
+ from botocore.awsrequest import AWSRequest
172
+
173
+ def store_entry(entry: dict[str, str]) -> bool:
174
+ body = json.dumps({"query": CREATE_ENTRY_MUTATION, "variables": {"input": entry}}).encode("utf-8")
175
+ request = AWSRequest(method="POST", url=endpoint, data=body, headers={"content-type": "application/json"})
176
+ SigV4Auth(boto3.Session().get_credentials().get_frozen_credentials(), "appsync", region).add_auth(request)
177
+ http_request = urllib.request.Request(endpoint, data=body, headers=dict(request.headers.items()), method="POST")
178
+ with urllib.request.urlopen(http_request, timeout=10) as response:
179
+ payload = json.loads(response.read())
180
+ errors = payload.get("errors") or []
181
+ if not errors:
182
+ return True
183
+ if all(any(marker in str(error) for marker in ALREADY_STORED_MARKERS) for error in errors):
184
+ return False
185
+ raise RuntimeError(f"createSignupEntry failed: {errors}")
186
+
187
+ return store_entry
188
+
189
+
190
+ def handler(event: dict[str, Any], _context: Any = None) -> dict[str, str]:
191
+ identity = event.get("identity") or {}
192
+ return submit_signup(
193
+ event.get("arguments") or {},
194
+ address=caller_address(identity),
195
+ identity_id=identity.get("cognitoIdentityId"),
196
+ now_seconds=time.time(),
197
+ settings=settings_from_environment(),
198
+ count_hit=counting_hits(os.environ["PAPYRUS_SIGNUP_RATE_TABLE"]),
199
+ store_entry=storing_entries(os.environ["PAPYRUS_GRAPHQL_ENDPOINT"], os.environ["AWS_REGION"]),
200
+ )
@@ -0,0 +1,27 @@
1
+ import { defineFunction } from "@aws-amplify/backend";
2
+ import { Duration, RemovalPolicy } from "aws-cdk-lib";
3
+ import { AttributeType, BillingMode, Table } from "aws-cdk-lib/aws-dynamodb";
4
+ import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
5
+ import { Architecture, Code, Function, Runtime } from "aws-cdk-lib/aws-lambda";
6
+ import path from "node:path";
7
+ import { fileURLToPath } from "node:url";
8
+ const functionDirectory = path.dirname(fileURLToPath(import.meta.url));
9
+ export const signupSubmit = defineFunction((scope) => {
10
+ const rateTable = new Table(scope, "PapyrusSignupRateCounters", {
11
+ partitionKey: { name: "pk", type: AttributeType.STRING },
12
+ billingMode: BillingMode.PAY_PER_REQUEST,
13
+ timeToLiveAttribute: "expiresAt",
14
+ removalPolicy: RemovalPolicy.DESTROY,
15
+ });
16
+ const lambda = new Function(scope, "papyrus-signup-submit", {
17
+ runtime: Runtime.PYTHON_3_12,
18
+ architecture: Architecture.ARM_64,
19
+ handler: "handler.handler",
20
+ timeout: Duration.seconds(10),
21
+ memorySize: 256,
22
+ code: Code.fromAsset(functionDirectory, { exclude: ["*.ts", "__pycache__", "*.pyc"] }),
23
+ environment: { PAPYRUS_SIGNUP_RATE_TABLE: rateTable.tableName },
24
+ });
25
+ lambda.addToRolePolicy(new PolicyStatement({ effect: Effect.ALLOW, actions: ["dynamodb:UpdateItem"], resources: [rateTable.tableArn] }));
26
+ return lambda;
27
+ }, { resourceGroupName: "data" });
@@ -0,0 +1,35 @@
1
+ import { defineFunction } from "@aws-amplify/backend";
2
+ import { Duration, RemovalPolicy } from "aws-cdk-lib";
3
+ import { AttributeType, BillingMode, Table } from "aws-cdk-lib/aws-dynamodb";
4
+ import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
5
+ import { Architecture, Code, Function, Runtime } from "aws-cdk-lib/aws-lambda";
6
+ import type { Construct } from "constructs";
7
+ import path from "node:path";
8
+ import { fileURLToPath } from "node:url";
9
+
10
+ const functionDirectory = path.dirname(fileURLToPath(import.meta.url));
11
+
12
+ export const signupSubmit = defineFunction(
13
+ (scope: Construct) => {
14
+ const rateTable = new Table(scope, "PapyrusSignupRateCounters", {
15
+ partitionKey: { name: "pk", type: AttributeType.STRING },
16
+ billingMode: BillingMode.PAY_PER_REQUEST,
17
+ timeToLiveAttribute: "expiresAt",
18
+ removalPolicy: RemovalPolicy.DESTROY,
19
+ });
20
+ const lambda = new Function(scope, "papyrus-signup-submit", {
21
+ runtime: Runtime.PYTHON_3_12,
22
+ architecture: Architecture.ARM_64,
23
+ handler: "handler.handler",
24
+ timeout: Duration.seconds(10),
25
+ memorySize: 256,
26
+ code: Code.fromAsset(functionDirectory, { exclude: ["*.ts", "__pycache__", "*.pyc"] }),
27
+ environment: { PAPYRUS_SIGNUP_RATE_TABLE: rateTable.tableName },
28
+ });
29
+ lambda.addToRolePolicy(
30
+ new PolicyStatement({ effect: Effect.ALLOW, actions: ["dynamodb:UpdateItem"], resources: [rateTable.tableArn] }),
31
+ );
32
+ return lambda;
33
+ },
34
+ { resourceGroupName: "data" },
35
+ );
@@ -0,0 +1,59 @@
1
+ import { Stack } from "aws-cdk-lib";
2
+ import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
3
+ import { signupSubmit } from "./function/resource.js";
4
+ import { signupSchemaDefinitions } from "./schema.js";
5
+ import { SIGNUP_MUTATION_FIELD, WAF_MINIMUM_RATE_LIMIT, addSignupRateLimit } from "./waf.js";
6
+ const FORM_NAME = /^[a-z0-9][a-z0-9-]{0,39}$/;
7
+ const KNOWN_KEYS = new Set(["name", "forms", "addressLimitPerMinute", "waf"]);
8
+ export const signupManifest = {
9
+ name: "signup",
10
+ contract: 1,
11
+ models: ["SignupEntry"],
12
+ mutations: [SIGNUP_MUTATION_FIELD],
13
+ guest: { mutations: [SIGNUP_MUTATION_FIELD] },
14
+ newsroomPanels: [{ id: "signups", label: "Signups" }],
15
+ staticAssets: ["_plugins/papyrus-signup@1.js"],
16
+ };
17
+ export function resolveSignupConfig(request) {
18
+ const raw = typeof request === "string" ? { name: request } : request;
19
+ const unknown = Object.keys(raw).filter((key) => !KNOWN_KEYS.has(key));
20
+ if (unknown.length > 0)
21
+ throw new Error(`Unknown signup plugin settings: ${unknown.join(", ")}`);
22
+ const forms = raw.forms ?? ["newsletter"];
23
+ if (!Array.isArray(forms) || forms.length === 0 || !forms.every((form) => typeof form === "string" && FORM_NAME.test(form))) {
24
+ throw new Error("signup plugin forms must be a non-empty list of lowercase names (letters, digits, hyphens; at most 40 characters)");
25
+ }
26
+ const addressLimitPerMinute = raw.addressLimitPerMinute ?? 20;
27
+ if (!Number.isInteger(addressLimitPerMinute) || addressLimitPerMinute < WAF_MINIMUM_RATE_LIMIT || addressLimitPerMinute > 1000) {
28
+ throw new Error(`signup plugin addressLimitPerMinute must be an integer from ${WAF_MINIMUM_RATE_LIMIT} to 1000`);
29
+ }
30
+ const waf = raw.waf ?? true;
31
+ if (typeof waf !== "boolean")
32
+ throw new Error("signup plugin waf must be a boolean");
33
+ return { forms: [...new Set(forms)], addressLimitPerMinute, waf };
34
+ }
35
+ export const signupPlugin = {
36
+ manifest: signupManifest,
37
+ resolveConfig: resolveSignupConfig,
38
+ functions: () => ({ signupSubmit }),
39
+ schemaDefinitions: signupSchemaDefinitions,
40
+ publicSettings: (config) => ({ contract: signupManifest.contract, forms: config.forms }),
41
+ wire(backend, config, context) {
42
+ const lambda = backend.signupSubmit.resources.lambda;
43
+ const stack = Stack.of(lambda);
44
+ lambda.addEnvironment("PAPYRUS_GRAPHQL_ENDPOINT", context.graphqlEndpoint);
45
+ lambda.addEnvironment("PAPYRUS_SIGNUP_FORMS", config.forms.join(","));
46
+ lambda.addEnvironment("PAPYRUS_SIGNUP_ADDRESS_LIMIT", String(config.addressLimitPerMinute));
47
+ lambda.addToRolePolicy(new PolicyStatement({
48
+ effect: Effect.ALLOW,
49
+ actions: ["appsync:GraphQL"],
50
+ resources: [`arn:aws:appsync:${stack.region}:${stack.account}:apis/${context.graphqlApiId}/types/Mutation/fields/createSignupEntry`],
51
+ }));
52
+ if (config.waf) {
53
+ addSignupRateLimit(context.createStack("signup-protection"), {
54
+ graphqlApiArn: `arn:aws:appsync:${stack.region}:${stack.account}:apis/${context.graphqlApiId}`,
55
+ limit: config.addressLimitPerMinute,
56
+ });
57
+ }
58
+ },
59
+ };
@@ -0,0 +1,74 @@
1
+ import { Stack } from "aws-cdk-lib";
2
+ import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
3
+ import type { Function as LambdaFunction } from "aws-cdk-lib/aws-lambda";
4
+ import type { PapyrusPlugin, PluginManifest, PluginRequest, PluginWiringContext } from "../types";
5
+ import { signupSubmit } from "./function/resource";
6
+ import { signupSchemaDefinitions } from "./schema";
7
+ import { SIGNUP_MUTATION_FIELD, WAF_MINIMUM_RATE_LIMIT, addSignupRateLimit } from "./waf";
8
+
9
+ export type SignupPluginConfig = {
10
+ /** Form names the site accepts. A submission naming any other form is rejected. */
11
+ forms: string[];
12
+ /** Most submissions one address may make in a minute: enforced by the function and, with `waf`, by the web ACL. */
13
+ addressLimitPerMinute: number;
14
+ /** A regional web ACL with a rate-based rule on the API (about 6 USD a month). Off: the function's counters only. */
15
+ waf: boolean;
16
+ };
17
+
18
+ const FORM_NAME = /^[a-z0-9][a-z0-9-]{0,39}$/;
19
+ const KNOWN_KEYS = new Set(["name", "forms", "addressLimitPerMinute", "waf"]);
20
+
21
+ export const signupManifest: PluginManifest = {
22
+ name: "signup",
23
+ contract: 1,
24
+ models: ["SignupEntry"],
25
+ mutations: [SIGNUP_MUTATION_FIELD],
26
+ guest: { mutations: [SIGNUP_MUTATION_FIELD] },
27
+ newsroomPanels: [{ id: "signups", label: "Signups" }],
28
+ staticAssets: ["_plugins/papyrus-signup@1.js"],
29
+ };
30
+
31
+ export function resolveSignupConfig(request: PluginRequest): SignupPluginConfig {
32
+ const raw = typeof request === "string" ? { name: request } : request;
33
+ const unknown = Object.keys(raw).filter((key) => !KNOWN_KEYS.has(key));
34
+ if (unknown.length > 0) throw new Error(`Unknown signup plugin settings: ${unknown.join(", ")}`);
35
+ const forms = (raw.forms as string[] | undefined) ?? ["newsletter"];
36
+ if (!Array.isArray(forms) || forms.length === 0 || !forms.every((form) => typeof form === "string" && FORM_NAME.test(form))) {
37
+ throw new Error("signup plugin forms must be a non-empty list of lowercase names (letters, digits, hyphens; at most 40 characters)");
38
+ }
39
+ const addressLimitPerMinute = (raw.addressLimitPerMinute as number | undefined) ?? 20;
40
+ if (!Number.isInteger(addressLimitPerMinute) || addressLimitPerMinute < WAF_MINIMUM_RATE_LIMIT || addressLimitPerMinute > 1000) {
41
+ throw new Error(`signup plugin addressLimitPerMinute must be an integer from ${WAF_MINIMUM_RATE_LIMIT} to 1000`);
42
+ }
43
+ const waf = (raw.waf as boolean | undefined) ?? true;
44
+ if (typeof waf !== "boolean") throw new Error("signup plugin waf must be a boolean");
45
+ return { forms: [...new Set(forms)], addressLimitPerMinute, waf };
46
+ }
47
+
48
+ export const signupPlugin: PapyrusPlugin<SignupPluginConfig, { signupSubmit: typeof signupSubmit }> = {
49
+ manifest: signupManifest,
50
+ resolveConfig: resolveSignupConfig,
51
+ functions: () => ({ signupSubmit }),
52
+ schemaDefinitions: signupSchemaDefinitions,
53
+ publicSettings: (config) => ({ contract: signupManifest.contract, forms: config.forms }),
54
+ wire(backend, config, context: PluginWiringContext) {
55
+ const lambda = backend.signupSubmit.resources.lambda as LambdaFunction;
56
+ const stack = Stack.of(lambda);
57
+ lambda.addEnvironment("PAPYRUS_GRAPHQL_ENDPOINT", context.graphqlEndpoint);
58
+ lambda.addEnvironment("PAPYRUS_SIGNUP_FORMS", config.forms.join(","));
59
+ lambda.addEnvironment("PAPYRUS_SIGNUP_ADDRESS_LIMIT", String(config.addressLimitPerMinute));
60
+ lambda.addToRolePolicy(
61
+ new PolicyStatement({
62
+ effect: Effect.ALLOW,
63
+ actions: ["appsync:GraphQL"],
64
+ resources: [`arn:aws:appsync:${stack.region}:${stack.account}:apis/${context.graphqlApiId}/types/Mutation/fields/createSignupEntry`],
65
+ }),
66
+ );
67
+ if (config.waf) {
68
+ addSignupRateLimit(context.createStack("signup-protection"), {
69
+ graphqlApiArn: `arn:aws:appsync:${stack.region}:${stack.account}:apis/${context.graphqlApiId}`,
70
+ limit: config.addressLimitPerMinute,
71
+ });
72
+ }
73
+ },
74
+ };
@@ -0,0 +1,40 @@
1
+ import { a } from "@aws-amplify/backend";
2
+ import { signupSubmit } from "./function/resource.js";
3
+ /**
4
+ * SignupEntry is never guest-readable and has no create rule for anyone: only the
5
+ * submitSignup function (IAM) writes it. Editors and admins list it; admins delete.
6
+ */
7
+ export function signupSchemaDefinitions() {
8
+ return {
9
+ SignupEntry: a
10
+ .model({
11
+ id: a.id().required(),
12
+ form: a.string().required(),
13
+ value: a.string().required(),
14
+ submittedAt: a.datetime().required(),
15
+ })
16
+ .disableOperations(["subscriptions"])
17
+ .secondaryIndexes((index) => [
18
+ index("form").sortKeys(["submittedAt"]).queryField("listSignupEntriesByFormAndSubmittedAt"),
19
+ ])
20
+ .authorization((allow) => [
21
+ allow.group("editor").to(["read"]),
22
+ allow.group("admin").to(["read", "delete"]),
23
+ ]),
24
+ SignupReceipt: a.customType({
25
+ status: a.string().required(),
26
+ message: a.string(),
27
+ }),
28
+ submitSignup: a
29
+ .mutation()
30
+ .arguments({
31
+ form: a.string().required(),
32
+ value: a.string().required(),
33
+ renderedAt: a.string().required(),
34
+ website: a.string(),
35
+ })
36
+ .returns(a.ref("SignupReceipt"))
37
+ .authorization((allow) => [allow.guest()])
38
+ .handler(a.handler.function(signupSubmit)),
39
+ };
40
+ }