posted 1st August 2008 09:41
SQLite is a mostly ACID-compliant relational database management system contained in a relatively small (~500kB) C programming library. The Adobe AIR runtime includes the SQLite embedded database for use by Adobe AIR applications. This allows applications to run and store data locally and or synchronise the datastore with online repositories.
Applications that depend on user input to create a SQL statement — concatenating the user input to the SQL query — can become vulnerable to SQL Injection attacks, much like those common to web applications.
SQL Injection is a technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed.
Fortunately, there is a simple solution to the problem: use parameterised SQL Statements. Parameterised statements not only make your applications more secure and run more efficiently, but they also enable you to use objects, rather than literal values, in your queries. SQL injection can’t happen because the parameter values are treated explicitly as substituted values, rather than becoming part of the literal statement text.
Parameters in a SQL statement can be either named or unnamed. Below are examples of both types of statement in ActionScript and JavaScript.
A named parameter has a specific name that is used to match the parameter value to its placeholder location in the SQL statement text. A parameter name consists of the colon (:) or an at (@) character followed by the variable’s name:
ActionScript 3
var conn:SQLConnection = new SQLConnection(); var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = conn; stmt.text = "INSERT INTO user VALUES(@title, @firstname, @lastname)"; stmt.parameters["@title"] = "Mr"; stmt.parameters["@firstname"] = "Simon"; stmt.parameters["@lastname"] = "Whatley"; stmt.execute()
JavaScript
var conn = new air.SQLConnection(); var stmt = new air.SQLStatement(); stmt.sqlConnection = conn; stmt.text = "INSERT INTO user VALUES(@title, @firstname, @lastname)"; stmt.parameters["@title"] = "Mr"; stmt.parameters["@firstname"] = "Simon"; stmt.parameters["@lastname"] = "Whatley"; stmt.execute()
As an alternative to using explicit named parameters, you can also use implicit unnamed parameters. To use an unnamed parameter you simply designate a parameter in the SQL statement using a question mark (?) character. Each parameter is assigned a numeric index, according to the order in which the parameters appear in the SQL statement, starting with index 0 (zero) for the first parameter.
ActionScript 3
var conn:SQLConnection = new SQLConnection(); var stmt:SQLStatement = new SQLStatement(); stmt.sqlConnection = conn; stmt.text = "INSERT INTO address VALUES(?, ?, ?, ?)"; stmt.parameters[0] = "123 Main Street"; stmt.parameters[1] = "Sometown"; stmt.parameters[2] = "12345"; stmt.parameters[3] = "USA"; stmt.execute()
JavaScript
var conn = new air.SQLConnection(); var stmt = new air.SQLStatement(); stmt.sqlConnection = conn; stmt.text = "INSERT INTO address VALUES(?, ?, ?, ?)"; stmt.parameters[0] = "123 Main Street"; stmt.parameters[1] = "Sometown"; stmt.parameters[2] = "12345"; stmt.parameters[3] = "USA"; stmt.execute()
Note: Use clearParameters() to empty the statement parameters array.