Skip to content

Fix #81742: open_basedir bypass in SQLite3 by using file URI #10018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

cmb69
Copy link
Member

@cmb69 cmb69 commented Nov 29, 2022

A previous fix[1] was not sufficient to catch all potential file URIs, because the patch did not cater to URL encoding. Properly parsing and decoding the URI may yield a different result than the handling of SQLite3, so we play it safe, and reject any file URIs if open_basedir is configured.

[1] https://bugs.php.net/bug.php?id=77967


Obviously, this can break existing code using (maybe even hard-coded) file URIs. As such, we should consider to postpone this fix to PHP-8.2.

cc @bohwaz

Verified

This commit was signed with the committer’s verified signature. The key has expired.
cmb69 Christoph M. Becker
A previous fix[1] was not sufficient to catch all potential file URIs,
because the patch did not cater to URL encoding.  Properly parsing and
decoding the URI may yield a different result than the handling of
SQLite3, so we play it safe, and reject any file URIs if open_basedir
is configured.

[1] <https://bugs.php.net/bug.php?id=77967>
@bohwaz
Copy link
Contributor

bohwaz commented Nov 29, 2022

Thanks for the notification.

I had trouble understanding how this can affect the sqlite3 extension as it does not support URIs by default. It is in PDO SQLite (see a8dd009 ), but not in SQLite3. A SQLITE_OPEN_URI is not set in the opening flags of the database, and there is no SQLITE3_OPEN_URI constant defined: there is no way to have URIs enabled.

So doing new SQLite3('file:/tmp/a') results in an error.

But it seems that if you compile SQLite3 with SQLITE_USE_URI then URIs are then allowed inside SQL statements by default.

So yes that fix is good, and is consistent with that exists in PDO.

I think we should merge this fix in the current versions, and not wait for 8.2, as:

  • URI support in SQLite3 extension was never documented in the first place
  • PDO SQLite extension also states that URIs and open_basedir cannot work together
  • this can lead to security issues (even though open_basedir is not a security feature, I know, it's still a useful measure against attacks)
  • this feature wasn't documented, and most likely this is not used widely

@bohwaz
Copy link
Contributor

bohwaz commented Nov 29, 2022

Maybe we should also enable SQLITE_OPEN_URI in SQLite3::open by default as well like PDO, for 8.2 or 8.3, to make it consistent? I can do a PR for that.

@cmb69
Copy link
Member Author

cmb69 commented Nov 29, 2022

Thanks for the analysis!

Maybe we should also enable SQLITE_OPEN_URI in SQLite3::open by default as well like PDO, for 8.2 or 8.3, to make it consistent? I can do a PR for that.

Yeah, that appears to be reasonable. That PR should probably target master, to have a clean cut (especially for documentation purposes).

Copy link
Member

@arnaud-lb arnaud-lb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that sqlite doesn't make it possible to use a white list approach here. The check seems to be consistent with how sqlite parses the filename, but we may be missing something, and this is not future proof.

I'm wondering if we could use a custom VFS to hook at a lower level.

Otherwise it looks reasonable.

@cmb69
Copy link
Member Author

cmb69 commented Dec 5, 2022

I'm wondering if we could use a custom VFS to hook at a lower level.

Oh, I wasn't aware of these. That might be an option, but on the other hand, it is apparently possible to specify the desired VFS as URI parameter, so that may end up in a mess.

Verified

This commit was signed with the committer’s verified signature. The key has expired.
cmb69 Christoph M. Becker
@cmb69 cmb69 closed this in 2f6b9e6 Dec 6, 2022
@cmb69 cmb69 deleted the cmb/81742 branch December 6, 2022 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants