mirror of
https://github.com/simonw/datasette.git
synced 2026-09-11 19:14:07 +02:00
Disable extension loading once --load-extension extensions are loaded
Refs GHSA-2mvv-ffvc-q5p6 Co-authored-by: Alex Garcia <15178711+asg017@users.noreply.github.com>
This commit is contained in:
parent
7403ae68bb
commit
bdaa8cc76c
2 changed files with 62 additions and 8 deletions
|
|
@ -1,4 +1,5 @@
|
|||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
|
|
@ -20,6 +21,29 @@ def has_compiled_ext():
|
|||
return False
|
||||
|
||||
|
||||
@pytest.mark.parametrize("load_fails", (False, True))
|
||||
def test_load_extension_is_disabled(load_fails):
|
||||
ds = Datasette(sqlite_extensions=[COMPILED_EXTENSION_PATH])
|
||||
connection = mock.Mock()
|
||||
if load_fails:
|
||||
connection.load_extension.side_effect = RuntimeError
|
||||
|
||||
if load_fails:
|
||||
with pytest.raises(RuntimeError):
|
||||
ds._prepare_connection(connection, "data")
|
||||
else:
|
||||
ds._prepare_connection(connection, "data")
|
||||
|
||||
# Extensions are loaded using the Python API, never via SQL
|
||||
assert connection.load_extension.mock_calls == [
|
||||
mock.call(COMPILED_EXTENSION_PATH),
|
||||
]
|
||||
assert connection.enable_load_extension.mock_calls == [
|
||||
mock.call(True),
|
||||
mock.call(False),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.skipif(not has_compiled_ext(), reason="Requires compiled ext.c")
|
||||
async def test_load_extension_default_entrypoint():
|
||||
|
|
@ -64,3 +88,20 @@ async def test_load_extension_multiple_entrypoints():
|
|||
response = await ds.client.get("/_memory/-/query.json?_shape=arrays&sql=select+c()")
|
||||
assert response.status_code == 200
|
||||
assert response.json()["rows"][0][0] == "c"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.skipif(not has_compiled_ext(), reason="Requires compiled ext.c")
|
||||
async def test_sql_cannot_load_additional_extension():
|
||||
ds = Datasette(sqlite_extensions=[COMPILED_EXTENSION_PATH])
|
||||
|
||||
response = await ds.client.get(
|
||||
"/_memory/-/query.json",
|
||||
params={
|
||||
"sql": "select load_extension(:path, :entrypoint)",
|
||||
"path": COMPILED_EXTENSION_PATH,
|
||||
"entrypoint": "sqlite3_ext_b_init",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 400
|
||||
assert response.json()["error"] == "not authorized"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue