diff --git a/src/favicon/favicon.controller.ts b/src/favicon/favicon.controller.ts index ac5f62f..92aaf12 100644 --- a/src/favicon/favicon.controller.ts +++ b/src/favicon/favicon.controller.ts @@ -1,4 +1,5 @@ import { Controller, Get, Param, Redirect, Res } from '@nestjs/common'; +import { EnvironmentService } from '../packages/environment/environment.service'; import { FaviconService } from './favicon.service'; import { Response } from 'express'; import { DEFAULT_SIZE, SUPPORTED_SIZES } from './favicon.constants'; @@ -7,12 +8,15 @@ import { Domain } from './utils/domain'; @Controller() export class FaviconController { - constructor(private readonly faviconService: FaviconService) {} + constructor(private readonly faviconService: FaviconService, + private readonly environmentService: EnvironmentService) { + } @Get() @Redirect('https://github.com/twentyhq/favicon/blob/main/README.md', 301) // eslint-disable-next-line @typescript-eslint/no-empty-function - redirectToReadme() {} + redirectToReadme() { + } @Get('/health') checkHealth(@Res() res: Response) { @@ -34,6 +38,8 @@ export class FaviconController { @Param() params: { domainName: string; size: string }, @Res() res: Response, ) { + res.set('Access-Control-Allow-Origin', this.environmentService.getAllowedOrigins()); + const domainName = params.domainName; const size = params.size ?? DEFAULT_SIZE; if (!Domain.checkDomainIsValid(domainName)) { diff --git a/src/packages/environment/environment.service.ts b/src/packages/environment/environment.service.ts index 43a5174..7a9785f 100644 --- a/src/packages/environment/environment.service.ts +++ b/src/packages/environment/environment.service.ts @@ -28,4 +28,10 @@ export class EnvironmentService { this.configService.get('STORAGE_LOCAL_PATH') ?? '.local-storage' ); } + + getAllowedOrigins(): string { + return ( + this.configService.get('ALLOWED_ORIGINS') ?? '*' + ); + } } diff --git a/src/packages/environment/environment.validation.ts b/src/packages/environment/environment.validation.ts index 0e1bde8..739779b 100644 --- a/src/packages/environment/environment.validation.ts +++ b/src/packages/environment/environment.validation.ts @@ -30,6 +30,10 @@ export class EnvironmentVariables { @IsString() @ValidateIf((env) => env.STORAGE_TYPE === StorageType.Local) STORAGE_LOCAL_PATH?: string; + + @IsString() + @IsOptional() + ALLOWED_ORIGINS: string; } export function validate(config: Record) {