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
15 changes: 13 additions & 2 deletions src/config/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,22 @@ export function applySecurityMiddleware(app: Application): void {
app.use(reportToMiddleware);

// 4. CORS (exact-match allowlist, no wildcards).
app.use(cors(corsOptions));
const globalCors = cors(corsOptions);
app.use((req, res, next) => {
if (req.path.startsWith("/api/admin")) {
return next();
}
return globalCors(req, res, next);
});

// 5. Maintenance Mode (blocks non-GET requests when active)
app.use(maintenanceModeMiddleware);

// 6. Respond to all OPTIONS preflight requests immediately.
app.options("*", cors(corsOptions));
app.options("*", (req, res, next) => {
if (req.path.startsWith("/api/admin")) {
return next();
}
return globalCors(req, res, next);
});
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ import { layeredCache } from "./services/layeredCache";
import { ERROR_CODES } from "./constants/errorCodes";
import { startApolloServer } from "./graphql/server";
import { applySecurityMiddleware } from "./config/express";
import { createCorsMiddleware } from "./middleware/cors";

dotenv.config();

Expand Down Expand Up @@ -454,6 +455,9 @@ app.use("/api/mtn", mtnCallbacksRouter);
app.use("/api/orange-madagascar", orangeMadagascarCallbacksRouter);
app.use("/api/orange-guinea", orangeGuineaCallbacksRouter);
app.use("/api/multisig", multisigCallbacksRouter);

// Apply custom configurable CORS allowlist for admin routes
app.use("/api/admin", createCorsMiddleware());
app.use("/api/admin/withdrawals", adminWithdrawalsRouter);
app.use("/api/reports", reportsRoutes);
app.use("/api/fees", feesRoutes);
Expand Down
10 changes: 10 additions & 0 deletions src/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ transactionRoutes.get(
error: "Transaction not found",
});

if (transaction.userId !== req.jwtUser?.userId) {
throw createError(ERROR_CODES.FORBIDDEN, "Access denied", {
error: "You do not have permission to access this transaction",
});
}

const pdf = await generateTransactionPdfBuffer(transaction);

res.setHeader("Content-Type", "application/pdf");
Expand Down Expand Up @@ -92,6 +98,10 @@ transactionRoutes.get(
if (!transaction)
return res.status(404).json({ error: "Transaction not found" });

if (transaction.userId !== req.jwtUser?.userId) {
return res.status(403).json({ error: "You do not have permission to access this transaction" });
}

if (transaction.status !== TransactionStatus.Completed)
return res.status(400).json({
error:
Expand Down
4 changes: 4 additions & 0 deletions src/routes/v1/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ transactionRoutesV1.get(
if (!transaction)
return res.status(404).json({ error: "Transaction not found" });

if (transaction.userId !== req.jwtUser?.userId && req.user?.role !== "admin") {
return res.status(403).json({ error: "You do not have permission to access this transaction" });
}

if (transaction.status !== TransactionStatus.Completed)
return res.status(400).json({
error:
Expand Down
Loading