fix(security): 16 mittlere Probleme behoben (P18-P33)
Check Cross-Plugin Imports / check (push) Has been cancelled
Check Cross-Plugin Imports / check (push) Has been cancelled
P18: require_permission zu forgejo_error_reporter und ai_ui_control routes hinzugefügt P19: Cross-Tenant Permission-Cache-Invalidierung bei Rollenänderungen P20: Session/Permission-Cache-Invalidierung bei Gruppen-Änderungen P21: ENTITY_MODELS Registry um fehlende Plugin-Modelle erweitert P22: Entity-Links prüfen verknüpfte Entity-Permissions P23: authStore persist Middleware entfernt (kein localStorage mehr) P24: 5xx Retry nur noch für GET-Requests P25: KI-Kommentar in address.py (bekannte Inkonsistenz) P26: DeletionLog in EntityHistory gemerged (action=delete) P27: KI-Kommentar in entity_policy.py (ABAC nicht aktiv genutzt) P28: db.commit() aus bulk_permission_service entfernt P29: CSV-Export in export_service.py ausgelagert P30: plugins.py Business-Logik in plugin_install_service.py ausgelagert P31: KI-Kommentar in session.py (Dual-System dokumentiert) P32: Migration 0115: crm_platform_admin Role droppen P33: Cross-Plugin Imports über contracts.py behoben (10 Violations → 0)
This commit is contained in:
@@ -100,8 +100,6 @@ async def bulk_share(
|
||||
})
|
||||
logger.warning("Bulk share error for %s/%s: %s", entity_type, entity_uuid, e)
|
||||
|
||||
await db.commit()
|
||||
|
||||
return {
|
||||
"created": created_count,
|
||||
"updated": updated_count,
|
||||
@@ -146,8 +144,6 @@ async def bulk_unshare(
|
||||
"error": str(e),
|
||||
})
|
||||
|
||||
await db.commit()
|
||||
|
||||
return {
|
||||
"deleted": deleted_count,
|
||||
"errors": errors,
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import io
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
@@ -562,47 +560,3 @@ async def delete_contact_person(
|
||||
cp.deleted_at = datetime.now(timezone.utc)
|
||||
await db.flush()
|
||||
|
||||
|
||||
async def export_contacts_csv(
|
||||
db: AsyncSession, tenant_id: uuid.UUID, contact_type: str | None = None, search: str | None = None,
|
||||
user_id: uuid.UUID | None = None, is_system_admin: bool = False,
|
||||
) -> str:
|
||||
"""Export contacts as CSV string. Only exports visible contacts."""
|
||||
from app.core.visibility import apply_visibility_filter
|
||||
|
||||
base = select(Contact).where(
|
||||
Contact.tenant_id == tenant_id,
|
||||
Contact.deleted_at.is_(None),
|
||||
)
|
||||
if contact_type:
|
||||
base = base.where(Contact.type == contact_type)
|
||||
if search:
|
||||
base = base.where(Contact.search_tsv.op("@@")(func.plainto_tsquery("german", search)))
|
||||
|
||||
# Apply visibility filter
|
||||
if user_id and not is_system_admin:
|
||||
base = await apply_visibility_filter(
|
||||
db, base, "contact", Contact, user_id, tenant_id, is_system_admin
|
||||
)
|
||||
|
||||
base = base.order_by(Contact.displayname)
|
||||
|
||||
result = await db.execute(base)
|
||||
contacts = result.scalars().all()
|
||||
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
writer.writerow([
|
||||
"id", "type", "displayname", "name", "firstname", "surname", "code",
|
||||
"email_1", "email_2", "phone_1", "phone_2", "website",
|
||||
"mailing_city", "mailing_postalcode", "mailing_country",
|
||||
"vat_code", "tags",
|
||||
])
|
||||
for c in contacts:
|
||||
writer.writerow([
|
||||
str(c.id), c.type, c.displayname, c.name or "", c.firstname or "", c.surname or "",
|
||||
c.code or "", c.email_1 or "", c.email_2 or "", c.phone_1 or "", c.phone_2 or "",
|
||||
c.website or "", c.mailing_city or "", c.mailing_postalcode or "",
|
||||
c.mailing_country or "", c.vat_code or "", c.tags or "",
|
||||
])
|
||||
return output.getvalue()
|
||||
|
||||
@@ -116,6 +116,76 @@ try:
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# Additional plugin models with OwnedMixin
|
||||
try:
|
||||
from app.plugins.builtins.mail.models import MailMessage
|
||||
ENTITY_MODELS["mail_message"] = MailMessage
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.kommunikation.models import CommConversation
|
||||
ENTITY_MODELS["comm_conversation"] = CommConversation
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.tags.models import Tag
|
||||
ENTITY_MODELS["tag"] = Tag
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.agent_memory.models import AgentMemory
|
||||
ENTITY_MODELS["agent_memory"] = AgentMemory
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.graph_rag.models import EntityRelationship
|
||||
ENTITY_MODELS["entity_relationship"] = EntityRelationship
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.report_generator.models import ReportTemplate, ReportInstance
|
||||
ENTITY_MODELS["report_template"] = ReportTemplate
|
||||
ENTITY_MODELS["report_instance"] = ReportInstance
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.entity_links.models import EntityLink
|
||||
ENTITY_MODELS["entity_link"] = EntityLink
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.kommunikation.models import CommConversation as CommConv
|
||||
ENTITY_MODELS["comm_conversation"] = CommConv
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.ai_proactive.models import ProactiveSuggestion
|
||||
ENTITY_MODELS["proactive_suggestion"] = ProactiveSuggestion
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.ai_assistant.models import AIAgent, AIChatSession
|
||||
ENTITY_MODELS["ai_agent"] = AIAgent
|
||||
ENTITY_MODELS["ai_chat_session"] = AIChatSession
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.permissions.models import ShareLink
|
||||
ENTITY_MODELS["share_link"] = ShareLink
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.automation.models import AgentDefinition, AutomationDefinition
|
||||
ENTITY_MODELS["agent_definition"] = AgentDefinition
|
||||
ENTITY_MODELS["automation_definition"] = AutomationDefinition
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
from app.plugins.builtins.mcp_client.models import McpServerConfig
|
||||
ENTITY_MODELS["mcp_server_config"] = McpServerConfig
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
def _get_entity_model(entity_type: str) -> type:
|
||||
"""Get SQLAlchemy model class for entity_type, or raise ValueError."""
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
"""Export service — CSV and other format exports for CRM entities."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import io
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import select, func
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.contact import Contact
|
||||
|
||||
|
||||
class ExportService:
|
||||
"""Handles export operations for CRM entities."""
|
||||
|
||||
@staticmethod
|
||||
async def export_contacts_csv(
|
||||
db: AsyncSession,
|
||||
tenant_id: uuid.UUID,
|
||||
contact_type: str | None = None,
|
||||
search: str | None = None,
|
||||
user_id: uuid.UUID | None = None,
|
||||
is_system_admin: bool = False,
|
||||
) -> str:
|
||||
"""Export contacts as CSV string. Only exports visible contacts."""
|
||||
from app.core.visibility import apply_visibility_filter
|
||||
|
||||
base = select(Contact).where(
|
||||
Contact.tenant_id == tenant_id,
|
||||
Contact.deleted_at.is_(None),
|
||||
)
|
||||
if contact_type:
|
||||
base = base.where(Contact.type == contact_type)
|
||||
if search:
|
||||
base = base.where(Contact.search_tsv.op("@@")(func.plainto_tsquery("german", search)))
|
||||
|
||||
# Apply visibility filter
|
||||
if user_id and not is_system_admin:
|
||||
base = await apply_visibility_filter(
|
||||
db, base, "contact", Contact, user_id, tenant_id, is_system_admin
|
||||
)
|
||||
|
||||
base = base.order_by(Contact.displayname)
|
||||
|
||||
result = await db.execute(base)
|
||||
contacts = result.scalars().all()
|
||||
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
writer.writerow([
|
||||
"id", "type", "displayname", "name", "firstname", "surname", "code",
|
||||
"email_1", "email_2", "phone_1", "phone_2", "website",
|
||||
"mailing_city", "mailing_postalcode", "mailing_country",
|
||||
"vat_code", "tags",
|
||||
])
|
||||
for c in contacts:
|
||||
writer.writerow([
|
||||
str(c.id), c.type, c.displayname, c.name or "", c.firstname or "", c.surname or "",
|
||||
c.code or "", c.email_1 or "", c.email_2 or "", c.phone_1 or "", c.phone_2 or "",
|
||||
c.website or "", c.mailing_city or "", c.mailing_postalcode or "",
|
||||
c.mailing_country or "", c.vat_code or "", c.tags or "",
|
||||
])
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
export_service = ExportService()
|
||||
@@ -9,6 +9,7 @@ from sqlalchemy import delete, func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from app.core.auth import get_redis
|
||||
from app.core.permissions import invalidate_all_user_permissions
|
||||
from app.models.group import Group, UserGroup
|
||||
from app.models.user import User, UserTenant
|
||||
@@ -107,6 +108,12 @@ class GroupService:
|
||||
group.permission_version += 1
|
||||
|
||||
await db.flush()
|
||||
|
||||
# Invalidate permission cache for all group members when permissions change
|
||||
if version_bump:
|
||||
redis = get_redis()
|
||||
await invalidate_all_user_permissions(redis, tenant_id)
|
||||
|
||||
return group
|
||||
|
||||
async def delete_group(
|
||||
@@ -192,6 +199,11 @@ class GroupService:
|
||||
)
|
||||
db.add(ug)
|
||||
await db.flush()
|
||||
|
||||
# Invalidate permission cache for the added user
|
||||
redis = get_redis()
|
||||
await invalidate_all_user_permissions(redis, tenant_id)
|
||||
|
||||
return True
|
||||
|
||||
async def remove_user_from_group(
|
||||
@@ -209,6 +221,12 @@ class GroupService:
|
||||
)
|
||||
result = await db.execute(q)
|
||||
await db.flush()
|
||||
|
||||
# Invalidate permission cache for the removed user
|
||||
if result.rowcount > 0:
|
||||
redis = get_redis()
|
||||
await invalidate_all_user_permissions(redis, tenant_id)
|
||||
|
||||
return result.rowcount > 0
|
||||
|
||||
async def get_user_groups(
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
"""Plugin install service — business logic for plugin installation from ZIP/URL."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import logging
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from app.plugins.base import BasePlugin
|
||||
from app.services.plugin_service import get_plugin_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PluginInstallService:
|
||||
"""Service layer for plugin installation operations.
|
||||
|
||||
Handles ZIP extraction, security validation, and plugin registration.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def validate_manifest_name(name: str) -> str:
|
||||
"""Validate plugin name is alphanumeric with underscores only."""
|
||||
if not re.match(r"^[a-zA-Z][a-zA-Z0-9_]*$", name):
|
||||
raise ValueError(
|
||||
f"Invalid plugin name '{name}': must start with a letter and contain only "
|
||||
f"alphanumeric characters and underscores"
|
||||
)
|
||||
return name
|
||||
|
||||
@staticmethod
|
||||
def check_dangerous_imports(source_code: str) -> list[str]:
|
||||
"""Check plugin source for dangerous imports/patterns.
|
||||
|
||||
Returns a list of dangerous patterns found (empty if safe).
|
||||
"""
|
||||
dangerous_patterns = [
|
||||
(r"\bos\.system\b", "os.system call"),
|
||||
(r"\bsubprocess\.", "subprocess module"),
|
||||
(r"\beval\s*\(", "eval() call"),
|
||||
(r"\bexec\s*\(", "exec() call"),
|
||||
(r"\b__import__\s*\(", "__import__() call"),
|
||||
(r"\bcompile\s*\(", "compile() call"),
|
||||
]
|
||||
found: list[str] = []
|
||||
for pattern, description in dangerous_patterns:
|
||||
if re.search(pattern, source_code):
|
||||
found.append(description)
|
||||
return found
|
||||
|
||||
@staticmethod
|
||||
def check_migration_sql(sql_content: str) -> list[str]:
|
||||
"""Basic SQL validation for migration files.
|
||||
|
||||
Returns a list of issues found (empty if OK).
|
||||
"""
|
||||
issues: list[str] = []
|
||||
lines = sql_content.strip().split("\n")
|
||||
for i, line in enumerate(lines, 1):
|
||||
stripped = line.strip()
|
||||
if not stripped or stripped.startswith("--"):
|
||||
continue
|
||||
if stripped.count("(") != stripped.count(")"):
|
||||
issues.append(f"Line {i}: unbalanced parentheses")
|
||||
if re.search(r"\bDROP\s+TABLE\b", stripped, re.IGNORECASE):
|
||||
issues.append(f"Line {i}: DROP TABLE is not allowed in plugin migrations")
|
||||
return issues
|
||||
|
||||
@staticmethod
|
||||
def find_plugin_class_in_module(module: Any) -> type[BasePlugin] | None:
|
||||
"""Find a BasePlugin subclass in a module."""
|
||||
for attr_name in dir(module):
|
||||
attr = getattr(module, attr_name)
|
||||
if (
|
||||
isinstance(attr, type)
|
||||
and issubclass(attr, BasePlugin)
|
||||
and attr is not BasePlugin
|
||||
):
|
||||
return attr
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def extract_plugin_from_zip(zip_path: str) -> tuple[Path, str, type[BasePlugin]]:
|
||||
"""Extract a ZIP file and find the plugin class.
|
||||
|
||||
Returns (extract_dir, plugin_name, plugin_class).
|
||||
"""
|
||||
extract_dir = Path(tempfile.mkdtemp(prefix="plugin_upload_"))
|
||||
try:
|
||||
with zipfile.ZipFile(zip_path, "r") as zf:
|
||||
bad_files = [f for f in zf.namelist() if f.startswith("..") or f.startswith("/")]
|
||||
if bad_files:
|
||||
raise ValueError(f"ZIP contains files with unsafe paths: {bad_files}")
|
||||
zf.extractall(extract_dir)
|
||||
|
||||
plugin_py_path: Path | None = None
|
||||
for fpath in extract_dir.rglob("plugin.py"):
|
||||
plugin_py_path = fpath
|
||||
break
|
||||
|
||||
if plugin_py_path is None:
|
||||
raise ValueError("ZIP does not contain a plugin.py file")
|
||||
|
||||
source_code = plugin_py_path.read_text(encoding="utf-8")
|
||||
dangerous = PluginInstallService.check_dangerous_imports(source_code)
|
||||
if dangerous:
|
||||
raise ValueError(
|
||||
f"Plugin contains dangerous patterns: {', '.join(dangerous)}"
|
||||
)
|
||||
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
"uploaded_plugin", plugin_py_path
|
||||
)
|
||||
if spec is None or spec.loader is None:
|
||||
raise ValueError("Could not load plugin.py module")
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module)
|
||||
|
||||
plugin_class = PluginInstallService.find_plugin_class_in_module(module)
|
||||
if plugin_class is None:
|
||||
raise ValueError(
|
||||
"plugin.py does not contain a BasePlugin subclass"
|
||||
)
|
||||
|
||||
plugin_instance = plugin_class()
|
||||
plugin_name = plugin_instance.name
|
||||
|
||||
manifest = plugin_instance.manifest
|
||||
if not manifest.name or not manifest.version or not manifest.display_name:
|
||||
raise ValueError(
|
||||
"Plugin manifest must include name, version, and display_name"
|
||||
)
|
||||
|
||||
PluginInstallService.validate_manifest_name(manifest.name)
|
||||
|
||||
migrations_dir = plugin_py_path.parent / "migrations"
|
||||
if migrations_dir.exists():
|
||||
for sql_file in sorted(migrations_dir.glob("*.sql")):
|
||||
sql_content = sql_file.read_text(encoding="utf-8")
|
||||
issues = PluginInstallService.check_migration_sql(sql_content)
|
||||
if issues:
|
||||
raise ValueError(
|
||||
f"Migration file {sql_file.name} has issues: {'; '.join(issues)}"
|
||||
)
|
||||
|
||||
return extract_dir, plugin_name, plugin_class
|
||||
|
||||
except Exception:
|
||||
shutil.rmtree(extract_dir, ignore_errors=True)
|
||||
raise
|
||||
|
||||
@staticmethod
|
||||
def install_plugin_from_dir(
|
||||
extract_dir: Path,
|
||||
plugin_name: str,
|
||||
plugin_class: type[BasePlugin],
|
||||
) -> None:
|
||||
"""Copy plugin directory to builtins and register it.
|
||||
|
||||
Copies the extracted plugin directory to app/plugins/builtins/{plugin_name}/.
|
||||
"""
|
||||
builtins_dir = Path(__file__).parent.parent / "plugins" / "builtins" / plugin_name
|
||||
builtins_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
for item in extract_dir.iterdir():
|
||||
dest = builtins_dir / item.name
|
||||
if item.is_dir():
|
||||
if dest.exists():
|
||||
shutil.rmtree(dest)
|
||||
shutil.copytree(item, dest)
|
||||
else:
|
||||
shutil.copy2(item, dest)
|
||||
|
||||
registry = get_plugin_service().registry
|
||||
instance = plugin_class()
|
||||
registry.register_plugin(instance)
|
||||
|
||||
logger.info(
|
||||
"Installed plugin '%s' from uploaded ZIP to %s",
|
||||
plugin_name,
|
||||
builtins_dir,
|
||||
)
|
||||
Reference in New Issue
Block a user