Skip to content

Commit

Permalink
fix: cors issue for http server
Browse files Browse the repository at this point in the history
  • Loading branch information
abdou6666 committed Jan 23, 2025
1 parent dee2eb5 commit 5a45242
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
11 changes: 10 additions & 1 deletion api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { HexabotModule } from './app.module';
import { config } from './config';
import { LoggerService } from './logger/logger.service';
import { seedDatabase } from './seeder';
import { SettingService } from './setting/services/setting.service';
import { swagger } from './swagger';
import { getSessionStore } from './utils/constants/session-store';
import { ObjectIdPipe } from './utils/pipes/object-id.pipe';
Expand All @@ -43,8 +44,16 @@ async function bootstrap() {
app.use(bodyParser.urlencoded({ verify: rawBodyBuffer, extended: true }));
app.use(bodyParser.json({ verify: rawBodyBuffer }));

const settingService = app.get<SettingService>(SettingService);
const allowedDomains = await settingService.getAllowedDomains();
app.enableCors({
origin: config.security.cors.allowOrigins,
origin: (origin, callback) => {
if (!origin || allowedDomains.has(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
methods: config.security.cors.methods,
credentials: config.security.cors.allowCredentials,
allowedHeaders: config.security.cors.headers.split(','),
Expand Down
23 changes: 22 additions & 1 deletion api/src/setting/services/setting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import { Cache } from 'cache-manager';
import { config } from '@/config';
import { Config } from '@/config/types';
import { LoggerService } from '@/logger/logger.service';
import { SETTING_CACHE_KEY } from '@/utils/constants/cache';
import {
ALLOWED_DOMAINS_CACHE_KEY,
SETTING_CACHE_KEY,
} from '@/utils/constants/cache';
import { Cacheable } from '@/utils/decorators/cacheable.decorator';
import { BaseService } from '@/utils/generics/base-service';

Expand Down Expand Up @@ -110,6 +113,7 @@ export class SettingService extends BaseService<Setting> {
*/
async clearCache() {
this.cacheManager.del(SETTING_CACHE_KEY);
this.cacheManager.del(ALLOWED_DOMAINS_CACHE_KEY);
}

/**
Expand All @@ -121,6 +125,23 @@ export class SettingService extends BaseService<Setting> {
this.clearCache();
}

/**
* Retrieves allowed_domains from the cache if available, or loads them from the
* repository and caches the result.
*
* @returns A promise that resolves to a Set of`allowed_domains` string.
*/
@Cacheable(ALLOWED_DOMAINS_CACHE_KEY)
async getAllowedDomains() {
// combines all allowed_doamins and whitelist them for cors
const settings = await this.find({ label: 'allowed_domains' });

const whiteListedOrigins = new Set(
settings.flatMap((setting) => setting.value.split(',')),
);
return whiteListedOrigins;
}

/**
* Retrieves settings from the cache if available, or loads them from the
* repository and caches the result.
Expand Down
2 changes: 2 additions & 0 deletions api/src/utils/constants/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ export const MENU_CACHE_KEY = 'menu';
export const LANGUAGES_CACHE_KEY = 'languages';

export const DEFAULT_LANGUAGE_CACHE_KEY = 'default_language';

export const ALLOWED_DOMAINS_CACHE_KEY = 'allowed-domains';

0 comments on commit 5a45242

Please sign in to comment.