Skip to content

Commit

Permalink
[IMP] auth_oidc: test all possible safe_eval exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
OdyX committed Feb 12, 2025
1 parent cc0e726 commit 51f452f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 6 additions & 2 deletions auth_oidc/models/auth_oauth_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ def _check_expression(self):
for this in self:
try:
this._eval_expression(self.env.user, {})
except (AttributeError, KeyError, NameError) as e:
raise exceptions.ValidationError("\n".join(e.args)) from e
except (AttributeError, KeyError, NameError, ValueError) as e:
# AttributeError: user object can be accessed via attributes: user.email
# KeyError: token is a dict of dicts
# NameError: only user and token can be used
# ValueError: for inexistant variables or attributes
raise exceptions.ValidationError(e) from e

def _eval_expression(self, user, token):
self.ensure_one()
Expand Down
25 changes: 24 additions & 1 deletion auth_oidc/tests/test_auth_oidc_auth_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from jose.utils import long_to_base64

import odoo
from odoo.exceptions import AccessDenied
from odoo.exceptions import AccessDenied, ValidationError
from odoo.tests import common

from odoo.addons.website.tools import MockRequest as _MockRequest
Expand Down Expand Up @@ -340,3 +340,26 @@ def test_group_expressions_with_token(self):
self.assertFalse(
group_line._eval_expression(self.env.user, {"groups": ["group-c"]})
)

def test_group_expression_with_inexistant_variable(self):
"""Test that group expression with inexistant variable fails"""
group_line = self.env.ref("auth_oidc.local_keycloak").group_line_ids[:1]

with self.assertRaises(ValidationError):
group_line.expression = "inexistant_variable"

def test_group_expression_with_inexistant_attribute(self):
"""Test that group expression with inexistant attribute (on user) fails"""
group_line = self.env.ref("auth_oidc.local_keycloak").group_line_ids[:1]

with self.assertRaises(ValidationError):
group_line.expression = "user.not_an_attribute"

def test_realistic_group_expression(self):
"""Test that group expression with inexistant attribute (on user) fails"""
group_line = self.env.ref("auth_oidc.local_keycloak").group_line_ids[:1]

group_line.expression = "user.email == token['mail']"
self.assertTrue(
group_line._eval_expression(self.env.user, {"mail": self.env.user.email})
)

0 comments on commit 51f452f

Please sign in to comment.