How do I HTML Encode all the output in a web application?

前端 未结 11 1521
暖寄归人
暖寄归人 2020-12-10 17:20

I want to prevent XSS attacks in my web application. I found that HTML Encoding the output can really prevent XSS attacks. Now the problem is that how do I HTML encode every

11条回答
  •  忘掉有多难
    2020-12-10 17:46

    there was a good essay from Joel on software (making wrong code look wrong I think, I'm on my phone otherwise I'd have a URL for you) that covered the correct use of Hungarian notation. The short version would be something like:

    Var dsFirstName, uhsFirstName : String;
    
    Begin
    
    uhsFirstName := request.queryfields.value['firstname'];
    
    dsFirstName := dsHtmlToDB(uhsFirstName);
    

    Basically prefix your variables with something like "us" for unsafe string, "ds" for database safe, "hs" for HTML safe. You only want to encode and decode where you actually need it, not everything. But by using they prefixes that infer a useful meaning looking at your code you'll see real quick if something isn't right. And you're going to need different encode/decode functions anyways.

提交回复
热议问题