Skip to content

sqlite3.Connection(...) bypasses 'sqlite3.connect' audit hooks #87600

Closed

Description

@erlend-aasland
BPO 43434
Nosy @berkerpeksag, @zooba, @miss-islington, @erlend-aasland
PRs
  • bpo-43434: Move sqlite3.connect audit events to sqlite3.Connection.__init__ #25818
  • [3.9] bpo-43434: Move sqlite3.connect audit event to sqlite3.Connection.__init__ (GH-25818) #25822
  • bpo-43434: Clean up after GH-25818 #25823
  • [3.8] bpo-43434: Move sqlite3.connect audit event to sqlite3.Connection.__init__ (GH-25818) #25825
  • [3.8] bpo-43434: Move sqlite3.connect audit event to sqlite3.Connection.__init__ (GH-25818) #25826
  • Files
  • audit.py
  • patch.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-05-02.22:56:58.153>
    created_at = <Date 2021-03-08.12:53:59.903>
    labels = ['type-security', '3.8', 'library', '3.9', '3.10']
    title = "sqlite3.Connection(...) bypasses 'sqlite3.connect' audit hooks"
    updated_at = <Date 2021-05-02.22:56:58.152>
    user = 'https://github.com/erlend-aasland'

    bugs.python.org fields:

    activity = <Date 2021-05-02.22:56:58.152>
    actor = 'steve.dower'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-05-02.22:56:58.153>
    closer = 'steve.dower'
    components = ['Library (Lib)']
    creation = <Date 2021-03-08.12:53:59.903>
    creator = 'erlendaasland'
    dependencies = []
    files = ['49857', '49858']
    hgrepos = []
    issue_num = 43434
    keywords = ['patch']
    message_count = 13.0
    messages = ['388264', '392393', '392702', '392709', '392724', '392735', '392736', '392737', '392742', '392743', '392745', '392748', '392749']
    nosy_count = 4.0
    nosy_names = ['berker.peksag', 'steve.dower', 'miss-islington', 'erlendaasland']
    pr_nums = ['25818', '25822', '25823', '25825', '25826']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'security'
    url = 'https://bugs.python.org/issue43434'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    Activity

    1. erlend-aasland commented on Mar 8, 2021

      @erlend-aasland
      ContributorAuthor

      The module level connect method is guarded by PySys_Audit(), but sqlite3.Connection.__init__() is not. It is possible to bypass the module level connect() method simply by creating a new sqlite3.Connection object directly.

      Easily fixed by either moving the PySys_Audit() check to pysqlite_connection_init(), or by adding an extra check in pysqlite_connection_init().

      >>> import sqlite3, sys
      >>> def hook(s, e):
      ...     if s == 'sqlite3.connect':
      ...             raise PermissionError
      ... 
      >>> sys.addaudithook(hook)
      >>> sqlite3.connect(':memory:')
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "<stdin>", line 3, in hook
      PermissionError
      >>> sqlite3.Connection(':memory:')
      <sqlite3.Connection object at 0x7f94b0157a80>
    2. erlend-aasland commented on Apr 30, 2021

      @erlend-aasland
      ContributorAuthor

      Steve, is it worth it to improve this?

    3. zooba commented on May 2, 2021

      @zooba
      Member

      Yes, let's move it into the init function.

    4. erlend-aasland commented on May 2, 2021

      @erlend-aasland
      ContributorAuthor

      The PR accidentally fixes a bug in #69433 (bpo-43762):
      The object passed to PySys_Audit() is now the connection object, not the module object.

    5. erlend-aasland commented on May 2, 2021

      @erlend-aasland
      ContributorAuthor

      Steve, can we get this in before beta1 (bco. the bugfix)?

    6. zooba commented on May 2, 2021

      @zooba
      Member

      New changeset c96cc08 by Erlend Egeberg Aasland in branch 'master':
      bpo-43434: Move sqlite3.connect audit events to sqlite3.Connection.__init__ (GH-25818)
      c96cc08

    7. zooba commented on May 2, 2021

      @zooba
      Member

      We could get this one in after beta 1 anyway, but sure, it's in.

      The backports are going to have to be manual, I suspect...

    8. erlend-aasland commented on May 2, 2021

      @erlend-aasland
      ContributorAuthor

      Thanks! :) I'll fix the backports.

    9. zooba commented on May 2, 2021

      @zooba
      Member

      Thanks, Erlend! Appreciate how quickly you got onto that, and the quality of your work.

    10. zooba commented on May 2, 2021

      @zooba
      Member

      New changeset cbb7b9e by Erlend Egeberg Aasland in branch 'master':
      bpo-43434: Clean up sqlite3.connect() after #70004 (GH-25823)
      cbb7b9e

    11. erlend-aasland commented on May 2, 2021

      @erlend-aasland
      ContributorAuthor

      Thanks, Steve, that means a lot! Glad to help. Thank you for getting it into beta1. Having the new event out there with the wrong object passed to it would have been a tiny bit embarrassing :)

    12. zooba commented on May 2, 2021

      @zooba
      Member

      New changeset ad73d16 by Erlend Egeberg Aasland in branch '3.9':
      bpo-43434: Move sqlite3.connect audit event to sqlite3.Connection.__init__ (GH-25818)
      ad73d16

    13. zooba commented on May 2, 2021

      @zooba
      Member

      New changeset 10665ac by Erlend Egeberg Aasland in branch '3.8':
      bpo-43434: Move sqlite3.connect audit events to sqlite3.Connection.__init__ (GH-25818)
      10665ac

    14. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10 (EOL)end of life3.8 (EOL)end of life3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-securityA security issue

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions