Configuration

laravel-go-captcha Alpha Laravel for Go

Laravel for Go has no config files: the package reads a few environment variables when it registers, and you change everything else in code, in the function you pass to captcha.New.

Environment

VariableDefaultWhat it does
CAPTCHA_DISABLEfalseTurns the captcha off: no routes, and every rule passes.
CAPTCHA_STOREthe default cache storeThe cache store for challenges and the failure limit.
CAPTCHA_THROTTLE30Images a client may request per minute, on every entry point. 0 turns it off.
CAPTCHA_FAILURES10Wrong answers a client may give per minute and profile. 0 turns it off.
CAPTCHA_SINGLE_PROCESSfalseAllows the process-local memory store, for an application that runs exactly one process.

Profiles

A profile is one captcha style, like a named array in mews/captcha's config/captcha.php. Start every profile from captcha.Defaults(), which holds the PHP class defaults:

captcha.New(func(cfg *captcha.Config) {
	contact := captcha.Defaults()
	contact.Length = 5
	contact.Width, contact.Height = 180, 54
	contact.Expire = 2 * time.Minute
	cfg.Profiles["contact"] = contact
})

Like PHP, a profile doesn't inherit from the default profile: keys you don't set keep the class defaults.

FieldRangeMeaning
Length1 to 32Number of characters.
Width, Height1 to 1000, 16 to 400Image size in pixels.
Quality1 to 100JPEG quality.
MathDraws x + y = and expects the sum. Needs Attempts from 1 to 3.
SensitiveKeeps the case of answers.
Characters1 to 256 entriesThe alphabet for this profile; an entry may be several characters.
ExpireHow long a challenge may be answered, or captcha.NoDeadline, which still ends after Config.NoDeadlineLifetime (24 hours by default).
AttemptsAnswers a challenge accepts: 1 (default), any number, or captcha.Unlimited.
Lines, LineWidth, LineColor0 to 100, 1 to 20Noise lines. mews/captcha draws Lines + 1.
Angle0 to 180The largest rotation of a character, in degrees.
Contrast, Sharpen, Blur, Invert−100 to 100, 0 to 100, 0 to 10Image filters, as in PHP.
BgImage, BgColorA random background image, or a plain colour.
FontColorsA palette for the characters; empty means a random colour each.
Fonts, BackgroundsReplace the bundled fonts or backgrounds for this profile.
TextLeftPadding, MarginTopWhere the characters start. MarginTop 0 means Height / Length.

Colours are hex (with or without #), rgb(), rgba() or an HTML colour name. Every value is checked when the application starts: a value out of range, an unknown colour or a profile named api stops it with an error that names the setting.

An unknown profile in a URL is a 404.

From a PHP config

captcha.ParsePHPConfig reads the output of json_encode(config('captcha')), so you can move an existing config as it is:

cfg, err := captcha.ParsePHPConfig(data)

Paths into vendor/mews/captcha/assets map to the bundled assets; any other path must exist.

A custom renderer

The images are drawn by the package's own pipeline, which follows mews/captcha's. To draw them yourself, set Config.Renderer:

captcha.New(func(cfg *captcha.Config) {
	cfg.Renderer = myRenderer{}
})

Render(ctx, profile, settings, text) returns a JPEG; the package refuses any other output. It is called by several requests at once, within the render limit below, and ctx carries the request's deadline.

(*Captcha).Config() returns a copy of the settings the package registered with, so changing it has no effect.

Fonts and backgrounds

The six fonts and twelve backgrounds of mews/captcha are bundled with their licences. To build without them, use the build tag captcha_noassets and set Config.Fonts and Config.Backgrounds to your own files or directories.

The challenge store

Each challenge is a record in a cache store. A check consumes it with an atomic compare-and-swap, so of many simultaneous correct answers exactly one passes. The store must therefore be shared by every process of your application:

  • Redis and the database cache on Postgres or on a SQLite file of its own are tested and accepted.
  • discard and failover are refused when the application starts.
  • memory is refused unless CAPTCHA_SINGLE_PROCESS=true.
  • Any other store, including a custom one registered under a built-in name, needs Config.TrustStore = true. That states the store is shared by every process and that its compare-and-swap and counters are atomic.

Durability. A used challenge can't pass again as long as the store keeps the write that marked it used. If a Redis primary fails over to a replica that missed that write, or the store is restored from an older backup, an open copy of the challenge can come back. Where that matters, configure the store to keep acknowledged writes, or clear the captcha keys after a failover or restore.

Redis retries. The Redis client retries a command when the connection drops, even after Redis ran it. A retried check still never passes a used challenge: the repeated swap finds the record already changed and fails. A retried give-back of the wrong-answer limit can return one extra unit, though. To make the limit exact, add max_retries=-1 to the Redis URL, which turns the client's retries off.

Store operations run outside the caller's database transaction and read from the writer, so a rollback can't bring a used challenge back and a lagging replica can't serve an old one. Each operation waits at most 2 seconds; if the store doesn't answer, the check fails.

On SQLite, check the captcha before you open a write transaction: inside one, the check can't reach the store and fails.

Limits

  • Issuance: CAPTCHA_THROTTLE images per minute per client. Beyond that, a 429.
  • Wrong answers: at most CAPTCHA_FAILURES committed wrong answers per client and profile in each clock minute. Every check reserves one unit before it reads anything, and gets it back unless the answer was wrong or the outcome is unknown, so simultaneous guesses can't exceed the limit. Because the window is a clock minute, up to twice the limit can land within 60 seconds across a minute boundary. It is a rate bound, not a guarantee: a math profile has only 29 possible sums, so keep its Attempts low (at most 3).
  • The client is the request's IP address, as the framework resolves it through your trusted proxies. A proxy you trust must set the forwarded address correctly. Checks made without a request share one bucket.
  • Rendering: at most Config.RenderConcurrency images are drawn at once (default: GOMAXPROCS). When all are busy for 50 ms, the image routes answer 503 with Retry-After: 1.