Skip to content

Commit caf6a46

Browse files
authored
feat: set CORS URLs endpoint (#472)
* feat: set CORS URLs endpoint * remove debug
1 parent 19f4758 commit caf6a46

File tree

5 files changed

+77
-7
lines changed

5 files changed

+77
-7
lines changed

src/server/routes/configuration/cors/add.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@ import { ReplySchema } from "./get";
1010
const BodySchema = Type.Object({
1111
urlsToAdd: Type.Array(
1212
Type.String({
13-
description:
14-
"Comma separated list of origins to allow CORS for. Thirdweb URLs are automatically added.",
13+
description: "Comma separated list of origins that will call Engine",
1514
minLength: 1,
1615
}),
1716
),
1817
});
1918

2019
BodySchema.examples = [
2120
{
22-
urlsToAdd: ["https://example.com", "https://example2.com"],
21+
urlsToAdd: ["https://example.com", "https://subdomain.example.com"],
2322
},
2423
];
2524

src/server/routes/configuration/cors/get.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
44
import { getConfig } from "../../../../utils/cache/getConfig";
55
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
6+
import { mandatoryAllowedCorsUrls } from "../../../utils/cors-urls";
67

78
export const ReplySchema = Type.Object({
8-
result: Type.Union([Type.Array(Type.String()), Type.String(), Type.Null()]),
9+
result: Type.Array(Type.String()),
910
});
1011

1112
export async function getCorsConfiguration(fastify: FastifyInstance) {
@@ -27,8 +28,13 @@ export async function getCorsConfiguration(fastify: FastifyInstance) {
2728
handler: async (req, res) => {
2829
const config = await getConfig(false);
2930

31+
// Omit required domains.
32+
const omitted = config.accessControlAllowOrigin
33+
.split(",")
34+
.filter((url) => !mandatoryAllowedCorsUrls.includes(url));
35+
3036
res.status(200).send({
31-
result: config.accessControlAllowOrigin.split(","),
37+
result: omitted,
3238
});
3339
},
3440
});

src/server/routes/configuration/cors/remove.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import { ReplySchema } from "./get";
1010
const BodySchema = Type.Object({
1111
urlsToRemove: Type.Array(
1212
Type.String({
13-
description: "Comma separated list urls",
13+
description: "Comma separated list of origins to remove",
1414
}),
1515
),
1616
});
1717

1818
BodySchema.examples = [
1919
{
20-
urlsToRemove: ["https://example.com", "https://example2.com"],
20+
urlsToRemove: ["https://example.com", "https://subdomain.example.com"],
2121
},
2222
];
2323

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Static, Type } from "@sinclair/typebox";
2+
import { FastifyInstance } from "fastify";
3+
import { StatusCodes } from "http-status-codes";
4+
import { updateConfiguration } from "../../../../db/configuration/updateConfiguration";
5+
import { getConfig } from "../../../../utils/cache/getConfig";
6+
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
7+
import { mandatoryAllowedCorsUrls } from "../../../utils/cors-urls";
8+
import { ReplySchema } from "./get";
9+
10+
const BodySchema = Type.Object({
11+
urls: Type.Array(
12+
Type.String({
13+
description: "Comma separated list of origins that will call Engine",
14+
minLength: 1,
15+
}),
16+
),
17+
});
18+
19+
BodySchema.examples = [
20+
{
21+
urls: ["https://example.com", "https://subdomain.example.com"],
22+
},
23+
];
24+
25+
export async function setUrlsToCorsConfiguration(fastify: FastifyInstance) {
26+
fastify.route<{
27+
Body: Static<typeof BodySchema>;
28+
Reply: Static<typeof ReplySchema>;
29+
}>({
30+
method: "PUT",
31+
url: "/configuration/cors",
32+
schema: {
33+
summary: "Set CORS URLs",
34+
description:
35+
"Replaces the CORS URLs to allow client-side calls to Engine",
36+
tags: ["Configuration"],
37+
operationId: "setUrlsToCorsConfiguration",
38+
body: BodySchema,
39+
response: {
40+
...standardResponseSchema,
41+
[StatusCodes.OK]: ReplySchema,
42+
},
43+
},
44+
handler: async (req, res) => {
45+
const urls = req.body.urls.map((url) => url.trim());
46+
47+
// Add required domains and dedupe.
48+
const dedupe = Array.from(
49+
new Set([...urls, ...mandatoryAllowedCorsUrls]),
50+
);
51+
52+
await updateConfiguration({
53+
accessControlAllowOrigin: dedupe.join(","),
54+
});
55+
56+
// Fetch and return the updated configuration
57+
const config = await getConfig(false);
58+
res.status(200).send({
59+
result: config.accessControlAllowOrigin.split(","),
60+
});
61+
},
62+
});
63+
}

src/server/routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ import { sendSignedUserOp } from "./transaction/blockchain/sendSignedUserOp";
116116
import { checkGroupStatus } from "./transaction/group";
117117

118118
// Indexer
119+
import { setUrlsToCorsConfiguration } from "./configuration/cors/set";
119120
import { getContractEventLogs } from "./contract/events/getContractEventLogs";
120121
import { getEventLogs } from "./contract/events/getEventLogsByTimestamp";
121122
import { pageEventLogs } from "./contract/events/paginateEventLogs";
@@ -162,6 +163,7 @@ export const withRoutes = async (fastify: FastifyInstance) => {
162163
await fastify.register(getCorsConfiguration);
163164
await fastify.register(addUrlToCorsConfiguration);
164165
await fastify.register(removeUrlToCorsConfiguration);
166+
await fastify.register(setUrlsToCorsConfiguration);
165167
await fastify.register(getCacheConfiguration);
166168
await fastify.register(updateCacheConfiguration);
167169

0 commit comments

Comments
 (0)