Sobes.tech
Junior

What are secure cookies?

sobes.tech AI

Answer from AI

Secure cookies are only sent over HTTPS connections, preventing their interception during transmission over the network. The Secure attribute instructs the browser to send cookies only to secure resources (using https).

// Setting a secure cookie on the server side (Node.js with express)
res.cookie('mycookie', 'myvalue', {
  secure: true, // Tells the browser to send the cookie only over HTTPS
  httpOnly: true, // Prevents access to the cookie via JavaScript (XSS)
  sameSite: 'Strict' // Protects against CSRF attacks
});

Key aspects:

  • HTTPS: Requires an HTTPS connection.
  • Encryption: Cookie data is encrypted during transmission.
  • Confidentiality: Reduces the risk of information leakage from cookies.

Using secure cookies is an important practice for protecting user data.