* feat(rbac): drop dataset_permissions + access_requests + users.role + is_public; v19 migration
BREAKING. Sjednocení datové RBAC vrstvy do per-group resource_grants modelu.
Před PR byla legacy data RBAC vrstva (dataset_permissions + is_public bypass)
de-facto neaktivní — is_public neměl API/UI/CLI surface, default true znamenal
že can_access_table vždycky bypassl. Dnes každý non-admin přístup vyžaduje
explicitní resource_grants(group, "table", id) řádek.
Schema v18 → v19 (src/db.py:_v18_to_v19_finalize):
- DROP TABLE dataset_permissions, access_requests
- DROP COLUMN users.role (NULL artifact since v13)
- DROP COLUMN table_registry.is_public
- Drops přes table-rebuild idiom (rename → create new → INSERT … SELECT
→ drop old) kvůli DuckDB ALTER DROP COLUMN limitacím na tabulkách
s historic FK constraints. INSERT picks intersection sloupců, takže
test fixtures s minimal pre-v19 schemou migrate cleanly.
Runtime:
- src/rbac.py:can_access_table → deleguje na app.auth.access.can_access
- DatasetPermissionRepository, AccessRequestRepository smazány
- AGNES_ENABLE_TABLE_GRANTS env-gate v app/resource_types.py odstraněn
(TABLE je unconditionally enabled)
API drop:
- app/api/permissions.py, app/api/access_requests.py celé soubory
- /admin/permissions web route + admin_permissions.html
- "Request Access" modal v catalog.html + locked-row UI
- ~10 if user.get("role") != "admin" checků nahrazeno (admin shortcut
je uvnitř can_access_table)
- /api/settings: drop permissions field z GET; PUT /api/settings/dataset
gate přepnut na can_access(user_id, "table", dataset, conn)
Auth:
- app/auth/jwt.py:create_access_token: drop role parametr (claim zmizí
z nově vydávaných JWT; staré tokeny zůstávají valid, claim ignored)
- app/api/users.py: drop role z CreateUserRequest / UpdateUserRequest
(admin promotion = explicit add to Admin group via memberships API)
- src/repositories/users.py: drop role z create() / update()
CLI:
- da admin set-role smazán → hard-fail s replacement command
- da admin add-user --role flag pryč
- da auth import-token --role flag pryč
- da auth whoami: drop "Role:" výpis
- cli/config.py:save_token: role parametr now optional, no longer written
(back-compat se starými token.json soubory zachována — pole se ignoruje)
Tests:
- DELETE: test_permissions.py, test_permissions_api.py, test_access_requests_api.py
- REWRITE: test_access_control.py (resource_grants flow), test_rbac.py
(can_access_table over resource_grants), test_journey_rbac.py
(drop access-request flow), test_resource_types.py (drop env-gate
tests, drop is_public from helpers), test_v2_*.py (drop role-based
user dicts in favor of id-based + Admin group membership),
test_settings_api.py (no permissions field, can_access gate)
- TRIVIAL: ~30 souborů — drop role="admin" arg z UserRepository.create
a 3rd positional role z create_access_token
- NEW: test_v18_to_v19 migration test (test_db.py),
test_can_access_table_no_implicit_public (test_rbac.py),
test_admin_set_role_returns_hardfail (test_cli_admin.py)
- OpenAPI snapshot regenerated
Docs:
- CHANGELOG: BREAKING entry pod [Unreleased]
- CLAUDE.md: schema v18 → v19
- docs/architecture.md: schema table + RBAC sekce přepsána
- docs/auth-google-oauth.md: admin promotion přes da admin break-glass
- cli/skills/security.md: kompletně přepsáno na group-based model
- docs/TODO-rbac-data-enforcement.md: smazáno (TODO splněn)
Test results: 2363 passed, 19 failed. Zbývající failures jsou pre-existing
Windows-specific issues (fcntl, charset) nesouvisející s tímto PR —
ověřeno git stash pop.
Plan: ~/.claude/plans/floofy-coalescing-parnas.md
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* chore(release): cut 0.27.0
---------
Co-authored-by: Minas Arustamyan <arustamyan.minas@gmail.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: ZdenekSrotyr <zdenek.srotyr@keboola.com>
400 lines
16 KiB
Python
400 lines
16 KiB
Python
"""Security tests — sandbox escapes, SQL injection, access control."""
|
|
|
|
import importlib
|
|
import os
|
|
import sys
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
|
|
@pytest.fixture
|
|
def client(tmp_path, monkeypatch):
|
|
monkeypatch.setenv("DATA_DIR", str(tmp_path))
|
|
monkeypatch.setenv("JWT_SECRET_KEY", "test-secret-key-minimum-32-characters!!")
|
|
monkeypatch.setenv("SCRIPT_TIMEOUT", "5")
|
|
|
|
from app.main import create_app
|
|
from src.db import get_system_db
|
|
from src.repositories.users import UserRepository
|
|
from app.auth.jwt import create_access_token
|
|
from tests.helpers.auth import grant_admin
|
|
|
|
conn = get_system_db()
|
|
repo = UserRepository(conn)
|
|
repo.create(id="admin1", email="admin@test.com", name="Admin")
|
|
repo.create(id="u1", email="user@test.com", name="User")
|
|
grant_admin(conn, "admin1")
|
|
conn.close()
|
|
|
|
app = create_app()
|
|
c = TestClient(app)
|
|
token = create_access_token("admin1", "admin@test.com")
|
|
return c, token
|
|
|
|
|
|
def _headers(token):
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
# ---- Script Sandbox ----
|
|
|
|
class TestScriptSandbox:
|
|
def test_blocks_os_system(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/scripts/run", json={"source": "import os\nos.system('whoami')"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_dunder_import(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/scripts/run", json={"source": "__import__('subprocess').run(['ls'])"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_eval(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/scripts/run", json={"source": "eval('print(1)')"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_exec(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/scripts/run", json={"source": "exec('import os')"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_open(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/scripts/run", json={"source": "open('/etc/passwd').read()"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_socket(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/scripts/run", json={"source": "import socket"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_pathlib(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/scripts/run", json={"source": "from pathlib import Path"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_allows_safe_script(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/scripts/run", json={
|
|
"source": "import math\nprint(math.sqrt(144))",
|
|
}, headers=_headers(token))
|
|
assert resp.status_code == 200
|
|
assert "12" in resp.json()["stdout"]
|
|
|
|
def test_allows_duckdb(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/scripts/run", json={
|
|
"source": "import duckdb\nconn=duckdb.connect(':memory:')\nprint(conn.execute('SELECT 42').fetchone()[0])",
|
|
}, headers=_headers(token))
|
|
assert resp.status_code == 200
|
|
assert "42" in resp.json()["stdout"]
|
|
|
|
def test_allows_json(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/scripts/run", json={
|
|
"source": "import json\nprint(json.dumps({'a': 1}))",
|
|
}, headers=_headers(token))
|
|
assert resp.status_code == 200
|
|
assert '"a"' in resp.json()["stdout"]
|
|
|
|
def test_runtime_import_blocked(self, client):
|
|
"""Even if static check passes, runtime __import__ override catches it."""
|
|
c, token = client
|
|
# This uses string concatenation to bypass static check
|
|
resp = c.post("/api/scripts/run", json={
|
|
"source": "x='sub'+'process'\ntry:\n m=type('',(),{'__init__':lambda s:None})()\nexcept:\n pass\nprint('safe')",
|
|
}, headers=_headers(token))
|
|
# Should still run but without access to dangerous modules
|
|
assert resp.status_code == 200
|
|
|
|
def test_sandbox_cannot_import_httpx(self, client):
|
|
"""httpx must be blocked — either by pattern check (400) or
|
|
ModuleNotFoundError at runtime due to stripped VIRTUAL_ENV/PYTHONPATH (200 with non-zero exit)."""
|
|
c, token = client
|
|
resp = c.post("/api/scripts/run", json={
|
|
"source": "import httpx\nprint('pwned')",
|
|
}, headers=_headers(token))
|
|
# Static pattern check should reject it outright
|
|
assert resp.status_code == 400 or (
|
|
resp.status_code == 200 and resp.json()["exit_code"] != 0
|
|
)
|
|
|
|
|
|
# ---- SQL Query Security ----
|
|
|
|
class TestQuerySecurity:
|
|
def test_blocks_copy_to(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "COPY (SELECT 1) TO '/tmp/pwned.csv'"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_read_csv(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT * FROM read_csv_auto('/etc/passwd')"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_semicolon(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT 1; SELECT 2"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_non_select(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "CREATE TABLE pwned (id INT)"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_attach(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "ATTACH '/tmp/pwned.db'"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_allows_select(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT 1 as test, 'hello' as msg"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 200
|
|
assert resp.json()["columns"] == ["test", "msg"]
|
|
|
|
def test_allows_with_cte(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "WITH t AS (SELECT 1 as x) SELECT * FROM t"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 200
|
|
|
|
def test_blocks_drop(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "DROP TABLE IF EXISTS users"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_blocks_parquet_scan(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT * FROM parquet_scan('/data/extracts/secret.parquet')"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_read_csv_auto(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT * FROM read_csv_auto('/etc/passwd')"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_query_table(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT * FROM query_table('secret_table')"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_no_auth(self, client):
|
|
c, _ = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT 1"})
|
|
assert resp.status_code == 401
|
|
|
|
def test_word_boundary_match_no_false_positive(self, client):
|
|
"""Verify that a table named 'id' doesn't block queries containing 'id' in other contexts."""
|
|
c, token = client
|
|
# Query contains 'id' in column name and function, but not as a forbidden table reference
|
|
resp = c.post("/api/query", json={"sql": "SELECT 1 as identity, 2 as valid_id"},
|
|
headers=_headers(token))
|
|
# Should succeed (not blocked by false positive substring match)
|
|
assert resp.status_code == 200
|
|
|
|
def test_word_boundary_match_blocks_actual_table(self, client):
|
|
"""Verify that actual table references are still properly blocked with word-boundary regex."""
|
|
c, token = client
|
|
# Create a scenario where a table named 'id' would be forbidden
|
|
# This tests that word boundaries work correctly
|
|
resp = c.post("/api/query", json={"sql": "SELECT * FROM id WHERE id = 1"},
|
|
headers=_headers(token))
|
|
# Without a real 'id' table and RBAC setup, this will fail with query error,
|
|
# but not with 403 access denied. The regex logic is sound if test_word_boundary_match_no_false_positive passes.
|
|
assert resp.status_code in [400, 200] # Either query error or success
|
|
|
|
def test_blocks_information_schema(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT table_name FROM information_schema.tables"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_duckdb_tables(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT * FROM duckdb_tables()"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_duckdb_columns(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT * FROM duckdb_columns()"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_duckdb_databases(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT * FROM duckdb_databases()"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_relative_path(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT * FROM read_parquet('../secret/data.parquet')"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
def test_blocks_pragma_table_info(self, client):
|
|
c, token = client
|
|
resp = c.post("/api/query", json={"sql": "SELECT * FROM pragma_table_info('users')"},
|
|
headers=_headers(token))
|
|
assert resp.status_code == 400
|
|
|
|
|
|
# ---- Auth Edge Cases ----
|
|
|
|
class TestAuthSecurity:
|
|
def test_garbage_token(self, client):
|
|
c, _ = client
|
|
resp = c.get("/api/scripts", headers={"Authorization": "Bearer garbage.token.here"})
|
|
assert resp.status_code == 401
|
|
|
|
def test_empty_bearer(self, client):
|
|
c, _ = client
|
|
resp = c.get("/api/scripts", headers={"Authorization": "Bearer "})
|
|
assert resp.status_code == 401
|
|
|
|
def test_no_bearer_prefix(self, client):
|
|
c, token = client
|
|
resp = c.get("/api/scripts", headers={"Authorization": token})
|
|
assert resp.status_code == 401
|
|
|
|
def test_missing_header(self, client):
|
|
c, _ = client
|
|
resp = c.get("/api/scripts")
|
|
assert resp.status_code == 401
|
|
|
|
|
|
# ---- Script RBAC ----
|
|
|
|
@pytest.fixture
|
|
def viewer_client(tmp_path, monkeypatch):
|
|
"""TestClient with a viewer-role user seeded."""
|
|
monkeypatch.setenv("DATA_DIR", str(tmp_path))
|
|
monkeypatch.setenv("JWT_SECRET_KEY", "test-secret-key-minimum-32-characters!!")
|
|
monkeypatch.setenv("SCRIPT_TIMEOUT", "5")
|
|
|
|
from app.main import create_app
|
|
from src.db import get_system_db
|
|
from src.repositories.users import UserRepository
|
|
from app.auth.jwt import create_access_token
|
|
from fastapi.testclient import TestClient
|
|
|
|
conn = get_system_db()
|
|
UserRepository(conn).create(id="viewer1", email="viewer@test.com", name="Viewer")
|
|
conn.close()
|
|
|
|
app = create_app()
|
|
c = TestClient(app)
|
|
token = create_access_token(user_id="viewer1", email="viewer@test.com")
|
|
return c, token
|
|
|
|
|
|
@pytest.mark.skip(
|
|
reason=(
|
|
"v12: scripts run/deploy is gated by Depends(get_current_user) "
|
|
"(any signed-in user), not by role hierarchy. The viewer-blocked "
|
|
"assertion is a v9-era expectation that no longer holds. "
|
|
"If we re-introduce a role/group gate on scripts, rewrite these "
|
|
"tests against require_resource_access(ResourceType.SCRIPT, ...)."
|
|
)
|
|
)
|
|
class TestScriptRBAC:
|
|
def test_viewer_cannot_run_scripts(self, viewer_client):
|
|
c, token = viewer_client
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
resp = c.post("/api/scripts/run", json={
|
|
"name": "test", "source": "print('hi')"
|
|
}, headers=headers)
|
|
assert resp.status_code == 403
|
|
|
|
def test_viewer_cannot_deploy_scripts(self, viewer_client):
|
|
c, token = viewer_client
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
resp = c.post("/api/scripts/deploy", json={
|
|
"name": "test", "source": "print('hi')", "schedule": ""
|
|
}, headers=headers)
|
|
assert resp.status_code == 403
|
|
|
|
|
|
# ---- JWT Claims ----
|
|
|
|
class TestJwtClaims:
|
|
def test_jwt_contains_jti_claim(self):
|
|
"""Token payload must include a jti claim with at least 16 hex chars."""
|
|
os.environ.setdefault("TESTING", "1")
|
|
from app.auth.jwt import create_access_token, verify_token
|
|
token = create_access_token("u1", "user@test.com")
|
|
payload = verify_token(token)
|
|
assert payload is not None
|
|
assert "jti" in payload
|
|
assert len(payload["jti"]) >= 16
|
|
|
|
def test_jwt_expiry_is_24_hours(self):
|
|
"""ACCESS_TOKEN_EXPIRE_HOURS must be 24 (not 30*24)."""
|
|
os.environ.setdefault("TESTING", "1")
|
|
from app.auth import jwt as jwt_module
|
|
assert jwt_module.ACCESS_TOKEN_EXPIRE_HOURS == 24
|
|
|
|
|
|
# ---- JWT Secret Hardening ----
|
|
|
|
class TestJwtSecretHardening:
|
|
def test_auto_generates_jwt_secret_when_absent(self, tmp_path):
|
|
"""When JWT_SECRET_KEY is absent and TESTING is not set,
|
|
the secret is auto-generated and persisted to a file."""
|
|
saved_key = os.environ.pop("JWT_SECRET_KEY", None)
|
|
saved_testing = os.environ.pop("TESTING", None)
|
|
saved_data_dir = os.environ.get("DATA_DIR")
|
|
os.environ["DATA_DIR"] = str(tmp_path)
|
|
# Eject cached modules so the re-import re-executes module-level code
|
|
sys.modules.pop("app.auth.jwt", None)
|
|
sys.modules.pop("app.secrets", None)
|
|
try:
|
|
mod = importlib.import_module("app.auth.jwt")
|
|
# Secret is now lazy — trigger it by calling the accessor
|
|
mod._SECRET_KEY_CACHE = None
|
|
mod._get_cached_secret_key()
|
|
secret_file = tmp_path / "state" / ".jwt_secret"
|
|
assert secret_file.exists(), "JWT secret file should be auto-generated"
|
|
secret = secret_file.read_text().strip()
|
|
assert len(secret) == 64, "Auto-generated secret should be 64 hex chars (32 bytes)"
|
|
finally:
|
|
# Restore environment before re-importing so the module loads cleanly
|
|
if saved_key is not None:
|
|
os.environ["JWT_SECRET_KEY"] = saved_key
|
|
if saved_testing is not None:
|
|
os.environ["TESTING"] = saved_testing
|
|
if saved_data_dir is not None:
|
|
os.environ["DATA_DIR"] = saved_data_dir
|
|
else:
|
|
os.environ.pop("DATA_DIR", None)
|
|
# If neither was set (bare test run), use TESTING flag so reload works
|
|
if saved_key is None and saved_testing is None:
|
|
os.environ["TESTING"] = "1"
|
|
sys.modules.pop("app.auth.jwt", None)
|
|
sys.modules.pop("app.secrets", None)
|
|
importlib.import_module("app.auth.jwt")
|
|
# Clean up the temporary TESTING flag if we added it
|
|
if saved_key is None and saved_testing is None:
|
|
os.environ.pop("TESTING", None)
|