Skip to content

Commit

Permalink
Merge branch 'feat/allowed-origins-config' into 'dev'
Browse files Browse the repository at this point in the history
feat(watcher-service): add allowed origin config

Closes #204

See merge request ergo/rosen-bridge/watcher!199
  • Loading branch information
zargarzadehm committed Dec 13, 2023
2 parents 6e2851b + 3920e48 commit aead3fb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions services/watcher/config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ database:
# name: '' # database name (for postgres)
api:
port: 3000 # port used to run express server
allowedOrigins: [] # list of allowed origins for CORS requests
healthCheck:
interval: 60 # health check update interval (in seconds)
asset:
Expand Down
13 changes: 13 additions & 0 deletions services/watcher/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class Config {
rosenConfigPath: string;
rosenTokensPath: string;
apiPort: number;
apiAllowedOrigins: string[];

constructor() {
this.networkType = getRequiredString('ergo.network').toLowerCase();
Expand Down Expand Up @@ -205,6 +206,18 @@ class Config {
path.join(this.rosenConfigPath, 'tokens.json')
);
this.apiPort = getOptionalNumber('api.port', 3000);
this.apiAllowedOrigins = config.get<string[]>('api.allowedOrigins');
if (
!Array.isArray(this.apiAllowedOrigins) ||
this.apiAllowedOrigins.some((origin) => typeof origin !== 'string')
) {
throw new Error('ImproperlyConfigured. Api allowed origins is invalid.');
}
if (this.apiAllowedOrigins.find((origin) => origin === '*')) {
console.warn(
'An allowed origin header with value "*" will cause all origins to be able to request this service, which may cause security issues'
);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion services/watcher/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ const init = async () => {
const initExpress = () => {
const app = express();
app.use(express.json());
app.use(cors());
const allowedOrigins = getConfig().general.apiAllowedOrigins;
app.use(
cors({
origin: allowedOrigins.includes('*') ? '*' : allowedOrigins,
})
);

const router = Router();
router.use('/address', addressRouter);
Expand Down

0 comments on commit aead3fb

Please sign in to comment.