Laravel for Go Alpha

laravel-go-captcha

A self-hosted image captcha for Laravel for Go, ported from mews/captcha

Draws the captcha on your own server and checks the answer there, so nothing goes to a third party. It keeps mews/captcha's profiles, routes and rules, and is tested against fixtures the PHP package generates. Each challenge passes at most once.

Read the docs Source on GitHub

go get github.com/bowerbird-si/laravel-go-captcha
Latest release
0.1.0, 2026-12-04
Maintenance
Active
Framework
go.laravel.com v0.2.0 or later
Go
Go 1.27 or later
Licence
MIT

PHP parity

Follows mews/captcha 3.5.1 (commit 7154a930e88b). Last verified .

Parity level
Core
Abilities
87
Covered
52
Diverged
35
Planned
0
PHP fixtures
44
Coverage by ability
  • captcha.config.disable · Covered · Core · 2 tests
    disable and CAPTCHA_DISABLE
  • captcha.config.characters.array · Covered · 3 tests
    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 · 1 test
    Class defaults for missing profile keys
  • captcha.config.profile_unknown · Diverged
    Class defaults for an unknown profile
  • captcha.config.length · Covered · 2 tests
    length
  • captcha.config.length_zero · Diverged
    length = 0 makes the route fail
  • captcha.config.width · Covered · 3 tests
    width
  • captcha.config.height · Covered · 3 tests
    height
  • captcha.config.quality · Diverged
    quality is ignored (JPEG quality is always 75)
  • captcha.config.math · Covered · 2 tests
    math
  • captcha.config.expire · Covered · 2 tests
    expire in seconds
  • captcha.config.encrypt · Diverged
    encrypt
  • captcha.config.sensitive · Covered · 2 tests
    sensitive
  • captcha.config.lines · Covered · 2 tests
    lines
  • captcha.config.line_width · Covered · 2 tests
    lineWidth
  • captcha.config.line_color · Covered · 2 tests
    lineColor
  • captcha.config.angle · Covered · 2 tests
    angle
  • captcha.config.bg_image · Covered · 2 tests
    bgImage
  • captcha.config.bg_color · Covered · 2 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 · 1 test
    invert
  • captcha.config.font_colors · Covered · 2 tests
    fontColors
  • captcha.config.text_left_padding · Covered · 2 tests
    textLeftPadding
  • captcha.config.margin_top · Covered · 2 tests
    marginTop
  • captcha.config.profile_characters · Covered · 2 tests
    Profile characters
  • captcha.config.profile_fonts · Covered · 2 tests
    Profile fonts
  • captcha.config.profile_backgrounds · Covered · 1 test
    Profile backgrounds
  • captcha.config.profile_directories · Diverged
    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 · 2 tests
    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 · 4 tests
    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 · 1 test
    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 · 3 tests
    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 · 1 test
    Draw lines + 1 lines, including off-canvas starts
  • captcha.draw.order · Covered · 2 tests
    Filter and draw order
  • captcha.draw.jpeg · Covered · 2 tests
    JPEG rendering and API data URI
  • captcha.helper.image · Covered · Core · 3 tests
    captcha(config) and Captcha::create(config)
  • captcha.helper.issue · Covered · 2 tests
    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 · 3 tests
    captcha_src and Captcha::src
  • captcha.helper.img · Diverged
    captcha_img and Captcha::img return unescaped attributes
  • captcha.helper.check · Covered · Core · 2 tests
    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 · Diverged
    Captcha facade alias
  • captcha.rule.captcha · Covered · Core · 2 tests
    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 · 3 tests
    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 (35)
Global characters string
A string alphabet is split into Unicode characters, so multibyte alphabets work; PHP split it into bytes.
captcha.driver (GD or Imagick)
Use an in-process Go renderer in place of PHP's GD or Imagick driver selection.
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
The port issues opaque identifiers and keeps verifiers on the server, so there is no verifier encryption setting.
Profile fontsDirectory and bgsDirectory have no effect
A profile's fontsDirectory or bgsDirectory is rejected at startup with a hint to use a fonts or backgrounds list, because PHP silently ignored it.
vendor:publish configuration
Configure the package through Go options and the environment in place of publishing a PHP configuration file.
PHP cache-store selection
Require a single authoritative store and reject failover to preserve single-use checks. A production SQLite cache needs a separate connection and 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
Laravel for Go registers routes with its own router and has no Lumen integration.
ob_clean before sending the image
Go writes the JPEG through a web response and has 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
A NoDeadline challenge has no verification deadline, and is removed after Config.NoDeadlineLifetime (24 hours by default); the same policy applies to session and keyed challenges.
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
A newer image replaces the open challenge of the same session and profile, so the older image's answer fails; PHP kept one challenge per session for every profile.
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
When the drawn text is longer than Length, as in math profiles, the step divides by the drawn length so every character starts inside the canvas; PHP divided by Length.
Captcha::configure can leak previous profile settings
Each issue reads the profile's configured settings, so nothing set while issuing one profile carries into the next; PHP's facade kept the previous profile's keys.
Prebound ImageManager
Use a Renderer interface so callers can supply a renderer without depending on PHP drivers.
captcha_img and Captcha::img return unescaped attributes
Attribute values are escaped, event-handler and malformed attribute names are dropped and src is reserved, because PHP's unescaped attributes allowed script injection.
Captcha facade alias
Resolve the application-scoped service through captcha.From and package functions in place of PHP's facade alias.
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 package explicitly in laravel.Options.Packages because Laravel for Go has no PHP package auto-discovery.

In practice

A session form

Register the package, show the image, and check the answer with the rule.

// bootstrap/app.go
Packages: []laravel.Package{captcha.New()},

// The form: <img src="{{captchaSrc "default"}}"> and <input name="captcha">
func (f *ContactForm) Rules(r *web.Request) (validation.Rules, error) {
	return validation.For(f, rule.Field(&f.Captcha, rule.Required, captcha.Rule(r))), nil
}

A keyed captcha for an API

The client fetches GET /captcha/api/default, shows img, and sends the answer back with key.

type KeyedForm struct {
	Captcha    string `json:"captcha"`
	CaptchaKey string `json:"captcha_key"`
}

func (f *KeyedForm) Rules() validation.Rules {
	return validation.For(f, rule.Field(&f.Captcha, rule.Required, captcha.Keyed()))
}

Built on

  • mews/captcha: The captcha package this ports: profiles, config keys, routes and validation, following version 3.5.1. Its bundled fonts ship with their own licences.

Go modules: github.com/bowerbird-si/laravel-go-captcha · Upstream notices

Documentation