Skip to content

Commit

Permalink
feat: set CORS URLs endpoint (#472)
Browse files Browse the repository at this point in the history
* feat: set CORS URLs endpoint

* remove debug
  • Loading branch information
arcoraven authored Mar 28, 2024
1 parent 19f4758 commit caf6a46
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/server/routes/configuration/cors/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ import { ReplySchema } from "./get";
const BodySchema = Type.Object({
urlsToAdd: Type.Array(
Type.String({
description:
"Comma separated list of origins to allow CORS for. Thirdweb URLs are automatically added.",
description: "Comma separated list of origins that will call Engine",
minLength: 1,
}),
),
});

BodySchema.examples = [
{
urlsToAdd: ["https://example.com", "https://example2.com"],
urlsToAdd: ["https://example.com", "https://subdomain.example.com"],
},
];

Expand Down
10 changes: 8 additions & 2 deletions src/server/routes/configuration/cors/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { getConfig } from "../../../../utils/cache/getConfig";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
import { mandatoryAllowedCorsUrls } from "../../../utils/cors-urls";

export const ReplySchema = Type.Object({
result: Type.Union([Type.Array(Type.String()), Type.String(), Type.Null()]),
result: Type.Array(Type.String()),
});

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

// Omit required domains.
const omitted = config.accessControlAllowOrigin
.split(",")
.filter((url) => !mandatoryAllowedCorsUrls.includes(url));

res.status(200).send({
result: config.accessControlAllowOrigin.split(","),
result: omitted,
});
},
});
Expand Down
4 changes: 2 additions & 2 deletions src/server/routes/configuration/cors/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import { ReplySchema } from "./get";
const BodySchema = Type.Object({
urlsToRemove: Type.Array(
Type.String({
description: "Comma separated list urls",
description: "Comma separated list of origins to remove",
}),
),
});

BodySchema.examples = [
{
urlsToRemove: ["https://example.com", "https://example2.com"],
urlsToRemove: ["https://example.com", "https://subdomain.example.com"],
},
];

Expand Down
63 changes: 63 additions & 0 deletions src/server/routes/configuration/cors/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { updateConfiguration } from "../../../../db/configuration/updateConfiguration";
import { getConfig } from "../../../../utils/cache/getConfig";
import { standardResponseSchema } from "../../../schemas/sharedApiSchemas";
import { mandatoryAllowedCorsUrls } from "../../../utils/cors-urls";
import { ReplySchema } from "./get";

const BodySchema = Type.Object({
urls: Type.Array(
Type.String({
description: "Comma separated list of origins that will call Engine",
minLength: 1,
}),
),
});

BodySchema.examples = [
{
urls: ["https://example.com", "https://subdomain.example.com"],
},
];

export async function setUrlsToCorsConfiguration(fastify: FastifyInstance) {
fastify.route<{
Body: Static<typeof BodySchema>;
Reply: Static<typeof ReplySchema>;
}>({
method: "PUT",
url: "/configuration/cors",
schema: {
summary: "Set CORS URLs",
description:
"Replaces the CORS URLs to allow client-side calls to Engine",
tags: ["Configuration"],
operationId: "setUrlsToCorsConfiguration",
body: BodySchema,
response: {
...standardResponseSchema,
[StatusCodes.OK]: ReplySchema,
},
},
handler: async (req, res) => {
const urls = req.body.urls.map((url) => url.trim());

// Add required domains and dedupe.
const dedupe = Array.from(
new Set([...urls, ...mandatoryAllowedCorsUrls]),
);

await updateConfiguration({
accessControlAllowOrigin: dedupe.join(","),
});

// Fetch and return the updated configuration
const config = await getConfig(false);
res.status(200).send({
result: config.accessControlAllowOrigin.split(","),
});
},
});
}
2 changes: 2 additions & 0 deletions src/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ import { sendSignedUserOp } from "./transaction/blockchain/sendSignedUserOp";
import { checkGroupStatus } from "./transaction/group";

// Indexer
import { setUrlsToCorsConfiguration } from "./configuration/cors/set";
import { getContractEventLogs } from "./contract/events/getContractEventLogs";
import { getEventLogs } from "./contract/events/getEventLogsByTimestamp";
import { pageEventLogs } from "./contract/events/paginateEventLogs";
Expand Down Expand Up @@ -162,6 +163,7 @@ export const withRoutes = async (fastify: FastifyInstance) => {
await fastify.register(getCorsConfiguration);
await fastify.register(addUrlToCorsConfiguration);
await fastify.register(removeUrlToCorsConfiguration);
await fastify.register(setUrlsToCorsConfiguration);
await fastify.register(getCacheConfiguration);
await fastify.register(updateCacheConfiguration);

Expand Down

0 comments on commit caf6a46

Please sign in to comment.