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): escape LIKE wildcards in snippet keyword search (#44708)

Tianyu Cai0e92c72ad9da
diff --git a/api/services/snippet_service.py b/api/services/snippet_service.py
index 17851e29e6b..9e8d46656c6 100644
--- a/api/services/snippet_service.py
+++ b/api/services/snippet_service.py
@@ -12,6 +12,7 @@ from configs import dify_config
from core.workflow.node_factory import LATEST_VERSION, NODE_TYPE_CLASSES_MAPPING
from enums import DeploymentEdition
from graphon.enums import BuiltinNodeTypes, NodeType
+from libs.helper import escape_like_pattern
from libs.infinite_scroll_pagination import InfiniteScrollPagination
from models import Account, TagBinding
from models.agent import (
@@ -233,8 +234,10 @@ class SnippetService:
)
if keyword:
+ escaped_keyword = escape_like_pattern(keyword)
stmt = stmt.where(
- CustomizedSnippet.name.ilike(f"%{keyword}%") | CustomizedSnippet.description.ilike(f"%{keyword}%")
+ CustomizedSnippet.name.ilike(f"%{escaped_keyword}%", escape="\\")
+ | CustomizedSnippet.description.ilike(f"%{escaped_keyword}%", escape="\\")
)
if is_published is not None:
diff --git a/api/tests/unit_tests/services/test_snippet_service.py b/api/tests/unit_tests/services/test_snippet_service.py
index 2fd63128376..885282527b7 100644
--- a/api/tests/unit_tests/services/test_snippet_service.py
+++ b/api/tests/unit_tests/services/test_snippet_service.py
@@ -37,6 +37,7 @@ from models.workflow import (
from services.errors.app import IsDraftWorkflowError, WorkflowHashNotEqualError, WorkflowNotFoundError
from services.errors.workflow_service import DraftWorkflowDeletionError, WorkflowInUseError
from services.snippet_service import SnippetService
+from services.tag_application_service import TagApplicationService
from tests.helpers.unit.model_factories import make_account
@@ -180,6 +181,53 @@ def test_get_snippets_applies_filters_and_paginates(
get_target_ids.assert_called_once_with(tag_type="snippet", tenant_id="tenant-1", tag_ids=["tag-1"], match_all=True)
+ ("keyword", "expected_id"),
+ [
+ ("search%", "percent-literal"),
+ ("search_", "underscore-literal"),
+ ("C:\\p", "backslash-literal"),
+ ],
+ ids=["percent", "underscore", "backslash"],
+)
+def test_get_snippets_keyword_treats_like_wildcards_literally(
+ application_tags: TagApplicationService,
+ sqlite_session: Session,
+ keyword: str,
+ expected_id: str,
+) -> None:
+ snippet_names = {
+ "percent-literal": "search% term",
+ "percent-decoy": "search term",
+ "underscore-literal": "search_term",
+ "underscore-decoy": "searchXterm",
+ "backslash-literal": "C:\\path",
+ "backslash-decoy": "C:path",
+ }
+ for snippet_id, name in snippet_names.items():
+ sqlite_session.add(
+ CustomizedSnippet(
+ id=snippet_id,
+ tenant_id="tenant-1",
+ name=name,
+ description="",
+ type=SnippetType.NODE,
+ created_by="account-1",
+ is_published=True,
+ )
+ )
+ sqlite_session.flush()
+ service = SnippetService(session=sqlite_session)
+
+ result, total, has_more = service.get_snippets(
+ tenant_id="tenant-1", session=sqlite_session, keyword=keyword, tags=application_tags
+ )
+
+ assert {s.id for s in result} == {expected_id}
+ assert total == 1
+ assert has_more is False
+
+
def test_update_snippet_allows_duplicate_names(sqlite_session: Session) -> None:
snippet = _snippet()
other = CustomizedSnippet(