XSS vulnerability attack prevention

Source: Internet
Author: User
Tags csrf attack

Recently, in the cnode community, an article about XSS published by @ Wu Zhonghua directly led the community to initiate various attacks on cnode. Here we summarize some of the problems and solutions encountered this time.

File Upload Vulnerability

The logic for nodeclub to upload images is as follows:

 
 
  1. // File name uploaded by the user
  2. Var filename = Date. now () + '_' + file. name;
  3. // User folder
  4. Var userDir = path. join (config. upload_dir, uid );
  5. // Path for storing the final file
  6. Var savepath = path. join (userDir, filename );
  7. // Move the uploaded files from the temporary directory to the final save path
  8. Fs. rename (file. path, savepath, callback );
  9.  

It seems that there is no problem. Each uploaded file is stored in a folder named after the user UID and prefixed with the current timestamp. However, when a user maliciously constructs the input, the problem arises. When the filename of the File Uploaded by the user is/../xxx, the uploaded file will be rename outside the user folder, so that the user can replace any file on the existing system.

This vulnerability is relatively low-level, but has the most serious consequences. It directly causes the entire system to be controlled by users. The solution is also simple:

 
 
  1. Var filename = Date. now () + '_' + file. name;
  2.  
  3. Var userDir = path. join (config. upload_dir, uid );
  4.  
  5. // Obtain the absolute path to which the object is finally saved
  6. Var savepath = path. resolve (path. join (userDir, filename ));
  7. // Verify
  8. If (savepath. indexOf (path. resolve (userDir ))! = 0 ){
  9. Return res. send ({status: 'foridden '});
  10. }
  11. Fs. rename (file. path, savepath, callback );
  12.  

XSS of Rich Text Editor

XSS has been described in detail in @ Wu Zhonghua's article. In the cnode community, users are also using a rich text editor that supports markdown format to publish and reply to topics. No XSS preventive measures have been taken before, so you can directly write:

 
 
  1. <script>alert(123);</script> 
  2. <div onmouseover="alert(123)"></div> 
  3. <a href="javascript:alert(123);">123</a> 
  4.  

The content in markdown format does not have URL validity detection, so the XSS of various styles came out again:

[Xss] [1]

[Xss] [2]

! [Xss] [3]

[1]: javascript: alert (123 );

[2]: http://www.baidu.com/# "onclick = 'alert (123 )'

[3]: http://www.baidu.com/img.jpg# "onmouseover = 'alert (123 )'

In this community Application Scenario, HTML tags are introduced only for typographical operations, while other style definitions only make the entire interface messy, not to mention the potential risks of XSS vulnerabilities. Therefore, we do not need to support HTML tags for content formatting. Everything can be replaced by markdown. Then, the XSS risk caused by directly inputting HTML can be eliminated through simple and crude HTML escape.

 
 
  1. function escape(html) {  
  2. return html.replace(/&(?!\w+;)/g, '&amp;')  
  3. .replace(/</g, '&lt;')  
  4. .replace(/>/g, '&gt;')  
  5. .replace(/"/g, '&quot;');  

However, such a rough escape may cause the special characters in the code entered by the user to be escaped and cannot be correctly displayed. You must extract and save the code segment first, only escape the parts of a non-code segment. The escape function becomes like this:

 
 
  1. function escape(html) {  
  2. var codeSpan = /(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm;  
  3. var codeBlock = /(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g;  
  4. var spans = [];  
  5. var blocks = [];  
  6. var text = String(html).replace(/\r\n/g, '\n')  
  7. .replace('/\r/g', '\n');  
  8. text = '\n\n' + text + '\n\n';  
  9. texttext = text.replace(codeSpan, function(code) {  
  10. spans.push(code);  
  11. return '`span`';  
  12. });  
  13. text += '~0';  
  14. return text.replace(codeBlock, function (whole, code, nextChar) {  
  15. blocks.push(code);  
  16. return '\n\tblock' + nextChar;  
  17. })  
  18. .replace(/&(?!\w+;)/g, '&amp;')  
  19. .replace(/</g, '&lt;')  
  20. .replace(/>/g, '&gt;')  
  21. .replace(/"/g, '&quot;')  
  22. .replace(/`span`/g, function() {  
  23. return spans.shift();  
  24. })  
  25. .replace(/\n\tblock/g, function() {  
  26. return blocks.shift();  
  27. })  
  28. .replace(/~0$/,'')  
  29. .replace(/^\n\n/, '')  
  30. .replace(/\n\n$/, '');  
  31. };  
  32.  

The URL validity check or xss filtering must be performed for the <a> tag generated by markdown and the href attribute in the tag. This ensures that the HTML code generated through markdown does not have the XSS vulnerability.

Because XSS has many methods, see XSS Filter Evasion Cheat Sheet. Therefore, HTML escape is the safest, but not every place can use markdown to replace HTML code. Therefore, not every place can use HTML escape, in this case, other methods are required to filter out XSS vulnerabilities.

XSS protection can only be implemented by defining a whitelist. For example, only <p> <div> <a> label is allowed and only the href class style attribute is allowed. Then, filter each attribute that may cause XSS.

The existing XSS filter module is node-validator and js-xss written by @ Lei zongmin.

The XSS module cannot prevent arbitrary XSS attacks, but at least it can filter out most of the vulnerabilities that can be imagined. Node-validator's XSS () still has bugs. For codes in the <p on = "> </p> Format, double quotation marks are not closed, leading to HTML element leakage detection.

XSS attacks caused by the template engine

The cnode community uses ejs as the template engine. In ejs, two methods are provided to output dynamic data to the page:

<% = Data %> // output for xss Filtering

<%-Data %> // directly output without Filtering

All filters must have one premise: the values of HTML attributes in the template file must use double quotation marks. For example:

 
 
  1. ' title='<%= reply.author.name %>' /> 
  2.  

In the preceding two statements, the first sentence uses single quotes. You can cut off the src attribute by constructing an avatar_url with single quotes, and then add javascript code at will.

CSRF attack

CSRF attacks have solutions in node's web development framework connect and express. By storing a random _ csrf field in the guest session, the template engine transmits the _ csrf value to the front end when generating an HTML file, and any POST request submitted by the visitor, this field must be included for verification, ensuring that only the current user can modify the current page.

However, when the page has an XSS vulnerability, the CSRF prevention measures become a cloud. Malicious attackers can use javascript code to obtain the _ csrf value of other users, and directly simulate users' POST requests for server-side data changes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.