fix(session): limit subqueries for mariadb

MariaDB does not support `connect-typeorm`s subqueries,
so they need to be disabled if this dialect is used.

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-03-05 19:22:21 +01:00
parent 9c6d3d9dab
commit f9448bb801
3 changed files with 11 additions and 2 deletions

View file

@ -11,6 +11,7 @@ import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
import { AppConfig } from './config/app.config'; import { AppConfig } from './config/app.config';
import { AuthConfig } from './config/auth.config'; import { AuthConfig } from './config/auth.config';
import { DatabaseConfig } from './config/database.config';
import { MediaConfig } from './config/media.config'; import { MediaConfig } from './config/media.config';
import { ErrorExceptionMapping } from './errors/error-mapping'; import { ErrorExceptionMapping } from './errors/error-mapping';
import { ConsoleLoggerService } from './logger/console-logger.service'; import { ConsoleLoggerService } from './logger/console-logger.service';
@ -31,10 +32,11 @@ async function bootstrap(): Promise<void> {
app.useLogger(logger); app.useLogger(logger);
const configService = app.get(ConfigService); const configService = app.get(ConfigService);
const appConfig = configService.get<AppConfig>('appConfig'); const appConfig = configService.get<AppConfig>('appConfig');
const databaseConfig = configService.get<DatabaseConfig>('databaseConfig');
const authConfig = configService.get<AuthConfig>('authConfig'); const authConfig = configService.get<AuthConfig>('authConfig');
const mediaConfig = configService.get<MediaConfig>('mediaConfig'); const mediaConfig = configService.get<MediaConfig>('mediaConfig');
if (!appConfig || !authConfig || !mediaConfig) { if (!appConfig || !databaseConfig || !authConfig || !mediaConfig) {
logger.error('Could not initialize config, aborting.', 'AppBootstrap'); logger.error('Could not initialize config, aborting.', 'AppBootstrap');
process.exit(1); process.exit(1);
} }
@ -55,7 +57,7 @@ async function bootstrap(): Promise<void> {
await setupSpecialGroups(app); await setupSpecialGroups(app);
setupSessionMiddleware(app, authConfig); setupSessionMiddleware(app, authConfig, databaseConfig);
app.enableCors({ app.enableCors({
origin: appConfig.rendererOrigin, origin: appConfig.rendererOrigin,

View file

@ -10,16 +10,20 @@ import session from 'express-session';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { AuthConfig } from '../config/auth.config'; import { AuthConfig } from '../config/auth.config';
import { DatabaseDialect } from '../config/database-dialect.enum';
import { DatabaseConfig } from '../config/database.config';
import { Session } from '../users/session.entity'; import { Session } from '../users/session.entity';
/** /**
* Setup the session middleware via the given authConfig. * Setup the session middleware via the given authConfig.
* @param {INestApplication} app - the nest application to configure the middleware for. * @param {INestApplication} app - the nest application to configure the middleware for.
* @param {AuthConfig} authConfig - the authConfig to configure the middleware with. * @param {AuthConfig} authConfig - the authConfig to configure the middleware with.
* @param {DatabaseConfig} dbConfig - the DatabaseConfig to configure the middleware with.
*/ */
export function setupSessionMiddleware( export function setupSessionMiddleware(
app: INestApplication, app: INestApplication,
authConfig: AuthConfig, authConfig: AuthConfig,
dbConfig: DatabaseConfig,
): void { ): void {
app.use( app.use(
session({ session({
@ -32,6 +36,7 @@ export function setupSessionMiddleware(
saveUninitialized: false, saveUninitialized: false,
store: new TypeormStore({ store: new TypeormStore({
cleanupLimit: 2, cleanupLimit: 2,
limitSubquery: dbConfig.dialect !== DatabaseDialect.MARIADB,
}).connect(app.get<Repository<Session>>(getRepositoryToken(Session))), }).connect(app.get<Repository<Session>>(getRepositoryToken(Session))),
}), }),
); );

View file

@ -19,6 +19,7 @@ import { MockAuthGuard } from '../src/auth/mock-auth.guard';
import { TokenAuthGuard } from '../src/auth/token.strategy'; import { TokenAuthGuard } from '../src/auth/token.strategy';
import { AuthorsModule } from '../src/authors/authors.module'; import { AuthorsModule } from '../src/authors/authors.module';
import { AuthConfig } from '../src/config/auth.config'; import { AuthConfig } from '../src/config/auth.config';
import { DatabaseConfig } from '../src/config/database.config';
import appConfigMock from '../src/config/mock/app.config.mock'; import appConfigMock from '../src/config/mock/app.config.mock';
import authConfigMock from '../src/config/mock/auth.config.mock'; import authConfigMock from '../src/config/mock/auth.config.mock';
import customizationConfigMock from '../src/config/mock/customization.config.mock'; import customizationConfigMock from '../src/config/mock/customization.config.mock';
@ -267,6 +268,7 @@ export class TestSetupBuilder {
setupSessionMiddleware( setupSessionMiddleware(
this.testSetup.app, this.testSetup.app,
this.testSetup.configService.get<AuthConfig>('authConfig'), this.testSetup.configService.get<AuthConfig>('authConfig'),
this.testSetup.configService.get<DatabaseConfig>('databaseConfig'),
); );
this.testSetup.app.useGlobalPipes( this.testSetup.app.useGlobalPipes(
setupValidationPipe( setupValidationPipe(