This commit fixes a possible SQL injection via policy-names. Policy names are now added to the policy-clause via bound parameters (the placeholders are `:policy_annotation_tag` and `:active_policies`. Policy names and policy annotation tags are now restricted. They can not contain the following characters: `"`, `'`, `;`, `\`.
130 lines
4.4 KiB
Python
130 lines
4.4 KiB
Python
import pytest
|
|
|
|
from dump_things_service import (
|
|
HTTP_200_OK,
|
|
HTTP_400_BAD_REQUEST,
|
|
)
|
|
from dump_things_service.backends.sqlite import _SQLiteBackend
|
|
from dump_things_service.tests.fixtures import test_schema_location
|
|
|
|
|
|
def test_policy_clause():
|
|
for default_policy, active_policies, result in (
|
|
('p1', {}, ('1=0', ())),
|
|
(
|
|
'p1',
|
|
{'p1', 'p2'},
|
|
(
|
|
"json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') is NULL or json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') in :active_policies",
|
|
('policy_annotation_tag', 'active_policies'),
|
|
),
|
|
),
|
|
(
|
|
'p1',
|
|
{'p2'},
|
|
(
|
|
"json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') in :active_policies",
|
|
('policy_annotation_tag', 'active_policies'),
|
|
),
|
|
),
|
|
(
|
|
None,
|
|
{'p1'},
|
|
(
|
|
"json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') is NULL or json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') in :active_policies",
|
|
('policy_annotation_tag', 'active_policies'),
|
|
),
|
|
),
|
|
(
|
|
None,
|
|
{},
|
|
(
|
|
"json_extract(object, '$.annotations.\"' || :policy_annotation_tag || '\"') is NULL",
|
|
('policy_annotation_tag',),
|
|
),
|
|
),
|
|
):
|
|
r = _SQLiteBackend._get_policy_clause(
|
|
policy_annotation_tag='tag',
|
|
default_policy=default_policy,
|
|
active_policies=active_policies,
|
|
)
|
|
assert r == result, f"Expected: '{result}', got: '{r}'"
|
|
|
|
r = _SQLiteBackend._get_policy_clause(
|
|
policy_annotation_tag=None,
|
|
default_policy=default_policy,
|
|
active_policies=active_policies,
|
|
)
|
|
assert r == ('1=1', ()), f"Expected: ('1=1', ()), got: {r}"
|
|
|
|
|
|
def test_policy_functionality(fastapi_client_simple):
|
|
test_client, _, _ = fastapi_client_simple
|
|
|
|
# Check that policy-less users can not access any record if a default
|
|
# policy is present.
|
|
response = test_client.get(
|
|
'/collection_8/records/Thing',
|
|
headers={'x-dumpthings-token': 'token-8'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
json_result = response.json()
|
|
assert json_result == []
|
|
|
|
# Check that users with default policy can access all records with no
|
|
# policy annotation or with the default policy annotation, but not records
|
|
# with other policy annotations.
|
|
response = test_client.get(
|
|
'/collection_8/records/Person',
|
|
headers={'x-dumpthings-token': 'token-8-default-policy'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
json_result = response.json()
|
|
assert len(json_result) == 2
|
|
|
|
# Check that users without default policy can access records that have
|
|
# a different policy
|
|
response = test_client.get(
|
|
'/collection_8/records/Thing',
|
|
headers={'x-dumpthings-token': 'token-8-policy-1'},
|
|
)
|
|
assert response.status_code == HTTP_200_OK
|
|
json_result = response.json()
|
|
assert len(json_result) == 1
|
|
|
|
|
|
# The following will fail until the collection-creation code checks for
|
|
# non-existing auth sources in the policy mapping.
|
|
@pytest.mark.xfail
|
|
def test_policy_mapping_errors(fastapi_client_simple):
|
|
test_client, _, admin_token = fastapi_client_simple
|
|
|
|
collection_request = {
|
|
'name': 'collection_8',
|
|
'default_token': 'test_default_token',
|
|
'curated': 'curated/collection_8',
|
|
'schema': test_schema_location,
|
|
'incoming': 'incoming/collection_8',
|
|
'backend': {
|
|
'type': 'sqlite',
|
|
},
|
|
'policy_annotation_tag': 'https://example.org/dumpthings/policy_set',
|
|
'default_policy': 'p1',
|
|
'policy_mapping': [
|
|
{
|
|
'auth_source': 'xyz-something-not-existing',
|
|
'user_id': 'user_x',
|
|
'authorized_by': ['p1', 'p2'],
|
|
},
|
|
],
|
|
}
|
|
|
|
# Check that mappings to non-existing auth sources are rejected with a
|
|
# correct error message.
|
|
response = test_client.put(
|
|
'/collections',
|
|
headers={'x-dumpthings-token': admin_token},
|
|
json=collection_request,
|
|
)
|
|
assert response.status_code == HTTP_400_BAD_REQUEST
|