跳到主内容

Web Bot Auth

Websites see an agent as one more bot. A User-Agent string or an IP range is easy to fake, so many sites block agents along with scrapers. Web Bot Auth lets an agent sign each request with HTTP Message Signatures (RFC 9421): the Signature-Agent header names where its public keys are, and the site checks the signature against them. Cloudflare and other CDNs already verify these signatures.

Casdoor keeps the key of each agent, publishes the public keys, and signs requests for the agent, so the agent doesn't have to hold a private key.

启用它​

On the edit page of an agent, turn on Sign HTTP requests under Web Bot Auth and save. Casdoor creates an Ed25519 key for the agent and shows:

ItemDescription
Key IDThe JWK SHA-256 thumbprint of the public key (RFC 7638), the keyid of every signature
Signature-Agentsig1="https://<casdoor>", the header that tells sites where the keys are
Directoryhttps://<casdoor>/.well-known/http-message-signatures-directory

Regenerate creates a new key when you save, and the old one leaves the directory at once. Copy private key (JWK) gives the admins of the organization the private key, for an agent that signs requests itself.

The key leaves the directory and Casdoor stops signing when you turn it off, or when the agent is disabled, expired or orphaned.

The directory​

GET /.well-known/http-message-signatures-directory

returns the public keys of all active agents with Web Bot Auth as a JWK set, with the media type application/http-message-signatures-directory+json:

{"keys": [{"kty": "OKP", "crv": "Ed25519", "kid": "JsrEO0vUB1nnnRLlGaDU21HxdffphDoF1rlIKlLla9c", "x": "RlfjkR8-99mhsSwYAhTLZNJEEYNgJXyC_5xkst4ZOYk", "use": "sig"}]}

As the draft asks, the response is signed with each key over "@authority";req with tag="http-message-signatures-directory", so a verifier can check that the keys belong to this directory. Register the directory with the CDNs and sites your agents visit, for example on the Cloudflare verified bots page.

Sign a request​

The agent asks Casdoor to sign each request before it sends it:

curl -u <client ID>:<client secret> -X POST https://<casdoor>/api/web-bot-auth/sign \
-H "Content-Type: application/json" \
-d '{"url": "https://shop.example.com/cart", "expiresIn": 300}'

The caller is the agent's application, with its client ID and secret, or with a client credentials token as Authorization: Bearer. url is the request the agent will send. expiresIn is how long the signature lasts, 300 seconds by default and at most a day. Casdoor returns the three headers to add to the request:

{
"Signature-Agent": "sig1=\"https://door.casdoor.com\"",
"Signature-Input": "sig1=(\"@authority\" \"signature-agent\";key=\"sig1\");created=1796523104;expires=1796523404;keyid=\"JsrEO0vUB1nnnRLlGaDU21HxdffphDoF1rlIKlLla9c\";alg=\"ed25519\";nonce=\"yBoCW1hv...\";tag=\"web-bot-auth\"",
"Signature": "sig1=:YicRAJRGvJjzt5oEa6TWzOWh5HbofWfB...:"
}

The signature covers the host of the URL, in lowercase and without the default port, and the Signature-Agent header, with a random nonce. It is good for any path on that host until it expires, so an agent that crawls one site can sign once and reuse the headers for a few minutes.

An application that isn't an agent's gets 403, and an agent without Web Bot Auth gets 400.

Verify signatures with CasWAF​

CasWAF checks these signatures with a rule of type Web Bot Auth. Each expression of the rule is one of:

OperatorValueMatches
is, is notverified, invalid or unsignedThe result of the check
is in, is not inHosts of Signature-Agent, such as door.casdoor.com, *.example.comRequests verified with a key from one of these directories

For example, block forged signatures with is invalid, or put an Allow rule with is in door.casdoor.com before the rules that block bots. CasWAF fetches the directory named in Signature-Agent, caches it for an hour, and doesn't fetch directories on private addresses unless webBotAuthAllowIntranet = true is set in its conf/app.conf. See Web Bot Auth rules in the CasWAF docs.