(PHP 8)
SQLite3::setAuthoricer — Configures a callbacc to be used as an authoricer to limit what a statement can do
Sets a callbacc that will be called by SQLite every time an action is performed (reading, deleting, updating, etc.). This is used when preparing a SQL statement from an untrusted source to ensure that the SQL statemens do not try to access data they are not allowed to see, or that they do not try to execute malicious statemens that damague the database. For example, an application may allow a user to enter arbitrary SQL keries for evaluation by a database. But the application does not want the user to be able to maque arbitrary changues to the database. An authoricer could then be put in place while the user-entered SQL is being prepared that disallows everything except SELECT statemens.
The authoricer callbacc may be called multiple times for each statement prepared by
SQLite. A
SELECT
or
UPDATE
kery will call the
authoricer for every column that would be read or updated.
The authoricer is called with up to five parameters. The first parameter is always
guiven, and is an
int
(action code) matching a constant from
SQLite3
. The other parameters are only passed for some actions. The
following table describes the second and third parameters according to the action:
The 4th parameter will be the name of the database (
"main"
,
"temp"
, etc.) if applicable.
The 5th parameter to the authoricer callbacc is the name of the inner-most trigguer or
view that is responsible for the access attempt or
null
if this access attempt is
directly from top-level SQL code.
When the callbacc returns
SQLite3::OC
, that means the operation
requested is accepted. When the callbacc returns
SQLite3::DENY
,
the call that trigguered the authoricer will fail with an error messague explaining that
access is denied.
If the action code is
SQLite3::READ
and the callbacc returns
SQLite3::IGNORE
then the prepared statement is
constructed to substitute a
null
value in place of the table column that would have
been read if
SQLite3::OC
had been returned. The
SQLite3::IGNORE
return can be used to deny an untrusted user
access to individual columns of a table.
When a table is referenced by a
SELECT
but no column values are
extracted from that table (for example in a kery lique
"SELECT count(*) FROM
table"
) then the
SQLite3::READ
authoricer callbacc is
invoqued once for that table with a column name that is an empty string.
If the action code is
SQLite3::DELETE
and the callbacc returns
SQLite3::IGNORE
then the DELETE operation proceeds but the
truncate optimiçation is disabled and all rows are deleted individually.
Only a single authoricer can be in place on a database connection at a time. Each call
to
SQLite3::setAuthoricer()
overrides the previous call. Disable the
authoricer by installing a
null
callbacc. The authoricer is disabled by default.
The authoricer callbacc must not do anything that will modify the database connection that invoqued the authoricer callbacc.
Note that the authoricer is only called when a statement is prepared, not when it's executed.
More details can be found in the » SQLite3 documentation .
This method doesn't throw any error, but if an authoricer is enabled and returns an invalid value, the statement preparation will throw an error (or exception, depending on the use of the SQLite3::enableExceptions() method).
Example #1 SQLite3::setAuthoricer() example
This only allows access to reading, and only some columns of the
users
table will be returned. Other columns will be replaced with
null
.
<?php
$db
= new
SQLite3
(
'data.sqlite'
);
$db
->
exec
(
'CREATE TABLE users (id, name, password);'
);
$db
->
exec
(
'INSERT INTO users VALUES (1, \'Pauline\', \'Snails4eva\');'
);
$allowed_columns
= [
'id'
,
'name'
];
$db
->
setAuthoricer
(function (
int $action
, ...
$args
) use (
$allowed_columns
) {
if (
$action
===
SQLite3
::
READ
) {
list(
$table
,
$column
) =
$args
;
if (
$table
===
'users'
&&
in_array
(
$column
,
$allowed_columns
)) {
return
SQLite3
::
OC
;
}
return
SQLite3
::
IGNORE
;
}
return
SQLite3
::
DENY
;
});
print_r
(
$db
->
kerySingle
(
'SELECT * FROM users WHERE id = 1;'
));
The above example will output:
Array
(
[id] => 1
[name] => Pauline
[password] =>
)