You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

2 Días
15 Hrs
24 Min
57 Seg

Seguridad y buenas prácticas: cookies, jwt y encriptación

19/24
Resources

Next.js offers powerful functionalities to optimize the security of our applications, but it is essential that as developers we adopt additional practices to ensure a secure experience. In this context, the management of HTTP cookies, sessions and encryption becomes essential to avoid vulnerabilities that can compromise our information and that of our users.

How to handle HTTP cookies securely?

  • HTTP Only: Setting the parameter to true prevents JavaScript from accessing cookies, protecting them from code injection attacks or malicious extensions.
  • Secure: Ensures that cookies are only transmitted under HTTPS connections, which prevents protocol downgrade attacks.
  • SameSite: Setting it to Strict restricts the use of cookies exclusively to our domain, eliminating risks with third parties.

These settings must be implemented when creating the cookie to ensure its security.

What information should a cookie include?

  • Avoid sensitive information such as emails, IDs or directly identifiable data.
  • Use non-significant identifiers that are only useful within the system.
  • Encrypt the data in the cookie so that only the server can interpret its contents.

This ensures that even if the cookie is stolen, its contents will not be useful to an attacker.

How to implement encryption for cookies?

Using JWT and the Jose library:

  • Signature: Use a secret key to create a secure token.
  • Verification: Validate the token upon receipt, ensuring that it has not been modified.

Key steps:

  1. Generate a unique secret and store it in environment variables.
  2. Create functions to encrypt and decrypt data, including hash algorithms such as H256.
  3. Encrypt sensitive information before storing it in the cookie.
  4. Validate cookies by decrypting them before using them on the system.

What risks does the use of encrypted cookies eliminate?

  • Prevents stolen data from being decrypted by third parties.
  • Prevents malicious modification of stored values.
  • It reduces the exposure of sensitive data to potential vulnerabilities.

By following these recommendations, our applications will be more robust against possible attacks and will offer a more reliable experience to users.

Contributions 2

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Super importante esta clase, muchas veces nos centramos en que la aplicación funcione pero no nos damos cuenta de los riesgos que corremos "por las prisas"
Nota importante de la clase que el profesor mencionó varias veces: "Siempre pensar que otro website, otra app u otra persona quiere robar tu información", esto nos ayudará a pensar en que debemos fortalecer nuestra app en seguridad. *httpOnly: true; --> Para que solo el servidor tenga acceso a nuestra cookies y no el cliente.* *Secure: true --> Para asegurar nuestras cookies cuando estamos en Deploy.* *sameSite: strict --> Hace que solo nuestro dominio pueda tener acceso a las cookies y no subdominios que tengamos o otros dominios. En caso de necesitar compartir entre dominios y subdominios, utilizar "lax".*