Usage of Session on Static Asset (CSRF)

Overview

  • Rule ID: javascript_express_static_asset_with_session

  • Applicable Languages: Javascript

  • Weakness ID: CWE-352

Description

Enabling sessions for static assets can inadvertently expose your application to Cross-Site Request Forgery (CSRF) attacks. This vulnerability occurs because static assets, such as images and stylesheets, are often cached by intermediary services like Content Delivery Networks (CDNs). If these assets are served with session information, attackers can exploit this to hijack a user's session.

Remediation Guidelines

  • Do ensure that static resources are served before session initialization in your application's middleware order. This practice prevents session cookies from being unnecessarily attached to static assets, reducing the risk of CSRF attacks.

    app.use(express.static(__dirname + "/public"));
    app.use(session());

References

Configuration

To omit this rule during a scan, and to provide you with continuous 24/7 code-level scanning, you can employ our SAST TOOL

Last updated