SQL or HQL precompiled statements to prevent SQL injection, but cannot handle% and _ special characters

Source: Internet
Author: User
Tags assert sql injection

Recent projects in the rectification, the direct splicing of all the DAO layer of SQL string code, the way to use the precompiled statement. Individual by writing the DAO layer of unit test, there are the following points.

The DAO layer code is as follows

Pre-compiled Sqlpublic list<indvconfigmodel> selectconfigbysuffix (string suffix) {        string hql = "from Indvconfigmodel where configkey like '% ' | |?| | ' %'";        Return THIS.SELECTCONFIGBYHQL (HQL, New Object[]{suffix});


The unit test code and execution results are as follows:

@Testpublic void Testlike () {    list<indvconfigmodel> List = Dao.selectconfigbysuffix ("picqual");    Assert.assertequals (List.size (), 2);//1.true    list = Dao.selectconfigbysuffix ("picqua%");    Assert.assertequals (List.size (), 2);//2.true        list = Dao.selectconfigbysuffix ("Pic ' Qual");    Assert.assertequals (List.size (), 0);//3. True

1, the first assertion is true, indicating that the above practice, indeed can play the effect of fuzzy query

2. The second assertion is true, stating that% is considered a fuzzy match and is not considered a normal character by Oracle. This indicates that the precompiled statement is not able to handle special characters in the parameter values. Encountering the special characters of the% and _ This database fuzzy query requires the user to escape themselves.

3. The third assertion does not report an exception. Description: The precompiled statement has escaped the special character single quotation marks of Oracle. The single quotation mark is considered to be the query content, not the delimiter of the string.

Because SQL injection is actually the use of special character single quotes, generate or 1 = 1 in this format of SQL. Precompilation already handles single quotes, so you can prevent SQL injection

SQL or HQL precompiled statements to prevent SQL injection, but cannot handle% and _ special characters

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.