Skip to content
下载《AI 应用 & AI Agent 开发新范式》电子书 了解构建 AI Agent 和 MCP Server 的一线实践Know more

Replay Attack Prevention

Functional Description

The replay prevention plugin prevents request replay attacks by verifying the one-time random number in the request. Each request needs to carry a unique nonce value. The server will record and verify the uniqueness of this value, thus preventing requests from being maliciously replayed.

Specifically, it includes the following functions:

  • Mandatory or Optional Nonce Verification: It can be configured to determine whether requests are required to carry a nonce value.
  • Nonce Uniqueness Verification Based on Redis: The nonce value is stored and verified in Redis to ensure its uniqueness.
  • Configurable Nonce Validity Period: It supports setting the validity period of the nonce, which will automatically expire after the period.
  • Nonce Format and Length Verification: It supports verifying the format (Base64) and length of the nonce value.
  • Custom Error Response: It supports configuring the status code and error message when a request is rejected.
  • Customizable Nonce Request Header: The name of the request header carrying the nonce can be customized.

Runtime Attributes

Plugin execution stage: Authentication Stage Plugin execution priority: 800

Configuration Fields

NameData TypeRequiredDefault ValueDescription
force_nonceboolNotrueWhether requests are required to carry a nonce value.
nonce_headerstringNoX-Higress-NonceSpecifies the name of the request header carrying the nonce value.
nonce_ttlintNo900The validity period of the nonce, in seconds.
nonce_min_lengthintNo8The minimum length of the nonce value.
nonce_max_lengthintNo128The maximum length of the nonce value.
reject_codeintNo429The status code returned when a request is rejected.
reject_msgstringNoReplay Attack DetectedThe error message returned when a request is rejected.
validate_base64boolNofalseWhether to verify the Base64 encoding format of the nonce.
redisObjectYes-Redis-related configuration

Description of each configuration field in redis

NameData TypeRequiredDefault ValueDescription
service_namestringYes-The name of the Redis service, the complete FQDN name with the service type, such as my-redis.dns, redis.my-ns.svc.cluster.local.
service_portintNo6379The port of the Redis service.
usernamestringNo-The username of Redis.
passwordstringNo-The password of Redis.
timeoutintNo1000The connection timeout time of Redis, in milliseconds.
databaseintNo0The ID of the database to be used. For example, if it is configured as 1, it corresponds to SELECT 1.
key_prefixstringNoreplay-protectionThe key prefix of Redis, used to distinguish different nonce keys.

Configuration Example

The following is a complete configuration example of the replay attack prevention plugin:

force_nonce: true
nonce_header: "X-Higress-Nonce" # Specifies the name of the nonce request header
nonce_ttl: 900 # The validity period of the nonce, set to 900 seconds
nonce_min_length: 8 # The minimum length of the nonce
nonce_max_length: 128 # The maximum length of the nonce
validate_base64: true # Whether to enable Base64 format verification
reject_code: 429 # The HTTP status code returned when a request is rejected
reject_msg: "Replay Attack Detected" # The error message content returned when a request is rejected
redis:
service_name: redis.static # The name of the Redis service
service_port: 80 # The port used by the Redis service
timeout: 1000 # The timeout time of Redis operations (unit: milliseconds)
key_prefix: "replay-protection" # The key prefix in Redis

Usage Instructions

Request Header Requirements

Request Header NameRequiredDescription
X-Higress-NonceDetermined by the force_nonce configurationThe randomly generated nonce value carried in the request, which needs to conform to the Base64 format.

Note: The name of the request header can be customized through the nonce_header configuration. The default value is X-Higress-Nonce.

Usage Example

Terminal window
# Generate nonce
nonce=$(openssl rand -base64 32)
# Send request
curl -X POST 'https://api.example.com/path' \
-H "X-Higress-Nonce: $nonce" \
-d '{"key": "value"}'

Return Results

{
"code": 429,
"message": "Replay Attack Detected"
}

Error Response Examples

Error ScenarioStatus CodeError Message
Missing nonce request header400Missing Required Header
Nonce length does not meet the requirements400Invalid Nonce
Nonce format does not conform to Base64400Invalid Nonce
Nonce has been used (replay attack)429Replay Attack Detected