Prevents path control and command injection

Source: Internet
Author: User

[Java] public class Test {public static void main (String [] args) {System. out. println (getSafeCommand ("abcd & efg"); System. out. println (getSafePath ("abcd/efg "));} /*** Get the safe path * @ param filePath Enter the path * @ return Safe path */public static String getSafePath (String filePath) {// return safe path StringBuffer safePath = new StringBuffer (); // safe path white list String whiteList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-= []; ',. ~! @ # $ % ^ & * () _ + \ "{}|:<>? "; Char [] safePathChars = filePath. toCharArray (); for (int I = 0, length = safePathChars. length; I <length; I ++) {int whiteListIndex = whiteList. indexOf (safePathChars [I]); if (-1 = whiteListIndex) {return safePath. toString ();} safePath. append (whiteList. charAt (whiteListIndex);} return safePath. toString ();}/*** Get the safe command * @ param command Enter the command * @ return Safe command * /Public static String getSafeCommand (String command) {// return safe command StringBuffer safeCommand = new StringBuffer (); // safe command white list String whiteList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-= [] \ ',. /~! @ # $ % ^ * () _ + \ "{}:<>? "; Char [] safeCommandChars = command. toCharArray (); for (int I = 0, length = safeCommandChars. length; I <length; I ++) {int whiteListIndex = whiteList. indexOf (safeCommandChars [I]); if (-1 = whiteListIndex) {return safeCommand. toString ();} safeCommand. append (whiteList. charAt (whiteListIndex);} return safeCommand. toString () ;}} output result: [java] abcd prevents path control: prevents path overlay, and the path cannot contain /.. /, cannot contain/\ characters in security characters to prevent command injection: prevents command batch execution, and cannot contain & |;

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.