fix(api): resolve allowed_domains redis bug

This commit is contained in:
yassinedorbozgithub 2025-04-12 04:58:56 +01:00
parent 66a8b1b940
commit 36fa34b303
4 changed files with 9 additions and 9 deletions

View File

@ -50,11 +50,11 @@ async function bootstrap() {
const settingService = app.get<SettingService>(SettingService);
app.enableCors({
origin: (origin, callback) => {
settingService
origin: async (origin, callback) => {
await settingService
.getAllowedOrigins()
.then((allowedOrigins) => {
if (!origin || allowedOrigins.has(origin)) {
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));

View File

@ -195,7 +195,7 @@ describe('SettingService', () => {
expect(settingService.find).toHaveBeenCalledWith({
label: 'allowed_domains',
});
expect(result).toEqual(new Set(['*', 'https://example.com']));
expect(result).toEqual(['*', 'https://example.com']);
});
});
});

View File

@ -135,7 +135,7 @@ export class SettingService extends BaseService<Setting> {
* @returns A promise that resolves to a set of allowed origins
*/
@Cacheable(ALLOWED_ORIGINS_CACHE_KEY)
async getAllowedOrigins() {
async getAllowedOrigins(): Promise<string[]> {
const settings = (await this.find({
label: 'allowed_domains',
})) as TextSetting[];
@ -150,7 +150,7 @@ export class SettingService extends BaseService<Setting> {
...allowedDomains,
]);
return uniqueOrigins;
return Array.from(uniqueOrigins);
}
/**

View File

@ -54,15 +54,15 @@ export const buildWebSocketGatewayOptions = (): Partial<ServerOptions> => {
...(config.sockets.cookie && { cookie: config.sockets.cookie }),
...(config.sockets.onlyAllowOrigins && {
cors: {
origin: (origin, cb) => {
origin: async (origin, cb) => {
// Retrieve the allowed origins from the settings
const app = AppInstance.getApp();
const settingService = app.get<SettingService>(SettingService);
settingService
await settingService
.getAllowedOrigins()
.then((allowedOrigins) => {
if (origin && allowedOrigins.has(origin)) {
if (origin && allowedOrigins.includes(origin)) {
cb(null, true);
} else {
// eslint-disable-next-line no-console