Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/config.devnet-old.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,8 @@ inflation:
nftProcess:
parallelism: 1
maxRetries: 3
restrictedRoutes:
enabled: true
routes:
- '/package.json'
- '/docs/package.json'
5 changes: 5 additions & 0 deletions config/config.devnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,8 @@ compression:
level: 6
threshold: 1024
chunkSize: 16384
restrictedRoutes:
enabled: true
routes:
- '/package.json'
- '/docs/package.json'
5 changes: 5 additions & 0 deletions config/config.e2e-mocked.mainnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,8 @@ test:
transaction-action:
mex:
microServiceUrl: 'https://graph.xexchange.com/graphql'
restrictedRoutes:
enabled: true
routes:
- '/package.json'
- '/docs/package.json'
7 changes: 6 additions & 1 deletion config/config.e2e.mainnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,9 @@ stakingV5Inflation:
- 1262802
nftProcess:
parallelism: 1
maxRetries: 3
maxRetries: 3
restrictedRoutes:
enabled: true
routes:
- '/package.json'
- '/docs/package.json'
6 changes: 6 additions & 0 deletions config/config.mainnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,9 @@ customUrlHeaders:
- urlPattern: ''
headers:
x-custom-auth: ''
restrictedRoutes:
enabled: true
routes:
- '/package.json'
- '/docs/package.json'

5 changes: 5 additions & 0 deletions config/config.testnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,8 @@ compression:
level: 6
threshold: 1024
chunkSize: 16384
restrictedRoutes:
enabled: true
routes:
- '/package.json'
- '/docs/package.json'
8 changes: 8 additions & 0 deletions src/common/api-config/api.config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1104,4 +1104,12 @@ export class ApiConfigService {

return timestamp;
}

isRestrictedRoutesEnabled(): boolean {
return this.configService.get<boolean>('restrictedRoutes.enabled') ?? false;
}

getRestrictedRoutes(): string[] {
return this.configService.get<string[]>('restrictedRoutes.routes') ?? [];
}
}
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import * as requestIp from 'request-ip';
import compression from 'compression';
import { IoAdapter } from '@nestjs/platform-socket.io';
import { WebsocketSubscriptionModule } from './crons/websocket/websocket.subscription.module';
import { RestrictedRoutesMiddleware } from './utils/restricted.routes.middleware';

async function bootstrap() {
const logger = new Logger('Bootstrapper');
Expand Down Expand Up @@ -186,9 +187,16 @@ async function bootstrap() {
logger.log(`Guest caching enabled: ${apiConfigService.isGuestCacheFeatureActive()}`);
logger.log(`Transaction pool enabled: ${apiConfigService.isTransactionPoolEnabled()}`);
logger.log(`Transaction pool cache warmer enabled: ${apiConfigService.isTransactionPoolCacheWarmerEnabled()}`);

logger.log(`Restricted routes enabled: ${apiConfigService.isRestrictedRoutesEnabled()}`);
}

async function configurePublicApp(publicApp: NestExpressApplication, apiConfigService: ApiConfigService) {
if (apiConfigService.isRestrictedRoutesEnabled()) {
const restrictedRoutesMiddleware = publicApp.get(RestrictedRoutesMiddleware);
publicApp.use(restrictedRoutesMiddleware.use.bind(restrictedRoutesMiddleware));
}

if (apiConfigService.getCompressionEnabled()) {
publicApp.use(compression({
filter: (req: any, res: any) => {
Expand Down
2 changes: 2 additions & 0 deletions src/public.app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { GuestCacheService } from '@multiversx/sdk-nestjs-cache';
import { LoggingModule } from '@multiversx/sdk-nestjs-common';
import { DynamicModuleUtils } from './utils/dynamic.module.utils';
import { LocalCacheController } from './endpoints/caching/local.cache.controller';
import { RestrictedRoutesMiddleware } from './utils/restricted.routes.middleware';

@Module({
imports: [
Expand All @@ -23,6 +24,7 @@ import { LocalCacheController } from './endpoints/caching/local.cache.controller
providers: [
DynamicModuleUtils.getNestJsApiConfigService(),
GuestCacheService,
RestrictedRoutesMiddleware,
],
exports: [
EndpointsServicesModule,
Expand Down
45 changes: 45 additions & 0 deletions src/test/unit/utils/restricted.routes.middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { NotFoundException } from '@nestjs/common';
import { Request, Response } from 'express';
import { ApiConfigService } from 'src/common/api-config/api.config.service';
import { RestrictedRoutesMiddleware } from 'src/utils/restricted.routes.middleware';

describe('RestrictedRoutesMiddleware', () => {
let middleware: RestrictedRoutesMiddleware;
let apiConfigService: jest.Mocked<ApiConfigService>;

beforeEach(() => {
apiConfigService = {
getRestrictedRoutes: jest.fn(),
} as unknown as jest.Mocked<ApiConfigService>;

middleware = new RestrictedRoutesMiddleware(apiConfigService);
});

it('should throw NotFoundException when route is restricted', () => {
apiConfigService.getRestrictedRoutes.mockReturnValue(['/blocked']);

const req = {
path: '/blocked',
} as Request;
const res = {} as Response;
const next = jest.fn();

expect(() => middleware.use(req, res, next)).toThrow(NotFoundException);
expect(() => middleware.use(req, res, next)).toThrow('Cannot GET /blocked');
expect(next).not.toHaveBeenCalled();
});

it('should call next when route is not restricted', () => {
apiConfigService.getRestrictedRoutes.mockReturnValue(['/blocked']);

const req = {
path: '/allowed',
} as Request;
const res = {} as Response;
const next = jest.fn();

middleware.use(req, res, next);

expect(next).toHaveBeenCalledTimes(1);
});
});
19 changes: 19 additions & 0 deletions src/utils/restricted.routes.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable, NestMiddleware, NotFoundException } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { ApiConfigService } from 'src/common/api-config/api.config.service';

@Injectable()
export class RestrictedRoutesMiddleware implements NestMiddleware {
constructor(
private readonly apiConfigService: ApiConfigService,
) { }

use(req: Request, _res: Response, next: NextFunction) {
const restrictedRoutes = this.apiConfigService.getRestrictedRoutes();
if (restrictedRoutes.includes(req.path)) {
throw new NotFoundException(`Cannot GET ${req.path}`);
}

next();
}
}
Loading