Small. Fast. Reliable.
Choose any three.

About Sitemap Documentation Download License News Developers Support

## SQLite C Interface

## Compile-Time Authorization Callbacks

>     int sqlite3_set_authorizer(
>       sqlite3*,
>       int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
>       void *pUserData
>     );

This routine registers a authorizer callback with a particular database connection, supplied in the first argument. The authorizer callback is invoked as SQL statements are being compiled by sqlite3_prepare() or its variants sqlite3_prepare_v2(), sqlite3_prepare16() and sqlite3_prepare16_v2(). At various points during the compilation process, as logic is being created to perform various actions, the authorizer callback is invoked to see if those actions are allowed. The authorizer callback should return SQLITE_OK to allow the action, SQLITE_IGNORE to disallow the specific action but allow the SQL statement to continue to be compiled, or SQLITE_DENY to cause the entire SQL statement to be rejected with an error. If the authorizer callback returns any value other than SQLITE_IGNORE, SQLITE_OK, or SQLITE_DENY then sqlite3_prepare_v2() or equivalent call that triggered the authorizer will fail with an error message.

When the callback returns SQLITE_OK, that means the operation requested is ok. When the callback returns SQLITE_DENY, the sqlite3_prepare_v2() or equivalent call that triggered the authorizer will fail with an error message explaining that access is denied. If the authorizer code is SQLITE_READ and the callback returns SQLITE_IGNORE then the prepared statement is constructed to insert a NULL value in place of the table column that would have been read if SQLITE_OK had been returned. The SQLITE_IGNORE return can be used to deny an untrusted user access to individual columns of a table.

The first parameter to the authorizer callback is a copy of the third parameter to the sqlite3_set_authorizer() interface. The second parameter to the callback is an integer action code that specifies the particular action to be authorized. The third through sixth parameters to the callback are zero-terminated strings that contain additional details about the action to be authorized.

An authorizer is used when preparing SQL statements from an untrusted source, to ensure that the SQL statements do not try to access data that they are not allowed to see, or that they do not try to execute malicious statements that damage the database. For example, an application may allow a user to enter arbitrary SQL queries for evaluation by a database. But the application does not want the user to be able to make arbitrary changes to the database. An authorizer could then be put in place while the user-entered SQL is being prepared that disallows everything except SELECT statements.

Only a single authorizer can be in place on a database connection at a time. Each call to sqlite3_set_authorizer overrides the previous call. Disable the authorizer by installing a NULL callback. The authorizer is disabled by default.

Note that the authorizer callback is invoked only during sqlite3_prepare() or its variants. Authorization is not performed during statement evaluation in sqlite3_step().

### Invariants:

F12501 | The sqlite3_set_authorizer(D,...) interface registers a authorizer callback with database connection D.
F12502 | The authorizer callback is invoked as SQL statements are being compiled
F12503 | If the authorizer callback returns any value other than SQLITE_IGNORE, SQLITE_OK, or SQLITE_DENY then the sqlite3_prepare_v2() or equivalent call that caused the authorizer callback to run shall fail with an SQLITE_ERROR error code and an appropriate error message.
F12504 | When the authorizer callback returns SQLITE_OK, the operation described is coded normally.
F12505 | When the authorizer callback returns SQLITE_DENY, the sqlite3_prepare_v2() or equivalent call that caused the authorizer callback to run shall fail with an SQLITE_ERROR error code and an error message explaining that access is denied.
F12506 | If the authorizer code (the 2nd parameter to the authorizer callback) is SQLITE_READ and the authorizer callback returns SQLITE_IGNORE then the prepared statement is constructed to insert a NULL value in place of the table column that would have been read if SQLITE_OK had been returned.
F12507 | If the authorizer code (the 2nd parameter to the authorizer callback) is anything other than SQLITE_READ, then a return of SQLITE_IGNORE has the same effect as SQLITE_DENY.
F12510 | The first parameter to the authorizer callback is a copy of the third parameter to the sqlite3_set_authorizer() interface.
F12511 | The second parameter to the callback is an integer action code that specifies the particular action to be authorized.
F12512 | The third through sixth parameters to the callback are zero-terminated strings that contain additional details about the action to be authorized.
F12520 | Each call to sqlite3_set_authorizer() overrides the any previously installed authorizer.
F12521 | A NULL authorizer means that no authorization callback is invoked.
F12522 | The default authorizer is NULL.

See also lists of Objects, Constants, and Functions.

This page last modified 2008/01/31 20:37:13 UTC