Skip to content

Webhooks 4.0 for Craft 6 (native) - #108

Merged
riasvdv merged 37 commits into
4.xfrom
feature/craft-6-native
Nov 13, 2026
Merged

riasvdv merged 37 commits into
4.xfrom
feature/craft-6-native

Conversation

@riasvdv

@riasvdv riasvdv commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Description

Webhooks 4.0: a native Craft 6 plugin. It no longer needs craftcms/yii2-adapter or Yii.

craft\webhooks\…                   →  CraftCms\Webhooks\…
Yii class + event name             →  Laravel event class + optional subject class
WebhookManager / records           →  Eloquent models (Webhook, Group, WebhookRequest)
Plugin::request()/sendRequest()    →  Webhooks service (container singleton)
SendRequestJob (Yii queue)         →  Jobs\SendRequest (re-dispatches up to maxAttempts)
Guzzle client + url-validator      →  Laravel Http + url-validator middleware (pins IPs per hop)
EVENT_REGISTER_FILTER_TYPES        →  Events\FilterTypesResolving
controllers, Twig templates, JS    →  routes/cp.php, form request, Craft UI screens
ECS / PHPStan / Prettier           →  Pint / Larastan / Pest (23 tests)
config/webhooks.php                →  config/craft/webhooks.php (vendor:publish stub)

On upgrade, a migration converts Craft 5 webhooks: element afterSave/afterDelete/… become ElementLifecycle* events with the element as subject class, Elements and Users service events become their Laravel event classes, and filter class names are renamed. Webhooks whose event has no Craft 6 equivalent are disabled and logged. See UPGRADE.md.

This also includes two open 3.x pull requests:

Craft 6 always renders templates in the Twig sandbox, and the sandbox doesn’t allow json_encode, atom or className(). The README’s payload example now writes the JSON out, escaping strings with |e('js'), and the docs warn against allowing json_encode: it can output properties of objects the sandbox otherwise hides, such as a relation field’s source element. Saving a webhook reports sandbox violations instead of failing later in the queue, and a template that still fails is logged without stopping the action that triggered it.

Spatie: laravel-webhook-server 4.x signs and retries one-off calls, but Webhooks needs a stored request log for the Activity page, debounce, redeliver, env vars parsed at send time, Twig URLs and headers, and SSRF validation on every redirect hop. It doesn’t sign requests either. Adding it would mean wrapping or bypassing most of it, so this uses Laravel’s HTTP client and queue directly. laravel-webhook-client receives webhooks, so it doesn’t apply.

The 3.x release that runs on Craft 6 through the adapter is #102.

Edit webhook

Evidence

Run against craftcms/cms 6.x 31ad9f1, 2e1929f, 5c6ff68 and fd1763f, with a local receiver. The lock now pins c48add6, which keeps archived table names within the database’s length limit; the tests, Pint and Larastan pass on it. 5c6ff68 lets the sandbox convert dates to strings, so now|date('c') works in payload templates, and defaults PluginTestCase’s user model to Craft’s. No external webhook services were called.

  • composer test: 23 passed on SQLite and on MySQL 8.4. Each check was confirmed by breaking it: no retry, no SSRF middleware, no debounce, unsandboxed validation. Pint and Larastan pass.
  • Fresh install with no adapter: entry create/update/delete and user saves trigger the configured webhooks. Draft, revision and new-element filters work. Debounce merges create and update into one request. Env var URLs and headers are parsed when sent and stored unparsed. A 500 is retried 3 times. A redirect to 169.254.169.254 is blocked. Garbage collection purges old requests. Redeliver, the index status toggle, groups and settings (saved to project config) work in the browser. A user with accessPlugin-webhooks gets 200 and one with only accessCp gets 403.
  • Upgrade from 3.x on Craft 6 + adapter: after composer update the adapter and Yii are removed, and craft:up runs the conversion migration. The four existing webhooks were converted and keep delivering, an unmappable one was disabled with a warning, and request history was kept.
  • Adds migration to increase length of url column #95 and Archive tables in install migration #98 on PostgreSQL, in a copy of the host database: the URL migration changed both url columns from varchar(255) to text, and running the install migration again moved the 7 webhooks and 27 requests to _old tables. Their indexes, sequences and foreign keys were renamed or pointed at the other _old tables, and the new tables accept rows. The core helper’s own test passes on SQLite, MySQL and PostgreSQL, with and without a table prefix.

Not yet done: delivery to a real external endpoint (Zapier, Netlify).

libordan and others added 10 commits September 1, 2024 11:45
- Pass sandboxed Twig templates the event’s properties as an array on Craft 6, whose sandbox no longer allows yii\base\Event objects
- Push request jobs right away from console requests on Craft 6, which only triggers EVENT_AFTER_REQUEST for web requests
- Convert Craft 6 components (Yiisoft\Arrays\ArrayableInterface) with toArray() so payload dates and extra attributes work
- Suggest events for Craft 6 element classes, which aren’t yii\base\Component subclasses
- Initialize CP scripts with Craft.whenReady() on bridged screens
- Support <craft-switch> lightswitches on the Webhooks index page
- Keep .hidden working for the filter pane and spinner
- Update craftcms/url-validator in composer.lock to satisfy ^1.2
- Document the json_encode sandbox requirement for payload templates

Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Co-authored-by: Amp <amp@ampcode.com>
Structural only: file contents change in the next commit.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Co-authored-by: Amp <amp@ampcode.com>
Webhooks listen for Laravel event classes, with an optional subject class,
and send requests with Laravel's HTTP client from queue jobs. URLs and
redirects are still validated against SSRF and pinned to the validated IPs.

The control panel pages are built with Craft's UI components, and a
migration converts Craft 5 element and service events.

The Yii-era JS tooling, ECS and DDEV config are removed; none of them run
on Craft 6.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Co-authored-by: Amp <amp@ampcode.com>
@riasvdv
riasvdv requested a review from brandonkelly as a code owner November 8, 2026 14:46
riasvdv and others added 2 commits November 8, 2026 14:55
The shared PHPStan job reports Http::create() as undefined, because
Larastan doesn't see the macro Craft registers when it boots.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Co-authored-by: Amp <amp@ampcode.com>
craftcms/cms 5c6ff68 defaults PluginTestCase's user model to Craft's, and
lets the Twig sandbox convert dates to strings, so now|date('c') works in
payload templates again.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Co-authored-by: Amp <amp@ampcode.com>
riasvdv and others added 7 commits November 8, 2026 15:56
Ports two open 3.x pull requests:

- #95: the url columns on webhooks and webhookrequests are now
  mediumtext, with a migration for existing installs, and saving a
  webhook no longer limits its URL to 255 characters.
- #98: the install migration archives tables left over from a previous
  install with Craft's Migration::archiveTableIfExists(), instead of
  dropping them.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
New installs now create the url columns as mediumtext too, and the
schema version bump makes Craft run the url length migration on update.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Picks up archiveTableIfExists() keeping archived names within the
database's identifier length limit.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Allowing json_encode lets sandboxed templates output public properties
of objects the sandbox otherwise hides, such as element queries and
their source elements, including a user's password hash right after it
changes. The README's payload example now writes the JSON out, escaping
string values with |e('js').

Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Co-authored-by: Amp <amp@ampcode.com>

[ci skip]

Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Co-authored-by: Amp <amp@ampcode.com>
A webhook template that failed to render threw from the event handler,
so it stopped the action that triggered it, such as saving an entry. On
Craft 6, payload templates that use json_encode always fail, because the
Twig sandbox doesn't allow it. Errors are now logged and the webhook is
skipped, as in Webhooks 4.

Allowing json_encode in the sandbox lets templates output public
properties of objects the sandbox otherwise hides, so the README no
longer suggests it. Its payload example writes the JSON out instead.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
riasvdv and others added 8 commits November 9, 2026 15:49
Craft 4 and 5 requirements are unchanged.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Craft 6 renders autosuggest inputs as comboboxes that ignore the
template's Vue methods, so Sender Class and Event Name listed nothing.
The controller now passes the suggestions in, and the Event Name list is
reloaded when the sender class changes.

The filter buttons used a "checkmark" icon that doesn't exist on any
version ("check" does), and Craft 6 doesn't style the ignore button's
status dot or align the buttons. Filter labels were centered by Craft 6's
table header styles.

Craft 6 slideouts have no padding and no $container property, so request
details were cut off and the Redeliver and Send now buttons were never
bound.

Craft 6-only styles are scoped to .cp-legacy-screen, so Craft 4 and 5
are unchanged.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Co-authored-by: Amp <amp@ampcode.com>
Brings in the 3.5.0 and 3.5.1 release notes. Their code changes were for the Yii 2 adapter, and the native port already covers them, so the branch's files are kept.

[ci skip]

Amp-Thread-ID: https://ampcode.com/threads/T-01a1c610-2b0e-771b-aa95-71df6e96ced3
Co-authored-by: Amp <amp@ampcode.com>
The URL validator is now created the first time a URL is validated. Before, the plugin created it while booting, so a typo such as `foo*` made every request and Artisan command fail. Now sending logs the error and marks the request as failed, and saving a webhook shows it as a URL error.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1c610-2b0e-771b-aa95-71df6e96ced3
Co-authored-by: Amp <amp@ampcode.com>
i-just and others added 9 commits November 13, 2026 08:34
Deleting a pending request made its queued job fail. Bulk delete now keeps
requests that haven't been sent yet, and a job whose request was deleted
exits without retrying.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A `handler` request option replaced Laravel's handler stack, so the URL
validation middleware never ran. The custom handler now goes at the bottom
of the stack instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Filters set to “No” were disabling other filters too, and a disabled filter
could be left set to “Yes” or “No”. As in 3.x, only a “Yes” filter disables
the filters it excludes, and those are reset to “Any”.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Retake the Edit Webhook, Event Filters and Custom Headers screenshots on
Craft CMS 6.0.0-beta.1, with event filters ordered No, Any, Yes. The
README now says only a filter set to “Yes” disables the ones it
conflicts with, as 0c82a47 changed.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Amp-Thread-ID: https://ampcode.com/threads/T-01a1bb52-1bc2-7562-8c0c-c85c057b73a4
Co-authored-by: Amp <amp@ampcode.com>
@riasvdv
riasvdv merged commit 65f7fe0 into 4.x Nov 13, 2026
7 checks passed
@riasvdv
riasvdv deleted the feature/craft-6-native branch November 13, 2026 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants