Skip to content

Add setAuthorizer to Pdo\Sqlite #17321

Description

@stancl

Description

Since we now have separate classes for different PDO types, could we get Pdo\Sqlite::setAuthorizer()?

The SQLite3 extension supports it: https://www.php.net/manual/en/sqlite3.setauthorizer.php

The C interface is pretty straightforward: https://www.sqlite.org/c3ref/set_authorizer.html, you get the action as an int, some string parameters, and return one of these values:

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

The SQLite3 extension's setAuthorizer() has a bool return type so I'm assuming it's SQLITE_OK and SQLITE_DENY, since SQLITE_IGNORE probably has pretty niche use cases.

Activity

  1. cmb69 commented on Jan 2, 2025

    @cmb69
    Member

    If I remember correctly, the only hurdle to also have this for PDO_Sqlite was indeed the lack of driver specific subclasses. Since that has changed, I think this is a reasonbable feature request. PR welcome!

  2. stancl commented on Jan 4, 2025

    @stancl
    ContributorAuthor

    For some context, I had a use case where setting the authorizer would be helpful in a package I maintain. For now I went with bundling compiled dll/so/dylib files with the composer package. This feature would simplify that a lot.

    Also, one thing I was trying to do before resorting to bundling compiled libraries with my package was seeing if I could use FFI to achieve this. I'm not sure if this is something you'd want, but imo it'd be very helpful if Pdo\Sqlite also exposed the sqlite3* pointer as FFI\CData.

    Then you could just use FFI to do these things at the application level. libsqlite3 has pretty simple headers so I think it's a good fit for FFI.

    One way to do this now is to load a SQLite extension which creates a function that returns the pointer as a u64, which then lets you do whatever you want using FFI, but the downside is you still need the actual compiled extension to create the function.

  3. self-assigned this
    on Feb 23, 2025
  4. added 2 commits that reference this issue on Apr 17, 2025
  5. stancl commented on Apr 18, 2025

    @stancl
    ContributorAuthor

    @nielsdos Thanks for the work on this! What do you think about the suggestion above for getting a SQLite pointer for FFI purposes? If you think the suggestion is reasonable, should I open a separate issue for discussing that?

  6. ndossche commented on Apr 18, 2025

    @ndossche
    Member

    What do you think about the suggestion above for getting a SQLite pointer for FFI purposes? If you think the suggestion is reasonable, should I open a separate issue for discussing that?

    It's not something I'm a fan of as it encourages hacks. Normally we don't expose such internal pointers to PHP code.

  7. stancl commented on Apr 18, 2025

    @stancl
    ContributorAuthor

    It does feel a bit weird for PHP to expose a pointer, but at the same time with FFI being a thing, getting access to sqlite3* seems fine (and I'd say it prevents other, more hacky approaches).

  8. ndossche commented on Apr 18, 2025

    @ndossche
    Member

    It does feel a bit weird for PHP to expose a pointer, but at the same time with FFI being a thing, getting access to sqlite3* seems fine (and I'd say it prevents other, more hacky approaches).

    The thing is that FFI is contained within its own thing, very much in the "you know what you're doing" territory. Exposing raw memory addresses to PHP can also be abused to mount an info leak attack in presence of a bug.
    Is there anything else you'd like to access the sqlite* for? If so we should just add the functionality to pdo.

  9. bohwaz commented on Mar 1, 2026

    @bohwaz
    Contributor

    Great work, thanks!

    It seems to be missing all the constants though no?

    Currently this is hard to use as the authorizer constants are only defined in SQLite3:

        /* Constants for authorizer actions */
    
        /** @cvalue SQLITE_CREATE_INDEX */
        public const int CREATE_INDEX = UNKNOWN;
        /** @cvalue SQLITE_CREATE_TABLE */
        public const int CREATE_TABLE = UNKNOWN;
        /** @cvalue SQLITE_CREATE_TEMP_INDEX */
        public const int CREATE_TEMP_INDEX = UNKNOWN;
    …
    

    Could you add the authorizer constants to the PDO\SQLite class? Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions