Skip to content

Commit fefae1a

Browse files
committed
#RI-4454 - Restrict loopback
1 parent 091e3eb commit fefae1a

22 files changed

+224
-14
lines changed

configs/webpack.config.renderer.prod.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ const devtoolsConfig =
2323
const configuration: webpack.Configuration = {
2424
...devtoolsConfig,
2525

26-
devtool: 'source-map',
27-
2826
mode: 'production',
2927

3028
target: ['web', 'electron-renderer'],

configs/webpack.config.renderer.stage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ DeleteSourceMaps();
1010
export default merge(baseConfig, {
1111
...rendererProdConfig,
1212

13+
devtool: 'source-map',
14+
1315
plugins: [
1416
...rendererProdConfig.plugins,
1517

redisinsight/api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"format": "prettier --write \"src/**/*.ts\"",
2222
"lint": "eslint --ext .ts .",
2323
"start": "nest start",
24-
"start:dev": "cross-env NODE_ENV=development SERVER_STATIC_CONTENT=1 nest start --watch",
24+
"start:dev": "cross-env NODE_ENV=development BUILD_TYPE=DOCKER_ON_PREMISE SERVER_STATIC_CONTENT=1 nest start --watch",
2525
"start:debug": "nest start --debug --watch",
2626
"start:stage": "cross-env NODE_ENV=staging SERVER_STATIC_CONTENT=true node dist/src/main",
2727
"start:prod": "cross-env NODE_ENV=production node dist/src/main",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const API_PARAM_DATABASE_ID = 'dbInstance';
22
export const API_HEADER_DATABASE_INDEX = 'ri-db-index';
3+
export const API_HEADER_WINDOW_ID = 'ri-window-id';
34
export const API_PARAM_CLI_CLIENT_ID = 'uuid';

redisinsight/api/src/constants/error-messages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ export default {
6464
APP_SETTINGS_NOT_FOUND: () => 'Could not find application settings.',
6565
SERVER_INFO_NOT_FOUND: () => 'Could not find server info.',
6666
INCREASE_MINIMUM_LIMIT: (count: string) => `Set MAXSEARCHRESULTS to at least ${count}.`,
67+
INVALID_WINDOW_ID: 'Invalid window id.',
68+
UNDEFINED_WINDOW_ID: 'Undefined window id.',
6769
};

redisinsight/api/src/core.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { AnalyticsModule } from 'src/modules/analytics/analytics.module';
1010
import { SshModule } from 'src/modules/ssh/ssh.module';
1111
import { NestjsFormDataModule } from 'nestjs-form-data';
1212
import { FeatureModule } from 'src/modules/feature/feature.module';
13+
import { AuthModule } from 'src/modules/auth/auth.module';
1314

1415
@Global()
1516
@Module({
@@ -25,6 +26,7 @@ import { FeatureModule } from 'src/modules/feature/feature.module';
2526
SshModule,
2627
NestjsFormDataModule,
2728
FeatureModule.register(),
29+
AuthModule.register(),
2830
],
2931
exports: [
3032
EncryptionModule,

redisinsight/api/src/main.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dotenv/config';
22
import { NestFactory } from '@nestjs/core';
33
import { SwaggerModule } from '@nestjs/swagger';
4-
import { NestApplicationOptions } from '@nestjs/common';
4+
import { INestApplication, NestApplicationOptions } from '@nestjs/common';
55
import * as bodyParser from 'body-parser';
66
import { WinstonModule } from 'nest-winston';
77
import { GlobalExceptionFilter } from 'src/exceptions/global-exception.filter';
@@ -14,7 +14,12 @@ import LOGGER_CONFIG from '../config/logger';
1414

1515
const serverConfig = get('server');
1616

17-
export default async function bootstrap(): Promise<Function> {
17+
interface IApp {
18+
app: INestApplication
19+
gracefulShutdown: Function
20+
}
21+
22+
export default async function bootstrap(): Promise<IApp> {
1823
await migrateHomeFolder();
1924

2025
const port = process.env.API_PORT || serverConfig.port;
@@ -67,7 +72,7 @@ export default async function bootstrap(): Promise<Function> {
6772
process.on('SIGTERM', gracefulShutdown);
6873
process.on('SIGINT', gracefulShutdown);
6974

70-
return gracefulShutdown;
75+
return { app, gracefulShutdown };
7176
}
7277

7378
if (process.env.APP_ENV !== 'electron') {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Module } from '@nestjs/common';
2+
import config from 'src/utils/config';
3+
import { WindowAuthModule } from './window-auth/window-auth.module';
4+
import { BuildType } from '../server/models/server';
5+
6+
const SERVER_CONFIG = config.get('server');
7+
8+
@Module({})
9+
export class AuthModule {
10+
static register() {
11+
const imports = [];
12+
13+
if (SERVER_CONFIG.buildType === BuildType.Electron) {
14+
imports.push(WindowAuthModule);
15+
}
16+
17+
return {
18+
module: AuthModule,
19+
imports,
20+
};
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { HttpException, HttpStatus } from '@nestjs/common';
2+
3+
export class WindowUnauthorizedException extends HttpException {
4+
constructor(message) {
5+
super(
6+
{
7+
statusCode: HttpStatus.UNAUTHORIZED,
8+
message,
9+
error: 'Unauthorized',
10+
},
11+
HttpStatus.UNAUTHORIZED,
12+
);
13+
}
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import {
2+
Injectable,
3+
Logger,
4+
NestMiddleware,
5+
} from '@nestjs/common';
6+
import { NextFunction, Request, Response } from 'express';
7+
import ERROR_MESSAGES from 'src/constants/error-messages';
8+
import { API_HEADER_WINDOW_ID } from 'src/common/constants';
9+
import { WindowAuthManager } from '../window-auth.manager';
10+
import { WindowUnauthorizedException } from '../constants/exceptions';
11+
12+
@Injectable()
13+
export class WindowAuthMiddleware implements NestMiddleware {
14+
private logger = new Logger('WindowAuthMiddleware');
15+
16+
constructor(
17+
private windowAuthService: WindowAuthManager,
18+
) {}
19+
20+
async use(req: Request, res: Response, next: NextFunction): Promise<any> {
21+
const { windowId } = WindowAuthMiddleware.getWindowIdFromReq(req);
22+
const { isExists: isWindowExists = false } = await this.windowAuthService.getStrategy()?.isWindowExists(windowId)
23+
?? {};
24+
25+
if (!isWindowExists) {
26+
this.throwError(req, ERROR_MESSAGES.UNDEFINED_WINDOW_ID);
27+
}
28+
29+
next();
30+
}
31+
32+
private static getWindowIdFromReq(req: Request) {
33+
return { windowId: `${req?.headers?.[API_HEADER_WINDOW_ID]}` };
34+
}
35+
36+
private throwError(req: Request, message: string) {
37+
const { method, url } = req;
38+
this.logger.error(`${message} ${method} ${url}`);
39+
40+
throw new WindowUnauthorizedException(message);
41+
}
42+
}

0 commit comments

Comments
 (0)