Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make CORS headers configurable #529

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ prepayminerfee = false
host = "127.0.0.1"
port = 9_001

# Configure CORS headers set by the backend
# "" to disable
cors = "*"

# The backend can expose a metrics endpoint about swap count, volume, etc
# [prometheus]
# host = "127.0.0.1"
Expand Down
2 changes: 2 additions & 0 deletions lib/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ type EthereumConfig = RskConfig & {
type ApiConfig = {
host: string;
port: number;
cors?: string | string[];
};

type GrpcConfig = {
Expand Down Expand Up @@ -244,6 +245,7 @@ class Config {
api: {
host: '127.0.0.1',
port: 9001,
cors: '*',
},

grpc: {
Expand Down
13 changes: 12 additions & 1 deletion lib/api/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ class Api {
) {
this.app = express();
this.app.set('trust proxy', 'loopback');
this.app.use(cors());

if (config.cors === undefined || config.cors.length !== 0) {
this.app.use(
cors({
origin: config.cors || '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
}),
);
}

this.app.use(
express.json({
verify(req, _, buf: Buffer, encoding: string) {
Expand Down
Loading