# 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.

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

## References

* [**Java MessageDigest class**](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/security/MessageDigest.html)
* [**CWE-352: Cross-Site Request Forgery (CSRF)**](https://cwe.mitre.org/data/definitions/352.html)
* [**OWASP Top 10: A01:2021 - Broken Access Control**](https://owasp.org/Top10/A01_2021-Broken_Access_Control/)

## 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**](https://scopy.sec1.io/login)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sec1.io/user-docs/4-sast/3-javascript/usage-of-session-on-static-asset-csrf.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
