-
Notifications
You must be signed in to change notification settings - Fork 4
security(dci): restrict Data API pull to non-sensitive provider variables #257
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
Open
gonzalesedwin1123
wants to merge
2
commits into
19.0
Choose a base branch
from
security-dci-crvs-cache
base: 19.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| # Part of OpenSPP. See LICENSE file for full copyright and licensing details. | ||
|
|
||
| from . import test_data_api | ||
| from . import test_data_api_pull_security |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| # Part of OpenSPP. See LICENSE file for full copyright and licensing details. | ||
| """Security tests for the Data API pull/list endpoints. | ||
|
|
||
| These drive the async router functions to completion via asyncio.run so the | ||
| endpoint bodies actually execute (a bare ``async def test_*`` on a unittest | ||
| TestCase is collected but never awaited, i.e. a silent no-op). They verify the | ||
| allowlist guard added to /Data/pull and /Data/variables. | ||
| """ | ||
|
|
||
| import asyncio | ||
|
|
||
| from odoo.tests.common import TransactionCase | ||
|
|
||
| from fastapi import HTTPException, status | ||
|
|
||
|
|
||
| class TestDataApiPullSecurity(TransactionCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| super().setUpClass() | ||
|
|
||
| org_type = cls.env.ref("spp_consent.org_type_government", raise_if_not_found=False) | ||
| if not org_type: | ||
| org_type = cls.env["spp.consent.org.type"].search([("code", "=", "government")], limit=1) | ||
|
|
||
| cls.partner = cls.env["res.partner"].create({"name": "Subject 1", "ref": "EDU-001"}) | ||
|
|
||
| cls.provider = cls.env["spp.data.provider"].create( | ||
| {"name": "Edu Ministry", "code": "edu_sec_t", "active": True} | ||
| ) | ||
| cls.ext_var = cls.env["spp.cel.variable"].create( | ||
| { | ||
| "name": "School Attendance Sec", | ||
| "cel_accessor": "school_attendance_sec", | ||
| "source_type": "external", | ||
| "external_provider_id": cls.provider.id, | ||
| "value_type": "number", | ||
| "cache_strategy": "ttl", | ||
| } | ||
| ) | ||
| cls.computed_var = cls.env["spp.cel.variable"].create( | ||
| { | ||
| "name": "Computed Sec", | ||
| "cel_accessor": "computed_sec", | ||
| "source_type": "computed", | ||
| "value_type": "number", | ||
| "cache_strategy": "none", | ||
| } | ||
| ) | ||
|
|
||
| api_partner = cls.env["res.partner"].create({"name": "API Partner Sec"}) | ||
| cls.api_client = cls.env["spp.api.client"].create( | ||
| { | ||
| "name": "Sec Client", | ||
| "partner_id": api_partner.id, | ||
| "organization_type_id": org_type.id, | ||
| } | ||
| ) | ||
| cls.env["spp.api.client.scope"].create({"client_id": cls.api_client.id, "resource": "data", "action": "read"}) | ||
|
|
||
| def _run(self, coro): | ||
| return asyncio.run(coro) | ||
|
|
||
| def _cache(self, variable_name, source_type="external", company=None): | ||
| self.env["spp.data.value"].create( | ||
| { | ||
| "company_id": (company or self.env.company).id, | ||
| "variable_name": variable_name, | ||
| "subject_id": self.partner.id, | ||
| "period_key": "current", | ||
| "value_json": {"value": 0.95}, | ||
| "value_type": "number", | ||
| "source_type": source_type, | ||
| } | ||
| ) | ||
|
|
||
| # --- pull --------------------------------------------------------------- | ||
|
|
||
| def test_pull_ordinary_external_variable_succeeds(self): | ||
| from ..routers.data import pull_values | ||
|
|
||
| self._cache("school_attendance_sec") | ||
| result = self._run( | ||
| pull_values( | ||
| env=self.env, | ||
| api_client=self.api_client, | ||
| variable="school_attendance_sec", | ||
| subject_external_ids="EDU-001", | ||
| period_key="current", | ||
| _count=100, | ||
| ) | ||
| ) | ||
| self.assertEqual(result.total, 1) | ||
| self.assertEqual(result.items[0].value, 0.95) | ||
|
|
||
| def test_pull_unknown_variable_rejected(self): | ||
| from ..routers.data import pull_values | ||
|
|
||
| with self.assertRaises(HTTPException) as cm: | ||
| self._run( | ||
| pull_values( | ||
| env=self.env, | ||
| api_client=self.api_client, | ||
| variable="does_not_exist", | ||
| subject_external_ids="EDU-001", | ||
| period_key="current", | ||
| _count=100, | ||
| ) | ||
| ) | ||
| self.assertEqual(cm.exception.status_code, status.HTTP_403_FORBIDDEN) | ||
|
|
||
| def test_pull_non_pullable_variable_rejected(self): | ||
| from ..routers.data import pull_values | ||
|
|
||
| # computed_var has a cached row but is not pullable -> 403 before read. | ||
| self._cache("computed_sec", source_type="computed") | ||
| with self.assertRaises(HTTPException) as cm: | ||
| self._run( | ||
| pull_values( | ||
| env=self.env, | ||
| api_client=self.api_client, | ||
| variable="computed_sec", | ||
| subject_external_ids="EDU-001", | ||
| period_key="current", | ||
| _count=100, | ||
| ) | ||
| ) | ||
| self.assertEqual(cm.exception.status_code, status.HTTP_403_FORBIDDEN) | ||
|
|
||
| def test_pull_other_company_value_not_returned(self): | ||
| from ..routers.data import pull_values | ||
|
|
||
| other = self.env["res.company"].create({"name": "Other Co Sec"}) | ||
| self._cache("school_attendance_sec", company=other) | ||
| result = self._run( | ||
| pull_values( | ||
| env=self.env, | ||
| api_client=self.api_client, | ||
| variable="school_attendance_sec", | ||
| subject_external_ids="EDU-001", | ||
| period_key="current", | ||
| _count=100, | ||
| ) | ||
| ) | ||
| self.assertEqual(result.total, 0) | ||
|
|
||
| # --- list --------------------------------------------------------------- | ||
|
|
||
| def test_list_variables_excludes_non_external(self): | ||
| from ..routers.data import list_variables | ||
|
|
||
| result = self._run( | ||
| list_variables( | ||
| env=self.env, | ||
| api_client=self.api_client, | ||
| provider_code=None, | ||
| source_type=None, | ||
| _count=500, | ||
| _last_id=None, | ||
| ) | ||
| ) | ||
| accessors = [v.cel_accessor for v in result.items] | ||
| self.assertIn("school_attendance_sec", accessors) | ||
| self.assertNotIn("computed_sec", accessors) | ||
| # total is computed on the same filtered domain, so it matches the items. | ||
| self.assertEqual(result.total, len(result.items)) | ||
| self.assertTrue(all(v.source_type == "external" for v in result.items)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Part of OpenSPP. See LICENSE file for full copyright and licensing details. | ||
| """Tests for spp.cel.variable.is_data_api_pullable (base rule). | ||
|
|
||
| Only ordinary external-provider variables may be exposed through the generic | ||
| external Data API; computed/scoring/aggregate variables must not be. | ||
| """ | ||
|
|
||
| from odoo.tests import TransactionCase, tagged | ||
|
|
||
|
|
||
| @tagged("post_install", "-at_install") | ||
| class TestDataApiPullable(TransactionCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| super().setUpClass() | ||
| cls.provider = cls.env["spp.data.provider"].create({"name": "Edu", "code": "edu_pullable_t"}) | ||
|
|
||
| def _var(self, accessor, source_type, provider=True): | ||
| return self.env["spp.cel.variable"].create( | ||
| { | ||
| "name": f"v_{accessor}", | ||
| "cel_accessor": accessor, | ||
| "source_type": source_type, | ||
| "external_provider_id": self.provider.id if provider else False, | ||
| "value_type": "number", | ||
| } | ||
| ) | ||
|
|
||
| def test_external_with_provider_is_pullable(self): | ||
| self.assertTrue(self._var("pa_ext", "external").is_data_api_pullable()) | ||
|
|
||
| def test_external_without_provider_is_not_pullable(self): | ||
| self.assertFalse(self._var("pa_ext_np", "external", provider=False).is_data_api_pullable()) | ||
|
|
||
| def test_computed_is_not_pullable(self): | ||
| self.assertFalse(self._var("pa_computed", "computed", provider=False).is_data_api_pullable()) | ||
|
|
||
| def test_scoring_is_not_pullable(self): | ||
| # Scoring values (e.g. PMT) land in the same cache but must not be pulled. | ||
| self.assertFalse(self._var("pa_scoring", "scoring", provider=False).is_data_api_pullable()) | ||
|
|
||
| def test_pullable_domain_selects_only_external_provider(self): | ||
| Variable = self.env["spp.cel.variable"] | ||
| ext = self._var("pa_dom_ext", "external") | ||
| computed = self._var("pa_dom_computed", "computed", provider=False) | ||
| scoring = self._var("pa_dom_scoring", "scoring", provider=False) | ||
|
|
||
| matched = Variable.search(Variable._get_data_api_pullable_domain()) | ||
| self.assertIn(ext, matched) | ||
| self.assertNotIn(computed, matched) | ||
| self.assertNotIn(scoring, matched) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.