Ccodemod

codemod/express-session-secure-cookies

Transform express-session configuration to include secure: true, httpOnly: true, and sameSite settings

transformationmigrationsecurityexpress-sessioncookiescwe-614httpOnlysecuresameSite
Public
0 downloads
0 stars
How to Use
Run this codemod on your codebase using one of the following commands

The easiest way to run this codemod without installing anything globally:

Documentation

Express Session Secure Cookies

This codemod transforms Express.js session configurations to include essential security flags, protecting against session hijacking and XSS attacks.

Security Issues Addressed

This codemod helps prevent the following security vulnerabilities:

  • CWE-614: Sensitive Cookie in HTTPS Session Without 'Secure' Attribute
  • CWE-79: Cross-site Scripting (XSS) - by adding httpOnly: true
  • CWE-352: Cross-Site Request Forgery (CSRF) - by adding sameSite: 'strict'

What it does

The codemod automatically:

  1. Adds missing security flags to Express session cookies
  2. Updates insecure configurations (e.g., secure: falsesecure: true)
  3. Preserves existing cookie properties while adding security flags
  4. Works with various syntax patterns (direct calls, variable assignments, etc.)

Transformations

Before

javascript

After

javascript

More Examples

Existing cookie object with other properties:

javascript

Fixing insecure configurations:

javascript

Installation & Usage

bash

Supported File Types

  • JavaScript (.js)
  • TypeScript (.ts)
  • JSX (.jsx)
  • TSX (.tsx)

What Gets Transformed

The codemod identifies Express session configurations by looking for objects containing typical session properties like:

  • secret
  • resave
  • saveUninitialized
  • store
  • name

It then ensures these configurations have secure cookie settings.

What Doesn't Get Transformed

  • Files without express-session imports/requires
  • Session configurations that are already secure
  • Test files (excluded by default)
  • Configuration objects that don't appear to be session-related

Development

bash

Security Best Practices

After running this codemod, ensure your application:

  1. Uses HTTPS in production - The secure: true flag requires HTTPS
  2. Considers sameSite policy - 'strict' provides maximum protection but may affect some use cases
  3. Reviews cookie domain settings - Ensure they're appropriate for your deployment
  4. Tests authentication flows - Verify the changes don't break your application

References

License

MIT

Ready to contribute?

Build your own codemod and share it with the community.