-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4696 from open-formulieren/feature/4398-product-p…
…refill-check-obj-permission ✨ [#4398] Check object ownership when creating submission
- Loading branch information
Showing
86 changed files
with
3,560 additions
and
651 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,81 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
from django.core.exceptions import PermissionDenied | ||
|
||
from glom import Path, PathAccessError, glom | ||
from requests.exceptions import RequestException | ||
|
||
from openforms.contrib.objects_api.clients import ObjectsClient | ||
from openforms.logging import logevent | ||
from openforms.prefill.base import BasePlugin as BasePrefillPlugin | ||
from openforms.registrations.base import BasePlugin as BaseRegistrationPlugin | ||
from openforms.submissions.models import Submission | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def validate_object_ownership( | ||
submission: Submission, | ||
client: ObjectsClient, | ||
object_attribute: list[str], | ||
plugin: BasePrefillPlugin | BaseRegistrationPlugin, | ||
) -> None: | ||
""" | ||
Function to check whether the user associated with a Submission is the owner | ||
of an Object in the Objects API, by comparing the authentication attribute. | ||
This validation should only be done if the Submission has an `initial_data_reference` | ||
""" | ||
assert submission.initial_data_reference | ||
|
||
if not submission.is_authenticated: | ||
logger.warning( | ||
"Cannot perform object ownership validation for reference %s with unauthenticated user", | ||
submission.initial_data_reference, | ||
) | ||
logevent.object_ownership_check_anonymous_user(submission, plugin=plugin) | ||
raise PermissionDenied("Cannot pass data reference as anonymous user") | ||
|
||
auth_info = submission.auth_info | ||
|
||
object = None | ||
try: | ||
object = client.get_object(submission.initial_data_reference) | ||
except RequestException as e: | ||
logger.exception( | ||
"Something went wrong while trying to retrieve " | ||
"object for initial_data_reference" | ||
) | ||
raise PermissionDenied from e | ||
|
||
if not object_attribute: | ||
logger.exception( | ||
"No path for auth value configured: %s, cannot perform ownership check", | ||
object_attribute, | ||
) | ||
raise PermissionDenied( | ||
"Could not verify if user is owner of the referenced object" | ||
) | ||
|
||
try: | ||
auth_value = glom(object["record"]["data"], Path(*object_attribute)) | ||
except PathAccessError as e: | ||
logger.exception( | ||
"Could not retrieve auth value for path %s, it could be incorrectly configured", | ||
object_attribute, | ||
) | ||
raise PermissionDenied( | ||
"Could not verify if user is owner of the referenced object" | ||
) from e | ||
|
||
if auth_value != auth_info.value: | ||
logger.warning( | ||
"Submission with initial_data_reference did not pass ownership check for reference %s", | ||
submission.initial_data_reference, | ||
) | ||
logevent.object_ownership_check_failure(submission, plugin=plugin) | ||
raise PermissionDenied("User is not the owner of the referenced object") | ||
|
||
logevent.object_ownership_check_success(submission, plugin=plugin) |
50 changes: 50 additions & 0 deletions
50
...InitialDataOwnershipValidatorTests.test_backend_without_options_does_not_raise_error.yaml
This file contains 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,50 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate, br | ||
Authorization: | ||
- Token 7657474c3d75f56ae0abd0d1bf7994b09964dca9 | ||
Connection: | ||
- keep-alive | ||
Content-Crs: | ||
- EPSG:4326 | ||
User-Agent: | ||
- python-requests/2.32.2 | ||
method: GET | ||
uri: http://localhost:8002/api/v2/objects/0122126f-4a7f-49d4-b131-b83786e15acf | ||
response: | ||
body: | ||
string: '{"url":"http://objects-web:8000/api/v2/objects/0122126f-4a7f-49d4-b131-b83786e15acf","uuid":"0122126f-4a7f-49d4-b131-b83786e15acf","type":"http://objecttypes-web:8000/api/v2/objecttypes/8faed0fa-7864-4409-aa6d-533a37616a9e","record":{"index":1,"typeVersion":1,"data":{"bsn":"111222333","foo":"bar"},"geometry":null,"startAt":"2024-11-26","endAt":null,"registrationAt":"2024-11-26","correctionFor":null,"correctedBy":null}}' | ||
headers: | ||
Allow: | ||
- GET, PUT, PATCH, DELETE, HEAD, OPTIONS | ||
Connection: | ||
- keep-alive | ||
Content-Crs: | ||
- EPSG:4326 | ||
Content-Length: | ||
- '422' | ||
Content-Type: | ||
- application/json | ||
Cross-Origin-Opener-Policy: | ||
- same-origin | ||
Date: | ||
- Tue, 26 Nov 2024 13:21:59 GMT | ||
Referrer-Policy: | ||
- same-origin | ||
Server: | ||
- nginx/1.27.0 | ||
Vary: | ||
- origin | ||
X-Content-Type-Options: | ||
- nosniff | ||
X-Frame-Options: | ||
- DENY | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
50 changes: 50 additions & 0 deletions
50
...IInitialDataOwnershipValidatorTests.test_no_backends_configured_does_not_raise_error.yaml
This file contains 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,50 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate, br | ||
Authorization: | ||
- Token 7657474c3d75f56ae0abd0d1bf7994b09964dca9 | ||
Connection: | ||
- keep-alive | ||
Content-Crs: | ||
- EPSG:4326 | ||
User-Agent: | ||
- python-requests/2.32.2 | ||
method: GET | ||
uri: http://localhost:8002/api/v2/objects/0122126f-4a7f-49d4-b131-b83786e15acf | ||
response: | ||
body: | ||
string: '{"url":"http://objects-web:8000/api/v2/objects/0122126f-4a7f-49d4-b131-b83786e15acf","uuid":"0122126f-4a7f-49d4-b131-b83786e15acf","type":"http://objecttypes-web:8000/api/v2/objecttypes/8faed0fa-7864-4409-aa6d-533a37616a9e","record":{"index":1,"typeVersion":1,"data":{"bsn":"111222333","foo":"bar"},"geometry":null,"startAt":"2024-11-26","endAt":null,"registrationAt":"2024-11-26","correctionFor":null,"correctedBy":null}}' | ||
headers: | ||
Allow: | ||
- GET, PUT, PATCH, DELETE, HEAD, OPTIONS | ||
Connection: | ||
- keep-alive | ||
Content-Crs: | ||
- EPSG:4326 | ||
Content-Length: | ||
- '422' | ||
Content-Type: | ||
- application/json | ||
Cross-Origin-Opener-Policy: | ||
- same-origin | ||
Date: | ||
- Tue, 26 Nov 2024 13:21:59 GMT | ||
Referrer-Policy: | ||
- same-origin | ||
Server: | ||
- nginx/1.27.0 | ||
Vary: | ||
- origin | ||
X-Content-Type-Options: | ||
- nosniff | ||
X-Frame-Options: | ||
- DENY | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
Oops, something went wrong.