"""Offline ASP.NET semantic verification lab for Chapter 14 (webpages-email)."""
from email.message import EmailMessage
import json
import re
VERSION = "CPython 3.12.14 offline ASP.NET WebPages/Core semantic verifier (IIS/WebMatrix absent)"
def case_appstart_websecurity_and_webmail_props():
websecurity_init = {
"database": "Users",
"userTable": "UserProfile",
"userIdColumn": "UserId",
"userNameColumn": "Email",
"autoCreateTables": True,
}
webmail_cfg = {
"SmtpServer": "smtp.example.com",
"SmtpPort": 587,
"EnableSsl": True,
"UserName": "support@example.com",
"Password": "<env:SMTP_PASSWORD>",
"From": "john@example.com",
}
expected = {
"websecurity_params": 5,
"webmail_props": ["EnableSsl", "From", "Password", "SmtpPort", "SmtpServer", "UserName"],
"tls_recommended": True,
}
actual = {
"websecurity_params": len(websecurity_init),
"webmail_props": sorted(webmail_cfg.keys()),
"tls_recommended": webmail_cfg["EnableSsl"] and webmail_cfg["SmtpPort"] == 587,
}
return {
"name": "appstart_websecurity_and_webmail_props",
"summary": "核对 _AppStart.cshtml 中 WebSecurity 初始化与 WebMail 六项 SMTP 属性及 TLS 587 安全建议",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_email_input_form_errata_validation():
broken_input = '<form method="post" action="EmailSend.cshtml"><input type="text name="customerEmail" /></form>'
fixed_input = '<form method="post" action="Email_Send.cshtml"><input type="text" name="customerEmail" /></form>'
expected = {
"broken_missing_quote": True,
"fixed_matches_target_file": True,
"fixed_valid_input_attrs": True,
}
actual = {
"broken_missing_quote": 'type="text name=' in broken_input,
"fixed_matches_target_file": 'action="Email_Send.cshtml"' in fixed_input,
"fixed_valid_input_attrs": 'type="text" name="customerEmail"' in fixed_input,
}
return {
"name": "email_input_form_errata_validation",
"summary": "检测并修正原 Email_Input.cshtml 表单中缺失的引号(type=\"text\")及 action 与 Email_Send.cshtml 命名不一致问题",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_smtp_mime_message_assembly():
customer_email = "alice@example.org"
customer_request = "Cannot reset account password."
msg = EmailMessage()
msg["From"] = "john@example.com"
msg["To"] = "someone@example.com"
msg["Subject"] = f"Help request from - {customer_email}"
msg.set_content(customer_request)
raw = msg.as_string()
expected = {
"to": "someone@example.com",
"from": "john@example.com",
"subject": "Help request from - alice@example.org",
"body_present": True,
}
actual = {
"to": msg["To"],
"from": msg["From"],
"subject": msg["Subject"],
"body_present": customer_request in raw,
}
return {
"name": "smtp_mime_message_assembly",
"summary": "模拟 Email_Send.cshtml 调用 WebMail.Send 组装标准 RFC 5322/MIME 邮件头与正文(离线不联网发信)",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_header_injection_and_exception_handling():
unsafe_subject_input = "alice@example.org\r\nBcc: attacker@evil.example"
has_crlf = bool(re.search(r"[\r\n]", unsafe_subject_input))
safe_error_response = "邮件提交失败,请稍后重试(详细异常已写入服务端日志)。"
expected = {
"crlf_injection_detected": True,
"raw_stacktrace_hidden": True,
}
actual = {
"crlf_injection_detected": has_crlf,
"raw_stacktrace_hidden": "Exception" not in safe_error_response and "Traceback" not in safe_error_response,
}
return {
"name": "header_injection_and_exception_handling",
"summary": "验证拦截邮件主题 CRLF 头注入并避免在生产 catch 块中直接向页面输出原始异常堆栈",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_appstart_websecurity_and_webmail_props(),
case_email_input_form_errata_validation(),
case_smtp_mime_message_assembly(),
case_header_injection_and_exception_handling(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。
入口文件:projects/web-forms-webpages-email-lab/explore-webpages-email.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-webpages-email.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET WebPages / Razor 语义验证器(本机未启动 IIS / IIS Express / WebMatrix 宿主环境)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2981.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3002.zip