您好,登錄后才能下訂單哦!
這篇文章主要介紹了go中如何實(shí)現(xiàn)web預(yù)防跨站腳本,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
現(xiàn)在的網(wǎng)站包含大量的動(dòng)態(tài)內(nèi)容以提高用戶體驗(yàn),比過(guò)去要復(fù)雜得多。所謂動(dòng)態(tài)內(nèi)容,就是根據(jù)用戶環(huán)境和需要,Web 應(yīng)用程序能夠輸出相應(yīng)的內(nèi)容。動(dòng)態(tài)站點(diǎn)會(huì)受到一種名為“跨站腳本攻擊”(Cross Site Scripting, 安全專家們通常將其縮寫(xiě)成 XSS)的威脅,而靜態(tài)站點(diǎn)則完全不受其影響。
攻擊者通常會(huì)在有漏洞的程序中插入 JavaScript、VBScript、 ActiveX 或 Flash 以欺騙用戶。一旦得手,他們可以盜取用戶帳戶信息,修改用戶設(shè)置,盜取或污染 cookie 和植入惡意廣告等。
對(duì) XSS 最佳的防護(hù)應(yīng)該結(jié)合以下兩種方式。
1 驗(yàn)證所有輸入數(shù)據(jù),有效檢測(cè)攻擊。
2 對(duì)所有輸出數(shù)據(jù)進(jìn)行適當(dāng)?shù)奶幚?,以防止任何已成功注入的腳本在瀏覽器端運(yùn)行。
針對(duì)第2種方式,Go 是怎樣預(yù)防的呢?Go 的 html/template 包中帶有下面幾個(gè)函數(shù)可以幫助轉(zhuǎn)義。
func HTMLEscape(w io.Writer, b []byte) // 把 b 進(jìn)行轉(zhuǎn)義之后寫(xiě)到 w
func HTMLEscapeString(s string) string // 轉(zhuǎn)義 s 之后返回結(jié)果字符串
func HTMLEscaper(args ...interface{}) string // 支持多個(gè)參數(shù)一起轉(zhuǎn)義,返回結(jié)果字符串
1 代碼
package main import ( "fmt" "html/template" "log" "net/http" ) // 登錄邏輯 func login(w http.ResponseWriter, r *http.Request) { fmt.Println("method:", r.Method) // 獲取請(qǐng)求的方法 if r.Method == "GET" { t, _ := template.ParseFiles("src\\goweb\\demo3\\login.html") // 解析模板 t.Execute(w, nil) // 渲染模板,并發(fā)送給前端 } else { // 請(qǐng)求的是登陸數(shù)據(jù),那么執(zhí)行登陸的邏輯判斷 // 解析表單 r.ParseForm() fmt.Println("username:", r.Form["username"]) fmt.Println("password:", r.Form["password"]) template.HTMLEscape(w, []byte(r.Form.Get("username"))) //輸出到客戶端 } } func main() { http.HandleFunc("/login", login) // 設(shè)置訪問(wèn)的路由 err := http.ListenAndServe(":9090", nil) // 設(shè)置監(jiān)聽(tīng)的端口 if err != nil { log.Fatal("ListenAndServe: ", err) } }
2 測(cè)試
如果在瀏覽器輸入的 username 是 <script>alert()</script>,在瀏覽器上將看到下面內(nèi)容。
3 說(shuō)明
Go 的 html/template 包默認(rèn)幫忙過(guò)濾了 html 標(biāo)簽,將其進(jìn)行了轉(zhuǎn)義。
4 問(wèn)題引出
如果要正常輸出<script>alert()</script>,怎樣處理呢?text/template 可以幫忙進(jìn)行處理。
1 代碼
package main import ( "log" "net/http" "text/template" ) // 轉(zhuǎn)義測(cè)試 func escape(w http.ResponseWriter, r *http.Request) { // 正常顯示 t, _ := template.New("foo").Parse(`{{define "T"}}Hello1, {{.}}!{{end}}`) t.ExecuteTemplate(w, "T", "<script>alert('you have been pwned')</script>") } func main() { http.HandleFunc("/escape", escape) // 設(shè)置轉(zhuǎn)義 err := http.ListenAndServe(":9090", nil) // 設(shè)置監(jiān)聽(tīng)的端口 if err != nil { log.Fatal("ListenAndServe: ", err) } }
2 測(cè)試
3 說(shuō)明
當(dāng)使用 text/template 這個(gè)包時(shí),可以正常顯示。
1 代碼
package main import ( "html/template" "log" "net/http" ) // 轉(zhuǎn)義測(cè)試 func escape(w http.ResponseWriter, r *http.Request) { // 轉(zhuǎn)義顯示 t, _ := template.New("foo").Parse(`{{define "T"}}Hello1, {{.}}!{{end}}`) t.ExecuteTemplate(w, "T", "<script>alert('you have been pwned')</script>") // 正常顯示 t, _ = template.New("foo").Parse(`{{define "T"}}Hello2, {{.}}!{{end}}`) t.ExecuteTemplate(w, "T", template.HTML("<script>alert('you have been pwned')</script>")) } func main() { http.HandleFunc("/escape", escape) // 設(shè)置轉(zhuǎn)義 err := http.ListenAndServe(":9090", nil) // 設(shè)置監(jiān)聽(tīng)的端口 if err != nil { log.Fatal("ListenAndServe: ", err) } }
2 測(cè)試結(jié)果
3 說(shuō)明
當(dāng)使用 html/template 這個(gè)包時(shí),如果使用 template.HTML 函數(shù),也可以正常顯示,不使用 template.HTML 函數(shù),轉(zhuǎn)義顯示。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“go中如何實(shí)現(xiàn)web預(yù)防跨站腳本”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。