Skip to content

Commit

Permalink
fix: remove SPARQL prefixes from subqueries before query injections
Browse files Browse the repository at this point in the history
Subquery injections did not remove SPARQL prefixes before injection,
resulting in invalid SPARQL. The change defines a regex for removing
PREFIX definitions and strips the subquery before injection.

Fixes #154.
  • Loading branch information
lu-pl committed Dec 2, 2024
1 parent 7110e27 commit 16c2970
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion rdfproxy/utils/sparql_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,25 @@ def replace_query_select_clause(query: str, repl: str) -> str:
return modified_query


def _remove_sparql_prefixes(query: str) -> str:
"""Remove SPARQL prefixes from a query.
This is needed for subquery injection, because subqueries cannot have prefixes.
Note that this is not generic, all prefixes are simply ut from the subquery
and do not get appended to the outer query prefixes.
"""
prefix_pattern = re.compile(r"PREFIX\s+\w*:\s?<[^>]+>\s*", flags=re.I)
cleaned_query = re.sub(prefix_pattern, "", query).strip()
return cleaned_query


def inject_subquery(query: str, subquery: str) -> str:
"""Inject a SPARQL query with a subquery."""
if (tail := re.search(r"}[^}]*\Z", query)) is None:
raise QueryConstructionException("Unable to inject subquery.")

tail_index: int = tail.start()
injected: str = f"{query[:tail_index]} {{{subquery}}} {query[tail_index:]}"
injected: str = f"{query[:tail_index]} {{{_remove_sparql_prefixes(subquery)}}} {query[tail_index:]}"
return injected


Expand Down

0 comments on commit 16c2970

Please sign in to comment.