Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ${_csrf} do? Is this an implicit EL object?

Tags:

java

jsp

I'm going over some source code and trying to figure out where _csrf came from. As far as I can guess, it looks like an implicit EL object. Maybe related to authentication and spring security.

The below is the code that contains _csrf.

<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />

What does ${_csrf} do? Is this an implicit EL object?

like image 930
Jin Lee Avatar asked Mar 14 '19 01:03

Jin Lee


People also ask

What does a CSRF token do?

A CSRF token is a secure random token (e.g., synchronizer token or challenge token) that is used to prevent CSRF attacks. The token needs to be unique per user session and should be of large random value to make it difficult to guess. A CSRF secure application assigns a unique CSRF token for every user session.

What is CSRF token in Java?

A CSRF Token is a secret, unique and unpredictable value a server-side application generates in order to protect CSRF vulnerable resources. The tokens are generated and submitted by the server-side application in a subsequent HTTP request made by the client.

How CSRF is implemented in Spring Security?

To protect MVC applications, Spring adds a CSRF token to each generated view. This token must be submitted to the server on every HTTP request that modifies state (PATCH, POST, PUT and DELETE — not GET). This protects our application against CSRF attacks since an attacker can't get this token from their own page.

How do I get CSRF token in Spring Security?

You can obtain the CSRF using the request attribute named _csrf as outlined in the reference. To add the CSRF to an HTML page, you will need to use JavaScript to obtain the token that needs to be included in the requests.


2 Answers

As described in the documentation, the _csrf variable is provided by Spring Security. A better approach in JSP is to use the csrfInput tag; Thymeleaf includes this automatically and doesn't require including it at all.

like image 88
chrylis -cautiouslyoptimistic- Avatar answered Sep 18 '22 02:09

chrylis -cautiouslyoptimistic-


Cross-site request forgery, also known as one-click attack or session riding and abbreviated as CSRF (sometimes pronounced sea-surf) or XSRF, is a type of malicious exploit of a website where unauthorized commands are transmitted from a user that the web application trusts.

This input protects form from csrf attacks by adding a special csrf token to all of your page requests

Check this out: https://en.wikipedia.org/wiki/Cross-site_request_forgery https://www.baeldung.com/spring-security-csrf

like image 24
Nikita Kalugin Avatar answered Sep 21 '22 02:09

Nikita Kalugin