Search code examples
sqlsecuritysql-injection

What this mean " Incorrectly Handled Query Assembly " in SQL injection?


im reading book is about " SQL-injection " so i defaced a title "Incorrectly Handled Query Assembly" what does this mean? and can you give me a example code ? Thanks.


Solution

  • I think you might have understood SQL injection. Incorrectly Handled Query Assembly seems to mean incorrect construction of query string.

    Think of a simple scenario where you have written a query to list the whole details of the (logged in) user. Let us

    String part1="SELECT * FROM TRANSACTIONS WHERE TAG=' ";
    part2=" ' AND ID=' ";
    part3=" ';";
    //Constructing query with user inputted tag and user ID
    String query=part1+ user_entered_tag + part2 + user_id + part3;
    //This is an unsafe construction of query.
    

    If the user enters tag like this:

    Abc' OR '2'='2' OR '1'='1

    The query will become like this:

    SELECT * FROM TRANSACTIONS WHERE TAG='Abc' OR '2'='2' OR '1'='1' AND ID='544678';
    

    If the query is then executed, all the transactions will be fetched.

    Thus unauthorized person will have access to data. This happens because a loophole is left in the construction of query. The developer should avoid such injection by adding type checking or using built-in features like prepared statements .