Skip to content

Commit 2746071

Browse files
authored
fix: Append env cors to make it backward compatible with db cors list (#385)
* fix: Append env cors list to make it backward compatible with db cors list * fix auth length check * clean up types around getWebhook since it doesn't return undefined * simplify cache get logic
1 parent a97a85c commit 2746071

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

src/db/configuration/getConfiguration.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ export const getConfiguration = async (): Promise<Config> => {
185185
authDomain: "thirdweb.com",
186186
authWalletEncryptedJson: await createAuthWalletEncryptedJson(),
187187
minWalletBalance: "20000000000000000",
188-
accessControlAllowOrigin: !!process.env.ACCESS_CONTROL_ALLOW_ORIGIN
189-
? process.env.ACCESS_CONTROL_ALLOW_ORIGIN
190-
: mandatoryAllowedCorsUrls.join(","),
188+
accessControlAllowOrigin: mandatoryAllowedCorsUrls.join(","),
191189
clearCacheCronSchedule: "*/30 * * * * *",
192190
},
193191
update: {},
@@ -200,9 +198,7 @@ export const getConfiguration = async (): Promise<Config> => {
200198
});
201199
} else if (!config.accessControlAllowOrigin) {
202200
config = await updateConfiguration({
203-
accessControlAllowOrigin: !!process.env.ACCESS_CONTROL_ALLOW_ORIGIN
204-
? process.env.ACCESS_CONTROL_ALLOW_ORIGIN
205-
: mandatoryAllowedCorsUrls.join(","),
201+
accessControlAllowOrigin: mandatoryAllowedCorsUrls.join(","),
206202
});
207203
}
208204

src/server/middleware/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export const withAuth = async (server: FastifyInstance) => {
301301
}
302302

303303
const authWebhooks = await getWebhook(WebhooksEventTypes.AUTH);
304-
if (authWebhooks) {
304+
if (authWebhooks.length > 0) {
305305
const authResponses = await Promise.all(
306306
authWebhooks.map((webhook) =>
307307
sendWebhookRequest(webhook, {

src/server/middleware/cors/cors.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,14 @@ export const fastifyCors = async (
143143
) => {
144144
const config = await getConfig();
145145

146-
const originArray = config.accessControlAllowOrigin.split(",") as string[];
146+
const corsListConfig = config.accessControlAllowOrigin.split(",");
147+
/**
148+
* @deprecated
149+
* Future versions will remove this env var.
150+
*/
151+
const corsListEnv = process.env.ACCESS_CONTROL_ALLOW_ORIGIN?.split(",") ?? [];
152+
const originArray = [...corsListConfig, ...corsListEnv];
153+
147154
opts.origin = originArray.map(sanitizeOrigin);
148155

149156
let hideOptionsRoute = true;

src/server/utils/webhook.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export const sendWebhooks = async (webhooks: WebhookData[]) => {
122122

123123
const webhookConfigs = await Promise.all([
124124
...((await getWebhook(WebhooksEventTypes.ALL_TX)) || []),
125-
...(webhookStatus ? (await getWebhook(webhookStatus)) || [] : []),
125+
...(webhookStatus ? await getWebhook(webhookStatus) : []),
126126
]);
127127

128128
await Promise.all(
@@ -161,11 +161,11 @@ export const sendBalanceWebhook = async (
161161
return;
162162
}
163163

164-
const webhookConfig = await getWebhook(
164+
const webhooks = await getWebhook(
165165
WebhooksEventTypes.BACKEND_WALLET_BALANCE,
166166
);
167167

168-
if (!webhookConfig) {
168+
if (webhooks.length === 0) {
169169
logger({
170170
service: "server",
171171
level: "debug",
@@ -175,7 +175,7 @@ export const sendBalanceWebhook = async (
175175
return;
176176
}
177177

178-
webhookConfig.map(async (config) => {
178+
webhooks.map(async (config) => {
179179
if (!config || !config.active) {
180180
logger({
181181
service: "server",

src/utils/cache/getWebhook.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ export const webhookCache = new Map<string, SanitizedWebHooksSchema[]>();
99
export const getWebhook = async (
1010
eventType: WebhooksEventTypes,
1111
retrieveFromCache = true,
12-
): Promise<SanitizedWebHooksSchema[] | undefined> => {
12+
): Promise<SanitizedWebHooksSchema[]> => {
1313
const cacheKey = eventType;
1414

15-
if (retrieveFromCache) {
16-
if (webhookCache.has(cacheKey) && webhookCache.get(cacheKey)) {
17-
return webhookCache.get(cacheKey) as SanitizedWebHooksSchema[];
18-
}
15+
if (retrieveFromCache && webhookCache.has(cacheKey)) {
16+
return webhookCache.get(cacheKey) as SanitizedWebHooksSchema[];
1917
}
2018

2119
const webhookConfig = await getAllWebhooks();

0 commit comments

Comments
 (0)