---
title: "Apache APISIX Enhances API Security by CSRF Plugin - API7.ai"
description: "This article describes how to secure your API information with the CSRF plugin in API Gateway Apache APISIX."
url: "https://api7.ai/blog/csrf"
---

[Blog](https://api7.ai/blog)

[Products](https://api7.ai/category/products)[Technology](https://api7.ai/category/technology)[Ecosystem](https://api7.ai/category/ecosystem)[Case Study](https://api7.ai/category/usercase)[Team & Community](https://api7.ai/category/community)

Search

⌘ + K

[Blog](https://api7.ai/blog)

Products

# Apache APISIX Enhances API Security by CSRF Plugin

API7.ai

February 23, 2022

[Products](https://api7.ai/category/products)

The key point of launching a CSRF (cross-site request forgery) attack is to make the target server unable to distinguish whether the source of many requests is a real user or an attacker. The general flow of the attack is that first the attacker will lure the user to navigate to a web page provided by the attacker. This page contains a request that is automatically sent to the target server. The page then loads normally and the request is automatically sent to the server. It appears to the server that the request is exactly the same as the request normally sent by the user, unaware that it was initiated by the attacker without the user\\'s knowledge. Since the request carries some of the user\\'s credentials, the attacker can get access to the user\\'s information by parsing these credentials, thus creating a security risk.

This article introduces `csrf`, the CSRF security plugin for Apache APISIX, and details how to secure your API information in Apache APISIX with the help of the `csrf` plugin.

## Plugin Introduction

The `csrf` plugin is implemented based on the `Double Submit Cookie` scheme. As defined in [RFC 7231#section-4.2.1](https://datatracker.ietf.org/doc/html/rfc7231.html#section-4.2.1), we consider the `GET`, `HEAD` and `OPTIONS` methods as **secure methods**. According to this convention, the `csrf` plug-in will let these three methods go directly, but will check the other methods and intercept any unsafe requests.

To defend against CSRF attacks, we need to create a token or identifier that cannot be forged and ensure that this is not sent with the attacker\\'s request. The user needs to carry the token that the `csrf` plugin relies on in the request header, and the token is computed using a key for signing. This ensures that the token cannot be forged by others, thus securing the API.

![Plugin](https://static.api7.ai/2022/10/03/633a416d3d54d.png?imageMogr2/format/webp)

When the `csrf` plugin is enabled in a route, all request responses to that route will contain a Cookie carrying a `csrf token`.

The user needs to carry this Cookie in an insecure request for this route and add an additional field in the request header to carry the content of the cookie. The field is the `name` value in the plugin configuration so that the request passes the CSRF plugin\\'s checks.

The user provides a random key in the plugin\\'s configuration, which is used by the plugin to encrypt the token information with a sha256 hash and generate a CSRF token, thus ensuring that the token cannot be forged.

## How to Use the Plugin

### Enable CSRF Plugin in a Route

Create a route in APISIX using the Admin API and enable the csrf plugin.

```
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
  "uri": "/hello",
  "plugins": {
    "csrf": {
      "key": "edd1c9f034335f136f87ad84b625c8f1"
    }
  },
  "upstream": {
    "type": "roundrobin",
    "nodes": {
      "127.0.0.1:9001": 1
    }
  }
}'
```

There are three configuration parameters for the plugin.

- `key`: Required field, the value of the random secret key. The user needs to provide a random key.
- `expires`: Optional, the expiration time of the random secret key, the default value is 7200 seconds. Since the CSRF token is sent to the client using a Cookie, this configuration is placed in the Cookie\\'s configuration to control the Cookie\\'s expiration time. In addition, the plugin will also calculate the time to determine whether the token expires.
- `name`: Optional, the name of the CSRF token, the default value is `apisix-csrf-token`.

### Send a Request

The route is first accessed using a `POST` request.

```
curl -i http://127.0.0.1:9080/hello -X POST
```

Apache APISIX will intercept the request and return a `401` error. In the returned header you will find a Cookie set, which should be the default value `apisix-csrf-token=....` inside the Cookie if the name field of the plugin is not configured . This is the CSRF token generated by the CSRF plugin. In the request, you need to make sure that the request carries this Cookie and that the token is written in the request header.

```
HTTP/1.1 401 Unauthorized
Set-Cookie: apisix-csrf-token= ${apisix-csrf-token};path=/;Expires=Mon, 13-Dec-21 09:33:55 GMT
{"error_msg":"no csrf token in headers"}
```

Example of using JavaScript on the client side: reading Cookies using `js-cookie` and sending requests using `axios`.

```
const token = Cookie.get('apisix-csrf-token');

const instance = axios.create({
  headers: {'apisix-csrf-token': token}
});
```

If the token in the Cookie does not match the token in the request header, the request will be intercepted by the `csrf` plugin, as shown in the following example.

```
curl -i http://127.0.0.1:9080/hello -X POST -H 'apisix-csrf-token: ${apisix-csrf-token}' -b 'apisix-csrf-token= ${apisix-csrf-token}'
```

```
HTTP/1.1 401 Unauthorized
Set-Cookie: apisix-csrf-token= ${apisix-csrf-token};path=/;Expires=Mon, 13-Dec-21 09:33:55 GMT
{"error_msg":"csrf token mismatch"}
```

Finally, use `curl` to verify regular access.

```
curl -i http://127.0.0.1:9080/hello -X POST -H 'apisix-csrf-token: ${apisix-csrf-token}' -b 'apisix-csrf-token= ${apisix-csrf-token}'
```

```
HTTP/1.1 200 OK
```

Internally, the plugin needs to verify that the token in the Cookie matches the token carried in the request header and recalculate the signature to verify that the token is valid.

### Disable the Plugin

Remove the relevant configuration information for the `csrf` plugin and then send a request to update the route to deactivate the plugin.

```
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
  "uri": "/hello",
  "upstream": {
    "type": "roundrobin",
    "nodes": {
      "127.0.0.1:1980": 1
    }
  }
}'
```

## Summary

This article describes in detail how the `csrf` plugin works and how to use it. We hope that this article can give you a clearer understanding of using the plugin to intercept CSRF attacks in Apache APISIX and facilitate its application in practical scenarios.

Apache APISIX is also currently working on additional [plugins](https://api7.ai/blog/how-to-build-an-apache-apisix-plugin-from-0-to-1) to support the integration of additional services, so if you are interested, feel free to start a discussion in [GitHub Discussion](https://github.com/apache/apisix/discussions), or via the [mailing list](https://apisix.apache.org/docs/general/join/#subscribe-to-the-mailing-list) to communicate.

Tags:

[APISIX Basics](https://api7.ai/tags/apisix-basics)

[API Security](https://api7.ai/tags/api-security)

[Plugin](https://api7.ai/tags/plugin)

Share article link

Share

Related Posts

[![API7 Gateway 3.10.8: Upgrade Identity Without Breaking Trust Boundaries](https://static.api7.ai/uploads/2026/10/19/z3DK9b7U_api7-3.10.8-identity-upgrade-runbook.webp)](https://api7.ai/blog/api7-3.10.8-identity-upgrade-runbook)

## [API7 Gateway 3.10.8: Upgrade Identity Without Breaking Trust Boundaries](https://api7.ai/blog/api7-3.10.8-identity-upgrade-runbook)

[![API7 Gateway 3.10.7: Close the Trust Gaps From Client to Upstream](https://static.api7.ai/uploads/2026/09/21/2CrEEFPQ_api7-3.10.7-client-upstream-trust.webp)](https://api7.ai/blog/api7-3.10.7-client-upstream-trust)

## [API7 Gateway 3.10.7: Close the Trust Gaps From Client to Upstream](https://api7.ai/blog/api7-3.10.7-client-upstream-trust)

Table of Contents

- [Plugin Introduction](#plugin-introduction)

- [How to Use the Plugin](#how-to-use-the-plugin)

  - [Enable CSRF Plugin in a Route](#enable-csrf-plugin-in-a-route)
  - [Send a Request](#send-a-request)
  - [Disable the Plugin](#disable-the-plugin)

- [Summary](#summary)

## Related Posts

[![FIPS 140-2 and Apache APISIX: Deployment Considerations](https://static.api7.ai/uploads/2024/09/06/rFoBcefJ_FIPS%20140-2.png)](https://api7.ai/blog/apisix-fips-140-2)

Products

January 6, 2023

#### [FIPS 140-2 and Apache APISIX: Deployment Considerations](https://api7.ai/blog/apisix-fips-140-2)

Understand FIPS 140-2 validation, the transition to FIPS 140-3, and how to plan an Apache APISIX deployment that uses an approved OpenSSL provider.

[APISIX Basics](https://api7.ai/tags/apisix-basics)

[API Security](https://api7.ai/tags/api-security)

[Data Management](https://api7.ai/tags/data-management)

[![360 Improves Cloud Resource Management With APISIX](https://static.api7.ai/uploads/2023/05/16/E6yFI1r4_cover2.png)](https://api7.ai/blog/apache-apisix-360)

Case Study

December 11, 2020

#### [360 Improves Cloud Resource Management With APISIX](https://api7.ai/blog/apache-apisix-360)

APISIX has addressed the challenges faced by 360 Cloud Computing in their Basic Operation and Maintenance Platform project.

[APISIX Basics](https://api7.ai/tags/apisix-basics)

[API Security](https://api7.ai/tags/api-security)

[Plugin](https://api7.ai/tags/plugin)

[![API7 Gateway 3.10.9: Require the Whole Keycloak Permission Contract](https://static.api7.ai/uploads/2026/11/02/FaHmPnuc_api7-3.10.9-keycloak-permission-contracts.webp)](https://api7.ai/blog/api7-3.10.9-keycloak-permission-contracts)

Products

November 2, 2026

#### [API7 Gateway 3.10.9: Require the Whole Keycloak Permission Contract](https://api7.ai/blog/api7-3.10.9-keycloak-permission-contracts)

Upgrade Keycloak API authorization safely by testing every required resource, scope, and HTTP-method scope before API7 Gateway 3.10.9 rollout.

[API7 Gateway](https://api7.ai/tags/api7-gateway)

[API Security](https://api7.ai/tags/api-security)

[Authorization](https://api7.ai/tags/authorization)
