Skip to content

Commit

Permalink
feat: Allow receiving an array of sites as CORS ORIGIN (#740)
Browse files Browse the repository at this point in the history
* feat: Allow receiving an array of sites as CORS ORIGIN

* fix: Allow CORS variable to accept regex
  • Loading branch information
LautaroPetaccio authored Apr 15, 2024
1 parent 85b5c8a commit 3387a3e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ BUILDER_URL=https://builder.decentraland.org
CHAIN_NAME=Ethereum Mainnet
COLLECTIONS_GRAPH_URL=https://subgraph.decentraland.org/collections-matic-mainnet
CONNECTION_STRING='postgres://localhost:5432/builder'
CORS_METHOD=*
CORS_ORIGIN=*
DEFAULT_ASSET_PACK_CACHE=86400000
DEFAULT_ETH_ADDRESS=
DEFAULT_USER_ID=
Expand All @@ -30,6 +28,8 @@ PEER_URL=https://peer.decentraland.org
RPC_URL=https://rpc.decentraland.org/polygon
SERVER_PORT=5000
THIRD_PARTY_GRAPH_URL=https://subgraph.decentraland.org/tpr-matic-mainnet
CORS_ORIGIN='http://localhost:5173'
CORS_METHOD=*
WAREHOUSE_CONTEXT_PREFIX=
WAREHOUSE_URL=
WAREHOUSE_TOKEN=
Expand Down
8 changes: 4 additions & 4 deletions src/common/ExpressApp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express'
import cors from 'cors'
import cors, { CorsOptions } from 'cors'
import { collectDefaultMetrics } from 'prom-client'
import { createTestMetricsComponent } from '@well-known-components/metrics'
import { getDefaultHttpMetrics } from '@well-known-components/metrics/dist/http'
Expand All @@ -21,9 +21,9 @@ export class ExpressApp {
return this
}

useCORS(origin: string, method: string) {
const corsOptions = {
origin: origin,
useCORS(origin: CorsOptions['origin'], method: string) {
const corsOptions: CorsOptions = {
origin,
methods: method,
allowedHeaders: '*',
exposedHeaders: [
Expand Down
17 changes: 16 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,24 @@ import { errorHandler } from './common/errorHandler'

const SERVER_PORT = env.get('SERVER_PORT', '5000')
const API_VERSION = env.get('API_VERSION', 'v1')
const CORS_ORIGIN = env.get('CORS_ORIGIN', '*')
let CORS_ORIGIN: string | RegExp | (string | RegExp)[] = env.get(
'CORS_ORIGIN',
'*'
)
const CORS_METHOD = env.get('CORS_METHOD', '*')

if (CORS_ORIGIN.split(';').length > 1) {
CORS_ORIGIN = CORS_ORIGIN.split(';')
.map((origin) => origin.trim())
.map((origin) =>
origin.startsWith('regex:')
? new RegExp(origin.replace('regex:', ''))
: origin
)
} else if (CORS_ORIGIN.startsWith('regex:')) {
CORS_ORIGIN = new RegExp(CORS_ORIGIN.replace('regex:', ''))
}

export const app = new ExpressApp()
const logs = createConsoleLogComponent()

Expand Down

0 comments on commit 3387a3e

Please sign in to comment.