Giveth / impact-graph

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add CORS switch in config

aminlatifi opened this issue · comments

CORS checking cannot be disabled, let's add a config entry to don't/do load cors middleware based on that.

@aminlatifi
We have it already

const whitelistHostnames: string[] = (
config.get('HOSTNAME_WHITELIST') as string
).split(',');
const corsOptions = {
origin(origin, callback) {
if (!origin) {
// allow requests with no origin (like mobile apps, Curl, ...)
return callback(null, true);
}
// removing http:// , https://, and :port
const formattedOrigin = origin
.replace('https://', '')
.replace('http://', '')
.split(':')[0];
for (const allowedOrigin of whitelistHostnames) {
// passing all subdomains of whitelist hosts, for instance x.vercel.app, x.giveth.io,...
if (
formattedOrigin === allowedOrigin ||
formattedOrigin.endsWith(`.${allowedOrigin}`)
) {
return callback(null, true);
}
}
logger.error('CORS error', { whitelistHostnames, origin });
callback(new Error('Not allowed by CORS'));
},
};
const bodyParserJson = bodyParser.json({

@aminlatifi We have it already

const whitelistHostnames: string[] = (
config.get('HOSTNAME_WHITELIST') as string
).split(',');
const corsOptions = {
origin(origin, callback) {
if (!origin) {
// allow requests with no origin (like mobile apps, Curl, ...)
return callback(null, true);
}
// removing http:// , https://, and :port
const formattedOrigin = origin
.replace('https://', '')
.replace('http://', '')
.split(':')[0];
for (const allowedOrigin of whitelistHostnames) {
// passing all subdomains of whitelist hosts, for instance x.vercel.app, x.giveth.io,...
if (
formattedOrigin === allowedOrigin ||
formattedOrigin.endsWith(`.${allowedOrigin}`)
) {
return callback(null, true);
}
}
logger.error('CORS error', { whitelistHostnames, origin });
callback(new Error('Not allowed by CORS'));
},
};
const bodyParserJson = bodyParser.json({

I meant this line

app.use(cors(corsOptions));

CORS is enabled whole the time

It's already merge to the staging. Devops can test it

@mhmdksh let's close this one if you confirm it's done.

Yes, I can confirm that it's working. We can close this issue now. Thanks @aminlatifi