7

Hi I have a project in node.js and I want to set the HttpOnly flag: true for header response.

I have written the following code in app.js but it make no effect in response header .

app.use(session({
 secret: "notagoodsecretnoreallydontusethisone",
 resave: false,
 saveUninitialized: true,
 cookie: {httpOnly: true, secure: true}
}));

So any suggestion for setting HttpOnly Flag in express.js is most welcome.

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
arjun kori
  • 1,090
  • 2
  • 14
  • 32

2 Answers2

8

I think you could try this!

app.use(session({
   cookieName: 'sessionName',
   secret: "notagoodsecretnoreallydontusethisone",
   resave: false,
   saveUninitialized: true,
   httpOnly: true,  // dont let browser javascript access cookie ever
   secure: true, // only use cookie over https
   ephemeral: true // delete this cookie while browser close
}));
Wayne Chiu
  • 5,830
  • 2
  • 22
  • 19
  • i already got my answer chao,httpOnly works on server when we deploy the code. – arjun kori Sep 30 '16 at 13:28
  • 2
    This answer is wrong -- that's how you use sessions but does not answer the actual question of how to set the flag -- sessions don't work on lambda for example. – SebastianG Aug 23 '19 at 10:07
0

This example uses cookie-parser library.

Setting a cookie: res.cookie("cookie_name", token, {})

Pass res.cookie() an options object with httpOnly: true,

 const options = {
    expires: duration,
    httpOnly: true,
  };

Final e.g.

res.cookie("cookie_name", token, options)
Luis
  • 91
  • 1
  • 3