2025年11月5日 星期三

[研究]IIS的「允許高位元字元」無法被 Web.config 覆蓋,為何 HSTS 的 max-age 可被覆蓋?

[研究]IIS的「允許高位元字元」無法被 Web.config 覆蓋,為何 HSTS 的 max-age 可被覆蓋?

2025-11-05

[研究]GCB 建議 IIS 10【不勾選「允許高位元字元」】,可在 Web.config 做設定,讓 URL 或下載檔案名稱允許繁體中文字 ?
https://shaurong.blogspot.com/2025/11/gcb-iis-10-webconfig-url.html

[研究]GCB,IIS和Web.config都設定HSTS值後最終 max-age值為何?
https://shaurong.blogspot.com/2025/11/gcbiiswebconfighsts-max-age.html

********************************************************************************

ChatGPT

🔹 一、問題背景

你提到的設定:

IIS 管理員 → 伺服器 → 「要求篩選」→ 功能設定 → 允許高位元字元(Allow double-byte characters)

這個選項對應的實際設定路徑是:

<system.webServer>
  <security>
    <requestFiltering allowHighBitCharacters="true" />
  </security>
</system.webServer>

當「不勾選」時,值為:

allowHighBitCharacters="false"   

🔹 二、這個設定屬於哪個層級?

這個屬性是屬於 IIS 原生模組(Request Filtering Module) 的一部分,

而 非 ASP.NET 管線或 Web.config 可自由覆蓋的範圍。

它的層級是:

層級 設定檔 是否可繼承 / 覆蓋
伺服器層級 %windir%\System32\inetsrv\config\applicationHost.config ✅ 預設在這裡定義
站台或應用程式層級 (Web.config) ❌ 限制覆蓋

🔹 三、為何 Web.config 覆蓋會失效

當「允許高位元字元」設為 false 時,IIS 的行為是:

  1. 在 請求進入 ASP.NET 之前(在 Request Filtering 階段)就先檢查 URL。
  2. 若偵測到 URL 中含有非 ASCII(>127)字元,例如繁體中文、日文、韓文等:
    • 請求 直接被 IIS 拒絕(HTTP 404.11 / 400.0)。
    • 根本不會進入 ASP.NET 管線(也不會載入 Web.config)。
  1. 因此,Web.config 根本沒有機會生效,所以無法「覆蓋」IIS 設定。

🔹 四、這個屬性屬於哪類設定?

這屬於 native IIS core configuration,不是 managed (.NET) 層設定。

只有在 IIS 的 applicationHost.config 或「要求篩選」功能中調整才有效。

🔸 可設定的範圍由 IIS 模組的 <section> 宣告控制,

在 %windir%\System32\inetsrv\config\schema\ 裡有定義:

<section name="requestFiltering" allowDefinition="MachineToApplication" 
   overrideModeDefault="Deny" />

注意這裡的 overrideModeDefault="Deny",表示:預設 禁止在 Web.config 覆蓋。

🔹 五、解決方法

如果你想在不勾選「允許高位元字元」的情況下,仍能讓特定網站允許繁體中文 / 日文 / 韓文 URL:

✅ 方法 1:在 applicationHost.config 手動針對站台開放覆蓋

<location path="YourSiteName">
  <system.webServer>
    <security>
      <requestFiltering allowHighBitCharacters="true" />  
    </security>
  </system.webServer>
</location>

這樣只影響該站台,不會放寬全伺服器。

✅ 方法 2:修改 Section schema 允許覆蓋(不建議)

可以改 schema 檔讓 overrideModeDefault="Allow",但不符合 GCB 要求。

🔹 六、總結對照

屬性 模組 可否 Web.config 覆蓋 原因
Strict-Transport-Security Managed (HTTP headers) ✅ 可覆蓋 ASP.NET 執行階段加入
allowHighBitCharacters Native (Request Filtering) ❌ 無法覆蓋 IIS 在 ASP.NET 之前攔截請求
maxAllowedContentLength Native ❌ 無法覆蓋 同上
requestLimits Native 部分可繼承 視 overrideMode 而定

另外

方法 可行性 風險 GCB 建議
Web.config 修改 requestFiltering ❌ 無效 N/A 不符合規範
修改 schema overrideModeDefault="Allow" ⚠️ 可行,但全域生效 高風險、安全性下降 不建議
在 applicationHost.config / <location> 指定站台 ✅ 推薦 安全、可控 ✅ 符合 GCB

🧩 IIS 處理管線流程(簡化示意)

┌─────────────────────────────────────────────┐
│ ① 網頁請求傳入 IIS (ex: https://site/中文.html) │
└─────────────────────────────────────────────┘
                 │
                 ▼
┌─────────────────────────────────────────────┐
│ [Native 模組階段]                           │
│ 由 IIS 核心模組處理 (C/C++ 實作)            │
│---------------------------------------------│
│ • URL Decoding                              │
│ • Request Filtering (要求篩選)               │
│   ┌─────────────────────────────────────┐  │
│   │ 設定來源 (優先順序)                   │  │
│   │ 1️⃣ 站台層級 `<location>` (applicationHost.config) │
│   │    └─ allowHighBitCharacters=true    │  │
│   │ 2️⃣ 伺服器層級 (applicationHost.config)│
│   │    └─ allowHighBitCharacters=false   │  │
│   │ 3️⃣ Web.config                        │  │
│   │    └─ ❌ 無法覆蓋 requestFiltering   │  │
│   └─────────────────────────────────────┘  │
│ • IP 限制、SSL 檢查、MIME 篩選...            │
│---------------------------------------------│
│ ✳️ 若高位元字元不允許 → IIS 直接拒絕       │
│    → HTTP 404.11 / 400,流程中止          │
└─────────────────────────────────────────────┘
                 │
                 ▼
┌─────────────────────────────────────────────┐
│ [Managed 模組階段]                          │
│ ASP.NET 管線啟動 (.NET CLR)                 │
│---------------------------------------------│
│ • 載入 Web.config(由站台、目錄繼承)       │
│ • 驗證與授權(authentication、authorization)│
│ • ASP.NET Page Lifecycle(Page_Load 等)     │
│ • Response.Headers 加入 Strict-Transport-Security 等 │
│   ┌─────────────────────────────────────┐  │
│   │ Web.config 可覆蓋的設定:             │  │
│   │ - HTTP Response Headers               │  │
│   │   (Strict-Transport-Security, X-Frame-Options) │
│   │ - ASP.NET 層級 authentication / authorization │
│   └─────────────────────────────────────┘  │
└─────────────────────────────────────────────┘
                 │
                 ▼
┌─────────────────────────────────────────────┐
│ [Response 階段]                             │
│ ASP.NET 將輸出交回 IIS,IIS 回傳至用戶端     │
└─────────────────────────────────────────────┘

📘 重點總結

階段 處理層級 代表設定 可否被 Web.config 覆蓋
Request Filtering (要求篩選) Native (IIS Core) allowHighBitCharacters, maxAllowedContentLength ❌ 否
HTTP Response Headers Managed (ASP.NET 層) Strict-Transport-Security, X-Frame-Options ✅ 可
Authentication / Authorization Managed (.NET 層) <authentication>, <authorization> ✅ 可
機碼安全層級 Registry / OS 層 SCHANNEL, SSL/TLS 設定 ❌ 否

🔍 延伸說明

  • 「允許高位元字元」屬於最早期的 IIS 原生模組階段。
  • ASP.NET Web.config 的載入,是在 Request Filtering 通過之後 才開始。
  • 所以當高位元字元(繁中、日文、韓文、歐洲語)URL 被攔下時,ASP.NET 根本沒有機會處理。

**********

感覺似乎先照 GCB 【不勾選「允許高位元字元」】設定,然後

在 applicationHost.config 手動針對站台開放覆蓋

<location path="YourSiteName">
  <system.webServer>
    <security>
      <requestFiltering allowHighBitCharacters="true" />  
    </security>
  </system.webServer>
</location>

這樣只影響該站台,不會放寬全伺服器,雖麻煩但是較好解法。

(完)

沒有留言:

張貼留言