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
Session and keyed captchas
A session captcha for server-rendered forms, and a keyed one for APIs and single-page apps that never touches the session.
mews/captcha's profiles and drawing
The five shipped profiles, custom ones and math captchas, drawn with the bundled fonts and backgrounds; GD's filters and resize are matched.
One pass per challenge
A check consumes the challenge atomically on Redis or the database cache: of many simultaneous correct answers, exactly one passes.
Nothing secret leaves the server
The answer is never stored or sent. The store keeps a keyed verifier, and the API key is an opaque random id.
Limits built in
Wrong answers are limited per client and profile, and image requests per client, on the same store as the challenges.
Moving from PHP
ParsePHPConfigreads an exportedconfig('captcha'), and the guide maps every mews/captcha call to its Go equivalent.
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 testsdisable and CAPTCHA_DISABLE -
captcha.config.characters.array· Covered · 3 testsGlobal 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 · 1 testClass defaults for missing profile keys -
captcha.config.profile_unknown· DivergedClass defaults for an unknown profile -
captcha.config.length· Covered · 2 testslength -
captcha.config.length_zero· Divergedlength = 0 makes the route fail -
captcha.config.width· Covered · 3 testswidth -
captcha.config.height· Covered · 3 testsheight -
captcha.config.quality· Divergedquality is ignored (JPEG quality is always 75) -
captcha.config.math· Covered · 2 testsmath -
captcha.config.expire· Covered · 2 testsexpire in seconds -
captcha.config.encrypt· Divergedencrypt -
captcha.config.sensitive· Covered · 2 testssensitive -
captcha.config.lines· Covered · 2 testslines -
captcha.config.line_width· Covered · 2 testslineWidth -
captcha.config.line_color· Covered · 2 testslineColor -
captcha.config.angle· Covered · 2 testsangle -
captcha.config.bg_image· Covered · 2 testsbgImage -
captcha.config.bg_color· Covered · 2 testsbgColor -
captcha.draw.contrast· Covered · 2 testscontrast -
captcha.draw.sharpen· Covered · 2 testssharpen -
captcha.draw.blur· Covered · 2 testsblur -
captcha.draw.invert· Covered · 1 testinvert -
captcha.config.font_colors· Covered · 2 testsfontColors -
captcha.config.text_left_padding· Covered · 2 teststextLeftPadding -
captcha.config.margin_top· Covered · 2 testsmarginTop -
captcha.config.profile_characters· Covered · 2 testsProfile characters -
captcha.config.profile_fonts· Covered · 2 testsProfile fonts -
captcha.config.profile_backgrounds· Covered · 1 testProfile backgrounds -
captcha.config.profile_directories· DivergedProfile 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 · 2 testsLowercase unless sensitive -
captcha.generate.verifier· Divergedbcrypt with optional encryption -
captcha.generate.session· DivergedSession holds captcha sensitive, key and encrypt -
captcha.generate.ttl· Covered · 4 testsCache 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 · 1 testA 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 · 3 testsKeyed 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 · 1 testDraw lines + 1 lines, including off-canvas starts -
captcha.draw.order· Covered · 2 testsFilter and draw order -
captcha.draw.jpeg· Covered · 2 testsJPEG rendering and API data URI -
captcha.helper.image· Covered · Core · 3 testscaptcha(config) and Captcha::create(config) -
captcha.helper.issue· Covered · 2 testsCaptcha::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 · 3 testscaptcha_src and Captcha::src -
captcha.helper.img· Divergedcaptcha_img and Captcha::img return unescaped attributes -
captcha.helper.check· Covered · Core · 2 testscaptcha_check and Captcha::check -
captcha.helper.api_check· Covered · Core · 1 testcaptcha_api_check helper and Captcha::check_api facade method -
captcha.facade.alias· DivergedCaptcha facade alias -
captcha.rule.captcha· Covered · Core · 2 testscaptcha 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 · 3 testsRules 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 (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
- OverviewA self-hosted image captcha for Laravel for Go, based on mews/captcha. It draws the image on your own server and checks the answer there, so nothing is sent…
- Getting startedInstall the package, protect a first form with a session captcha, and add a keyed captcha for an API.
- ConfigurationLaravel 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…
- TestingThe captchatest package helps your application's tests get past a captcha without reading the image.
- Migrating from PHPEach mews/captcha call and its Go equivalent, followed by every place where the Go port behaves differently.
- ChangelogWhat changed in each release of laravel-go-captcha: additions, changes and fixes.