Skip to main content

SA Rule Expression Guide

This document is compiled from the NamesLink SA (Security Acceleration) Help Center to guide users in writing and AI-generating SA rule expressions.


1. Overview​

SA rule expressions match traffic based on request characteristics and apply differentiated policies. A rule consists of a matching condition and an executing action: when a request matches the condition, the rule-level configuration takes precedence over global settings.

Limits​

  • A single rule configuration (condition + action) can be up to 4 KB.
  • Total site-level configuration (global settings + rules + certificates) must not exceed 512 KB (security rules excluded).
  • Rule name length: 1–128 characters.
  • Maximum nested sub-conditions per rule: up to 20 for the Enterprise plan.

2. Expression Structure​

Single expression​

A single expression consists of a match field, a match operator, and a match value:

( <match field> <match operator> <match value> )

Example:

(http.host eq "www.example.com")

Compound expressions​

Use logical operators to combine multiple sub-conditions:

  • AND (and): all conditions must be satisfied.
  • OR (or): any condition can be satisfied.

Example:

(http.host eq "www.example.com" and ip.geoip.country eq "CN")

Nesting​

SA rules support nesting an AND relationship inside an OR relationship, with a maximum nesting depth of 2 levels. Use parentheses () to control precedence:

(http.host eq "www.example.com")
or
(http.host eq "blog.example.com" and not (ip.geoip.country eq "CN"))
or
(http.host eq "login.example.com")

Match all requests​

Use true to apply a rule to all requests:

true

3. Match Fields​

SA match fields fall into three categories:

  • Standard fields: attributes from standard protocols such as HTTP, IP, and TLS.
  • Extended fields: computed values derived by SA from requests/responses.
  • Original fields: raw request attributes preserved across SA processing modules.

3.1 Standard fields​

FieldTypeDescriptionExample
http.hostStringRequest hostnamewww.example.com
http.request.uriStringURI (path + query string)/image/cat.jpg?width=400
http.request.uri.pathStringURI path/image/cat.jpg
http.request.uri.path.extensionStringFile extensionjpg
http.request.uri.queryStringQuery stringwidth=400&height=300
http.request.uri.argsMap<Array<String>>Query parameter map{"format":["webp"]}
http.request.full_uriStringFull URI (including protocol and hostname)https://www.example.com/image/cat.jpg
http.request.methodStringHTTP methodGET
http.request.headersObjectRequest header map (keys are lowercase){"content-type":["application/json"]}
http.request.cookiesStringCookie map{"sessionid":["330668"]}
http.cookieStringRaw Cookie headersessionid=330688;userid=abc123
http.refererStringReferer headerhttp://www.example.com/index
http.user_agentStringUser-Agent headerMozilla/5.0
http.request.body.formMap<Array<String>>Form-format request body{"username":["admin"]}
http.request.body.mimeStringRequest body MIME typeapplication/json
http.request.body.rawStringRaw request bodyABC123
http.request.timestamp.secIntegerUnix timestamp (seconds) when SA POP receives request1735019278
http.request.versionStringHTTP protocol versionHTTP/1.1
http.x_forwarded_forStringX-Forwarded-For header192.168.0.1, 10.10.0.1
ip.srcIP addressClient source IP192.0.2.1
ip.src.ispStringISP code100017 (China Telecom)
ip.src.versionStringIP protocol versionIPv4 / IPv6
ip.src.region_codeStringLoad balancer regionEAS
ip.src.subdivision_1_iso_codeStringFirst-level subdivisionCN-ZJ
ip.src.cityStringCity code310000
ip.src.city_nameStringCity namehangzhou
ip.src.lonFloatLongitude120.12
ip.src.latFloatLatitude30.16
ip.src.postal_codeStringPostal code310000
ip.src.timezoneStringTime zoneUTC+8
ip.geoip.asnumIntegerASN number37963
ip.geoip.continentStringContinent codeAS (Asia)
ip.geoip.countryStringCountry/region codeCN
sslBooleanWhether HTTPS is usedtrue

Common country/region codes​

CodeMeaning
CNChinese mainland
USUnited States
HKHong Kong, China
TWTaiwan, China
JPJapan
SGSingapore
GBUnited Kingdom
DEGermany
FRFrance
KRSouth Korea

Continent codes​

CodeContinent
AFAfrica
ANAntarctica
ASAsia
EUEurope
NANorth America
OCOceania
SASouth America

3.2 Extended fields​

FieldTypeDescription
ali.ja3_hashStringJA3 TLS fingerprint
ali.ja4StringJA4 TLS fingerprint
ali.js_detection.passedBooleanWhether JavaScript detection passed
ali.static_resourceBooleanWhether the request is for a static resource
ali.tls_client_auth.cert_verifiedBooleanWhether the client certificate is verified
ali.tls_hashStringTLS fingerprint hash
ali.site.nameStringSA site name

3.3 Original fields​

FieldTypeDescription
http.request.body.rawStringRaw HTTP request body

4. Operators​

4.1 Comparison operators​

OperatorNameValue typeExample
eqEqual toString / int / Boolean / IP(http.host eq "www.example.com")
neNot equal toString / int / Boolean / IP(http.host ne "www.example.com")
containsContainsString(http.host contains "example.com")
not ... containsDoes not containString(not http.host contains "example.com")
matchesMatches regexString`(http.host matches "(www\
not ... matchesDoes not match regexString`(not http.host matches "(www\
inIs inArray(http.host in {"www.example.com" "blog.example.com"})
not ... inIs not inArray(not http.host in {"www.example.com" "blog.example.com"})
starts_withStarts withString(starts_with(http.host, "blog"))
not starts_withDoes not start withString(not starts_with(http.host, "blog"))
ends_withEnds withString(ends_with(http.host, "cn"))
not ends_withDoes not end withString(not ends_with(http.host, "cn"))
ltLess thanint(ip.geoip.asnum lt 45104)
leLess than or equal toint(ip.geoip.asnum le 45104)
gtGreater thanint(ip.geoip.asnum gt 45104)
geGreater than or equal toint(ip.geoip.asnum ge 45104)
len eqLength equalsString(len(http.request.cookies["session"]) eq 330688)
len gtLength greater thanString(len(http.request.cookies["session"]) gt 330688)
len ltLength less thanString(len(http.request.cookies["session"]) lt 330688)
existsExistsString(exists(http.request.headers["user-agent"]))
not existsDoes not existString(not exists(http.request.headers["user-agent"]))
lowerCase-insensitiveString(lower(http.request.uri))

Note: matches (regex) is supported only in the Advanced and Enterprise plans.

4.2 Logical operators​

OperatorNameExample
notLogical NOTnot (http.host eq "www.example.com")
andLogical AND(http.host eq "www.example.com" and ip.geoip.country eq "CN")
orLogical OR(http.host eq "www.example.com") or (ip.geoip.country eq "CN")

4.3 Arrays and list references​

The in operator uses braces {} with elements separated by spaces:

(http.host in {"www.example.com" "blog.example.com" "shop.example.com"})

You can also reference a named list created in the SA console:

(ip.src in $<LIST_NAME>)

5. Match Values​

5.1 Strings​

Strings are enclosed in double quotes " or single quotes '. Special characters such as backslash \ must be escaped.

(http.host eq "www.example.com")
(http.request.uri contains "/api/v1/")

5.2 Booleans​

Use true or false directly, without quotes.

(ssl eq true)

5.3 Numbers​

Integers and floating-point numbers are supported.

(ip.geoip.asnum ge 45104)

5.4 Arrays​

Arrays use square brackets [] with comma-separated elements. However, SA rule expressions prefer the brace {} space-separated syntax for set matching.

5.5 Null values​

Some fields allow empty string match values, depending on the operator. For http.request.uri.args, empty values are supported only with eq, ne, contains, and matches.


6. Transform Functions​

SA rule language provides transform functions for manipulating and validating values.

FunctionDescriptionFormatExample
concatConcatenate stringsconcat(String | Integer | Bytes | Array elements):Stringconcat("https://www.example.com", http.request.uri.path)
ends_withEnds with substringends_with(source, substring):Booleanends_with(http.request.uri.path, ".html")
starts_withStarts with substringstarts_with(source, substring):Booleanstarts_with(http.request.uri.path, "/api/")
lowerConvert to lowercaselower(String):Stringlower(http.host) eq "www.example.com"
upperConvert to uppercaseupper(String):Stringupper(http.host)
regex_replaceRegex replacementregex_replace(source, regex, replacement):Stringregex_replace(http.request.uri.path, "/cat/(.*)$", "/dog/${1}")
wildcard_replaceWildcard replacementwildcard_replace(source, pattern, replacement, flags?):Stringwildcard_replace(http.request.uri.path, "/*", "/apps/${1}")
to_stringConvert to stringto_string(Integer | Boolean | IP address):Stringto_string(ip.src.asnum)

6.1 Function usage limits​

  • For ends_with / starts_with, the source argument must be a field, not a literal string. Do not write ends_with("foo.html", ".html").
  • regex_replace replaces only the first match by default; matching is case-sensitive; $ in the replacement string must be escaped as $$; supports ${N} capture group references (up to 8).
  • For wildcard_replace, the source must be a field; literal * in wildcard_pattern must be escaped as \*; \ must be escaped as \\; ** is invalid; set flags to s for case-sensitive matching; uses lazy matching.

7. Common Examples​

7.1 Hostname match​

(http.host eq "www.example.com")

7.2 Path prefix match​

(http.request.uri.path starts_with "/api/")

7.3 Static resource match​

(http.request.uri.path.extension in {"jpg" "jpeg" "png" "gif" "css" "js" "webp" "svg" "ico"})

7.4 Country/region match​

(ip.geoip.country eq "CN")
(not ip.geoip.country eq "CN")

7.5 IP allowlist​

(ip.src in {"1.2.3.4" "5.6.7.8"})

7.6 HTTP method match​

(http.request.method eq "POST")

7.7 Compound condition​

(http.host eq "www.example.com" and http.request.method eq "POST" and http.request.uri.path starts_with "/api/")

7.8 Header match​

(http.request.headers["x-api-key"] eq "secret-key")
(exists(http.request.headers["authorization"]))
(http.request.cookies["sessionid"] eq "abc123")

7.10 User-Agent match​

(http.user_agent contains "bot")

7.11 HTTPS match​

(ssl eq true)

7.12 Regex match​

(http.request.uri.path matches "^/api/v[0-9]+/.*$")

8. Expression Builder and Editor​

The SA console provides two configuration methods:

  1. Graphical controls: configure conditions via dropdowns and input fields; expressions are generated automatically.
  2. Expression editor: write expressions directly; supports parentheses and grouping symbols.

When using the expression editor:

  • Wrap single expressions in parentheses.
  • Use parentheses explicitly to control precedence in compound expressions.
  • Escape special characters in strings correctly.
  • Regular expressions must follow the PCRE standard.

9. Notes​

  1. Field case sensitivity: fields differ in case sensitivity; verify the field properties before configuration.
  2. Empty string support: some fields do not allow empty string match values.
  3. Plan limits: regex matching and sub-condition count are limited by subscription plan.
  4. Performance impact: excessively long strings, deep nesting, or complex regex may affect matching performance.
  5. Expression length: a single rule must not exceed 4 KB.