HTTP 協定定義 `GET` 與 `POST` 兩種主要請求方法,ASP 開發者必須理解其語意差異、技術限制與安全含義,才能正確選擇並防範常見漏洞 (CSRF、敏感資訊洩露)。
核心差異速查表
| 維度 | GET | POST |
| 語意 | 獲取資源 (冪等、安全、可快取) | 提交/變更資源 (非冪等、不可快取) |
| 資料位置 | URL 查詢字串 (`?key=val&...`) | HTTP Request Body |
| 可見性 | 瀏覽器網址列、歷史記錄、Proxy/伺服器 Log 全可見 | Body 不顯示於網址列,但可透過 DevTools/Fiddler 檢視 |
| 長度限制 | ~2 KB (IE/舊伺服器) ~8 KB (現代) | 理論無上限 (IIS 預設 30 MB,可調 `maxAllowedContentLength`) |
| 編碼方式 | `application/x-www-form-urlencoded` (URL Encoding) | `application/x-www-form-urlencoded` 或 `multipart/form-data` (上傳檔案) |
| ASP 接收 | `Request.QueryString("key")` | `Request.Form("key")` |
| 書籤/分享 | 可直接複製 URL 分享/收藏 | 無法直接分享 (需表單重送) |
| 重新整理行為 | 無副作用 (安全) | 瀏覽器會詢問「重新提交表單?」 |
| CSRF 風險 | 高 (可構造連結誘導點擊) | 中 (需構造表單自動提交),仍需 CSRF Token |
| 敏感資料 | **嚴禁** 傳遞密碼、Token、身分證號 | 可接受 (需 HTTPS + 無快取 Header) |
ASP 伺服端接收代碼對照
'--- GET 範例: search.asp?q=關鍵字&page=2 ---
Dim kw, pg
kw = Request.QueryString("q")
pg = CLng(Request.QueryString("page"))
If pg < 1 Then pg = 1
'--- POST 範例: login.asp (form method="post") ---
Dim user, pwd
user = Request.Form("username")
pwd = Request.Form("password")
'--- 通用寫法 (同時支援 GET/POST,不推薦) ---
Dim val
val = Request("key") ' 先找 Form,再找 QueryString,再找 Cookies/ServerVariables
' 風險:無法區分來源,易誤用
HTML 表單範例
<!-- GET: 搜尋、分頁、篩選 -->
<form action="search.asp" method="get">
<input type="text" name="q" placeholder="關鍵字">
<input type="hidden" name="page" value="1">
<button type="submit">搜尋</button>
</form>
<!-- POST: 登入、註冊、修改資料、上傳 -->
<form action="login.asp" method="post">
<input type="text" name="username" required>
<input type="password" name="password" required>
<button type="submit">登入</button>
</form>
<!-- 檔案上傳必須 POST + multipart/form-data -->
<form action="upload.asp" method="post" enctype="multipart/form-data">
<input type="file" name="avatar" accept="image/*">
<button type="submit">上傳</button>
</form>
安全性深度剖析
1. GET 洩露敏感資訊實例
' 登入頁錯誤使用 GET
<form action="login.asp" method="get">
帳號: <input name="user"> 密碼: <input name="pwd" type="password">
</form>
' 送出後 URL 變成:
' https://site/login.asp?user=admin&pwd=123456
' 後果:
' 1. 瀏覽器歷史記錄留存明文密碼
' 2. Proxy/防火牆/伺服器 Log 記錄密碼
' 3. Referer 標頭將密碼傳給第三方網站 (如頁面載入外部圖片/腳本)
' 4. 使用者分享連結洩露帳密
2. CSRF 攻擊與防禦
' 受害頁面 (POST 修改密碼)
<form action="changepwd.asp" method="post">
新密碼: <input name="newpwd" type="password">
<input type="hidden" name="csrf_token" value="<%= Session("CSRF_TOKEN") %>">
<button>變更</button>
</form>
' 攻擊頁面 (駭客網站)
<form action="https://victim.com/changepwd.asp" method="post" id="evil">
<input name="newpwd" value="hacked123">
<!-- 無法猜測 csrf_token,攻擊失敗 -->
</form>
<script>document.getElementById('evil').submit();</script>
' 伺服端驗證
If Request.Form("csrf_token") <> Session("CSRF_TOKEN") Then
Response.Write "非法請求來源"
Response.End
End If
3. 密碼傳輸最佳實務
- **全站 HTTPS** (TLS 1.2+),防止中間人竊聽
- **登入/修改密碼** 使用 `POST` + `autocomplete="new-password"`
- **前端雜湊** (選擇性):`SHA-256(password + salt)` 傳送雜湊值,伺服端再雜湊 (防明文在記憶體/Log)
- **禁止 GET 傳遞敏感參數** — 程式碼審查強制規則
IIS/ASP 設定調整
' ASP 最大請求實體 (預設 200KB,IIS 6.0)
' metabase: AspMaxRequestEntityAllowed = 10485760 (10MB)
' IIS 7+ web.config
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="10485760" /> <!-- 10MB -->
</requestFiltering>
</security>
</system.webServer>
現代框架對應
| ASP 3.0 | ASP.NET Core | Node.js (Express) |
| `Request.QueryString("q")` | `Request.Query["q"]` | `req.query.q` |
| `Request.Form("pwd")` | `Request.Form["pwd"]` | `req.body.pwd` (需 `express.urlencoded()`) |
| `Request.TotalBytes` | `Request.ContentLength` | `req.headers['content-length']` |
| `Request.BinaryRead` | `Request.Body` (Stream) | `req` (Stream) |
黃金法則:
✅ **查詢、搜尋、分頁、篩選、可分享連結** → **GET**
✅ **登入、註冊、修改、刪除、上傳、付款、任何狀態變更** → **POST**
❌ **永不在 GET 傳遞密碼、Token、身分證、信用卡號**
🔒 **所有 POST 表單必須包含 CSRF Token**
🔐 **全站強制 HTTPS**