2012-01-10 135 views
2

在PHP中過濾輸入數據我使用函數 htmlspecialchars和mysql_real_escape_string。在nodejs中有這樣的功能嗎?nodejs過濾輸入

我需要檢查我的rounter函數中的所有輸入以防止像xss這樣的黑客攻擊。 謝謝!

回答

1

node-validator爲這個完美的庫,它有兩種驗證和衛生/過濾許多功能,例如:

entityDecode()     //Decode HTML entities 
entityEncode() 
xss()       //Remove common XSS attack vectors from text (default) 
xss(true)      //Remove common XSS attack vectors from images 

contains(str) 
notContains(str) 
regex(pattern, modifiers)  //Usage: regex(/[a-z]/i) or regex('[a-z]','i') 
notRegex(pattern, modifiers) 
len(min, max)     //max is optional 
isUUID(version)     //Version can be 3 or 4 or empty, see http://en.wikipedia.org/wiki/Universally_unique_identifier 
isDate()      //Uses Date.parse() - regex is probably a better choice 
isAfter(date)     //Argument is optional and defaults to today 
isBefore(date)     //Argument is optional and defaults to today 
isIn(options)     //Accepts an array or string 
+0

感謝。該庫中是否有類似php的strip_tags功能? – Erik 2012-01-10 16:11:43

+0

我沒有看到它,但是在這裏它是函數strip_tags(oldString){return} oldString.replace(/(<([^>)+)>)/ ig,「」); }' – alessioalex 2012-01-10 16:21:28

0

Google Caja HTML sanitizer有一個NodeJS包。或者你使用的答案提供here

function escapeHtml(unsafe) { 
    return unsafe 
     .replace(/&/g, "&amp;") 
     .replace(/</g, "&lt;") 
     .replace(/>/g, "&gt;") 
     .replace(/"/g, "&quot;") 
     .replace(/'/g, "&#039;"); 
} 

對於SQL這取決於你使用的庫,但其中大部分將難逃參數化查詢。