Skip to content

Commit e2873e2

Browse files
author
gw
authored
sqlalchemy-oso: handle get_checked_permissions() is None (#1440)
* sqlalchemy no checked permissions bug fix * less specific allow rule * fmt * changelog
1 parent 581d063 commit e2873e2

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

docs/content/any/project/changelogs/NEXT.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ oso.query('f(x)', { bindings });
4949
- Thanks to [`@Kn99HN`](https://github.com/Kn99HN) for adding the
5050
`acceptExpression` query flag to the Node.js lib!
5151

52+
## `sqlalchemy-oso` `NEW_VERSION`
53+
54+
### Other bugs & improvements
55+
56+
- `scoped_session` now correctly handles a `get_checked_permission` callback that
57+
returns `None`.
58+
5259
## `RELEASED_PACKAGE_1` NEW_VERSION
5360

5461
### LANGUAGE (e.g., 'Core' or 'Python' or 'Node.js')
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
black==21.5b0
1+
black~=21.12b
22
flake8==3.9.2
33
mypy==0.812
44
sqlalchemy-stubs==0.4
5-
tox==3.23.1
5+
tox==3.23.1

languages/python/sqlalchemy-oso/sqlalchemy_oso/session.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ def scoped_session(
176176
scopefunc = scopefunc or (lambda: None)
177177

178178
def _scopefunc():
179-
checked_permissions = frozenset(get_checked_permissions().items())
180-
return (get_oso(), checked_permissions, get_user(), scopefunc())
179+
perms = get_checked_permissions()
180+
perms = frozenset() if perms is None else frozenset(perms.items())
181+
return (get_oso(), perms, get_user(), scopefunc())
181182

182183
factory = authorized_sessionmaker(
183184
get_oso, get_user, get_checked_permissions, **kwargs

languages/python/sqlalchemy-oso/tests/test_sqlalchemy.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ def test_authorized_session_relationship(engine, oso, fixture_data):
215215
assert post_7.created_by is None
216216

217217

218+
def test_scoped_session_with_no_checked_permissions(engine, oso, fixture_data):
219+
# the policy denies all requests
220+
oso.load_str("allow(_, _, _) if false;")
221+
# but passing None skips authorization
222+
session = scoped_session(lambda: oso, lambda: "user", lambda: None)
223+
session.configure(bind=engine)
224+
posts = session.query(Post)
225+
# check that any posts are allowed
226+
assert posts.count()
227+
228+
218229
def test_scoped_session_relationship(engine, oso, fixture_data):
219230
oso.load_str(
220231
"""allow("user", "read", post: Post) if post.id = 1;

0 commit comments

Comments
 (0)