Migrating from PHP

laravel-rust-captcha Alpha Laravel for Rust

Lookup

PHP (mews/captcha)Rust
Mews\Captcha\CaptchaServiceProviderCaptchaServiceProvider::new() in with_providers (no auto-discovery)
config/captcha.phpCaptchaConfig in with_config, or CaptchaConfig::from_php_json
CAPTCHA_DISABLECAPTCHA_DISABLE
GET captcha/{config?}GET captcha/{profile?}
GET captcha/api/{config?}GET captcha/api/{profile?}
captcha($config), Captcha::create($config)the image route, or CaptchaManager::image
Captcha::create($config, true)laravel_rust_captcha::issue(&request, profile)
captcha_src($config), Captcha::srcsrc(profile); MiniJinja captcha_src(profile)
captcha_img($config, $attrs), Captcha::imgimg(profile, attrs); MiniJinja captcha_img(profile, **attrs)
captcha_check($value), Captcha::checkcheck(&request, profile, value) → Verdict
captcha_api_check($value, $key, $config), Captcha::check_apicheck_keyed(ip, key, profile, value) → Verdict
'captcha' ruleCaptcha::session()
'captcha_api:'.$key.','.$configCaptcha::keyed().profile(config), key in the captcha_key field
a prebound ImageManagerCaptchaServiceProvider::new().renderer(MyRenderer)
captcha.driver (gd, imagick)none: one built-in renderer
Lumen routesnone
ob_clean() before the imagenot needed: responses aren't written through an output buffer

Name the profile in the rule

In PHP the captcha rule accepted the latest challenge of any profile. Here a rule checks one profile, default unless you name another. If your form shows captcha_src('contact'), the rule becomes Captcha::session().profile("contact"). With APP_DEBUG on, a check that finds a challenge for another profile logs a warning naming both.

Intended differences

Every difference has a reason. The numbers match the plan and conformance/fixtures/differences.json, which the tests check.

#PHPHereWhy
1The API key is the bcrypt hash of the answerAn opaque random idA client could check guesses offline
2bcrypt per image; encrypt optionHMAC-SHA256; encrypt ignoredCPU per image; nothing secret leaves the server
3One challenge per session, stored in the session; the API route overwrites itOne current challenge per session and profile, kept by the store; the API route has no sessionTabs, APIs, Inertia; old cookies can't revive challenges
4Characters picked with rand()The OS random generator, without biasSecurity
5Checking reads, then deletesAt most one pass, every comparison countedSimultaneous submissions both passed
6Any profile's challenge passes any formBound to the rule's profileA weak profile can't pass a strict form
7captcha_img doesn't escapeEscaped, safe names only, default altCross-site scripting
8quality ignored (always 75)HonouredIt's a documented key
9An unknown profile serves the class defaults404Typos show; no probing
10Cache-Control: no-cache, privateno-storeImages must never be cached
11A string alphabet is split by bytesBy charactersMultibyte alphabets
12Surrounding spaces failTrimmedLaravel's TrimStrings did this anyway
13Layout divides by length, so math text overflowedBy the drawn text lengthMath profiles with a short length
14The rule passes a blank field without requiredFailsFail closed
15The rule fails the answer "0"PassesPHP's falsy-string check
16Message validation.captcha"The :attribute is incorrect."No raw key on screen
17A common fork ignores expireexpire: None keeps the challenge up to retentionBounded store
18Lumen; GD or ImagickOne built-in rendererSingle binary
19blur up to 100; no limitsThe limits in ConfigurationBounded work per image
20The facade keeps settings between profilesEach profile is resolved freshSettings leaked into the next profile
21Any cache storeShared, non-failover stores; SQLite needs its own fileOne pass needs one authority
22HEAD on the image route issues a challengeHEAD issues nothingA probe shouldn't replace the visitor's image
23length 0 fails every requestOut-of-range settings stop the app at bootMistakes show at deploy time

Tests

PHP tests that wrote captcha into the session and captcha_<md5> into the cache move to testing::seed or testing::fix_answers; see Testing.