mirror of
https://github.com/hexastack/hexabot
synced 2025-05-05 05:15:02 +00:00
fix(api): resolve allowed_domains redis bug
This commit is contained in:
parent
66a8b1b940
commit
36fa34b303
@ -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'));
|
||||
|
@ -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']);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user