Laravel for Rust Alpha
laravel-rust-captcha
mews/captcha's image captcha, in Laravel for Rust.
A self-hosted image captcha with mews/captcha's profiles, routes, helpers and validation rules. Nothing is sent to Google or Cloudflare, and each challenge passes at most once, even when two submissions arrive together.
Read the docs Source on GitHub
cargo add laravel-rust-captcha- Latest release
- 0.1.0, 2026-12-04
- Maintenance
- Active
- Framework
- laravel-rust 0.2.x
- MSRV
- Rust 1.99 or later
- Licence
- MIT
PHP is the oracle
The real mews/captcha 3.5.1 runs 44 scenarios in a pinned PHP image. Every ability in
parity.tomlis covered by a test or listed as a deliberate difference with its reason.One pass per challenge
Answers are verified with a keyed HMAC and never stored or sent. A correct answer passes only for the check that removes the challenge, tested on memory, SQLite, PostgreSQL and Redis.
Session and keyed modes
Captcha::session()checks the image the visitor last loaded, as in PHP.Captcha::keyed()takes the challenge id from a form field, for Inertia, APIs and pages with several captchas.Images like PHP's
Profiles with every PHP setting, the PHP package's fonts and backgrounds, math captchas, and filters that match GD within one colour unit.
PHP parity
Follows mews/captcha 3.5.1 (commit 7154a930e88b). Last verified .
- Parity level
- Full
- Abilities
- 87
- Covered
- 54
- Diverged
- 33
- Planned
- 0
- PHP fixtures
- 44
Fixtures are generated by PHP itself: PHP 8.5, intervention/gif 5.0.2, intervention/image 4.3.2, laravel/framework v13.45.0, mews/captcha 3.5.1, orchestra/testbench v11.3.1, orchestra/testbench-core v11.6.2, phpunit/phpunit 13.4.8.
Coverage by ability
-
captcha.config.disable· Covered · Core · 2 testsdisable and CAPTCHA_DISABLE -
captcha.config.characters.array· Covered · 1 testGlobal characters array -
captcha.config.characters.string· DivergedGlobal characters string -
captcha.config.fonts_directory· Covered · 1 testfontsDirectory -
captcha.config.bgs_directory· Covered · 1 testbgsDirectory -
captcha.config.driver· Divergedcaptcha.driver (GD or Imagick) -
captcha.config.profiles· Covered · 1 testFive shipped named profiles -
captcha.config.profile_replace· Covered · 1 testApplication profiles replace shipped profiles in full -
captcha.config.profile_defaults· Covered · 2 testsClass defaults for missing profile keys -
captcha.config.profile_unknown· DivergedClass defaults for an unknown profile -
captcha.config.length· Covered · 1 testlength -
captcha.config.length_zero· Divergedlength = 0 makes the route fail -
captcha.config.width· Covered · 1 testwidth -
captcha.config.height· Covered · 1 testheight -
captcha.config.quality· Divergedquality is ignored (JPEG quality is always 75) -
captcha.config.math· Covered · 1 testmath -
captcha.config.expire· Covered · 1 testexpire in seconds -
captcha.config.encrypt· Divergedencrypt -
captcha.config.sensitive· Covered · 1 testsensitive -
captcha.config.lines· Covered · 1 testlines -
captcha.config.line_width· Covered · 2 testslineWidth -
captcha.config.line_color· Covered · 3 testslineColor -
captcha.config.angle· Covered · 2 testsangle -
captcha.config.bg_image· Covered · 2 testsbgImage -
captcha.config.bg_color· Covered · 3 testsbgColor -
captcha.draw.contrast· Covered · 2 testscontrast -
captcha.draw.sharpen· Covered · 2 testssharpen -
captcha.draw.blur· Covered · 2 testsblur -
captcha.draw.invert· Covered · 2 testsinvert -
captcha.config.font_colors· Covered · 2 testsfontColors -
captcha.config.text_left_padding· Covered · 2 teststextLeftPadding -
captcha.config.margin_top· Covered · 1 testmarginTop -
captcha.config.profile_characters· Covered · 1 testProfile characters -
captcha.config.profile_fonts· Covered · 2 testsProfile fonts -
captcha.config.profile_backgrounds· Covered · 1 testProfile backgrounds -
captcha.config.profile_directories· Covered · 1 testProfile fontsDirectory and bgsDirectory have no effect -
captcha.config.publish· Divergedvendor:publish configuration -
captcha.config.store· DivergedPHP cache-store selection -
captcha.config.limits· DivergedBlur up to 100 and no comparable profile limits -
captcha.route.image· Covered · Core · 1 testGET captcha/{config?} -
captcha.route.api· DivergedGET captcha/api/{config?} returns a bcrypt-based key -
captcha.route.web· Divergedweb middleware on both routes -
captcha.route.cache_control· DivergedCache-Control: no-cache, private -
captcha.route.disabled· Covered · 1 testRoutes absent when disable is enabled -
captcha.route.lumen· DivergedLumen routes -
captcha.route.output_buffer· Divergedob_clean before sending the image -
captcha.route.throttle· DivergedIssuance without throttling -
captcha.route.head· DivergedHEAD on the image route issues a challenge -
captcha.generate.random· Divergedrand selects answers -
captcha.generate.lowercase· Covered · 1 testLowercase unless sensitive -
captcha.generate.verifier· Divergedbcrypt with optional encryption -
captcha.generate.session· DivergedSession holds captcha sensitive, key and encrypt -
captcha.generate.ttl· Covered · 1 testCache entry expires after expire seconds -
captcha.generate.no_deadline· DivergedThe upstream fork ignores session expiry -
captcha.check.no_session· Covered · 1 testCheck fails when captcha session data is absent -
captcha.check.expired_session· Covered · 1 testMissing cache entry clears the session -
captcha.check.wrong· Covered · Core · 2 testsA wrong answer consumes the default single attempt -
captcha.check.consume· DivergedSuccess pulls the challenge from cache -
captcha.check.latest· DivergedOnly the newest image counts across the session -
captcha.check.form_after_api· DivergedIssuing via the API overwrites the form challenge -
captcha.check.profile· DivergedA challenge can pass a rule for another profile -
captcha.check.whitespace· DivergedSurrounding spaces cause a wrong answer -
captcha.check.api· Covered · Core · 1 testKeyed challenge verification, including cache pull and consumption -
captcha.draw.canvas· Covered · 3 testsCanvas, resized background, and drawing on the background image -
captcha.draw.glyphs· Covered · 2 testsRandom font, size, colour and angle per glyph -
captcha.draw.spacing· DivergedText spacing divides by length -
captcha.draw.lines· Covered · 2 testsDraw lines + 1 lines, including off-canvas starts -
captcha.draw.order· Covered · 1 testFilter and draw order -
captcha.draw.jpeg· Covered · 2 testsJPEG rendering and API data URI -
captcha.helper.image· Covered · Core · 1 testcaptcha(config) and Captcha::create(config) -
captcha.helper.issue· Covered · 1 testCaptcha::create(config, true) returns the API array -
captcha.facade.profile_state· DivergedCaptcha::configure can leak previous profile settings -
captcha.facade.renderer· DivergedPrebound ImageManager -
captcha.helper.src· Covered · Core · 1 testcaptcha_src and Captcha::src -
captcha.helper.img· Divergedcaptcha_img and Captcha::img return unescaped attributes -
captcha.helper.check· Covered · Core · 1 testcaptcha_check and Captcha::check -
captcha.helper.api_check· Covered · Core · 1 testcaptcha_api_check helper and Captcha::check_api facade method -
captcha.facade.alias· Covered · 1 testCaptcha facade alias -
captcha.rule.captcha· Covered · Core · 1 testcaptcha validation rule -
captcha.rule.implicit· DivergedBlank or absent answers pass the captcha rule without required -
captcha.rule.captcha_api· Covered · 1 testcaptcha_api:{key},{config} validation rule -
captcha.rule.captcha_api_default· Covered · 1 testcaptcha_api:{key} uses the default profile -
captcha.rule.disabled· Covered · 1 testRules pass when disable is enabled -
captcha.rule.zero· Divergedcaptcha rejects the string 0 while captcha_check accepts it -
captcha.rule.message· DivergedAbsent translations expose validation.captcha -
captcha.provider.discovery· DivergedLaravel automatic package discovery -
captcha.assets.bundled· Covered · 1 testBundled fonts and backgrounds
Differences from PHP (33)
Global characters string- Split the alphabet into Unicode characters so multibyte alphabets work.
captcha.driver (GD or Imagick)- Accept and ignore the PHP driver key because the port uses one in-process renderer.
Class defaults for an unknown profile- Return HTTP 404 to expose misspelled profiles and avoid probing arbitrary settings.
length = 0 makes the route fail- Reject out-of-range lengths at startup so configuration failures surface before requests.
quality is ignored (JPEG quality is always 75)- Honor the documented quality setting.
encrypt- Accept and ignore encrypt because no verifier leaves the server.
vendor:publish configuration- Laravel for Rust has no published config files. CaptchaConfig::php_defaults() and CaptchaConfig::from_php_json replace config publishing, and the guide will show the full default config.
PHP cache-store selection- Require a single authoritative store and reject failover to preserve single-use checks. SQLite requires a separate file.
Blur up to 100 and no comparable profile limits- Reject out-of-range settings at startup to bound CPU and memory per image.
GET captcha/api/{config?} returns a bcrypt-based key- Use an opaque challenge identifier to prevent offline guessing from the response.
web middleware on both routes- Keep the keyed route outside session middleware so keyed clients need no session.
Cache-Control: no-cache, private- Send no-store so browsers and intermediaries do not retain challenge images.
Lumen routes- The Rust framework has no Lumen integration.
ob_clean before sending the image- Rust image responses have no PHP output buffer to clear.
Issuance without throttling- Limit issuance per client to bound rendering work.
HEAD on the image route issues a challenge- Do not issue for HEAD, so a probe leaves the challenge visible to the user valid.
rand selects answers- Use unbiased OS randomness for challenges.
bcrypt with optional encryption- Use a server-side HMAC verifier to limit CPU per image and keep the verifier private.
Session holds captcha sensitive, key and encrypt- Use the store as the authority for each session and profile, with only a challenge marker in the session.
The upstream fork ignores session expiry- Allow no verification deadline while bounding storage with retention. The same policy applies to both modes.
Success pulls the challenge from cache- Atomically count comparisons and allow only the caller that removes the live record to pass.
Only the newest image counts across the session- Use one current challenge per session and profile so forms for different profiles are independent.
Issuing via the API overwrites the form challenge- Keep keyed issuance independent so it cannot replace a form challenge.
A challenge can pass a rule for another profile- Bind the challenge to its profile so a weaker profile cannot satisfy a stricter form.
Surrounding spaces cause a wrong answer- Trim surrounding ASCII whitespace, matching normal Laravel form input handling.
Text spacing divides by length- Divide by the drawn text length when it differs so math expressions fit the canvas.
Captcha::configure can leak previous profile settings- Resolve each profile fresh to prevent settings leaking between calls.
Prebound ImageManager- Use a Renderer trait so callers can supply a renderer without depending on PHP drivers.
captcha_img and Captcha::img return unescaped attributes- Escape attribute values and restrict attribute names to prevent HTML injection.
Blank or absent answers pass the captcha rule without required- The rule is implicit, so a blank or missing answer fails even without required.
captcha rejects the string 0 while captcha_check accepts it- Treat 0 as a valid answer in both paths and consume its challenge on success.
Absent translations expose validation.captcha- Provide The :attribute is incorrect. by default, with a per-rule override.
Laravel automatic package discovery- Register the provider explicitly because Laravel for Rust has no auto-discovery.
In practice
A protected form
Register the provider, show the image, and validate the answer with the Captcha rule.
use laravel_rust_captcha::{Captcha, CaptchaServiceProvider};
Application::configure()
.with_providers(|providers| {
providers.add(CaptchaServiceProvider::new());
})
.with_middleware(|middleware, config| {
CaptchaServiceProvider::configure_input(middleware, config, &[]);
});
#[laravel::input]
#[validate(anyone)]
pub struct StoreTopic {
#[validate(required, Captcha::session())]
captcha: String,
}In a MiniJinja view
The image URL changes on every render, so a refreshed form gets a new challenge.
<img src="{{ captcha_src() }}" alt="captcha">
<input type="text" name="captcha" autocomplete="off">Built on
- mews/captcha: The captcha package this ports: profiles, config keys, routes and validation, following version 3.5.1. Its backgrounds and fonts ship with their own licences.
Crates: laravel-rust-captcha
· Upstream notices
Documentation
- OverviewA self-hosted image captcha for Laravel for Rust, based on the PHP package mews/captcha. Nothing is sent to Google or Cloudflare.
- Getting startedInstall the crate, register its provider and protect your first form with an image captcha.
- ConfigurationThe captcha reads the CaptchaConfig section. Its keys follow PHP's config/captcha.php.
- Forms and viewscaptcha_img escapes attribute values, keeps only safe attribute names (no on handlers, style or srcset), reserves src, and adds alt="captcha" when you don't…
- Keyed challengesIn session mode the server remembers the visitor's current image. In keyed mode the client carries the challenge's id instead, in a hidden field. Use it for…
- Validationeasier profile can't pass a stricter form. In PHP any challenge passed any form.
- TestingEach test app has its own captcha service and store, so these helpers are safe in parallel tests.
- DeploymentSet the exceptions for both input normalizers in the application's final with_middleware callback. Include the fields other providers need alongside the…
- Migrating from PHPIn 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…
- Security designThis page explains how challenges are kept safe. The design is shared with the Go port of the same package.
- ChangelogWhat changed in each release of laravel-rust-captcha: additions, changes and fixes.