You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Cross-site request forgery, abbreviated as CSRF or XSRF and also known as a one-click attack or session riding, is a malicious attack that takes advantage of a user's previously authenticated session to execute unwanted actions by manipulating the interaction between a client browser and a trusted web application.
12
+
</p>
13
+
<p>
14
+
To better understand the CSRF attack, consider the following scenario:
15
+
</p>
16
+
<ul>
17
+
<liclass="mb-2">
18
+
A user signs into his account on a vulnerable website, which trusts any request received with a valid authentication cookie.
19
+
</li>
20
+
<liclass="mb-2">
21
+
Then, the user visits a malicious site that contains a fake HTML form to win a prize, but in the background, it posts to the vulnerable website like the following example:
22
+
</li>
23
+
</ul>
24
+
<pre><codeclass="language-html"><h1>Congratulations! You're a Winner!</h1>
<input type="submit" value="Collect the prize!" />
28
+
</form>
29
+
</code></pre>
30
+
<ul>
31
+
<liclass="mb-2">
32
+
When the user clicks on the submit button. The browser sends a request that includes the authentication cookie for the requested domain.
33
+
</li>
34
+
<liclass="mb-2">
35
+
The vulnerable server trusts the request with the authentication context and allows any action that an authenticated user can perform.
36
+
</li>
37
+
</ul>
38
+
<p>
39
+
In addition to this scenario, the malicious site could run a script that automatically submits the form by sending the form submission as an AJAX request.
40
+
</p>
41
+
<p>
42
+
To prevent cross-site request forgery attacks, the DevNet framework provides the <code>Antiforgery</code> service that generates a CSRF token, which should be included in the form data or in the request header to be verified by the server when the form is submitted.
43
+
</p>
44
+
<br>
45
+
<h3>Configuration</h3>
46
+
<p>
47
+
To use the <code>Antiforgory</code> service across your application, you need to register it as a dependency in your application services with the help of the extension method <code>addAntiforgory()</code> inside the method <code>register()</code> of the <code>WebHostBuilder</code> and have the option to customize the default configurations.
48
+
</p>
49
+
<pre><codeclass="language-php"><?php
50
+
namespace Application;
51
+
52
+
use DevNet\Web\Hosting\WebHost;
53
+
use DevNet\Web\Extensions\ApplicationBuilderExtensions;
54
+
use DevNet\Web\Extensions\ServiceCollectionExtensions;
// Optimally you can modify the following default options.
65
+
$options->FieldName = "X-CSRF-TOKEN";
66
+
$options->HeaderName = "X-XSRF-TOKEN";
67
+
// If an cookie name is not provided, the system will generate a unique name.
68
+
$options->CookieName = null;
69
+
});
70
+
});
71
+
...
72
+
}
73
+
}
74
+
</code></pre>
75
+
<br>
76
+
<h3>X-CSRF-TOKEN</h3>
77
+
<p>
78
+
In traditional HTML-based applications, the Antiforgery tokens are passed to the server using hidden form fields, and usually, the token used in this technique is called <samp>X-CSRF-TOKEN</samp>.
79
+
</p>
80
+
<p>
81
+
After registering the Antiforgery service with the MVC web application, it will be injected into the view so that you can generate a token in the HTML form.
The controller in this example sends the view that is presented above with an Antiforgory token via the <code>edit()</code> method when the user requests to edit his account, and when the user submits the HTML form, the controller receives the request via the <code>update()</code> method, which is decorated with the <code>DevNet\Web\Action\Filters\AntiForgery</code> attribute to check if the request is trusted before updating the data.
In modern JavaScript-based applications, the Antiforgery tokens are sent to the server via the AJAX request headers, and usually, the token used in this technique is called <samp>X-XSRF-TOKEN</samp>.
147
+
</p>
148
+
<p>
149
+
In the following example, when the user requests the <samp>"/account/create"</samp> endpoint to create a new account, the server returns an HTML form response with <samp>XSRF-TOKEN</samp> as a cookie, which must be sent back to the server via the AJAX request header when the user submits the form to the <samp>"/account/store"</samp> endpoint, which this one has an Antiforgory filter to check if the request is trusted or not before storing the data.
// add Antiforgery filter to the endpoint "/account/store".
180
+
->addFilter(Antiforgery::class);
181
+
});</code></pre>
182
+
<p>
183
+
Here is a Javascript example that uses the AJAX request to send back the <samp>XSRF-TOKEN</samp> to the server after receiving it from the server via the response cookie.
0 commit comments