fix: harden maintenance notice recipients

go
codex 3 weeks ago
parent d903e0850d
commit 35d8ebc205

@ -58,12 +58,21 @@ final class CloudMaintenanceNoticeDeliveryService
$recipientId = (int) $recipient->id;
$noticeId = (int) $recipient->notice_id;
try {
if (!User::where('id', (int) $recipient->user_id)->exists()) {
$user = User::where('id', (int) $recipient->user_id)
->first(['email']);
if ($user === null) {
$this->finalize($recipientId, $noticeId, $token,
'skipped', 'user_deleted', '');
return true;
}
if (($this->blacklist)((string) $recipient->email_snapshot)) {
$snapshot = (string) $recipient->email_snapshot;
$currentEmail = strtolower(trim((string) $user->email));
if (!hash_equals($snapshot, $currentEmail)) {
$this->finalize($recipientId, $noticeId, $token,
'skipped', 'recipient_email_changed', '');
return true;
}
if (($this->blacklist)($snapshot)) {
$this->finalize($recipientId, $noticeId, $token,
'skipped', 'recipient_blacklisted', '');
return true;

@ -350,6 +350,13 @@ final class CloudMaintenanceNoticeService
private function validateRequest(array $input): array
{
foreach (['scope_type', 'subject', 'message_text', 'impact',
'audience_digest'] as $key) {
if (!array_key_exists($key, $input)
|| !is_string($input[$key])) {
throw new CloudError('维护通知内容无效', 422);
}
}
$scopeType = trim((string) ($input['scope_type'] ?? ''));
$scopeId = filter_var($input['scope_id'] ?? null,
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
@ -362,7 +369,10 @@ final class CloudMaintenanceNoticeService
if (!in_array($scopeType, ['host', 'public_ip_pool'], true)
|| $scopeId === false
|| $subject === '' || mb_strlen($subject) > 180
|| preg_match('/[\x00-\x1F\x7F]/', $subject) === 1
|| $message === '' || mb_strlen($message) > 5000
|| preg_match('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/',
$message) === 1
|| !in_array($impact, self::IMPACTS, true)
|| preg_match('/^[a-f0-9]{64}$/D', $digest) !== 1) {
throw new CloudError('维护通知内容无效', 422);

@ -86,6 +86,7 @@ Capsule::table('user')->insert([
['id' => 1, 'email' => 'ok@example.com'],
['id' => 2, 'email' => 'unknown@example.com'],
['id' => 3, 'email' => 'blocked@example.com'],
['id' => 4, 'email' => 'new@example.com'],
]);
$insertNotice = static function (int $id, string $status = 'scheduled') use ($now): void {
Capsule::table('cloud_maintenance_notices')->insert([
@ -190,6 +191,15 @@ $assert($row->status === 'skipped'
&& $notice->status === 'completed',
'Blacklisted delivery was sent or aggregated as a failure.');
$insertNotice(6);
$insertRecipient(7, 6, 4, 'old@example.com', 'pending', $now);
$assert($delivery->tick() === true && count($sent) === 2,
'An email-changed recipient did not settle without transport.');
$row = Capsule::table('cloud_maintenance_notice_recipients')->find(7);
$assert($row->status === 'skipped'
&& $row->last_error_code === 'recipient_email_changed',
'Frozen recipient PII was sent after the account email changed.');
$insertNotice(4);
$insertRecipient(5, 4, 99, 'deleted@example.com', 'pending', $now);
$assert($delivery->tick() === true && count($sent) === 2
@ -198,7 +208,7 @@ $assert($delivery->tick() === true && count($sent) === 2
'A deleted user still reached the transport.');
$insertNotice(5);
$insertRecipient(6, 5, 1, 'fenced@example.com', 'pending', $now);
$insertRecipient(6, 5, 1, 'ok@example.com', 'pending', $now);
$fenced = new CloudMaintenanceNoticeDeliveryService(
static function (): bool {
Capsule::table('cloud_maintenance_notice_recipients')

@ -229,6 +229,28 @@ $expect(
409,
'An idempotency key must not accept a different request.'
);
$invalidType = $request;
$invalidType['subject'] = ['unexpected'];
$expect(
static fn () => $service->schedule(
9,
'maintenance-test-typed-input',
$invalidType
),
422,
'Array input must not be coerced into an email subject.'
);
$invalidHeader = $request;
$invalidHeader['subject'] = "Maintenance\r\nBcc: attacker@example.com";
$expect(
static fn () => $service->schedule(
9,
'maintenance-test-header-input',
$invalidHeader
),
422,
'Email subjects must reject control characters.'
);
$cancelled = $service->cancel($created['notice_key']);
$assert($cancelled['status'] === 'cancelled'

Loading…
Cancel
Save