Skip to content

Commit

Permalink
fix(admin-ui-plugin): Implement rate limiting on static server
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Sep 18, 2024
1 parent cb556d8 commit 9516c71
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/admin-ui-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"dependencies": {
"date-fns": "^2.30.0",
"express-rate-limit": "^7.4.0",
"fs-extra": "^11.2.0"
}
}
13 changes: 11 additions & 2 deletions packages/admin-ui-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
VendurePlugin,
} from '@vendure/core';
import express from 'express';
import { rateLimit } from 'express-rate-limit';
import fs from 'fs-extra';
import path from 'path';

Expand Down Expand Up @@ -220,7 +221,7 @@ export class AdminUiPlugin implements NestModule {
await overwriteConfig();
} else {
Logger.info('Creating admin ui middleware (prod mode)', loggerCtx);
consumer.apply(await this.createStaticServer(app)).forRoutes(route);
consumer.apply(this.createStaticServer(app)).forRoutes(route);

if (app && typeof app.compile === 'function') {
Logger.info('Compiling Admin UI app in production mode...', loggerCtx);
Expand All @@ -241,10 +242,18 @@ export class AdminUiPlugin implements NestModule {
registerPluginStartupMessage('Admin UI', route);
}

private async createStaticServer(app?: AdminUiAppConfig) {
private createStaticServer(app?: AdminUiAppConfig) {
const adminUiAppPath = (app && app.path) || DEFAULT_APP_PATH;

const limiter = rateLimit({
windowMs: 60 * 1000,
limit: process.env.NODE_ENV === 'production' ? 500 : 2000,
standardHeaders: true,
legacyHeaders: false,
});

const adminUiServer = express.Router();
adminUiServer.use(limiter);
adminUiServer.use(express.static(adminUiAppPath));
adminUiServer.use((req, res) => {
res.sendFile(path.join(adminUiAppPath, 'index.html'));
Expand Down

0 comments on commit 9516c71

Please sign in to comment.