Skip to content

AnujRathi079/Secure-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” Secure Form App (CORS + CSRF Protection)

πŸ“Œ Overview

This project demonstrates a secure full-stack web application using:

  • Node.js + Express (Backend)
  • HTML + JavaScript (Frontend)
  • CSRF Protection using Tokens
  • CORS handling for secure communication

It shows how to prevent Cross-Site Request Forgery (CSRF) attacks in real-world applications.


πŸš€ Features

  • βœ… Secure form submission
  • βœ… CSRF token generation & validation
  • βœ… Cookie-based session handling
  • βœ… Express backend API
  • βœ… Static frontend serving
  • βœ… Clean and simple UI

πŸ› οΈ Tech Stack

  • Frontend: HTML, JavaScript
  • Backend: Node.js, Express
  • Security: CSRF (csurf), CORS
  • Session Management: express-session

πŸ“ Project Structure

secure-app/
β”‚
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ server.js
β”‚   β”œβ”€β”€ package.json
β”‚   └── node_modules/
β”‚
└── frontend/
    β”œβ”€β”€ index.html
    └── script.js


1. Install backend dependencies

cd backend
npm install

2. Run the server

node server.js

3. Open in browser

http://localhost:5000

πŸ” How CSRF Protection Works

  1. Server generates a CSRF token
  2. Token is sent to frontend (/csrf-token)
  3. Frontend includes token in request headers
  4. Server validates token before processing request

πŸ§ͺ Testing Security

βœ… Valid Request

  • Form submission works successfully

❌ Invalid Request

  • Remove CSRF token from script.js
  • You will get:
ForbiddenError: invalid csrf token

🌐 CORS Explanation

  • CORS controls which origins can access the backend
  • Used when frontend and backend run on different ports

🧠 Learning Outcomes

  • Understanding of CORS vs CSRF
  • Real implementation of web security
  • Handling cookies and sessions
  • Secure API design

πŸš€ Future Improvements

  • πŸ” Add JWT Authentication
  • πŸ—„οΈ Integrate MongoDB database
  • 🎨 Improve UI with Tailwind/React
  • 🌍 Deploy using Render/Vercel

About

Secure form app with CSRF protection and CORS using Node.js, Express, and JavaScript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors