Node.js


Securing Node.js applications All Versions

v0.1
v0.2
v0.3
v0.4
v0.5
v0.6
v0.7
v0.8
v0.9
v0.10
v0.11
io.js v1.0
io.js v1.1
io.js v1.2
io.js v1.3
io.js v1.4
io.js v1.5
io.js v1.6
io.js v1.7
io.js v1.8
io.js v2.0
io.js v2.1
io.js v2.2
io.js v2.3
io.js v2.4
io.js v2.5
io.js v3.0
io.js v3.1
io.js v3.2
io.js v3.3
v4.0
v4.1
v4.2
v5.0
v5.1
v5.2
v5.3
v5.4
v5.5
v0.12
v4.3
v5.6
v5.7
v4.4
v5.8
v5.9
v5.10
v5.10.1
v5.11.0
v6.0.0
v5.11.1
v6.1.0
v6.2.0
v6.2.1
v6.2.2
v5.12.0
v6.3.0
v6.3.1
v6.4.0
v6.5.0
v6.6.0
v6.7.0
v6.8.0
v6.8.1
v6.9.0
v6.9.1
v7.0.0
v7.1.0
v7.2.0

This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 6

    If you choose to handle SSL/TLS in your Node.js application, consider that you are also responsible for maintaining SSL/TLS attack prevention at this point. In many server-client architectures, SSL/TLS terminates on a reverse proxy, both to reduce application complexity and reduce the scope of security configuration.

    If your Node.js application should handle SSL/TLS, it can be secured by loading the key and cert files.

    If your certificate provider requires a certificate authority (CA) chain, it can be added in the ca option as an array. A chain with multiple entries in a single file must be split into multiple files and entered in the same order into the array as Node.js does not currently support multiple ca entries in one file. An example is provided in the code below for files 1_ca.crt and 2_ca.crt. If the ca array is required and not set properly, client browsers may display messages that they could not verify the authenticity of the certificate.

    Example

    const https = require('https');
    const fs = require('fs');
    
    const options = {
      key: fs.readFileSync('privatekey.pem'),
      cert: fs.readFileSync('certificate.pem'),
      ca: [fs.readFileSync('1_ca.crt'), fs.readFileSync('2_ca.crt')]
    };
    
    https.createServer(options, (req, res) => {
      res.writeHead(200);
      res.end('hello world\n');
    }).listen(8000);
    
  • 5

    CSRF is an attack which forces an end user to execute unwanted actions on a web application in which he/she is currently authenticated.

    It can happen because cookies are sent with every request to a website - even when those requests come from a different site.

    We can use csurf module for creating csrf token and validating it.

    Example

    var express = require('express')
    var cookieParser = require('cookie-parser')    //for cookie parsing
    var csrf = require('csurf')    //csrf module
    var bodyParser = require('body-parser')    //for body parsing
    
    // setup route middlewares
    var csrfProtection = csrf({ cookie: true })
    var parseForm = bodyParser.urlencoded({ extended: false })
    
    // create express app
    var app = express()
    
    // parse cookies
    app.use(cookieParser())
    
    app.get('/form', csrfProtection, function(req, res) {
      // generate and pass the csrfToken to the view
      res.render('send', { csrfToken: req.csrfToken() })
    })
    
    app.post('/process', parseForm, csrfProtection, function(req, res) {
      res.send('data is being processed')
    })
    

    So, when we access GET /form, it will pass the csrf token csrfToken to the view.

    Now, inside the view, set the csrfToken value as the value of a hidden input field named csrf.

    e.g. for handlebar templates

    <form action="/process" method="POST">
        <input type="hidden" name="csrf" value="{{csrfToken}}">
        Name: <input type="text" name="name">
        <button type="submit">Submit</button>
    </form>
    
  • 2

    Once you have node.js installed on your system, just follow the procedure below to get a basic web server running with support for both HTTP and HTTPS!

    Step 1 : Build a Certificate Authority

    1. create the folder where you want to store your key & certificate :

      mkdir conf


    1. go to that directory :

      cd conf


    1. grab this ca.cnf file to use as a configuration shortcut :

      wget https://raw.githubusercontent.com/anders94/https-authorized-clients/master/keys/ca.cnf


    1. create a new certificate authority using this configuration :

      openssl req -new -x509 -days 9999 -config ca.cnf -keyout ca-key.pem -out ca-cert.pem


    1. now that we have our certificate authority in ca-key.pem and ca-cert.pem, let's generate a private key for the server :

      openssl genrsa -out key.pem 4096


    1. grab this server.cnf file to use as a configuration shortcut :

      wget https://raw.githubusercontent.com/anders94/https-authorized-clients/master/keys/server.cnf


    1. generate the certificate signing request using this configuration :

      openssl req -new -config server.cnf -key key.pem -out csr.pem


    1. sign the request :

      openssl x509 -req -extfile server.cnf -days 999 -passin "pass:password" -in csr.pem -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem


    Step 2 : Install your certificate as a root certificate

    1. copy your certificate to your root certificates' folder :

      sudo cp ca-crt.pem /usr/local/share/ca-certificates/ca-crt.pem


    1. update CA store :

      sudo update-ca-certificates

Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Securing Node.js applications? Ask Question

Topic Outline