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 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 tests
    disable and CAPTCHA_DISABLE
  • captcha.config.characters.array · Covered · 1 test
    Global characters array
  • captcha.config.characters.string · Diverged
    Global characters string
  • captcha.config.fonts_directory · Covered · 1 test
    fontsDirectory
  • captcha.config.bgs_directory · Covered · 1 test
    bgsDirectory
  • captcha.config.driver · Diverged
    captcha.driver (GD or Imagick)
  • captcha.config.profiles · Covered · 1 test
    Five shipped named profiles
  • captcha.config.profile_replace · Covered · 1 test
    Application profiles replace shipped profiles in full
  • captcha.config.profile_defaults · Covered · 2 tests
    Class defaults for missing profile keys
  • captcha.config.profile_unknown · Diverged
    Class defaults for an unknown profile
  • captcha.config.length · Covered · 1 test
    length
  • captcha.config.length_zero · Diverged
    length = 0 makes the route fail
  • captcha.config.width · Covered · 1 test
    width
  • captcha.config.height · Covered · 1 test
    height
  • captcha.config.quality · Diverged
    quality is ignored (JPEG quality is always 75)
  • captcha.config.math · Covered · 1 test
    math
  • captcha.config.expire · Covered · 1 test
    expire in seconds
  • captcha.config.encrypt · Diverged
    encrypt
  • captcha.config.sensitive · Covered · 1 test
    sensitive
  • captcha.config.lines · Covered · 1 test
    lines
  • captcha.config.line_width · Covered · 2 tests
    lineWidth
  • captcha.config.line_color · Covered · 3 tests
    lineColor
  • captcha.config.angle · Covered · 2 tests
    angle
  • captcha.config.bg_image · Covered · 2 tests
    bgImage
  • captcha.config.bg_color · Covered · 3 tests
    bgColor
  • captcha.draw.contrast · Covered · 2 tests
    contrast
  • captcha.draw.sharpen · Covered · 2 tests
    sharpen
  • captcha.draw.blur · Covered · 2 tests
    blur
  • captcha.draw.invert · Covered · 2 tests
    invert
  • captcha.config.font_colors · Covered · 2 tests
    fontColors
  • captcha.config.text_left_padding · Covered · 2 tests
    textLeftPadding
  • captcha.config.margin_top · Covered · 1 test
    marginTop
  • captcha.config.profile_characters · Covered · 1 test
    Profile characters
  • captcha.config.profile_fonts · Covered · 2 tests
    Profile fonts
  • captcha.config.profile_backgrounds · Covered · 1 test
    Profile backgrounds
  • captcha.config.profile_directories · Covered · 1 test
    Profile fontsDirectory and bgsDirectory have no effect
  • captcha.config.publish · Diverged
    vendor:publish configuration
  • captcha.config.store · Diverged
    PHP cache-store selection
  • captcha.config.limits · Diverged
    Blur up to 100 and no comparable profile limits
  • captcha.route.image · Covered · Core · 1 test
    GET captcha/{config?}
  • captcha.route.api · Diverged
    GET captcha/api/{config?} returns a bcrypt-based key
  • captcha.route.web · Diverged
    web middleware on both routes
  • captcha.route.cache_control · Diverged
    Cache-Control: no-cache, private
  • captcha.route.disabled · Covered · 1 test
    Routes absent when disable is enabled
  • captcha.route.lumen · Diverged
    Lumen routes
  • captcha.route.output_buffer · Diverged
    ob_clean before sending the image
  • captcha.route.throttle · Diverged
    Issuance without throttling
  • captcha.route.head · Diverged
    HEAD on the image route issues a challenge
  • captcha.generate.random · Diverged
    rand selects answers
  • captcha.generate.lowercase · Covered · 1 test
    Lowercase unless sensitive
  • captcha.generate.verifier · Diverged
    bcrypt with optional encryption
  • captcha.generate.session · Diverged
    Session holds captcha sensitive, key and encrypt
  • captcha.generate.ttl · Covered · 1 test
    Cache entry expires after expire seconds
  • captcha.generate.no_deadline · Diverged
    The upstream fork ignores session expiry
  • captcha.check.no_session · Covered · 1 test
    Check fails when captcha session data is absent
  • captcha.check.expired_session · Covered · 1 test
    Missing cache entry clears the session
  • captcha.check.wrong · Covered · Core · 2 tests
    A wrong answer consumes the default single attempt
  • captcha.check.consume · Diverged
    Success pulls the challenge from cache
  • captcha.check.latest · Diverged
    Only the newest image counts across the session
  • captcha.check.form_after_api · Diverged
    Issuing via the API overwrites the form challenge
  • captcha.check.profile · Diverged
    A challenge can pass a rule for another profile
  • captcha.check.whitespace · Diverged
    Surrounding spaces cause a wrong answer
  • captcha.check.api · Covered · Core · 1 test
    Keyed challenge verification, including cache pull and consumption
  • captcha.draw.canvas · Covered · 3 tests
    Canvas, resized background, and drawing on the background image
  • captcha.draw.glyphs · Covered · 2 tests
    Random font, size, colour and angle per glyph
  • captcha.draw.spacing · Diverged
    Text spacing divides by length
  • captcha.draw.lines · Covered · 2 tests
    Draw lines + 1 lines, including off-canvas starts
  • captcha.draw.order · Covered · 1 test
    Filter and draw order
  • captcha.draw.jpeg · Covered · 2 tests
    JPEG rendering and API data URI
  • captcha.helper.image · Covered · Core · 1 test
    captcha(config) and Captcha::create(config)
  • captcha.helper.issue · Covered · 1 test
    Captcha::create(config, true) returns the API array
  • captcha.facade.profile_state · Diverged
    Captcha::configure can leak previous profile settings
  • captcha.facade.renderer · Diverged
    Prebound ImageManager
  • captcha.helper.src · Covered · Core · 1 test
    captcha_src and Captcha::src
  • captcha.helper.img · Diverged
    captcha_img and Captcha::img return unescaped attributes
  • captcha.helper.check · Covered · Core · 1 test
    captcha_check and Captcha::check
  • captcha.helper.api_check · Covered · Core · 1 test
    captcha_api_check helper and Captcha::check_api facade method
  • captcha.facade.alias · Covered · 1 test
    Captcha facade alias
  • captcha.rule.captcha · Covered · Core · 1 test
    captcha validation rule
  • captcha.rule.implicit · Diverged
    Blank or absent answers pass the captcha rule without required
  • captcha.rule.captcha_api · Covered · 1 test
    captcha_api:{key},{config} validation rule
  • captcha.rule.captcha_api_default · Covered · 1 test
    captcha_api:{key} uses the default profile
  • captcha.rule.disabled · Covered · 1 test
    Rules pass when disable is enabled
  • captcha.rule.zero · Diverged
    captcha rejects the string 0 while captcha_check accepts it
  • captcha.rule.message · Diverged
    Absent translations expose validation.captcha
  • captcha.provider.discovery · Diverged
    Laravel automatic package discovery
  • captcha.assets.bundled · Covered · 1 test
    Bundled 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