ASP 正則表達式過濾 `
函式目的
移除使用者輸入中完整的 `` 區塊,降低儲存型 XSS 風險。注意:這只是**基礎過濾**,完整防 XSS 需結合輸出編碼、CSP、輸入驗證多層防禦。
核心函式
<%
Function RemoveScriptTags(inputStr)
Dim objRegExp, result
If IsNull(inputStr) Or inputStr = "" Then
RemoveScriptTags = inputStr
Exit Function
End If
Set objRegExp = New RegExp
objRegExp.IgnoreCase = True ' 不區分大小寫 (貪婪? 改為懶惰 .+?)
objRegExp.Pattern = ""
result = objRegExp.Replace(inputStr, "")
Set objRegExp = Nothing
RemoveScriptTags = result
End Function
' 使用範例
Dim userInput, cleanInput
userInput = Request.Form("content")
cleanInput = RemoveScriptTags(userInput)
' 後續儲存 cleanInput...
%>
正則模式解析
為什麼不用 `.+?`?因為 `.` 在 VBScript RegExp 預設**不匹配換行符**,跨行 `", _ ' Script 標籤
"", _ ' Iframe
"", _ ' Object
"