Skip to content

Commit

Permalink
Disable write api when not in development mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Francis Duvivier committed Nov 27, 2024
1 parent 3c2ab26 commit 6d2b6da
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ POSTGRES_PASSWORD=badgehub
PGADMIN_DEFAULT_PASSWORD=badgehub
PGADMIN_DEFAULT_EMAIL=[email protected]

JWT_SIGNING_KEY="!!HackThePlanet!!"
NODE_ENV=development

JWT_SIGNING_KEY="!!HackThePlanet!!"
18 changes: 18 additions & 0 deletions src/disableWriteWhenNotDev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Express, NextFunction, Request, Response } from "express";
import { NODE_ENV } from "@config";

export function disableWriteWhenNotDev(app: Express) {
app.use((req: Request, res: Response, next: NextFunction) => {
if (req.method !== "GET") {
if (NODE_ENV !== "development") {
res
.status(403)
.send(
"Write API is disabled in non-development environments for now"
);
return;
}
}
next();
});
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import app from "./app";
import { RegisterRoutes } from "./generated/routes";
import { addTsoaValidationFailureLogging } from "@util/logging";
import { EXPRESS_PORT } from "@config";
import { disableWriteWhenNotDev } from "@disableWriteWhenNotDev";

async function startServer() {
disableWriteWhenNotDev(app);

RegisterRoutes(app);

addTsoaValidationFailureLogging(app);
Expand Down

0 comments on commit 6d2b6da

Please sign in to comment.