Skip to content
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

Can't handle PREFIXes in SPARQL queries #154

Closed
kevinstadler opened this issue Dec 2, 2024 · 0 comments · Fixed by #157
Closed

Can't handle PREFIXes in SPARQL queries #154

kevinstadler opened this issue Dec 2, 2024 · 0 comments · Fixed by #157
Labels
bug Something isn't working

Comments

@kevinstadler
Copy link

kevinstadler commented Dec 2, 2024

e823e28 breaks support for PREFIXes in queries:

PREFIX crm: <http://www.cidoc-crm.org/cidoc-crm/>
PREFIX lrmoo: <http://iflastandards.info/ns/lrm/lrmoo/>
PREFIX star: <https://r11.eu/ns/star/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX r11: <https://r11.eu/ns/spec/>
PREFIX r11pros: <https://r11.eu/ns/prosopography/>

SELECT
?written_work
  ?written_work__written_work_label
  ?written_work__work_creation_assertion
    ?written_work__work_creation_assertion__work_creation
      ?written_work__work_creation_assertion__work_creation__work_creation_6464e7f7408f5
      ?written_work__work_creation_assertion__work_creation__text_creation_timeframe_assertio
    ?written_work__work_creation_assertion__work_creation_assertion_by
    ?written_work__work_creation_assertion__work_creation_assertion_by_64676da87229e

WHERE {
?written_work a r11:Text_Expression.

  ?written_work crm:P3_has_note ?written_work__written_work_label.

  OPTIONAL {
  ?written_work__work_creation_assertion a star:E13_lrmoo_R17.
  ?written_work__work_creation_assertion crm:P141_assigned ?written_work.

    ?written_work__work_creation_assertion__work_creation a lrmoo:F28_Expression_Creation.
    ?written_work__work_creation_assertion___wc crm:P141_assigned ?written_work__work_creation_assertion.
    ?written_work__work_creation_assertion___wc crm:P140_assigned_attribute_to ?written_work__work_creation_assertion__work_creation.

      OPTIONAL {
      ?written_work__work_creation_assertion__work_creation__work_creation_6464e7f7408f5 a star:E13_crm_P14.
      ?written_work__work_creation_assertion__work_creation___wc6 crm:P141_assigned ?written_work__work_creation_assertion__work_creation.
      ?written_work__work_creation_assertion__work_creation___wc6 crm:P140_assigned_attribute_to ?written_work__work_creation_assertion__work_creation___wc6___wc6.
      ?written_work__work_creation_assertion__work_creation__work_creation_6464e7f7408f5 crm:P140_assigned_attribute_to ?written_work__work_creation_assertion__work_creation___wc6___wc6.
      }

      ?written_work__work_creation_assertion__work_creation__text_creation_timeframe_assertio a star:E13_crm_P4.
      ?written_work__work_creation_assertion__work_creation___tcta crm:P141_assigned ?written_work__work_creation_assertion__work_creation.
      ?written_work__work_creation_assertion__work_creation___tcta crm:P140_assigned_attribute_to ?written_work__work_creation_assertion__work_creation___tcta___tcta.
      ?written_work__work_creation_assertion__work_creation__text_creation_timeframe_assertio crm:P140_assigned_attribute_to ?written_work__work_creation_assertion__work_creation___tcta___tcta.

    ?written_work__work_creation_assertion__work_creation_assertion_by a crm:E21_Person.
    ?written_work__work_creation_assertion___wcab crm:P141_assigned ?written_work__work_creation_assertion.
    ?written_work__work_creation_assertion___wcab crm:P14_carried_out_by ?written_work__work_creation_assertion__work_creation_assertion_by.

    ?written_work__work_creation_assertion__work_creation_assertion_by_64676da87229e a r11:Author_Group.
    ?written_work__work_creation_assertion___wcab6 crm:P141_assigned ?written_work__work_creation_assertion.
    ?written_work__work_creation_assertion___wcab6 crm:P14_carried_out_by ?written_work__work_creation_assertion__work_creation_assertion_by_64676da87229e.
  }
}

b'MALFORMED QUERY: Encountered " "prefix" "PREFIX "" at line 15, column 3.\nWas expecting one of:\n "(" ...\n "{" ...\n "}" ...\n "[" ...\n ...\n ...\n "select" ...\n "optional" ...\n "graph" ...\n "minus" ...\n "filter" ...\n "true" ...\n "false" ...\n "bind" ...\n "service" ...\n "values" ...\n <Q_IRI_REF> ...\n <PNAME_NS> ...\n <PNAME_LN> ...\n <BLANK_NODE_LABEL> ...\n ...\n ...\n ...\n <INTEGER_POSITIVE> ...\n <INTEGER_NEGATIVE> ...\n ...\n <DECIMAL_POSITIVE> ...\n <DECIMAL_NEGATIVE> ...\n ...\n <DOUBLE_POSITIVE> ...\n <DOUBLE_NEGATIVE> ...\n <STRING_LITERAL1> ...\n <STRING_LITERAL2> ...\n <STRING_LITERAL_LONG1> ...\n <STRING_LITERAL_LONG2> ...\n "<<" ...\n '

full stack trace:

Traceback (most recent call last):
  File "/home/kstadler/.local/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 401, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/home/kstadler/.local/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 70, in __call__
    return await self.app(scope, receive, send)
  File "/home/kstadler/.local/lib/python3.10/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/applications.py", line 113, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 187, in __call__
    raise exc
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/middleware/errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/middleware/cors.py", line 85, in __call__
    await self.app(scope, receive, send)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 62, in wrapped_app
    raise exc
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 51, in wrapped_app
    await app(scope, receive, sender)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/routing.py", line 715, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/routing.py", line 735, in app
    await route.handle(scope, receive, send)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/routing.py", line 288, in handle
    await self.app(scope, receive, send)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/routing.py", line 76, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 62, in wrapped_app
    raise exc
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/_exception_handler.py", line 51, in wrapped_app
    await app(scope, receive, sender)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/routing.py", line 73, in app
    response = await f(request)
  File "/home/kstadler/.local/lib/python3.10/site-packages/fastapi/routing.py", line 301, in app
    raw_response = await run_endpoint_function(
  File "/home/kstadler/.local/lib/python3.10/site-packages/fastapi/routing.py", line 214, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/home/kstadler/.local/lib/python3.10/site-packages/starlette/concurrency.py", line 39, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/home/kstadler/.local/lib/python3.10/site-packages/anyio/to_thread.py", line 56, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
  File "/home/kstadler/.local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 2177, in run_sync_in_worker_thread
    return await future
  File "/home/kstadler/.local/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 859, in run
    result = context.run(func, *args)
  File "/home/kstadler/releven-backend/releven.py", line 51, in written_work
    return adapter.query()
  File "/home/kstadler/releven-backend/rdfproxy/adapter.py", line 60, in query
    items_query_bindings: Iterator[dict] = query_with_wrapper(
  File "/home/kstadler/releven-backend/rdfproxy/utils/sparql_utils.py", line 154, in query_with_wrapper
    result: QueryResult = sparql_wrapper.query()
  File "/home/kstadler/.local/lib/python3.10/site-packages/SPARQLWrapper/Wrapper.py", line 960, in query
    return QueryResult(self._query())
  File "/home/kstadler/.local/lib/python3.10/site-packages/SPARQLWrapper/Wrapper.py", line 930, in _query
    raise QueryBadFormed(e.read())
SPARQLWrapper.SPARQLExceptions.QueryBadFormed: QueryBadFormed: A bad request has been sent to the endpoint: probably the SPARQL query is badly formed.

This non-nested query on the other hand works: fine

PREFIX crm: <http://www.cidoc-crm.org/cidoc-crm/>
PREFIX lrmoo: <http://iflastandards.info/ns/lrm/lrmoo/>
PREFIX star: <https://r11.eu/ns/star/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX r11: <https://r11.eu/ns/spec/>
PREFIX r11pros: <https://r11.eu/ns/prosopography/>

SELECT
?location
  ?location__location_descriptive_name

WHERE {
?location a crm:E53_Place.

  ?location crm:P3_has_note ?location__location_descriptive_name.
}
@kevinstadler kevinstadler added the bug Something isn't working label Dec 2, 2024
lu-pl added a commit that referenced this issue Dec 2, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant