Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions app/Console/Commands/PurgeSecurityProbeEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

use App\Event;
use App\Rules\NotReservedEmailDomain;
use App\Rules\NotSecurityProbeContent;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;

/**
* 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
Expand All @@ -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
{
Expand Down Expand Up @@ -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}");
Expand All @@ -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.'%@%');
}
});
}
}
5 changes: 3 additions & 2 deletions app/Http/Requests/EventRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down
35 changes: 20 additions & 15 deletions app/Rules/NotReservedEmailDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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.');
}
}
Expand All @@ -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;
}
}
73 changes: 73 additions & 0 deletions app/Rules/NotSecurityProbeContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

/**
* Rejects activity title/description markers used by authorized security probes
* so test submissions do not land in ambassador moderation queues.
*/
class NotSecurityProbeContent implements ValidationRule
{
public const TITLE_PREFIXES = [
'CW Probe',
'CW SSRF',
'CW Restore',
'CW Defaults',
'CW Isolate',
'CW Path',
'CW PT',
'CW S3',
'CW Pic',
'CW Cert',
'CW TeX',
'CW Event',
'CW Combine',
'CW Final',
'CW Report',
'CW pending probe',
'CW marker test',
'CW Probe Create',
];

public const DESCRIPTION_NEEDLES = [
'authorized security testing',
'cwmark benign',
'benign baseline description for tester probe',
'ssrf probe',
'pathtraversal',
];

public function validate(string $attribute, mixed $value, Closure $fail): void
{
if ($value === null || $value === '') {
return;
}

if (self::matches((string) $value)) {
$fail('This activity looks like a security/test probe and cannot be submitted.');
}
}

public static function matches(string $value): bool
{
$normalized = trim($value);
$lower = strtolower($normalized);

foreach (self::TITLE_PREFIXES as $prefix) {
if (str_starts_with($lower, strtolower($prefix))) {
return true;
}
}

foreach (self::DESCRIPTION_NEEDLES as $needle) {
if (str_contains($lower, $needle)) {
return true;
}
}

return false;
}
}
14 changes: 14 additions & 0 deletions tests/Feature/CreateEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ public function event_rejects_reserved_test_email_domains(): void
->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
{
Expand Down
11 changes: 11 additions & 0 deletions tests/Feature/PurgeSecurityProbeEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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]);
}
}
3 changes: 3 additions & 0 deletions tests/Unit/Rules/NotReservedEmailDomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static function reservedEmails(): array
['user@something.test'],
['user@foo.example'],
['user@localhost'],
['cwprobe.intake154639@gmail.com'],
['CWPROBE@school.be'],
];
}

Expand All @@ -56,6 +58,7 @@ public static function allowedEmails(): array
['organiser@codeweek.eu'],
['person@example.com'],
['person@gmail.com'],
['notcwprobe@gmail.com'],
];
}
}
60 changes: 60 additions & 0 deletions tests/Unit/Rules/NotSecurityProbeContentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Tests\Unit\Rules;

use App\Rules\NotSecurityProbeContent;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

final class NotSecurityProbeContentTest extends TestCase
{
#[Test]
#[DataProvider('probeValues')]
public function it_rejects_probe_markers(string $value): void
{
$this->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'],
];
}
}