mirror of
https://github.com/hexastack/hexabot
synced 2025-05-05 13:24:37 +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);
|
const settingService = app.get<SettingService>(SettingService);
|
||||||
app.enableCors({
|
app.enableCors({
|
||||||
origin: (origin, callback) => {
|
origin: async (origin, callback) => {
|
||||||
settingService
|
await settingService
|
||||||
.getAllowedOrigins()
|
.getAllowedOrigins()
|
||||||
.then((allowedOrigins) => {
|
.then((allowedOrigins) => {
|
||||||
if (!origin || allowedOrigins.has(origin)) {
|
if (!origin || allowedOrigins.includes(origin)) {
|
||||||
callback(null, true);
|
callback(null, true);
|
||||||
} else {
|
} else {
|
||||||
callback(new Error('Not allowed by CORS'));
|
callback(new Error('Not allowed by CORS'));
|
||||||
|
@ -195,7 +195,7 @@ describe('SettingService', () => {
|
|||||||
expect(settingService.find).toHaveBeenCalledWith({
|
expect(settingService.find).toHaveBeenCalledWith({
|
||||||
label: 'allowed_domains',
|
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
|
* @returns A promise that resolves to a set of allowed origins
|
||||||
*/
|
*/
|
||||||
@Cacheable(ALLOWED_ORIGINS_CACHE_KEY)
|
@Cacheable(ALLOWED_ORIGINS_CACHE_KEY)
|
||||||
async getAllowedOrigins() {
|
async getAllowedOrigins(): Promise<string[]> {
|
||||||
const settings = (await this.find({
|
const settings = (await this.find({
|
||||||
label: 'allowed_domains',
|
label: 'allowed_domains',
|
||||||
})) as TextSetting[];
|
})) as TextSetting[];
|
||||||
@ -150,7 +150,7 @@ export class SettingService extends BaseService<Setting> {
|
|||||||
...allowedDomains,
|
...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.cookie && { cookie: config.sockets.cookie }),
|
||||||
...(config.sockets.onlyAllowOrigins && {
|
...(config.sockets.onlyAllowOrigins && {
|
||||||
cors: {
|
cors: {
|
||||||
origin: (origin, cb) => {
|
origin: async (origin, cb) => {
|
||||||
// Retrieve the allowed origins from the settings
|
// Retrieve the allowed origins from the settings
|
||||||
const app = AppInstance.getApp();
|
const app = AppInstance.getApp();
|
||||||
const settingService = app.get<SettingService>(SettingService);
|
const settingService = app.get<SettingService>(SettingService);
|
||||||
|
|
||||||
settingService
|
await settingService
|
||||||
.getAllowedOrigins()
|
.getAllowedOrigins()
|
||||||
.then((allowedOrigins) => {
|
.then((allowedOrigins) => {
|
||||||
if (origin && allowedOrigins.has(origin)) {
|
if (origin && allowedOrigins.includes(origin)) {
|
||||||
cb(null, true);
|
cb(null, true);
|
||||||
} else {
|
} else {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
|
Loading…
Reference in New Issue
Block a user