aboutsummaryrefslogtreecommitdiffstats
path: root/bsfs
diff options
context:
space:
mode:
authorMatthias Baumgartner <dev@igsor.net>2023-03-02 16:40:00 +0100
committerMatthias Baumgartner <dev@igsor.net>2023-03-02 16:40:00 +0100
commit28a021483c13e974e00b6159f0653b0727df9d10 (patch)
treee5aa55337a5b2e5c5e34b7892a0cbe0997eb0da8 /bsfs
parentb66ed641d5cbb4cb83f4a571223e4d65d80ed05c (diff)
downloadbsfs-28a021483c13e974e00b6159f0653b0727df9d10.tar.gz
bsfs-28a021483c13e974e00b6159f0653b0727df9d10.tar.bz2
bsfs-28a021483c13e974e00b6159f0653b0727df9d10.zip
prohibit certain characters in URI and ensure URIs in bsfs.graph
Diffstat (limited to 'bsfs')
-rw-r--r--bsfs/graph/nodes.py5
-rw-r--r--bsfs/schema/types.py2
-rw-r--r--bsfs/triple_store/sparql/parse_filter.py6
-rw-r--r--bsfs/triple_store/sparql/sparql.py10
-rw-r--r--bsfs/utils/uri.py8
5 files changed, 12 insertions, 19 deletions
diff --git a/bsfs/graph/nodes.py b/bsfs/graph/nodes.py
index c3530c1..84996c7 100644
--- a/bsfs/graph/nodes.py
+++ b/bsfs/graph/nodes.py
@@ -52,9 +52,8 @@ class Nodes():
self._backend = backend
self._ac = access_control
self._node_type = node_type
- self._guids = set(guids)
- # create helper instances
- # FIXME: Assumes that the schema does not change while the instance is in use!
+ # convert to URI since this is not guaranteed by Graph
+ self._guids = {URI(guid) for guid in guids}
def __eq__(self, other: typing.Any) -> bool:
return isinstance(other, Nodes) \
diff --git a/bsfs/schema/types.py b/bsfs/schema/types.py
index 54adffb..104580d 100644
--- a/bsfs/schema/types.py
+++ b/bsfs/schema/types.py
@@ -98,7 +98,7 @@ class _Type():
parent: typing.Optional['_Type'] = None,
**annotations: typing.Any,
):
- self.uri = uri
+ self.uri = URI(uri)
self.parent = parent
self.annotations = annotations
diff --git a/bsfs/triple_store/sparql/parse_filter.py b/bsfs/triple_store/sparql/parse_filter.py
index 8959b2c..bf19a02 100644
--- a/bsfs/triple_store/sparql/parse_filter.py
+++ b/bsfs/triple_store/sparql/parse_filter.py
@@ -154,7 +154,7 @@ class Filter():
puri = f'<{puri}>' # type: ignore [assignment] # variable re-use confuses mypy
# apply reverse flag
if node.reverse:
- puri = URI('^' + puri)
+ puri = '^' + puri
dom, rng = rng, dom # type: ignore [assignment] # variable re-use confuses mypy
# check path consistency
if not node_type <= dom:
@@ -267,9 +267,7 @@ class Filter():
"""
if not isinstance(node_type, bsc.Node):
raise errors.BackendError(f'expected Node, found {node_type}')
- if not rdflib.term._is_valid_uri(node.value): # pylint: disable=protected-access
- raise errors.BackendError(f'<{node.value}> is not a serializable uri')
- return f'VALUES {head} {{ <{node.value}> }}'
+ return f'VALUES {head} {{ <{URI(node.value)}> }}'
def _equals(self, node_type: bsc.Vertex, node: ast.filter.Equals, head: str) -> str:
"""
diff --git a/bsfs/triple_store/sparql/sparql.py b/bsfs/triple_store/sparql/sparql.py
index bd98f46..68c0027 100644
--- a/bsfs/triple_store/sparql/sparql.py
+++ b/bsfs/triple_store/sparql/sparql.py
@@ -284,10 +284,7 @@ class SparqlStore(base.TripleStoreBase):
raise errors.ConsistencyError(f'{node_type} is not defined in the schema')
# check and create guids
for guid in guids:
- # check convert to rdflib.URIRef
- if not rdflib.term._is_valid_uri(guid): # pylint: disable=protected-access
- raise ValueError(guids)
- subject = rdflib.URIRef(guid)
+ subject = rdflib.URIRef(URI(guid))
# check node existence
if (subject, rdflib.RDF.type, None) in self._graph:
# FIXME: node exists and may have a different type! ignore? raise? report?
@@ -326,10 +323,7 @@ class SparqlStore(base.TripleStoreBase):
raise errors.InstanceError(inconsistent)
# check guids
# FIXME: Fail or skip inexistent nodes?
- guids = set(guids)
- invalid = {guid for guid in guids if not rdflib.term._is_valid_uri(guid)} # pylint: disable=protected-access
- if len(invalid) > 0:
- raise ValueError(invalid)
+ guids = {URI(guid) for guid in guids}
inconsistent = {guid for guid in guids if not self._has_type(guid, node_type)}
if len(inconsistent) > 0:
raise errors.InstanceError(inconsistent)
diff --git a/bsfs/utils/uri.py b/bsfs/utils/uri.py
index 0693017..5755a6e 100644
--- a/bsfs/utils/uri.py
+++ b/bsfs/utils/uri.py
@@ -4,6 +4,8 @@ import re
import typing
# constants
+RX_CHARS = re.compile(r'[<>" {}|\\^]')
+
RX_URI = re.compile(r'''
^
(?:(?P<scheme>[^:/?#]+):)? # scheme, ://-delimited
@@ -77,6 +79,9 @@ class URI(str):
no claim about the validity of an URI!
"""
+ # check characters
+ if RX_CHARS.search(query) is not None:
+ return False
# check uri
parts = RX_URI.match(query)
if parts is not None:
@@ -227,9 +232,6 @@ class URI(str):
# overload formatting methods
- def format(self, *args, **kwargs) -> 'URI':
- return URI(super().format(*args, **kwargs))
-
def __mod__(self, *args) -> 'URI':
return URI(super().__mod__(*args))