蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 Bin 部署 DLL 清单、DbProviderFactories XML、App_Data 防覆盖与连接串脱敏

PYTHONWindows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。explore-mvc-publish.py
explore-mvc-publish.py143 行
"""Offline ASP.NET MVC publishing & config hygiene verification lab for Chapter 36 (mvc-publish)."""
import json
import re
import xml.etree.ElementTree as ET

VERSION = "CPython 3.12.14 offline ASP.NET MVC deployment & config hygiene verifier (IIS/ASP.NET host absent)"


def case_manual_deploy_bin_dll_manifest():
    assembly_sources = [
        r"C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies",
        r"C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies",
        r"C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v4.0\Private",
    ]
    bin_dlls = [
        "bin/System.Web.Mvc.dll",
        "bin/System.Web.Razor.dll",
        "bin/System.Web.WebPages.dll",
        "bin/System.Web.Helpers.dll",
        "bin/System.Data.SqlServerCe.dll",
    ]
    expected = {
        "source_dir_count": 3,
        "all_paths_have_backslashes": True,
        "all_deployed_to_bin": True,
        "mvc_dll_present": True,
    }
    actual = {
        "source_dir_count": len(assembly_sources),
        "all_paths_have_backslashes": all("\\" in p and p.startswith("C:\\") for p in assembly_sources),
        "all_deployed_to_bin": all(d.startswith("bin/") and d.endswith(".dll") for d in bin_dlls),
        "mvc_dll_present": "bin/System.Web.Mvc.dll" in bin_dlls,
    }
    return {
        "name": "manual_deploy_bin_dll_manifest",
        "summary": "校验手动 Bin 部署时 ASP.NET Web Pages、MVC 3 与 SQL Server Compact 私有程序集路径及目标 bin/ 清单",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_sqlce_dbproviderfactories_xml_parse():
    web_config = (
        '<?xml version="1.0" encoding="UTF-8"?>'
        '<configuration><system.data><DbProviderFactories>'
        '<remove invariant="System.Data.SqlServerCe.4.0" />'
        '<add invariant="System.Data.SqlServerCe.4.0" '
        'name="Microsoft SQL Server Compact 4.0" '
        'description=".NET Framework Data Provider for Microsoft SQL Server Compact" '
        'type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, '
        'Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />'
        '</DbProviderFactories></system.data></configuration>'
    )
    root = ET.fromstring(web_config)
    remove_el = root.find("./system.data/DbProviderFactories/remove")
    add_el = root.find("./system.data/DbProviderFactories/add")
    expected = {
        "remove_invariant": "System.Data.SqlServerCe.4.0",
        "add_invariant": "System.Data.SqlServerCe.4.0",
        "has_public_key_token": True,
        "remove_before_add": True,
    }
    children = [el.tag for el in root.find("./system.data/DbProviderFactories")]
    actual = {
        "remove_invariant": remove_el.attrib["invariant"],
        "add_invariant": add_el.attrib["invariant"],
        "has_public_key_token": "PublicKeyToken=89845dcd8080cc91" in add_el.attrib["type"],
        "remove_before_add": children == ["remove", "add"],
    }
    return {
        "name": "sqlce_dbproviderfactories_xml_parse",
        "summary": "解析 Web.config 中 <DbProviderFactories> 先 <remove> 再 <add> 注册 SQL Server Compact 4.0 提供程序的 XML 结构",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_appdata_test_db_exclusion_guard():
    dev_files = [
        "Global.asax",
        "Web.config",
        "Content/Site.css",
        "Views/Home/Index.cshtml",
        "bin/System.Web.Mvc.dll",
        "App_Data/Movies.sdf",
    ]
    deploy_plan = [f for f in dev_files if not f.startswith("App_Data/")]
    expected = {
        "excluded_test_db": True,
        "deploy_count": 5,
        "web_config_included": True,
    }
    actual = {
        "excluded_test_db": "App_Data/Movies.sdf" not in deploy_plan,
        "deploy_count": len(deploy_plan),
        "web_config_included": "Web.config" in deploy_plan,
    }
    return {
        "name": "appdata_test_db_exclusion_guard",
        "summary": "验证发布清单自动过滤含测试数据的 App_Data/Movies.sdf,防止覆盖生产服务器已有数据库",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_connection_string_secret_redaction_check():
    raw_cs = "Server=prod-db.internal;Database=MoviesProd;User Id=app_user;Password=S3cr3t!Pass;Encrypt=True;"
    redacted = re.sub(r"(?i)(Password|Pwd)=[^;]*", r"\1=***REDACTED***", raw_cs)
    expected = {
        "secret_removed": True,
        "redacted": "Server=prod-db.internal;Database=MoviesProd;User Id=app_user;Password=***REDACTED***;Encrypt=True;",
        "dotnet_publish_cmd": "dotnet publish -c Release -o ./publish",
    }
    actual = {
        "secret_removed": "S3cr3t!Pass" not in redacted,
        "redacted": redacted,
        "dotnet_publish_cmd": "dotnet publish -c Release -o ./publish",
    }
    return {
        "name": "connection_string_secret_redaction_check",
        "summary": "验证发布前连接串凭据脱敏规则(屏蔽 Password/Pwd)与现代 `dotnet publish -c Release` 发布命令",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_manual_deploy_bin_dll_manifest(),
        case_sqlce_dbproviderfactories_xml_parse(),
        case_appdata_test_db_exclusion_guard(),
        case_connection_string_secret_redaction_check(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。

入口文件:projects/aspnet-mvc-publish-lab/explore-mvc-publish.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-mvc-publish.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 ASP.NET Razor / MVC 语义验证器(未启动 Visual Studio / IIS / ASP.NET MVC 运行时宿主)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:ASP.NET

课程关联工程包(登记关联55章) · 含当前章节或实例 · mbskycom202612a2794.zip

下载课程关联工程包:ASP.NET

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2814.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习