Files
coolify/app/Rules/ManualWebhookSecret.php
Andras Bacsai 0ed423aaac feat(webhooks): require 16 characters for new manual webhook secrets
New or changed manual webhook secrets (GitHub, GitLab, Bitbucket,
Gitea) must have at least 16 characters, in the Webhooks page and in
the application API. Stored secrets keep working: an unchanged value
passes, and an empty value still disables the webhook. Each secret
field gets a Generate button that fills a random 40-character secret;
the user saves it after adding it to the Git provider.
2026-10-01 20:31:00 +02:00

46 lines
1.2 KiB
PHP

<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Translation\PotentiallyTranslatedString;
/**
* Requires a minimum length for new or changed manual webhook secrets.
*
* An empty value disables the webhook and stays allowed. A value equal to the
* currently stored secret is accepted, so secrets saved before this rule existed
* keep working until they are changed.
*/
class ManualWebhookSecret implements ValidationRule
{
public const MIN_LENGTH = 16;
public function __construct(private ?string $currentValue = null) {}
/**
* @param Closure(string, ?string=): PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if ($value === null || $value === '') {
return;
}
if (! is_string($value)) {
$fail('The :attribute must be a string.');
return;
}
if ($this->currentValue !== null && hash_equals($this->currentValue, $value)) {
return;
}
if (mb_strlen($value) < self::MIN_LENGTH) {
$fail('The :attribute must be at least '.self::MIN_LENGTH.' characters.');
}
}
}