create-ailk 0.5.0 → 0.6.0
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.
- package/README.md +55 -8
- package/component-catalog.json +336 -27
- package/component-catalog.md +25 -12
- package/dist/lib/api-boot-assertion.d.ts +46 -0
- package/dist/lib/api-boot-assertion.js +70 -0
- package/dist/lib/import-graph.d.ts +29 -0
- package/dist/lib/import-graph.js +140 -0
- package/dist/module-architecture.d.ts +1 -0
- package/dist/module-architecture.js +253 -127
- package/dist/surfaces.js +105 -7
- package/dist/template-manifest.d.ts +33 -0
- package/dist/template-manifest.js +99 -16
- package/dist/template-names.d.ts +1 -1
- package/dist/template-names.js +1 -0
- package/package.json +3 -2
- package/site-templates/marketing-site/TEMPLATE-MANIFEST.json +22 -2
- package/site-templates/marketing-site/TEMPLATE-MANIFEST.md +9 -2
- package/site-templates/marketing-site/ailk-template.json +1 -1
- package/site-templates/marketing-site-free/TEMPLATE-MANIFEST.json +2 -2
- package/site-templates/marketing-site-free/TEMPLATE-MANIFEST.md +1 -0
- package/site-templates/minimalism-site/TEMPLATE-MANIFEST.json +10 -2
- package/site-templates/minimalism-site/TEMPLATE-MANIFEST.md +47 -35
- package/site-templates/minimalism-site/ailk-template.json +2 -1
- package/site-templates/minimalism-site/assets/wordmark-placeholder.svg +28 -0
- package/site-templates/minimalism-site/content/en/HomePage/home.mdx +49 -19
- package/site-templates/simple-service/TEMPLATE-MANIFEST.json +240 -0
- package/site-templates/simple-service/TEMPLATE-MANIFEST.md +278 -0
- package/site-templates/simple-service/ailk-template.json +10 -0
- package/site-templates/simple-service/content/en/AboutPage/about.mdx +38 -0
- package/site-templates/simple-service/content/en/ContactPage/contact.mdx +67 -0
- package/site-templates/simple-service/content/en/FAQPage/faq.mdx +56 -0
- package/site-templates/simple-service/content/en/HomePage/home.mdx +85 -0
- package/site-templates/simple-service/content/en/Service/landscape-design.mdx +40 -0
- package/site-templates/simple-service/content/en/Service/lawn-maintenance.mdx +40 -0
- package/site-templates/simple-service/content/en/Service/seasonal-cleanup.mdx +40 -0
- package/site-templates/simple-service/content/en/ServicesList/services.mdx +72 -0
- package/site-templates/simple-service/provenance.json +4 -0
- package/templates/.env.example +72 -7
- package/templates/apps/api/.ailk/inbox/2026-09-20T14-57-31.552Z-fallback-1789916251590-037e9005.json +8 -0
- package/templates/apps/api/.ailk/inbox/2026-09-20T14-57-33.840Z-fallback-1789916253842-b52659e9.json +12 -0
- package/templates/apps/api/.ailk/inbox/2026-09-20T14-57-33.844Z-fallback-1789916253845-6b266f8c.json +12 -0
- package/templates/apps/api/.env.example +13 -0
- package/templates/apps/api/CLAUDE.md +26 -2
- package/templates/apps/api/jest.config.cjs +3 -0
- package/templates/apps/api/package.json +1 -0
- package/templates/apps/api/src/__tests__/commerce-claim-registration.test.ts +407 -0
- package/templates/apps/api/src/__tests__/module-exclusion.test.ts +83 -7
- package/templates/apps/api/src/__tests__/server.test.ts +471 -0
- package/templates/apps/api/src/__tests__/vercel-handler.test.ts +45 -0
- package/templates/apps/api/src/__tests__/workspace-module-registration.test.ts +2 -0
- package/templates/apps/api/src/config/index.ts +62 -0
- package/templates/apps/api/src/config/modules.ts +7 -3
- package/templates/apps/api/src/lib/__mocks__/prisma.ts +238 -2
- package/templates/apps/api/src/lib/__tests__/auth-email-budget.test.ts +143 -0
- package/templates/apps/api/src/lib/__tests__/content-adapter.test.ts +23 -0
- package/templates/apps/api/src/lib/__tests__/deliverable-asset-store.test.ts +125 -0
- package/templates/apps/api/src/lib/__tests__/error-serializer.test.ts +20 -0
- package/templates/apps/api/src/lib/__tests__/require-commerce-claim.test.ts +317 -0
- package/templates/apps/api/src/lib/__tests__/site.test.ts +238 -0
- package/templates/apps/api/src/lib/__tests__/stripe.test.ts +75 -0
- package/templates/apps/api/src/lib/auth-email-budget.ts +160 -0
- package/templates/apps/api/src/lib/content-adapter.ts +57 -31
- package/templates/apps/api/src/lib/deliverable-asset-store.ts +76 -0
- package/templates/apps/api/src/lib/error-serializer.ts +23 -3
- package/templates/apps/api/src/lib/outbound-http.ts +7 -2
- package/templates/apps/api/src/lib/require-commerce-claim.ts +250 -0
- package/templates/apps/api/src/lib/site.ts +208 -9
- package/templates/apps/api/src/lib/stripe.ts +66 -0
- package/templates/apps/api/src/middleware/__tests__/default-tenant.test.ts +182 -0
- package/templates/apps/api/src/middleware/__tests__/rate-limit.test.ts +42 -0
- package/templates/apps/api/src/middleware/rate-limit.ts +7 -1
- package/templates/apps/api/src/openapi/spec.ts +543 -2
- package/templates/apps/api/src/routes/auth/__tests__/auth-user.functional.test.ts +48 -0
- package/templates/apps/api/src/routes/auth/__tests__/transactional-email.test.ts +309 -0
- package/templates/apps/api/src/routes/auth/__tests__/user.test.ts +123 -0
- package/templates/apps/api/src/routes/auth/transactional-email.ts +273 -0
- package/templates/apps/api/src/routes/auth/user.ts +61 -0
- package/templates/apps/api/src/routes/checkout/__tests__/session.test.ts +434 -0
- package/templates/apps/api/src/routes/checkout/__tests__/sessions.test.ts +888 -0
- package/templates/apps/api/src/routes/checkout/index.ts +34 -0
- package/templates/apps/api/src/routes/checkout/session.ts +139 -0
- package/templates/apps/api/src/routes/checkout/sessions.ts +197 -0
- package/templates/apps/api/src/routes/content/__tests__/get-page.test.ts +554 -0
- package/templates/apps/api/src/routes/content/__tests__/list-pages.test.ts +108 -0
- package/templates/apps/api/src/routes/content/__tests__/search.test.ts +450 -0
- package/templates/apps/api/src/routes/content/get-page.ts +94 -12
- package/templates/apps/api/src/routes/content/index.ts +13 -73
- package/templates/apps/api/src/routes/content/list-pages.ts +15 -3
- package/templates/apps/api/src/routes/content/rate-limit.ts +15 -6
- package/templates/apps/api/src/routes/content/search.ts +129 -0
- package/templates/apps/api/src/routes/leads/__tests__/index.test.ts +447 -31
- package/templates/apps/api/src/routes/leads/index.ts +100 -13
- package/templates/apps/api/src/routes/project-listings/__tests__/configured-application.test.ts +51 -0
- package/templates/apps/api/src/routes/project-listings/__tests__/drafts.test.ts +351 -0
- package/templates/apps/api/src/routes/project-listings/__tests__/founder-identity.test.ts +51 -0
- package/templates/apps/api/src/routes/project-listings/__tests__/me.test.ts +5 -2
- package/templates/apps/api/src/routes/project-listings/__tests__/public.test.ts +4 -1
- package/templates/apps/api/src/routes/project-listings/__tests__/resume-email.test.ts +58 -0
- package/templates/apps/api/src/routes/project-listings/__tests__/site-answers.test.ts +107 -6
- package/templates/apps/api/src/routes/project-listings/__tests__/site-key.test.ts +87 -2
- package/templates/apps/api/src/routes/project-listings/__tests__/structured-address.test.ts +51 -0
- package/templates/apps/api/src/routes/project-listings/start.ts +5 -1
- package/templates/apps/api/src/routes/requests/__tests__/confirm.test.ts +686 -0
- package/templates/apps/api/src/routes/requests/confirm.ts +601 -0
- package/templates/apps/api/src/routes/requests/index.ts +54 -0
- package/templates/apps/api/src/routes/webhooks/README.md +265 -0
- package/templates/apps/api/src/routes/webhooks/__tests__/stripe-dunning.test.ts +749 -0
- package/templates/apps/api/src/routes/webhooks/__tests__/stripe-flow-checkout.test.ts +330 -0
- package/templates/apps/api/src/routes/webhooks/__tests__/stripe-idempotency.test.ts +262 -0
- package/templates/apps/api/src/routes/webhooks/__tests__/stripe-org-billing.test.ts +285 -0
- package/templates/apps/api/src/routes/webhooks/__tests__/stripe-rate-limit.test.ts +213 -0
- package/templates/apps/api/src/routes/webhooks/__tests__/stripe-request-paid.test.ts +301 -0
- package/templates/apps/api/src/routes/webhooks/__tests__/stripe.test.ts +648 -0
- package/templates/apps/api/src/routes/webhooks/index.ts +89 -0
- package/templates/apps/api/src/routes/webhooks/stripe.ts +1399 -0
- package/templates/apps/api/src/server.ts +439 -49
- package/templates/apps/api/src/services/__tests__/abandon-email.test.ts +39 -0
- package/templates/apps/api/src/services/__tests__/aeo-score.test.ts +217 -40
- package/templates/apps/api/src/services/__tests__/audit-log.test.ts +620 -0
- package/templates/apps/api/src/services/__tests__/consent.test.ts +2 -0
- package/templates/apps/api/src/services/__tests__/deliverable-fulfillment.test.ts +300 -5
- package/templates/apps/api/src/services/__tests__/deliverable-resend.test.ts +20 -0
- package/templates/apps/api/src/services/__tests__/dunning-policy.test.ts +368 -0
- package/templates/apps/api/src/services/__tests__/lead-routing.test.ts +160 -0
- package/templates/apps/api/src/services/__tests__/listing-config.test.ts +166 -0
- package/templates/apps/api/src/services/__tests__/project-listings.fence.test.ts +154 -0
- package/templates/apps/api/src/services/__tests__/public-site-resolver.test.ts +137 -0
- package/templates/apps/api/src/services/__tests__/request-confirm.test.ts +444 -0
- package/templates/apps/api/src/services/__tests__/revalidation.test.ts +238 -0
- package/templates/apps/api/src/services/__tests__/verified-email.test.ts +78 -0
- package/templates/apps/api/src/services/abandon-email.ts +8 -2
- package/templates/apps/api/src/services/aeo-score.ts +49 -12
- package/templates/apps/api/src/services/audit-log.ts +668 -0
- package/templates/apps/api/src/services/consent.ts +18 -0
- package/templates/apps/api/src/services/default-tenant.ts +70 -31
- package/templates/apps/api/src/services/deliverable-fulfillment.ts +141 -13
- package/templates/apps/api/src/services/dunning-policy.ts +294 -0
- package/templates/apps/api/src/services/entitlements.ts +80 -0
- package/templates/apps/api/src/services/lead-promotion.ts +13 -14
- package/templates/apps/api/src/services/lead-routing.ts +161 -2
- package/templates/apps/api/src/services/leads-inbox.ts +23 -2
- package/templates/apps/api/src/services/listing-config.ts +27 -9
- package/templates/apps/api/src/services/project-listings.ts +201 -6
- package/templates/apps/api/src/services/public-site-resolver.ts +106 -0
- package/templates/apps/api/src/services/request-confirm.ts +646 -0
- package/templates/apps/api/src/services/revalidation.ts +173 -0
- package/templates/apps/api/src/services/verified-email.ts +71 -0
- package/templates/apps/api/src/vercel-handler.ts +75 -35
- package/templates/apps/mcp/.env.example +17 -0
- package/templates/apps/mcp/CLAUDE.md +57 -24
- package/templates/apps/mcp/__tests__/aeo-cli-parity.test.ts +6 -0
- package/templates/apps/mcp/__tests__/audit.test.ts +137 -8
- package/templates/apps/mcp/__tests__/catalog-drift.test.ts +79 -36
- package/templates/apps/mcp/__tests__/get_offerings.test.ts +111 -2
- package/templates/apps/mcp/__tests__/get_pricing.test.ts +82 -2
- package/templates/apps/mcp/__tests__/http-transport.test.ts +541 -2
- package/templates/apps/mcp/__tests__/index.test.ts +39 -0
- package/templates/apps/mcp/__tests__/lib/audit-single-page-readiness-scan.test.ts +143 -0
- package/templates/apps/mcp/__tests__/lib/commerce-claim.test.ts +111 -0
- package/templates/apps/mcp/__tests__/lib/content-adapter-parity.test.ts +65 -0
- package/templates/apps/mcp/__tests__/lib/content-adapter.test.ts +188 -0
- package/templates/apps/mcp/__tests__/lib/fail-closed-tools.test.ts +165 -0
- package/templates/apps/mcp/__tests__/lib/public-workspace.test.ts +124 -0
- package/templates/apps/mcp/__tests__/list_pages.test.ts +116 -0
- package/templates/apps/mcp/__tests__/module-exclusion.test.ts +7 -1
- package/templates/apps/mcp/__tests__/parity.test.ts +10 -7
- package/templates/apps/mcp/__tests__/purchase-disabled.test.ts +89 -22
- package/templates/apps/mcp/__tests__/read_content.test.ts +322 -1
- package/templates/apps/mcp/__tests__/search_content.test.ts +105 -2
- package/templates/apps/mcp/__tests__/server.test.ts +67 -14
- package/templates/apps/mcp/__tests__/start_purchase.test.ts +252 -0
- package/templates/apps/mcp/__tests__/tools/purchase.test.ts +300 -123
- package/templates/apps/mcp/jest.config.cjs +13 -0
- package/templates/apps/mcp/package.json +2 -0
- package/templates/apps/mcp/src/abuse/index.ts +159 -24
- package/templates/apps/mcp/src/config/modules.ts +10 -5
- package/templates/apps/mcp/src/http-transport.ts +102 -29
- package/templates/apps/mcp/src/index.ts +73 -11
- package/templates/apps/mcp/src/lib/audit-single-page.ts +90 -3
- package/templates/apps/mcp/src/lib/commerce-claim.ts +94 -0
- package/templates/apps/mcp/src/lib/content-adapter.ts +113 -0
- package/templates/apps/mcp/src/lib/empty-content-adapter.ts +139 -0
- package/templates/apps/mcp/src/lib/prisma.ts +39 -0
- package/templates/apps/mcp/src/lib/public-workspace.ts +133 -0
- package/templates/apps/mcp/src/lib/stripe-tax-signal.ts +59 -0
- package/templates/apps/mcp/src/server.ts +59 -35
- package/templates/apps/mcp/src/tools/audit.ts +26 -2
- package/templates/apps/mcp/src/tools/get_business_hours.ts +4 -3
- package/templates/apps/mcp/src/tools/get_faq.ts +4 -3
- package/templates/apps/mcp/src/tools/get_locations.ts +5 -3
- package/templates/apps/mcp/src/tools/get_offerings.ts +16 -4
- package/templates/apps/mcp/src/tools/get_pricing.ts +25 -6
- package/templates/apps/mcp/src/tools/get_site_info.ts +4 -3
- package/templates/apps/mcp/src/tools/index.ts +6 -4
- package/templates/apps/mcp/src/tools/list_pages.ts +4 -3
- package/templates/apps/mcp/src/tools/purchase.ts +128 -45
- package/templates/apps/mcp/src/tools/read_content.ts +20 -6
- package/templates/apps/mcp/src/tools/search_content.ts +11 -4
- package/templates/apps/web/.env.example +71 -5
- package/templates/apps/web/CLAUDE.md +44 -0
- package/templates/apps/web/app/.well-known/mcp.json/build-manifest.ts +18 -2
- package/templates/apps/web/app/.well-known/mcp.json/route.ts +11 -3
- package/templates/apps/web/app/[locale]/(auth)/__tests__/auth-pages.test.tsx +282 -0
- package/templates/apps/web/app/[locale]/(auth)/__tests__/password-reset.test.tsx +515 -0
- package/templates/apps/web/app/[locale]/(auth)/forgot-password/forgot-password-card.tsx +89 -0
- package/templates/apps/web/app/[locale]/(auth)/forgot-password/page.tsx +109 -0
- package/templates/apps/web/app/[locale]/(auth)/layout.tsx +92 -0
- package/templates/apps/web/app/[locale]/(auth)/reset-password/page.tsx +135 -0
- package/templates/apps/web/app/[locale]/(auth)/reset-password/reset-password-card.tsx +109 -0
- package/templates/apps/web/app/[locale]/{sign-in → (auth)/sign-in}/__tests__/page.test.tsx +48 -2
- package/templates/apps/web/app/[locale]/{sign-in → (auth)/sign-in}/page.tsx +20 -6
- package/templates/apps/web/app/[locale]/(auth)/sign-in/sign-in-card.tsx +125 -0
- package/templates/apps/web/app/[locale]/(auth)/sign-up/page.tsx +96 -0
- package/templates/apps/web/app/[locale]/(auth)/sign-up/sign-up-card.tsx +101 -0
- package/templates/apps/web/app/[locale]/(auth)/use-auth-card-flow.ts +102 -0
- package/templates/apps/web/app/[locale]/(auth)/verify-email/page.tsx +87 -0
- package/templates/apps/web/app/[locale]/(authed)/__tests__/gate.test.tsx +20 -10
- package/templates/apps/web/app/[locale]/(authed)/account/__tests__/page.test.tsx +84 -0
- package/templates/apps/web/app/[locale]/(authed)/account/actions.ts +58 -0
- package/templates/apps/web/app/[locale]/(authed)/account/page.tsx +63 -8
- package/templates/apps/web/app/[locale]/(authed)/layout.tsx +18 -1
- package/templates/apps/web/app/[locale]/cancellation/page.tsx +44 -0
- package/templates/apps/web/app/[locale]/layout.tsx +133 -87
- package/templates/apps/web/app/[locale]/purchase/__tests__/cancel.test.tsx +162 -0
- package/templates/apps/web/app/[locale]/purchase/__tests__/module-scope.test.ts +68 -0
- package/templates/apps/web/app/[locale]/purchase/__tests__/success.test.tsx +361 -0
- package/templates/apps/web/app/[locale]/purchase/cancel/page.tsx +115 -0
- package/templates/apps/web/app/[locale]/purchase/success/page.tsx +307 -0
- package/templates/apps/web/app/[locale]/returns/page.tsx +44 -0
- package/templates/apps/web/app/[locale]/shipping/page.tsx +44 -0
- package/templates/apps/web/app/api/revalidate/__tests__/route.test.ts +670 -0
- package/templates/apps/web/app/api/revalidate/route.ts +194 -0
- package/templates/apps/web/auth.ts +407 -8
- package/templates/apps/web/jest.config.cjs +8 -4
- package/templates/apps/web/lib/__tests__/auth-email.test.ts +214 -0
- package/templates/apps/web/lib/__tests__/auth-routes.test.ts +74 -0
- package/templates/apps/web/lib/__tests__/meeting-slugs.test.ts +72 -0
- package/templates/apps/web/lib/__tests__/structured-data.test.tsx +18 -1
- package/templates/apps/web/lib/auth-email.ts +318 -0
- package/templates/apps/web/lib/auth-env.ts +85 -0
- package/templates/apps/web/lib/auth-routes.ts +55 -0
- package/templates/apps/web/lib/meeting-slugs.ts +53 -0
- package/templates/apps/web/lib/telemetry-scrub.ts +5 -1
- package/templates/apps/web/messages/en.json +187 -0
- package/templates/apps/web/package.json +2 -1
- package/templates/apps/web/src/lib/commerce-claim.ts +33 -0
- package/templates/apps/web/src/lib/commerce-sections.ts +61 -0
- package/templates/content/en/CancellationPolicyPage/cancellation.mdx +108 -0
- package/templates/content/en/ReturnsPolicyPage/returns.mdx +108 -0
- package/templates/content/en/ShippingPolicyPage/shipping.mdx +108 -0
- package/templates/database/CHANGELOG.md +142 -0
- package/templates/database/README.md +10 -3
- package/templates/database/__tests__/appointment.db.test.ts +106 -0
- package/templates/database/__tests__/artifact-content-store.db.test.ts +316 -0
- package/templates/database/__tests__/composite-fence.db.test.ts +206 -0
- package/templates/database/__tests__/fence-fan-out.db.test.ts +711 -0
- package/templates/database/__tests__/fixtures/appointment-create-probe.ts +79 -0
- package/templates/database/__tests__/fixtures/project-listing-consent-probe.ts +100 -0
- package/templates/database/__tests__/flow-workspace-key.db.test.ts +263 -0
- package/templates/database/__tests__/prefixed-ids.db.test.ts +85 -9
- package/templates/database/__tests__/project-listing-consent-at.db.test.ts +111 -0
- package/templates/database/__tests__/tenancy.test.ts +141 -0
- package/templates/database/config/schema.prisma +79 -2
- package/templates/database/content/schema.prisma +87 -11
- package/templates/database/inbox/schema.prisma +452 -9
- package/templates/database/jest.config.cjs +5 -3
- package/templates/database/migrations/20260915120000_project_listing_solution/migration.sql +15 -0
- package/templates/database/migrations/20260916120000_artifact_content_store/migration.sql +158 -0
- package/templates/database/migrations/20260917120000_callback_request_confirm/migration.sql +40 -0
- package/templates/database/migrations/20260918120000_add_appointment/migration.sql +67 -0
- package/templates/database/migrations/20260918120000_asset_metadata_columns/migration.sql +70 -0
- package/templates/database/migrations/20260919120000_flow_workspace_key/migration.sql +181 -0
- package/templates/database/migrations/20260919130000_flow_definition_admin_columns/migration.sql +172 -0
- package/templates/database/migrations/20260919130000_workspace_meetings/migration.sql +41 -0
- package/templates/database/migrations/20260919140000_inbox_workspace_key/migration.sql +185 -0
- package/templates/database/migrations/20260920120000_lead_handled_at/migration.sql +70 -0
- package/templates/database/migrations/20260920130000_lead_workspace_source_index/migration.sql +57 -0
- package/templates/database/migrations/20260921120000_deliverable_grant_workspace_key/migration.sql +68 -0
- package/templates/database/migrations/20260922120000_tenancy_fence/migration.sql +1067 -0
- package/templates/database/migrations/20260923120000_listing_capturing_magnet/migration.sql +210 -0
- package/templates/database/package.json +1 -1
- package/templates/database/tenancy.ts +86 -1
- package/templates/database/tsconfig.json +1 -0
- package/versions.json +34 -34
- package/templates/apps/api/src/routes/content/__tests__/create.test.ts +0 -253
- package/templates/apps/api/src/routes/content/__tests__/delete.test.ts +0 -215
- package/templates/apps/api/src/routes/content/__tests__/update.test.ts +0 -266
- package/templates/apps/api/src/routes/content/create.ts +0 -115
- package/templates/apps/api/src/routes/content/delete.ts +0 -82
- package/templates/apps/api/src/routes/content/update.ts +0 -112
- package/templates/apps/mcp/__tests__/create_page.test.ts +0 -202
- package/templates/apps/mcp/__tests__/delete_page.test.ts +0 -177
- package/templates/apps/mcp/__tests__/update_page.test.ts +0 -196
- package/templates/apps/mcp/src/tools/create_page.ts +0 -96
- package/templates/apps/mcp/src/tools/delete_page.ts +0 -74
- package/templates/apps/mcp/src/tools/update_page.ts +0 -92
- package/templates/apps/web/app/[locale]/sign-in/sign-in-card.tsx +0 -123
- /package/templates/apps/web/app/[locale]/{sign-in → (auth)/sign-in}/__tests__/next-path.test.ts +0 -0
- /package/templates/apps/web/app/[locale]/{sign-in → (auth)/sign-in}/next-path.ts +0 -0
|
@@ -18,14 +18,32 @@
|
|
|
18
18
|
* - Abuse rules reject invalid email on capture_lead
|
|
19
19
|
* - Honeypot field rejection on capture_lead
|
|
20
20
|
* - Blocked UA returns 403
|
|
21
|
+
* - #5971: a start_purchase burst past the per-IP daily cap is refused with
|
|
22
|
+
* the rate-limit error, and no Checkout Session is created
|
|
21
23
|
*/
|
|
22
24
|
|
|
23
25
|
import type { AddressInfo } from "node:net";
|
|
24
26
|
|
|
25
|
-
import type { FastifyInstance } from "fastify";
|
|
26
|
-
import {
|
|
27
|
+
import type { FastifyInstance, FastifyRequest } from "fastify";
|
|
28
|
+
import { checkout } from "@working-theory/api-client";
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
__setMcpCommerceClaimForTest,
|
|
32
|
+
resetMcpCommerceClaim,
|
|
33
|
+
} from "../src/lib/commerce-claim.js";
|
|
34
|
+
import { AbuseGuard, AbuseReasonCode } from "../src/abuse/index.js";
|
|
35
|
+
import {
|
|
36
|
+
ABUSE_REFUSAL_STATUS,
|
|
37
|
+
buildHttpTransportApp,
|
|
38
|
+
WRITE_TOOLS,
|
|
39
|
+
} from "../src/http-transport.js";
|
|
27
40
|
import { listRegisteredTools } from "../src/server.js";
|
|
28
41
|
|
|
42
|
+
jest.mock("@working-theory/api-client", () => {
|
|
43
|
+
const actual = jest.requireActual("@working-theory/api-client");
|
|
44
|
+
return { ...actual, checkout: { createSession: jest.fn() } };
|
|
45
|
+
});
|
|
46
|
+
|
|
29
47
|
/** Permissive JSON-RPC 2.0 envelope type for response body assertions. */
|
|
30
48
|
type JsonRpcResponse = {
|
|
31
49
|
jsonrpc?: string;
|
|
@@ -321,3 +339,524 @@ describe("Abuse rules", () => {
|
|
|
321
339
|
}
|
|
322
340
|
});
|
|
323
341
|
});
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* AC-3 — a `start_purchase` burst beyond the AbuseGuard per-IP cap is refused
|
|
345
|
+
* with the rate-limit error and creates no Checkout Session (#5971 D6).
|
|
346
|
+
*
|
|
347
|
+
* The refusal happens in the POST preHandler, BEFORE the MCP handler runs, so
|
|
348
|
+
* `checkout.createSession` is the thing to count: it is called once per call
|
|
349
|
+
* that got through and never for the one that was refused.
|
|
350
|
+
*
|
|
351
|
+
* `start_purchase` takes the write branch only because #5971 added it to
|
|
352
|
+
* `WRITE_TOOLS`; on the read branch the AbuseGuard checks the UA deny list
|
|
353
|
+
* alone and the cap would never be consulted.
|
|
354
|
+
*/
|
|
355
|
+
describe("start_purchase — per-IP daily cap (#5971)", () => {
|
|
356
|
+
const CAP = 2;
|
|
357
|
+
const CAPPED_CONFIG = {
|
|
358
|
+
...DEFAULT_CONFIG,
|
|
359
|
+
cors: { writeAllowedOrigins: ["*"] as string[] },
|
|
360
|
+
abuse: {
|
|
361
|
+
captureLeadDailyCapPerIp: 50,
|
|
362
|
+
startPurchaseDailyCapPerIp: CAP,
|
|
363
|
+
blockedUserAgents: "",
|
|
364
|
+
},
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
const createSession = checkout.createSession as jest.MockedFunction<
|
|
368
|
+
typeof checkout.createSession
|
|
369
|
+
>;
|
|
370
|
+
|
|
371
|
+
let app: FastifyInstance;
|
|
372
|
+
let port: number;
|
|
373
|
+
|
|
374
|
+
function purchasePayload(id: number) {
|
|
375
|
+
return {
|
|
376
|
+
jsonrpc: "2.0",
|
|
377
|
+
id,
|
|
378
|
+
method: "tools/call",
|
|
379
|
+
params: {
|
|
380
|
+
name: "start_purchase",
|
|
381
|
+
arguments: { productCode: "pro-plan" },
|
|
382
|
+
},
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
beforeEach(async () => {
|
|
387
|
+
__setMcpCommerceClaimForTest("claimed");
|
|
388
|
+
createSession.mockResolvedValue({
|
|
389
|
+
ok: true,
|
|
390
|
+
body: {
|
|
391
|
+
sessionId: "cs_test_burst",
|
|
392
|
+
url: "https://checkout.stripe.com/c/pay/cs_test_burst",
|
|
393
|
+
},
|
|
394
|
+
});
|
|
395
|
+
app = await buildHttpTransportApp(CAPPED_CONFIG);
|
|
396
|
+
await app.listen({ host: "127.0.0.1", port: 0 });
|
|
397
|
+
port = (app.server.address() as AddressInfo).port;
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
afterEach(async () => {
|
|
401
|
+
await app.close();
|
|
402
|
+
jest.clearAllMocks();
|
|
403
|
+
resetMcpCommerceClaim();
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("refuses the call past the cap with 403 and a 'daily cap' reason", async () => {
|
|
407
|
+
const first = await mcpPost(port, purchasePayload(30));
|
|
408
|
+
const second = await mcpPost(port, purchasePayload(31));
|
|
409
|
+
const third = await mcpPost(port, purchasePayload(32));
|
|
410
|
+
|
|
411
|
+
expect(first.status).toBe(200);
|
|
412
|
+
expect(second.status).toBe(200);
|
|
413
|
+
expect(third.status).toBe(403);
|
|
414
|
+
|
|
415
|
+
const body = (await third.json()) as { error?: string };
|
|
416
|
+
expect(body.error).toContain("daily");
|
|
417
|
+
expect(body.error).toContain("cap");
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
it("creates no Checkout Session for the refused call", async () => {
|
|
421
|
+
await mcpPost(port, purchasePayload(33));
|
|
422
|
+
await mcpPost(port, purchasePayload(34));
|
|
423
|
+
await mcpPost(port, purchasePayload(35));
|
|
424
|
+
|
|
425
|
+
expect(createSession).toHaveBeenCalledTimes(CAP);
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
it("start_purchase takes the write branch, so the guard sees it at all", async () => {
|
|
429
|
+
const res = await mcpPost(port, purchasePayload(36), {
|
|
430
|
+
"user-agent": "curl/8.0",
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
// A built-in blocked UA is refused on the write branch; a read-branch
|
|
434
|
+
// tool would be too, but only the write branch consults the cap above.
|
|
435
|
+
expect(res.status).toBe(403);
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* JSON-RPC BATCH envelope — the write branch must survive it (#5971 security
|
|
441
|
+
* review, High).
|
|
442
|
+
*
|
|
443
|
+
* JSON-RPC 2.0 allows an ARRAY of messages in one POST, and the pinned MCP SDK
|
|
444
|
+
* dispatches every one of them. The write-tool guard used to read
|
|
445
|
+
* `body.method` on a body that may be an array: an array has no `.method`, so
|
|
446
|
+
* the whole batch was classified as a READ, skipping the write-origin
|
|
447
|
+
* allow-list and every AbuseGuard rule — including the per-IP daily cap. N
|
|
448
|
+
* batched `start_purchase` calls then executed on zero cap units, each one
|
|
449
|
+
* creating a real Stripe Checkout Session on the seller's account.
|
|
450
|
+
*
|
|
451
|
+
* These tests pin the envelope, not the tool: any future WRITE_TOOLS addition
|
|
452
|
+
* inherits them. The gap predated `start_purchase` and covered the three other
|
|
453
|
+
* write tools too, which is why the last case here is `capture_lead`.
|
|
454
|
+
*/
|
|
455
|
+
describe("JSON-RPC batch bodies take the write branch (#5971)", () => {
|
|
456
|
+
const CAP = 2;
|
|
457
|
+
const BATCH_CONFIG = {
|
|
458
|
+
...DEFAULT_CONFIG,
|
|
459
|
+
cors: { writeAllowedOrigins: ["*"] as string[] },
|
|
460
|
+
abuse: {
|
|
461
|
+
captureLeadDailyCapPerIp: CAP,
|
|
462
|
+
startPurchaseDailyCapPerIp: CAP,
|
|
463
|
+
blockedUserAgents: "",
|
|
464
|
+
},
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
const createSession = checkout.createSession as jest.MockedFunction<
|
|
468
|
+
typeof checkout.createSession
|
|
469
|
+
>;
|
|
470
|
+
|
|
471
|
+
let app: FastifyInstance;
|
|
472
|
+
let port: number;
|
|
473
|
+
|
|
474
|
+
function purchaseMessage(id: number) {
|
|
475
|
+
return {
|
|
476
|
+
jsonrpc: "2.0",
|
|
477
|
+
id,
|
|
478
|
+
method: "tools/call",
|
|
479
|
+
params: {
|
|
480
|
+
name: "start_purchase",
|
|
481
|
+
arguments: { productCode: "pro-plan" },
|
|
482
|
+
},
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
beforeEach(async () => {
|
|
487
|
+
__setMcpCommerceClaimForTest("claimed");
|
|
488
|
+
createSession.mockResolvedValue({
|
|
489
|
+
ok: true,
|
|
490
|
+
body: {
|
|
491
|
+
sessionId: "cs_test_batch",
|
|
492
|
+
url: "https://checkout.stripe.com/c/pay/cs_test_batch",
|
|
493
|
+
},
|
|
494
|
+
});
|
|
495
|
+
app = await buildHttpTransportApp(BATCH_CONFIG);
|
|
496
|
+
await app.listen({ host: "127.0.0.1", port: 0 });
|
|
497
|
+
port = (app.server.address() as AddressInfo).port;
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
afterEach(async () => {
|
|
501
|
+
await app.close();
|
|
502
|
+
jest.clearAllMocks();
|
|
503
|
+
resetMcpCommerceClaim();
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it("a batch of N write calls consumes N cap units, not one", async () => {
|
|
507
|
+
// One POST carrying CAP + 1 messages. Under the old per-request check this
|
|
508
|
+
// returned 200 and executed every one; the cap must refuse it outright.
|
|
509
|
+
const res = await mcpPost(port, [
|
|
510
|
+
purchaseMessage(40),
|
|
511
|
+
purchaseMessage(41),
|
|
512
|
+
purchaseMessage(42),
|
|
513
|
+
]);
|
|
514
|
+
|
|
515
|
+
expect(res.status).toBe(403);
|
|
516
|
+
const body = (await res.json()) as { error?: string };
|
|
517
|
+
expect(body.error).toContain("daily cap");
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
it("refuses the batch before the SDK dispatches any of its messages", async () => {
|
|
521
|
+
await mcpPost(port, [
|
|
522
|
+
purchaseMessage(43),
|
|
523
|
+
purchaseMessage(44),
|
|
524
|
+
purchaseMessage(45),
|
|
525
|
+
]);
|
|
526
|
+
|
|
527
|
+
// Refused in the preHandler ⇒ no Checkout Session for ANY message.
|
|
528
|
+
expect(createSession).not.toHaveBeenCalled();
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
it("a batch within the cap still works, and charges one unit per message", async () => {
|
|
532
|
+
const res = await mcpPost(port, [purchaseMessage(46), purchaseMessage(47)]);
|
|
533
|
+
expect(res.status).toBe(200);
|
|
534
|
+
|
|
535
|
+
// Both units are now spent, so a single further call is refused.
|
|
536
|
+
const third = await mcpPost(port, purchaseMessage(48));
|
|
537
|
+
expect(third.status).toBe(403);
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it("a batch is subject to the write-origin allow-list", async () => {
|
|
541
|
+
const strictApp = await buildHttpTransportApp({
|
|
542
|
+
...BATCH_CONFIG,
|
|
543
|
+
cors: { writeAllowedOrigins: [] as string[] },
|
|
544
|
+
});
|
|
545
|
+
await strictApp.listen({ host: "127.0.0.1", port: 0 });
|
|
546
|
+
const strictPort = (strictApp.server.address() as AddressInfo).port;
|
|
547
|
+
|
|
548
|
+
try {
|
|
549
|
+
const res = await mcpPost(strictPort, [purchaseMessage(49)], {
|
|
550
|
+
origin: "https://evil.example",
|
|
551
|
+
});
|
|
552
|
+
expect(res.status).toBe(403);
|
|
553
|
+
const body = (await res.json()) as { error?: string };
|
|
554
|
+
expect(body.error).toContain("CORS");
|
|
555
|
+
} finally {
|
|
556
|
+
await strictApp.close();
|
|
557
|
+
}
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
it("the same protection covers the pre-existing write tools (capture_lead)", async () => {
|
|
561
|
+
const leadMessage = (id: number) => ({
|
|
562
|
+
jsonrpc: "2.0",
|
|
563
|
+
id,
|
|
564
|
+
method: "tools/call",
|
|
565
|
+
params: {
|
|
566
|
+
name: "capture_lead",
|
|
567
|
+
arguments: { name: "A", email: "a@example.com" },
|
|
568
|
+
},
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
const res = await mcpPost(port, [
|
|
572
|
+
leadMessage(50),
|
|
573
|
+
leadMessage(51),
|
|
574
|
+
leadMessage(52),
|
|
575
|
+
]);
|
|
576
|
+
|
|
577
|
+
expect(res.status).toBe(403);
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
it("a batch of READ messages is unaffected", async () => {
|
|
581
|
+
const res = await mcpPost(port, [TOOLS_LIST_PAYLOAD]);
|
|
582
|
+
expect(res.status).toBe(200);
|
|
583
|
+
});
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Guardrail — batch coverage is driven from WRITE_TOOLS itself (#5971 security
|
|
588
|
+
* review round 2), not from hand-picked names.
|
|
589
|
+
*
|
|
590
|
+
* The round-1 defect was invisible because every existing test used the
|
|
591
|
+
* single-message envelope. A comment on WRITE_TOOLS saying "enforce this
|
|
592
|
+
* batch-aware" is the weakest available remedy for a defect whose whole
|
|
593
|
+
* character was that nobody thought to send an array — so the inheritance is
|
|
594
|
+
* mechanical here: a tool added to WRITE_TOOLS without batch enforcement fails
|
|
595
|
+
* this suite on its first run.
|
|
596
|
+
*
|
|
597
|
+
* The assertion is the write-branch ORIGIN check rather than the cap, because
|
|
598
|
+
* it needs no per-tool arguments and no per-tool budget — only two of the four
|
|
599
|
+
* write tools carry a daily cap (see DAILY_CAPPED_TOOLS).
|
|
600
|
+
*/
|
|
601
|
+
describe.each([...WRITE_TOOLS])(
|
|
602
|
+
"a batch naming %s takes the write branch (#5971)",
|
|
603
|
+
(toolName) => {
|
|
604
|
+
let app: FastifyInstance;
|
|
605
|
+
let port: number;
|
|
606
|
+
|
|
607
|
+
beforeEach(async () => {
|
|
608
|
+
// Empty allow-list ⇒ a cross-origin write must be refused. On the read
|
|
609
|
+
// branch the origin is never consulted, so a 200 here means the batch
|
|
610
|
+
// was misclassified.
|
|
611
|
+
app = await buildHttpTransportApp({
|
|
612
|
+
...DEFAULT_CONFIG,
|
|
613
|
+
cors: { writeAllowedOrigins: [] as string[] },
|
|
614
|
+
});
|
|
615
|
+
await app.listen({ host: "127.0.0.1", port: 0 });
|
|
616
|
+
port = (app.server.address() as AddressInfo).port;
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
afterEach(async () => {
|
|
620
|
+
await app.close();
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
it("is refused by the write-origin allow-list inside an array body", async () => {
|
|
624
|
+
const res = await mcpPost(
|
|
625
|
+
port,
|
|
626
|
+
[
|
|
627
|
+
{
|
|
628
|
+
jsonrpc: "2.0",
|
|
629
|
+
id: 60,
|
|
630
|
+
method: "tools/call",
|
|
631
|
+
params: { name: toolName, arguments: {} },
|
|
632
|
+
},
|
|
633
|
+
],
|
|
634
|
+
{ origin: "https://evil.example" },
|
|
635
|
+
);
|
|
636
|
+
|
|
637
|
+
expect(res.status).toBe(403);
|
|
638
|
+
});
|
|
639
|
+
},
|
|
640
|
+
);
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* Guardrail — a blank or malformed cap must not silently disable enforcement
|
|
644
|
+
* (#5971 security review round 2, Medium).
|
|
645
|
+
*
|
|
646
|
+
* `parseInt("")` is NaN, NaN survives every `??` guard, and `count >= NaN` is
|
|
647
|
+
* always false — so the cap stops refusing anything, with no log and no
|
|
648
|
+
* startup warning. A misconfigured deployment looks identical to a correct one
|
|
649
|
+
* at rest, which is why this is pinned rather than left to convention.
|
|
650
|
+
*/
|
|
651
|
+
describe("a non-finite daily cap falls back to the default, never to unlimited (#5971)", () => {
|
|
652
|
+
let app: FastifyInstance;
|
|
653
|
+
let port: number;
|
|
654
|
+
|
|
655
|
+
afterEach(async () => {
|
|
656
|
+
if (app) await app.close();
|
|
657
|
+
jest.clearAllMocks();
|
|
658
|
+
resetMcpCommerceClaim();
|
|
659
|
+
});
|
|
660
|
+
|
|
661
|
+
async function startWithCap(cap: number): Promise<void> {
|
|
662
|
+
__setMcpCommerceClaimForTest("claimed");
|
|
663
|
+
(
|
|
664
|
+
checkout.createSession as jest.MockedFunction<
|
|
665
|
+
typeof checkout.createSession
|
|
666
|
+
>
|
|
667
|
+
).mockResolvedValue({
|
|
668
|
+
ok: true,
|
|
669
|
+
body: { sessionId: "cs_nan", url: "https://checkout.stripe.com/c/nan" },
|
|
670
|
+
});
|
|
671
|
+
app = await buildHttpTransportApp({
|
|
672
|
+
...DEFAULT_CONFIG,
|
|
673
|
+
cors: { writeAllowedOrigins: ["*"] as string[] },
|
|
674
|
+
abuse: {
|
|
675
|
+
captureLeadDailyCapPerIp: 50,
|
|
676
|
+
startPurchaseDailyCapPerIp: cap,
|
|
677
|
+
blockedUserAgents: "",
|
|
678
|
+
},
|
|
679
|
+
});
|
|
680
|
+
await app.listen({ host: "127.0.0.1", port: 0 });
|
|
681
|
+
port = (app.server.address() as AddressInfo).port;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
it("NaN does not disable the start_purchase cap", async () => {
|
|
685
|
+
// What `parseInt("")` on a blank MCP_ABUSE_START_PURCHASE_DAILY_CAP yields.
|
|
686
|
+
await startWithCap(Number.NaN);
|
|
687
|
+
|
|
688
|
+
// The default (20) must still bite. 21 calls ⇒ the last one is refused.
|
|
689
|
+
let lastStatus = 0;
|
|
690
|
+
for (let i = 0; i < 21; i += 1) {
|
|
691
|
+
const res = await mcpPost(port, {
|
|
692
|
+
jsonrpc: "2.0",
|
|
693
|
+
id: 70 + i,
|
|
694
|
+
method: "tools/call",
|
|
695
|
+
params: {
|
|
696
|
+
name: "start_purchase",
|
|
697
|
+
arguments: { productCode: "pro-plan" },
|
|
698
|
+
},
|
|
699
|
+
});
|
|
700
|
+
lastStatus = res.status;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
expect(lastStatus).toBe(403);
|
|
704
|
+
});
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* #5994 — the 403-vs-422 split is keyed on a TYPED reason code, never on the
|
|
709
|
+
* wording of the refusal reason.
|
|
710
|
+
*
|
|
711
|
+
* The guard refuses for two different kinds of reason and the transport owes
|
|
712
|
+
* them different statuses: a refusal of the CALLER (blocked UA, per-IP daily
|
|
713
|
+
* cap) is 403, a refusal of the INPUT (honeypot, malformed email) is 422.
|
|
714
|
+
* Before this change the transport told them apart with
|
|
715
|
+
* `reason.includes("daily cap")`, so a reworded reason silently turned a
|
|
716
|
+
* rate-limit refusal into a 422 — and `capture_lead`'s wording carried the
|
|
717
|
+
* substring only by accident ("daily capture_lead cap" contains "daily cap"
|
|
718
|
+
* because the tool name starts with "cap").
|
|
719
|
+
*
|
|
720
|
+
* These tests pin the mapping from the code, and assert the two daily-capped
|
|
721
|
+
* tools — whose reason wordings differ — both reach 403 through it.
|
|
722
|
+
*/
|
|
723
|
+
describe("abuse refusal status is keyed on the typed reason code (#5994)", () => {
|
|
724
|
+
const CODE_KEYED_CONFIG = {
|
|
725
|
+
...DEFAULT_CONFIG,
|
|
726
|
+
cors: { writeAllowedOrigins: ["*"] as string[] },
|
|
727
|
+
abuse: {
|
|
728
|
+
captureLeadDailyCapPerIp: 1,
|
|
729
|
+
startPurchaseDailyCapPerIp: 1,
|
|
730
|
+
blockedUserAgents: "",
|
|
731
|
+
},
|
|
732
|
+
};
|
|
733
|
+
|
|
734
|
+
const createSession = checkout.createSession as jest.MockedFunction<
|
|
735
|
+
typeof checkout.createSession
|
|
736
|
+
>;
|
|
737
|
+
|
|
738
|
+
let app: FastifyInstance;
|
|
739
|
+
let port: number;
|
|
740
|
+
|
|
741
|
+
beforeEach(async () => {
|
|
742
|
+
__setMcpCommerceClaimForTest("claimed");
|
|
743
|
+
createSession.mockResolvedValue({
|
|
744
|
+
ok: true,
|
|
745
|
+
body: {
|
|
746
|
+
sessionId: "cs_test_5994",
|
|
747
|
+
url: "https://checkout.stripe.com/c/pay/cs_test_5994",
|
|
748
|
+
},
|
|
749
|
+
});
|
|
750
|
+
app = await buildHttpTransportApp(CODE_KEYED_CONFIG);
|
|
751
|
+
await app.listen({ host: "127.0.0.1", port: 0 });
|
|
752
|
+
port = (app.server.address() as AddressInfo).port;
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
afterEach(async () => {
|
|
756
|
+
await app.close();
|
|
757
|
+
jest.clearAllMocks();
|
|
758
|
+
resetMcpCommerceClaim();
|
|
759
|
+
});
|
|
760
|
+
|
|
761
|
+
function captureLeadPayload(id: number, args: Record<string, unknown>) {
|
|
762
|
+
return {
|
|
763
|
+
jsonrpc: "2.0",
|
|
764
|
+
id,
|
|
765
|
+
method: "tools/call",
|
|
766
|
+
params: { name: "capture_lead", arguments: args },
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
it("maps a capture_lead daily-cap refusal to 403", async () => {
|
|
771
|
+
// The first call spends the IP's single cap unit. Whatever the handler
|
|
772
|
+
// then makes of it is irrelevant here — only the SECOND call, refused in
|
|
773
|
+
// the preHandler, is under test.
|
|
774
|
+
await mcpPost(
|
|
775
|
+
port,
|
|
776
|
+
captureLeadPayload(40, { name: "First", email: "first@example.com" }),
|
|
777
|
+
);
|
|
778
|
+
|
|
779
|
+
const capped = await mcpPost(
|
|
780
|
+
port,
|
|
781
|
+
captureLeadPayload(41, { name: "Second", email: "second@example.com" }),
|
|
782
|
+
);
|
|
783
|
+
|
|
784
|
+
expect(capped.status).toBe(403);
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
it("maps a start_purchase daily-cap refusal to 403", async () => {
|
|
788
|
+
const purchase = (id: number) => ({
|
|
789
|
+
jsonrpc: "2.0",
|
|
790
|
+
id,
|
|
791
|
+
method: "tools/call",
|
|
792
|
+
params: { name: "start_purchase", arguments: { productCode: "pro-plan" } },
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
const first = await mcpPost(port, purchase(42));
|
|
796
|
+
const capped = await mcpPost(port, purchase(43));
|
|
797
|
+
|
|
798
|
+
expect(first.status).toBe(200);
|
|
799
|
+
expect(capped.status).toBe(403);
|
|
800
|
+
});
|
|
801
|
+
|
|
802
|
+
it("maps a validation refusal to 422", async () => {
|
|
803
|
+
const res = await mcpPost(
|
|
804
|
+
port,
|
|
805
|
+
captureLeadPayload(44, { name: "Spammer", email: "not-an-email" }),
|
|
806
|
+
);
|
|
807
|
+
|
|
808
|
+
expect(res.status).toBe(422);
|
|
809
|
+
});
|
|
810
|
+
|
|
811
|
+
/**
|
|
812
|
+
* The AbuseGuard reads exactly two things off the request — the
|
|
813
|
+
* `user-agent` header and `ip`. The UA here is one the built-in deny list
|
|
814
|
+
* lets through, so Rule 4 (the cap) is what refuses these calls rather than
|
|
815
|
+
* Rule 1.
|
|
816
|
+
*/
|
|
817
|
+
function makeGuardRequest(): FastifyRequest {
|
|
818
|
+
// @ts-expect-error: a stub carrying only the two members the guard reads;
|
|
819
|
+
// a full FastifyRequest cannot be constructed without a live server.
|
|
820
|
+
return {
|
|
821
|
+
headers: { "user-agent": "Mozilla/5.0 (compatible; TestAgent/1.0)" },
|
|
822
|
+
ip: "203.0.113.7",
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
it("derives the status from the code alone — the two daily-capped tools word their reasons differently yet both map to 403", () => {
|
|
827
|
+
const guard = new AbuseGuard({
|
|
828
|
+
captureLeadDailyCapPerIp: 1,
|
|
829
|
+
startPurchaseDailyCapPerIp: 1,
|
|
830
|
+
blockedUserAgents: "",
|
|
831
|
+
});
|
|
832
|
+
const request = makeGuardRequest();
|
|
833
|
+
|
|
834
|
+
// Spend each tool's single cap unit, then collect the refusal.
|
|
835
|
+
guard.validate("capture_lead", {}, request);
|
|
836
|
+
guard.validate("start_purchase", {}, request);
|
|
837
|
+
const lead = guard.validate("capture_lead", {}, request);
|
|
838
|
+
const purchase = guard.validate("start_purchase", {}, request);
|
|
839
|
+
|
|
840
|
+
expect(lead.allowed).toBe(false);
|
|
841
|
+
expect(purchase.allowed).toBe(false);
|
|
842
|
+
if (lead.allowed || purchase.allowed) return;
|
|
843
|
+
|
|
844
|
+
// Same code, different wording — the wording is prose for the caller, the
|
|
845
|
+
// code is what the transport reads.
|
|
846
|
+
expect(lead.code).toBe(AbuseReasonCode.DAILY_CAP_REACHED);
|
|
847
|
+
expect(purchase.code).toBe(AbuseReasonCode.DAILY_CAP_REACHED);
|
|
848
|
+
expect(lead.reason).not.toBe(purchase.reason);
|
|
849
|
+
|
|
850
|
+
expect(ABUSE_REFUSAL_STATUS[lead.code]).toBe(403);
|
|
851
|
+
expect(ABUSE_REFUSAL_STATUS[purchase.code]).toBe(403);
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
it("declares a status for every reason code the guard can return", () => {
|
|
855
|
+
expect(Object.keys(ABUSE_REFUSAL_STATUS).sort()).toEqual(
|
|
856
|
+
Object.values(AbuseReasonCode).sort(),
|
|
857
|
+
);
|
|
858
|
+
expect(ABUSE_REFUSAL_STATUS[AbuseReasonCode.BLOCKED_USER_AGENT]).toBe(403);
|
|
859
|
+
expect(ABUSE_REFUSAL_STATUS[AbuseReasonCode.HONEYPOT_FILLED]).toBe(422);
|
|
860
|
+
expect(ABUSE_REFUSAL_STATUS[AbuseReasonCode.INVALID_EMAIL]).toBe(422);
|
|
861
|
+
});
|
|
862
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structural ordering guard for apps/mcp's bootstrap (#5876 spec D4, AC-3).
|
|
3
|
+
*
|
|
4
|
+
* `apps/mcp/src/index.ts` has real side effects at import time (starts a
|
|
5
|
+
* transport, opens a stdio/HTTP listener) — it cannot safely be imported by
|
|
6
|
+
* a test. This reads its source text instead, mirroring the existing
|
|
7
|
+
* "architectural guard" pattern (`__tests__/read_content.test.ts`).
|
|
8
|
+
*/
|
|
9
|
+
import { readFileSync } from "node:fs";
|
|
10
|
+
import { resolve } from "node:path";
|
|
11
|
+
|
|
12
|
+
describe("apps/mcp/src/index.ts — bootstrap ordering", () => {
|
|
13
|
+
const src = readFileSync(resolve(__dirname, "../src/index.ts"), "utf-8");
|
|
14
|
+
|
|
15
|
+
it("awaits initMcpPublicWorkspace()", () => {
|
|
16
|
+
expect(src).toMatch(/await initMcpPublicWorkspace\(\)/);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("awaits initMcpPublicWorkspace() before either transport branch", () => {
|
|
20
|
+
const initIdx = src.indexOf("await initMcpPublicWorkspace()");
|
|
21
|
+
const transportBranchIdx = src.indexOf('if (transport === "http")');
|
|
22
|
+
|
|
23
|
+
expect(initIdx).toBeGreaterThan(-1);
|
|
24
|
+
expect(transportBranchIdx).toBeGreaterThan(-1);
|
|
25
|
+
expect(initIdx).toBeLessThan(transportBranchIdx);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("wraps the await in try/catch — a rejection (DB down at boot) must not crash the whole server (code review, #5876)", () => {
|
|
29
|
+
const tryIdx = src.indexOf("try {");
|
|
30
|
+
const initIdx = src.indexOf("await initMcpPublicWorkspace()");
|
|
31
|
+
const catchIdx = src.indexOf("} catch");
|
|
32
|
+
|
|
33
|
+
expect(tryIdx).toBeGreaterThan(-1);
|
|
34
|
+
expect(catchIdx).toBeGreaterThan(-1);
|
|
35
|
+
// The await sits strictly between its own try{ and the matching }catch.
|
|
36
|
+
expect(tryIdx).toBeLessThan(initIdx);
|
|
37
|
+
expect(initIdx).toBeLessThan(catchIdx);
|
|
38
|
+
});
|
|
39
|
+
});
|