11const express = require ( 'express' ) ;
22const cors = require ( 'cors' ) ;
3+ const cookieParser = require ( 'cookie-parser' ) ;
4+ const csurf = require ( 'csurf' ) ;
35require ( 'dotenv' ) . config ( ) ;
46
57const app = express ( ) ;
@@ -29,7 +31,18 @@ let tasks = [
2931
3032// Middleware
3133app . use ( cors ( ) ) ;
34+ app . use ( cookieParser ( ) ) ;
3235app . use ( express . json ( ) ) ;
36+ app . use ( express . urlencoded ( { extended : true } ) ) ;
37+
38+ // CSRF protection middleware
39+ const csrfProtection = csurf ( { cookie : true } ) ;
40+ app . use ( csrfProtection ) ;
41+
42+ // Endpoint to provide CSRF token to clients
43+ app . get ( '/csrf-token' , ( req , res ) => {
44+ res . json ( { csrfToken : req . csrfToken ( ) } ) ;
45+ } ) ;
3346
3447// Request logging middleware
3548app . use ( ( req , res , next ) => {
@@ -107,7 +120,7 @@ app.get('/api/users/:id', (req, res) => {
107120 res . json ( user ) ;
108121} ) ;
109122
110- app . post ( '/api/users' , ( req , res ) => {
123+ app . post ( '/api/users' , csrfProtection , ( req , res ) => {
111124 const { name, email, role = 'user' } = req . body ;
112125
113126 if ( ! name || ! email ) {
@@ -136,7 +149,7 @@ app.post('/api/users', (req, res) => {
136149 } ) ;
137150} ) ;
138151
139- app . put ( '/api/users/:id' , ( req , res ) => {
152+ app . put ( '/api/users/:id' , csrfProtection , ( req , res ) => {
140153 const userId = parseInt ( req . params . id ) ;
141154 const userIndex = users . findIndex ( u => u . id === userId ) ;
142155
@@ -153,7 +166,7 @@ app.put('/api/users/:id', (req, res) => {
153166 } ) ;
154167} ) ;
155168
156- app . delete ( '/api/users/:id' , ( req , res ) => {
169+ app . delete ( '/api/users/:id' , csrfProtection , ( req , res ) => {
157170 const userId = parseInt ( req . params . id ) ;
158171 const userIndex = users . findIndex ( u => u . id === userId ) ;
159172
@@ -217,7 +230,7 @@ app.get('/api/products/:id', (req, res) => {
217230 res . json ( product ) ;
218231} ) ;
219232
220- app . post ( '/api/products' , ( req , res ) => {
233+ app . post ( '/api/products' , csrfProtection , ( req , res ) => {
221234 const { name, price, category, stock, description } = req . body ;
222235
223236 if ( ! name || ! price || ! category ) {
@@ -261,7 +274,7 @@ app.get('/api/orders', (req, res) => {
261274 } ) ;
262275} ) ;
263276
264- app . post ( '/api/orders' , ( req , res ) => {
277+ app . post ( '/api/orders' , csrfProtection , ( req , res ) => {
265278 const { userId, productId, quantity = 1 } = req . body ;
266279
267280 if ( ! userId || ! productId ) {
@@ -331,7 +344,7 @@ app.get('/api/tasks', (req, res) => {
331344 } ) ;
332345} ) ;
333346
334- app . post ( '/api/tasks' , ( req , res ) => {
347+ app . post ( '/api/tasks' , csrfProtection , ( req , res ) => {
335348 const { title, priority = 'medium' , assignedTo } = req . body ;
336349
337350 if ( ! title ) {
@@ -355,7 +368,7 @@ app.post('/api/tasks', (req, res) => {
355368 } ) ;
356369} ) ;
357370
358- app . put ( '/api/tasks/:id' , ( req , res ) => {
371+ app . put ( '/api/tasks/:id' , csrfProtection , ( req , res ) => {
359372 const taskId = parseInt ( req . params . id ) ;
360373 const taskIndex = tasks . findIndex ( t => t . id === taskId ) ;
361374
@@ -508,7 +521,7 @@ app.get('/api/test/large-data', (req, res) => {
508521 } ) ;
509522} ) ;
510523
511- app . post ( '/api/test/echo' , ( req , res ) => {
524+ app . post ( '/api/test/echo' , csrfProtection , ( req , res ) => {
512525 res . json ( {
513526 message : 'Echo endpoint - returning your data' ,
514527 received : req . body ,
@@ -575,7 +588,7 @@ app.get('/api/users/paginated', (req, res) => {
575588} ) ;
576589
577590// ============ BULK OPERATIONS ============
578- app . post ( '/api/users/bulk' , ( req , res ) => {
591+ app . post ( '/api/users/bulk' , csrfProtection , ( req , res ) => {
579592 const { users : newUsers } = req . body ;
580593
581594 if ( ! Array . isArray ( newUsers ) ) {
0 commit comments