From 3c4d2b0d99393ea2b96dc45d809895fb3e8583e0 Mon Sep 17 00:00:00 2001 From: bernardhanna Date: Thu, 19 Nov 2026 12:34:47 +0000 Subject: [PATCH] Block cwprobe Gmail and probe titles from activity submissions. The reserved-TLD email guard was bypassed with cwprobe*@gmail.com; reject those local-parts and CW probe title/description markers, and widen events:purge-security-probes to match them. Co-authored-by: Cursor --- .../Commands/PurgeSecurityProbeEvents.php | 46 ++++++++---- app/Http/Requests/EventRequest.php | 5 +- app/Rules/NotReservedEmailDomain.php | 35 +++++---- app/Rules/NotSecurityProbeContent.php | 73 +++++++++++++++++++ tests/Feature/CreateEventTest.php | 14 ++++ .../Feature/PurgeSecurityProbeEventsTest.php | 11 +++ .../Unit/Rules/NotReservedEmailDomainTest.php | 3 + .../Rules/NotSecurityProbeContentTest.php | 60 +++++++++++++++ 8 files changed, 214 insertions(+), 33 deletions(-) create mode 100644 app/Rules/NotSecurityProbeContent.php create mode 100644 tests/Unit/Rules/NotSecurityProbeContentTest.php diff --git a/app/Console/Commands/PurgeSecurityProbeEvents.php b/app/Console/Commands/PurgeSecurityProbeEvents.php index f2033c46d..8c7b2650d 100644 --- a/app/Console/Commands/PurgeSecurityProbeEvents.php +++ b/app/Console/Commands/PurgeSecurityProbeEvents.php @@ -4,6 +4,7 @@ use App\Event; use App\Rules\NotReservedEmailDomain; +use App\Rules\NotSecurityProbeContent; use App\User; use Illuminate\Console\Command; use Illuminate\Database\Eloquent\Builder; @@ -11,8 +12,8 @@ /** * Soft-delete leftover security/probe test activities and their probe users. * - * Matches reserved test email TLDs (.invalid, .test, .example, .localhost) - * and known CW probe title/description markers from authorized testing. + * Matches reserved test email TLDs (.invalid, .test, .example, .localhost), + * cwprobe* organiser emails, and known CW probe title/description markers. * * Usage: * php artisan events:purge-security-probes @@ -23,9 +24,9 @@ class PurgeSecurityProbeEvents extends Command { protected $signature = 'events:purge-security-probes {--delete : Soft-delete matched events (default: dry-run)} - {--users : Also soft-delete users whose email uses a reserved test domain}'; + {--users : Also soft-delete users whose email uses a reserved test domain or cwprobe* local-part}'; - protected $description = 'Soft-delete security/probe test activities (and optionally probe users) that use reserved .invalid/.test email domains.'; + protected $description = 'Soft-delete security/probe test activities (and optionally probe users).'; public function handle(): int { @@ -72,11 +73,7 @@ public function handle(): int } if ($purgeUsers) { - $usersQuery = User::query()->where(function (Builder $query): void { - foreach (NotReservedEmailDomain::RESERVED_TLDS as $tld) { - $query->orWhere('email', 'like', '%.'.$tld); - } - }); + $usersQuery = $this->probeUsersQuery(); $userCount = (clone $usersQuery)->count(); $this->info("Matched probe users: {$userCount}"); @@ -99,13 +96,30 @@ private function probeEventsQuery(): Builder $query->orWhere('user_email', 'like', '%.'.$tld); } - $query->orWhere('title', 'like', 'CW Probe%') - ->orWhere('title', 'like', 'CW pending probe%') - ->orWhere('title', 'like', 'CW marker test%') - ->orWhere('title', 'like', 'CW Probe Create%') - ->orWhere('description', 'like', '%authorized security testing%') - ->orWhere('description', 'like', '%cwmark benign%') - ->orWhere('description', 'like', '%Benign baseline description for tester probe%'); + foreach (NotReservedEmailDomain::PROBE_LOCAL_PREFIXES as $prefix) { + $query->orWhere('user_email', 'like', $prefix.'%@%'); + } + + foreach (NotSecurityProbeContent::TITLE_PREFIXES as $prefix) { + $query->orWhere('title', 'like', $prefix.'%'); + } + + foreach (NotSecurityProbeContent::DESCRIPTION_NEEDLES as $needle) { + $query->orWhere('description', 'like', '%'.$needle.'%'); + } + }); + } + + private function probeUsersQuery(): Builder + { + return User::query()->where(function (Builder $query): void { + foreach (NotReservedEmailDomain::RESERVED_TLDS as $tld) { + $query->orWhere('email', 'like', '%.'.$tld); + } + + foreach (NotReservedEmailDomain::PROBE_LOCAL_PREFIXES as $prefix) { + $query->orWhere('email', 'like', $prefix.'%@%'); + } }); } } diff --git a/app/Http/Requests/EventRequest.php b/app/Http/Requests/EventRequest.php index 76af28287..2e22c0edd 100644 --- a/app/Http/Requests/EventRequest.php +++ b/app/Http/Requests/EventRequest.php @@ -3,6 +3,7 @@ namespace App\Http\Requests; use App\Rules\NotReservedEmailDomain; +use App\Rules\NotSecurityProbeContent; use App\Rules\validAudience; use App\Rules\validTheme; use Illuminate\Foundation\Http\FormRequest; @@ -30,8 +31,8 @@ public function rules(): array return [ 'activity_type' => 'required', - 'title' => 'required|min:5', - 'description' => 'required|min:5', + 'title' => ['required', 'min:5', new NotSecurityProbeContent], + 'description' => ['required', 'min:5', new NotSecurityProbeContent], 'organizer' => 'required', 'duration' => 'required', 'location' => 'required_unless:activity_type,open-online,invite-online', diff --git a/app/Rules/NotReservedEmailDomain.php b/app/Rules/NotReservedEmailDomain.php index 7796f7fe4..912c0b8fe 100644 --- a/app/Rules/NotReservedEmailDomain.php +++ b/app/Rules/NotReservedEmailDomain.php @@ -7,8 +7,9 @@ /** * Rejects emails on RFC 2606 / RFC 6761 reserved testing domains - * (.invalid, .test, .example, .localhost) so probe accounts cannot - * create pending activities that land in ambassador moderation queues. + * (.invalid, .test, .example, .localhost) and known CW probe local-parts + * (cwprobe*) so probe accounts cannot create pending activities that land + * in ambassador moderation queues. */ class NotReservedEmailDomain implements ValidationRule { @@ -19,24 +20,17 @@ class NotReservedEmailDomain implements ValidationRule 'localhost', ]; + public const PROBE_LOCAL_PREFIXES = [ + 'cwprobe', + ]; + public function validate(string $attribute, mixed $value, Closure $fail): void { if ($value === null || $value === '') { return; } - $email = strtolower(trim((string) $value)); - $at = strrpos($email, '@'); - - if ($at === false) { - return; - } - - $domain = substr($email, $at + 1); - $labels = explode('.', $domain); - $tld = end($labels); - - if ($tld !== false && in_array($tld, self::RESERVED_TLDS, true)) { + if (self::matches((string) $value)) { $fail('Please enter a real email address. Test domains are not allowed.'); } } @@ -50,10 +44,21 @@ public static function matches(string $email): bool return false; } + $local = substr($email, 0, $at); $domain = substr($email, $at + 1); $labels = explode('.', $domain); $tld = end($labels); - return $tld !== false && in_array($tld, self::RESERVED_TLDS, true); + if ($tld !== false && in_array($tld, self::RESERVED_TLDS, true)) { + return true; + } + + foreach (self::PROBE_LOCAL_PREFIXES as $prefix) { + if (str_starts_with($local, $prefix)) { + return true; + } + } + + return false; } } diff --git a/app/Rules/NotSecurityProbeContent.php b/app/Rules/NotSecurityProbeContent.php new file mode 100644 index 000000000..b067ce8b4 --- /dev/null +++ b/app/Rules/NotSecurityProbeContent.php @@ -0,0 +1,73 @@ +assertSessionHasErrors('user_email'); } + #[Test] + public function event_rejects_cwprobe_gmail_addresses(): void + { + $this->publishEvent(['user_email' => 'cwprobe.intake154639@gmail.com']) + ->assertSessionHasErrors('user_email'); + } + + #[Test] + public function event_rejects_security_probe_titles(): void + { + $this->publishEvent(['title' => 'CW Probe PathTraversal PATCH']) + ->assertSessionHasErrors('title'); + } + #[Test] public function event_should_get_valid_slug(): void { diff --git a/tests/Feature/PurgeSecurityProbeEventsTest.php b/tests/Feature/PurgeSecurityProbeEventsTest.php index 6c8607703..3b4956986 100644 --- a/tests/Feature/PurgeSecurityProbeEventsTest.php +++ b/tests/Feature/PurgeSecurityProbeEventsTest.php @@ -26,6 +26,15 @@ public function it_soft_deletes_probe_events_and_users(): void 'creator_id' => $probeUser->id, ]); + $gmailProbeUser = User::factory()->create(['email' => 'cwprobe.intake154639@gmail.com']); + $gmailProbeEvent = Event::factory()->create([ + 'title' => 'CW Defaults Check', + 'user_email' => 'cwprobe.intake154639@gmail.com', + 'description' => 'Benign.', + 'status' => 'PENDING', + 'creator_id' => $gmailProbeUser->id, + ]); + $realEvent = Event::factory()->create([ 'title' => 'Real Belgium coding club', 'user_email' => 'teacher@school.be', @@ -38,8 +47,10 @@ public function it_soft_deletes_probe_events_and_users(): void ->assertSuccessful(); $this->assertSoftDeleted($probeEvent); + $this->assertSoftDeleted($gmailProbeEvent); $this->assertDatabaseHas('events', ['id' => $realEvent->id, 'deleted_at' => null]); $this->assertSoftDeleted($probeUser); + $this->assertSoftDeleted($gmailProbeUser); $this->assertDatabaseHas('users', ['id' => $realUser->id, 'deleted_at' => null]); } } diff --git a/tests/Unit/Rules/NotReservedEmailDomainTest.php b/tests/Unit/Rules/NotReservedEmailDomainTest.php index eda5f39a7..2970287c6 100644 --- a/tests/Unit/Rules/NotReservedEmailDomainTest.php +++ b/tests/Unit/Rules/NotReservedEmailDomainTest.php @@ -46,6 +46,8 @@ public static function reservedEmails(): array ['user@something.test'], ['user@foo.example'], ['user@localhost'], + ['cwprobe.intake154639@gmail.com'], + ['CWPROBE@school.be'], ]; } @@ -56,6 +58,7 @@ public static function allowedEmails(): array ['organiser@codeweek.eu'], ['person@example.com'], ['person@gmail.com'], + ['notcwprobe@gmail.com'], ]; } } diff --git a/tests/Unit/Rules/NotSecurityProbeContentTest.php b/tests/Unit/Rules/NotSecurityProbeContentTest.php new file mode 100644 index 000000000..63a3284d8 --- /dev/null +++ b/tests/Unit/Rules/NotSecurityProbeContentTest.php @@ -0,0 +1,60 @@ +assertTrue(NotSecurityProbeContent::matches($value)); + + $failed = false; + (new NotSecurityProbeContent)->validate('title', $value, function () use (&$failed): void { + $failed = true; + }); + + $this->assertTrue($failed); + } + + #[Test] + #[DataProvider('allowedValues')] + public function it_allows_normal_activity_copy(string $value): void + { + $this->assertFalse(NotSecurityProbeContent::matches($value)); + + $failed = false; + (new NotSecurityProbeContent)->validate('title', $value, function () use (&$failed): void { + $failed = true; + }); + + $this->assertFalse($failed); + } + + public static function probeValues(): array + { + return [ + ['CW Probe SrcRef SSRF'], + ['CW SSRF Restored 1795087516'], + ['CW Restore 1795087848'], + ['CW Defaults Check'], + ['CW Isolate PicPath'], + ['PATCH test for SSRF probe - authorized security testing.'], + ]; + } + + public static function allowedValues(): array + { + return [ + ['Belgium coding club for kids'], + ['Learn Scratch at school'], + ['Community workshop in Brussels'], + ]; + } +}