App protection

Use these routes to read and set who can reach an HTTP application: allowed IPs, blocked IPs, countries, robots and exempt paths. For the console walkthrough, see App protection.

Authentication and permissions

Accepts an organization API Key (x-api-key: znf_... or Authorization: Bearer znf_...) or a user token with x-organization-id.

RoutePermission on project:<project-id>Limit
GET /v1/project/:id/protectionproject.metrics.read100 requests per minute
PUT /v1/project/:id/protectionproject.network.update10 requests per minute
PATCH /v1/project/:id/protection/blocksproject.network.update30 requests per minute
POST /v1/project/:id/protection/cache/purgeproject.network.update5 requests per minute

owner has full access. To change settings, the project must be on a plan with capabilities.network_access: true in the GET /v1/project/plans catalog.

Read the protection

GET /v1/project/:id/protection
{
  "status": "success",
  "data": {
    "protection": {
      "trusted_ips": [{ "cidr": "203.0.113.10/32", "description": "Office" }],
      "countries": { "mode": "block", "codes": ["RU"] },
      "user_agents": { "presets": ["ai"], "terms": ["BadBot"] },
      "exempt_paths": ["/api/webhooks"],
      "rate_limits": [{ "id": "login", "path_prefix": "/login", "methods": ["POST"], "rate": "10r/m", "burst": 5 }],
      "cache": { "mode": "headers", "version": 3 },
      "challenge": { "under_attack": false, "paths": ["/signup"] }
    },
    "network_access": {
      "ingress_white_list": [{ "cidr": "0.0.0.0/0", "description": "Any IP" }],
      "ingress_black_list": [{ "cidr": "198.51.100.7/32", "description": "Abuse" }]
    }
  }
}

protection is null when nothing has been configured. network_access carries the lists from Network access.

Set the protection

PUT /v1/project/:id/protection

The call replaces the configuration, with one exception: an absent rate_limits, cache or challenge keeps what is already stored. To clear one, send rate_limits: [], cache: { "mode": "off" } or challenge: { "under_attack": false, "paths": [] }.

FieldTypeRequiredDescription
protection.trusted_ipsarrayYesAllowed IPs, up to 200 items; { "cidr", "description" } in IPv4 or IPv6
protection.countries.modestringYesblock (block the listed) or allow_only (allow only the listed)
protection.countries.codesarrayYesISO country codes, up to 50
protection.user_agents.presetsarrayYesCategories: ai, seo, tools, empty
protection.user_agents.termsarrayYesUp to 20 terms of 2 to 40 characters (letters, digits, space and . _ / -), matched as text, case-insensitively
protection.exempt_pathsarrayYesUp to 10 path prefixes, such as /api/webhooks, exempt from country and robot rules. Each prefix starts with / and is up to 101 characters (letters, digits and . _ ~ / -)
protection.rate_limitsarrayNoUp to 5 per-route limit rules. Each: id (1 to 8 lowercase letters or digits, unique in the project), path_prefix (same format as exempt_paths), methods (optional; GET, POST, PUT, PATCH, DELETE; empty or absent applies to all), rate (10r/m, 60r/m, 10r/s or 50r/s, per IP) and burst (integer from 0 to 100)
protection.cache.modestringNooff, headers (honors the application's Cache-Control and Expires) or static (also stores .js, .css, .png, .jpg, .jpeg, .gif, .svg, .webp, .ico, .woff, .woff2 for 1 hour)
protection.cache.versionnumberNoRead-only: the value sent is ignored. Goes up on every cache purge and every successful deploy
protection.challenge.under_attackbooleanNotrue challenges every browser page GET
protection.challenge.pathsarrayNoUp to 10 path prefixes (same format as exempt_paths) challenged even without under-attack mode
ingress_black_listarrayNoBlocked IPs, up to 200 items; { "cidr", "description" } in IPv4 or IPv6
{
  "protection": {
    "trusted_ips": [{ "cidr": "203.0.113.10/32", "description": "Office" }],
    "countries": { "mode": "block", "codes": ["RU"] },
    "user_agents": { "presets": ["ai", "seo"], "terms": ["BadBot"] },
    "exempt_paths": ["/api/webhooks"],
    "rate_limits": [{ "id": "login", "path_prefix": "/login", "methods": ["POST"], "rate": "10r/m", "burst": 5 }],
    "cache": { "mode": "headers" },
    "challenge": { "under_attack": false, "paths": ["/signup"] }
  },
  "ingress_black_list": [{ "cidr": "198.51.100.7/32", "description": "Abuse" }]
}
{
  "status": "success",
  "data": {
    "protection": {
      "trusted_ips": [{ "cidr": "203.0.113.10/32", "description": "Office" }],
      "countries": { "mode": "block", "codes": ["RU"] },
      "user_agents": { "presets": ["ai", "seo"], "terms": ["BadBot"] },
      "exempt_paths": ["/api/webhooks"],
      "rate_limits": [{ "id": "login", "path_prefix": "/login", "methods": ["POST"], "rate": "10r/m", "burst": 5 }],
      "cache": { "mode": "headers", "version": 3 },
      "challenge": { "under_attack": false, "paths": ["/signup"] }
    },
    "network_access": {
      "ingress_white_list": [{ "cidr": "0.0.0.0/0", "description": "Any IP" }],
      "ingress_black_list": [{ "cidr": "198.51.100.7/32", "description": "Abuse" }]
    }
  }
}

The response carries the applied protection and IP lists, in the same format as the read. The same applies to quick blocks.

The rules take effect in 1 to 2 seconds, without restarting instances.

Block or unblock quickly

PATCH /v1/project/:id/protection/blocks
FieldTypeRequiredDescription
opstringYesadd or remove
typestringYesip, country or user_agent
valuestringYesThe IP (an IP without a prefix becomes /32 or /128), the country code or, for user_agent, a category name (ai, seo, tools, empty)
curl -X PATCH "https://api.zenifra.com/v1/project/6650f1a2b3c4d5e6f7a8b9c0/protection/blocks" \
  -H "x-api-key: znf_..." \
  -H "Content-Type: application/json" \
  -d '{"op": "add", "type": "ip", "value": "203.0.113.7"}'

Purge the cache

POST /v1/project/:id/protection/cache/purge

Invalidates all of Zenifra's cache for the project and responds with the same body as the read, with protection.cache.version incremented. It takes no body. Every successful deploy does the same when cache.mode is not off.

curl -X POST "https://api.zenifra.com/v1/project/6650f1a2b3c4d5e6f7a8b9c0/protection/cache/purge" \
  -H "x-api-key: znf_..."

With cache.mode set to off, the response is 409 PROTECTION_CACHE_OFF.

Limit, cache and challenge

  • Limit: above the rate and tolerance, the request gets 429. Counting is per IP and per server, so the limit is approximate. A trusted IP is never limited.
  • Cache: GET and HEAD only. .js and .css files may also be kept at the network edge; deploys and the purge apply to Zenifra's cache. Responses with Set-Cookie and requests with Authorization or a cookie are never stored. The response carries X-Cache with HIT, MISS or BYPASS.
  • Challenge: only a browser's page GET. API calls, webhooks and certificates are never challenged, nor are trusted IPs and exempt paths. The proof takes about 1 second and the cookie is valid for 24 hours. The /.zenifra/ prefix is reserved.

These features require the same plan as the rest of the protection, starting with Premium Plus. On another plan the API returns PROTECTION_NOT_AVAILABLE_ON_PLAN when changing.

Order of evaluation

  1. The certificate validation path always passes.
  2. Allowed IP passes.
  3. Private project: anyone outside the allow list gets 404 ("Application not found").
  4. Blocked IP: 403 with the "Access blocked by the site owner" page.
  5. Exempt path passes.
  6. Country. An unknown country is never blocked.
  7. Robot.

Protection rules do not apply to previews. Country data uses IP Geolocation by DB-IP, under CC BY 4.0.

Errors

CodeSituation
400 PROTECTION_INVALID_VALUEInvalid value; the field is in field. Includes a sixth limit rule, a rate outside the list, burst outside 0 to 100, an invalid path or method
400 NETWORK_RULES_LIMIT_EXCEEDEDMore items than a list's limit
400 PROTECTION_BLOCKS_EVERYONEQuick block of 0.0.0.0/0 or ::/0; to make the project private, use IP access control
403 PROTECTION_NOT_AVAILABLE_ON_PLANThe plan does not allow blocking IPs
404Project not found
409 PROTECTION_COUNTRY_MODE_CONFLICTQuick country block or unblock while the configuration is allow_only
409 PROTECTION_CACHE_OFFCache purge with cache.mode set to off
409Project with private exposure
422The project is a preview or is not an HTTP application
429Request limit exceeded

Next steps

Last updated on

On this page