Korzen
Bot Protection
In these docs

Go net/http

Bot Protection in a Go server with net/http alone: one client for the process, the whole call under one timeout, and the visitor's IP.

This guide uses the standard library alone: net/http and one http.Client for the process, which keeps its connections open. Its Timeout bounds the whole call, from connecting to reading the body.

net/http does not decode Brotli by itself. If your module does, with github.com/andybalholm/brotli, use v1.2.7 or later, which fixes the logic error behind CVE-2026-111497 (in Go, malformed compressed data can make the decoder panic); with cbrotli, link libbrotli 1.0.10, 1.1.1 or 1.2.1 or later, whose earlier releases let malformed compressed data corrupt the process's memory.

Payment, login and sign-up routes: turn on protectedAction ("protected_action": true in the call), so a script that never loads your page cannot take the action. Protecting a checkout

The tag

Put the tag in the <head> of the layout that renders your sign-up, sign-in and checkout pages:

<script src="https://eu.korzen.dev/v1/tag.js" data-site-key="YOUR_SITE_KEY" async></script>

In the page, before the form is sent, call await window.korzen?.ready(): it reports what the visitor did since the tag last reported, and resolves within a few seconds whatever happens. The tag

For visitors whose network blocks Korzen's host, Options.FirstParty serves the tag and its beacon from your own domain. First-party mode

The call

Behind Cloudflare? A Worker can do this without changing your app. Cloudflare Workers

The client is the Go module korzen.dev/bot-protection/go/v3, on the standard library and two of the Go team's modules, golang.org/x/net and golang.org/x/text: it asks /v1/decide, gives up at your timeout and lets the visitor through on any error. Add it to your module:

go get korzen.dev/bot-protection/go/v3

One Client for the process: it keeps its connections open, and its Timeout bounds the whole call. Protect asks before the handler that creates the account, and puts the answer in the request's context:

import botprotection "korzen.dev/bot-protection/go/v3"

// The timeout for your server's region: https://korzen.dev/bot-protection/docs/server-call#latency
var bot = botprotection.New(os.Getenv("KORZEN_API_KEY"), botprotection.WithURL("https://eu.korzen.dev"), botprotection.WithTimeout(200*time.Millisecond))

mux.Handle("POST /signup", bot.Protect(http.HandlerFunc(signup), botprotection.Options{
	IP: botprotection.RemoteIP, // see "The visitor's IP"
	ProtectedAction: true, // sign-up, login and payment: a script that never loaded your page holds no token
	OnBlock: func(w http.ResponseWriter, r *http.Request, d botprotection.Decision) bool {
		http.Error(w, "We could not create your account.", http.StatusBadRequest)
		return true
	},
}))

func signup(w http.ResponseWriter, r *http.Request) {
	d, _ := botprotection.FromContext(r.Context()) // no answer: the zero Decision
	user, err := createAccount(r) // your own code
	// ...
	saveRequestID(user.ID, d.RequestID) // see "Apply the action"
}

To ask inside a handler instead: d, err := bot.Decide(r.Context(), botprotection.FromHTTP(r, botprotection.RemoteIP(r))). On any error d.Action is allow, and err says why, for your log. An answer whose action is not allow or block is an error too; an error answer's *botprotection.Problem is in err (errors.As). To log every call, set the client's OnAnswer: it gets the status, Korzen-Request-Id and the RateLimit headers.

The visitor's IP

r.RemoteAddr is the connection's address: your proxy's, if you have one. botprotection.RemoteIP reads it: use it when nothing stands in front of your server. botprotection.CloudflareIP reads CF-Connecting-IP, which is right only if your server takes traffic from Cloudflare alone; otherwise a visitor could write it. Behind your own load balancer, botprotection.LastForwardedIP takes the last entry of X-Forwarded-For, the one it added.

Apply the action

Read only action. On block, answer with the same error the action shows when it fails for any other reason, so a bot learns nothing. The account starts in monitor mode, where action is always allow: nothing changes for visitors until you turn on block mode.

Keep the answer's request_id with what the action created, such as the account or the order: it names that decision when you ask us about it. The answer

Test it on your machine

Run go run . and open your server on localhost. Pages on localhost always accept the tag and are never listed as a domain. Locally the IP is 127.0.0.1 or ::1, and that is fine. In Docker on your machine, the app sees the bridge's address instead, such as 172.17.0.1, and that is fine too.

  1. Set KORZEN_API_KEY in the environment and open the sign-up page. In the browser's developer tools, the _kz cookie appears after a few seconds.
  2. Sign up. The decision shows on the dashboard's Decisions page, under the path you sent.
  3. Check fail open: set a wrong key and sign up again. The call answers 401, and the sign-up goes through.

Common mistakes

  • An HTTPClient with a new http.Transport per call, or the standard library's default, which keeps two connections open. Each new one is a new TLS connection: leave HTTPClient empty, or give yours botprotection.NewTransport(), once for the process.
  • Reading CF-Connecting-IP on a server Cloudflare does not front. A bot writes the header and picks its own IP.
  • Calling /v1/decide from the browser. The API key would reach every visitor; the call belongs on your server.
  • No timeout, or an error that stops the sign-up. Any answer other than 200, and no answer in time, means: let the visitor through.
  • A line in your log that starts korzen: the visitor's IP sent to Bot Protection. The client writes it once, at the first decision whose IP is your own network's (a load balancer's, a proxy's, a container's), which Bot Protection judges as no IP: set Options.IP to the visitor's address (the visitor's IP). In Docker on your machine it is the bridge's address, and harmless there.