Search by

silarhi / cap-bundle

guillaume-sainthillier

Cap CAPTCHA (trycap.dev) for Symfony forms: a self-hosted, proof-of-work reCAPTCHA alternative with a form type, a validator constraint and a Turbo-ready widget

Package info

github.com/silarhi/cap-bundle

Homepage

Documentation

Type:symfony-bundle

pkg:composer/silarhi/cap-bundle

Statistics

Installs: 2 659

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 3

v0.6.1 2026-11-27 11:13 UTC

This package is auto-updated.

Last update: 2026-11-27 16:12:33 UTC


README

License PHP Version CI Status Coverage JavaScript coverage

Cap Bundle

Cap CAPTCHA (trycap.dev), the self-hosted proof-of-work CAPTCHA, in your Symfony forms.
A form type, a validator constraint and a widget that keeps working across Turbo visits — the KarserRecaptcha3Bundle way, without Google.

📖 Documentation

Features

  • One form field: CapType renders the widget and validates the token on submit, with the Cap constraint
  • Checkbox, floating or invisible: Cap's widget, the widget next to the submit button once the form is submitted, or nothing at all: the challenge is solved while the visitor fills the form
  • Several site keys: a light challenge for the newsletter, a harder one for signing up
  • SPA ready: a framework-agnostic core for any SPA, the back-forward cache and forms you submit with fetch(), plus opt-in adapters for Turbo Drive and Live Components
  • Stimulus or not: a controller StimulusBundle or Symfony Reprise registers for you, or a script the form renders, with no build step
  • Self-hosted end to end: your Cap server, and the widget script and its WebAssembly solver from your domain if you like
  • Fails closed: a Cap server out of reach rejects the submission, and the error stays on the field
  • Any payload: the #[Cap] constraint and the CapVerifierInterface service protect JSON endpoints too
  • Testable: test tokens that skip the Cap server, and an opt-in profiler panel

Requirements

  • PHP 8.2+
  • Symfony 6.4, 7.x or 8.x (symfony/form, symfony/validator): the bundle class is optional, see Without the bundle
  • A PSR-18 HTTP client and a PSR-17 implementation: Composer installs symfony/http-client and nyholm/psr7 when the application has none (php-http/discovery), or set your own client
  • A Cap Standalone server, and a site key created in its dashboard

Installation

composer require silarhi/cap-bundle
# .env
CAP_ENDPOINT=https://cap.example.com/<site-key>/
CAP_SECRET_KEY=<the secret key of the site key>
# config/packages/cap.yaml
when@test:
    cap:
        enabled: false

Without the bundle

The bundle class is optional: an application without the framework, symfony/dependency-injection or symfony/http-kernel builds the verifier, the validator and the form type by hand. Pass the widget paths the bundle sets by default, or the widget never loads:

use Silarhi\CapBundle\Form\CapType;
use Silarhi\CapBundle\Validator\CapValidator;
use Silarhi\CapBundle\Verifier\CapVerifier;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Forms;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Validation;

$httpClient = new Psr18Client(); // any PSR-18 client, with PSR-17 request and stream factories
$verifier = CapVerifier::fromEndpoint($endpoint, $secretKey, null, $httpClient, $httpClient, $httpClient, $logger);

$validator = Validation::createValidatorBuilder()
    ->setConstraintValidatorFactory(new ConstraintValidatorFactory([CapValidator::class => new CapValidator($verifier)]))
    ->setTranslator($translator) // the messages are keys of vendor/silarhi/cap-bundle/translations/validators.*.xlf
    ->setTranslationDomain('validators')
    ->getValidator();

$formFactory = Forms::createFormFactoryBuilder()
    ->addExtension(new ValidatorExtension($validator))
    ->addType(new CapType(
        $endpoint,
        widgetScript: CapType::WIDGET_SCRIPT_PATH,
        wasmUrl: CapType::WASM_PATH,
        hashwxUrl: CapType::HASHWX_PATH,
    ))
    ->getFormFactory();

Then add vendor/silarhi/cap-bundle/templates to the Twig loader as the SilarhiCap namespace, @SilarhiCap/form/cap_widget.html.twig to the form themes, and copy vendor/silarhi/cap-bundle/public/ to public/bundles/silarhicap/. The installation guide wires Twig and the other site keys.

Quick start

use Silarhi\CapBundle\Form\CapType;

$builder
    ->add('message', TextareaType::class)
    ->add('captcha', CapType::class);

{{ form(form) }} renders the widget and its script. On submit, the token is redeemed against your Cap server: a missing or rejected one is a form error on the field.

Documentation

Everything else is on cap-bundle.silarhi.dev:

Contributing

See CONTRIBUTING.md. Release notes are in CHANGELOG.md.

License

Released under the MIT License.