<?php
declare(strict_types=1);
$cases = [];
// Case 1: Demonstrate URL-decoded email header injection payload structure
$encodedAttack = 'someone@example.com%0ACc:person2@example.com'
. '%0ABcc:person3@example.com,person4@example.com'
. '%0ATo:person6@example.com';
$decodedAttack = urldecode($encodedAttack);
$naiveHeader = 'From:' . $decodedAttack;
$headerLines = explode("\n", $naiveHeader);
$actual1 = [
'contains_crlf' => preg_match('/[\r\n]/', $decodedAttack) === 1,
'injected_line_count' => count($headerLines),
'has_cc_header' => in_array('Cc:person2@example.com', $headerLines, true),
'has_bcc_header' => str_starts_with($headerLines[2], 'Bcc:'),
'has_to_header' => $headerLines[3] === 'To:person6@example.com',
];
$expected1 = [
'contains_crlf' => true,
'injected_line_count' => 4,
'has_cc_header' => true,
'has_bcc_header' => true,
'has_to_header' => true,
];
$cases[] = [
'name' => 'detect_email_header_injection',
'passed' => $actual1 === $expected1,
'expected' => $expected1,
'actual' => $actual1,
];
// Case 2: FILTER_SANITIZE_EMAIL + FILTER_VALIDATE_EMAIL spamcheck behavior
$spamcheck = static function (string $field): array {
$sanitized = filter_var($field, FILTER_SANITIZE_EMAIL);
$valid = $sanitized !== false && filter_var($sanitized, FILTER_VALIDATE_EMAIL) !== false;
return ['sanitized' => $sanitized, 'valid' => $valid];
};
$cleanRes = $spamcheck('alice@example.com');
$attackRes = $spamcheck($decodedAttack);
$badRes = $spamcheck('not-an-email@@example..com');
$actual2 = [
'clean_valid' => $cleanRes['valid'],
'clean_value' => $cleanRes['sanitized'],
'attack_valid' => $attackRes['valid'],
'attack_newline_stripped' => !str_contains((string)$attackRes['sanitized'], "\n"),
'malformed_valid' => $badRes['valid'],
];
$expected2 = [
'clean_valid' => true,
'clean_value' => 'alice@example.com',
'attack_valid' => false,
'attack_newline_stripped' => true,
'malformed_valid' => false,
];
$cases[] = [
'name' => 'filter_sanitize_and_validate_email',
'passed' => $actual2 === $expected2,
'expected' => $expected2,
'actual' => $actual2,
];
// Case 3: Strict production guard rejecting CRLF in both email and subject + array headers
$validateMailInput = static function (string $rawEmail, string $rawSubject): array {
if (preg_match('/[\r\n]/', $rawEmail) === 1 || preg_match('/[\r\n]/', $rawSubject) === 1) {
return ['accepted' => false, 'reason' => 'header_injection_blocked'];
}
$cleanEmail = filter_var(trim($rawEmail), FILTER_VALIDATE_EMAIL);
if ($cleanEmail === false) {
return ['accepted' => false, 'reason' => 'invalid_email'];
}
return [
'accepted' => true,
'subject' => trim($rawSubject),
'headers' => [
'From' => 'no-reply@example.com',
'Reply-To' => $cleanEmail,
],
];
};
$goodGuard = $validateMailInput('alice@example.com', '咨询反馈');
$badSubjectGuard = $validateMailInput('alice@example.com', "正常标题\r\nBcc:evil@example.com");
$badEmailGuard = $validateMailInput($decodedAttack, '测试');
$actual3 = [
'good_accepted' => $goodGuard['accepted'],
'good_reply_to' => $goodGuard['headers']['Reply-To'],
'bad_subject_accepted' => $badSubjectGuard['accepted'],
'bad_subject_reason' => $badSubjectGuard['reason'],
'bad_email_accepted' => $badEmailGuard['accepted'],
'bad_email_reason' => $badEmailGuard['reason'],
];
$expected3 = [
'good_accepted' => true,
'good_reply_to' => 'alice@example.com',
'bad_subject_accepted' => false,
'bad_subject_reason' => 'header_injection_blocked',
'bad_email_accepted' => false,
'bad_email_reason' => 'header_injection_blocked',
];
$cases[] = [
'name' => 'safe_mail_array_header_guard',
'passed' => $actual3 === $expected3,
'expected' => $expected3,
'actual' => $actual3,
];
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、PHP 8.4 本地 CLI 环境。
入口文件:projects/php-secure-mail-lab/explore-secure-mail.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-secure-mail.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1305.zip