Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): require trial app registration for reads (#38858)

林玮 (Jade Lin)8e0d10bf5e5e
diff --git a/api/controllers/console/app/wraps.py b/api/controllers/console/app/wraps.py
index 3d79273685f..699178ab945 100644
--- a/api/controllers/console/app/wraps.py
+++ b/api/controllers/console/app/wraps.py
@@ -17,7 +17,7 @@ from controllers.common.session import with_session
from controllers.console.app.error import AppNotFoundError
from extensions.ext_database import db
from libs.login import current_account_with_tenant
-from models import App, AppMode
+from models import App, AppMode, TrialApp
__all__ = ["get_app_model", "get_app_model_with_trial", "with_session"]
@@ -41,7 +41,10 @@ def _load_app_model_from_scoped_session(app_id: str) -> App | None:
def _load_app_model_with_trial(app_id: str) -> App | None:
- app_model = db.session.scalar(select(App).where(App.id == app_id, App.status == "normal").limit(1))
+ """Load a normal app through its trial registration without applying current-tenant scope."""
+ app_model = db.session.scalar(
+ select(App).join(TrialApp, TrialApp.app_id == App.id).where(App.id == app_id, App.status == "normal").limit(1)
+ )
return app_model
diff --git a/api/tests/unit_tests/controllers/console/app/test_wraps.py b/api/tests/unit_tests/controllers/console/app/test_wraps.py
index 4060d101c80..4a5a47b2194 100644
--- a/api/tests/unit_tests/controllers/console/app/test_wraps.py
+++ b/api/tests/unit_tests/controllers/console/app/test_wraps.py
@@ -3,11 +3,12 @@ from __future__ import annotations
from types import SimpleNamespace
import pytest
+from sqlalchemy import Select
from controllers.common.session import with_session
from controllers.console.app import wraps as wraps_module
from controllers.console.app.error import AppNotFoundError
-from models.model import AppMode
+from models.model import App, AppMode, TrialApp
class FakeSession:
@@ -54,6 +55,25 @@ def test_get_app_model_rejects_wrong_mode(monkeypatch: pytest.MonkeyPatch) -> No
handler(app_id="app-1")
+def test_get_app_model_with_trial_requires_trial_app_registration(monkeypatch: pytest.MonkeyPatch) -> None:
+ app_model = SimpleNamespace(id="app-1", mode=AppMode.CHAT.value, status="normal", tenant_id="t1")
+
+ def scalar(statement: Select[tuple[App]]) -> object | None:
+ has_trial_app_join = any(
+ from_clause.is_derived_from(TrialApp.__table__) for from_clause in statement.get_final_froms()
+ )
+ return None if has_trial_app_join else app_model
+
+ monkeypatch.setattr(wraps_module.db, "session", SimpleNamespace(scalar=scalar))
+
+ @wraps_module.get_app_model_with_trial
+ def handler(app_model):
+ return app_model.id
+
+ with pytest.raises(AppNotFoundError):
+ handler(app_id="app-1")
+
+
def test_get_app_model_requires_app_id() -> None:
@wraps_module.get_app_model
def handler(app_model):