Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is sqlite3_bind_text sufficient to prevent SQL injection on the iPhone

Given the statement:

const char *sql = "INSERT INTO FooTable (barStr) VALUES (?)";

is the following use of sqlite3_bind_text (and related sqlite3_bind_* functions) sufficient to prevent SQL injection attacks?

sqlite3 *db; 
sqlite3_stmt *dbps;

int dbrc = sqlite3_open([dbFilePath UTF8String], &db); 
if (dbrc) { 
    // handle error
    return; 
} 

dbrc = sqlite3_prepare_v2 (db, sql, -1, &dbps, NULL);

sqlite3_bind_text(dbps, 1, [userContent UTF8String], -1, SQLITE_TRANSIENT);

dbrc = sqlite3_step(dbps);
if (SQLITE_DONE != dbrc) {
    // handle error
}

sqlite3_finalize (dbps); 
sqlite3_close(db); 
like image 544
Bradley Dwyer Avatar asked May 23 '09 09:05

Bradley Dwyer


People also ask

Which methods can be used to avoid SQL injection?

The only sure way to prevent SQL Injection attacks is input validation and parametrized queries including prepared statements. The application code should never use the input directly. The developer must sanitize all input, not only web form inputs such as login forms.

Does Cloudflare block SQL injection?

Cloudflare's Web Application Firewall (WAF) protects your website from SQL injection, cross-site scripting (XSS) and zero-day attacks, including OWASP-identified vulnerabilities and threats targeting the application layer.


1 Answers

Yes, if you only pass the user supplied data to sqlite3_bind_* functions, then you are safe from SQL injection attacks (these attacks assume that you dynamically build your query string and don't quote/escape the user supplied data correctly).

like image 92
cmeerw Avatar answered Oct 21 '22 04:10

cmeerw