Giter Site home page Giter Site logo

express-security's Introduction

CSRF (Cross-Site Request Forgery):

Use Anti-CSRF Tokens:

Implement anti-CSRF tokens in your web forms. Libraries like csurf for Express can help you generate and validate these tokens. Example using csurf:

const csurf = require('csurf'); const csrfProtection = csurf({ cookie: true });

app.use(csrfProtection);

app.get('/myform', (req, res) => { // Use csrfToken in your form res.render('myform', { csrfToken: req.csrfToken() }); });

SameSite Cookies:

Set the SameSite attribute for cookies to mitigate CSRF attacks.

app.use(session({ secret: 'your-secret-key', cookie: { sameSite: 'strict' }, }));

XSS (Cross-Site Scripting):

Content Security Policy (CSP):

Implement a Content Security Policy to control which resources can be loaded on your page.

app.use((req, res, next) => { res.setHeader('Content-Security-Policy', 'script-src 'self''); next(); });

HTML Encoding:

Sanitize user input and encode it properly before rendering it on the page. Libraries like he can be useful for HTML encoding.

const he = require('he');

const userInput = '<script>alert("XSS attack")</script>'; const encodedInput = he.encode(userInput);

Helmet Middleware:

Use the helmet middleware to set various HTTP headers, including XSS protection headers.

const helmet = require('helmet'); app.use(helmet());

Use Template Engines Safely:

If you're using template engines, ensure they automatically escape content by default. For example, in EJS:

<%- userContent %>

express-security's People

Contributors

nphattai avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.