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: fix skill search not escape (#44922)

wangxiaolei85ae1eb16471
diff --git a/api/services/skill_management_service.py b/api/services/skill_management_service.py
index 18358d46120..b6a2b59c6d9 100644
--- a/api/services/skill_management_service.py
+++ b/api/services/skill_management_service.py
@@ -56,6 +56,7 @@ from graphon.model_runtime.entities.model_entities import ModelFeature, ModelTyp
from graphon.model_runtime.errors.invoke import InvokeError
from graphon.nodes.llm.reasoning import split_reasoning
from libs.datetime_utils import naive_utc_now
+from libs.helper import escape_like_pattern
from models.account import Account
from models.agent import (
Agent,
@@ -738,9 +739,11 @@ class SkillManagementService:
with self._session_scope() as session:
stmt = select(Skill).where(Skill.tenant_id == tenant_id).order_by(Skill.updated_at.desc())
if keyword:
- like = f"%{keyword.strip()}%"
+ like = f"%{escape_like_pattern(keyword.strip())}%"
stmt = stmt.where(
- (Skill.name.ilike(like)) | (Skill.display_name.ilike(like)) | (Skill.description.ilike(like))
+ (Skill.name.ilike(like, escape="\\"))
+ | (Skill.display_name.ilike(like, escape="\\"))
+ | (Skill.description.ilike(like, escape="\\"))
)
requested_tags = self._normalize_tags(tags or [])
if requested_tags:
diff --git a/api/tests/unit_tests/services/test_skill_management_service.py b/api/tests/unit_tests/services/test_skill_management_service.py
index 452266a3644..61e9b36840c 100644
--- a/api/tests/unit_tests/services/test_skill_management_service.py
+++ b/api/tests/unit_tests/services/test_skill_management_service.py
@@ -365,6 +365,36 @@ def test_list_skills_keyword_matches_display_name() -> None:
assert [item["name"] for item in result["data"]] == ["employee-onboarding"]
[email protected]("field", ["display_name", "description"])
+ ("keyword", "matching", "nonmatching"),
+ [
+ ("%", "Report % growth", "Report growth"),
+ ("my_key", "prefix my_key suffix", "prefix my key suffix"),
+ ("\\", r"path\file", "path/file"),
+ (r"\%_", r"prefix \%_ suffix", "prefix %_ suffix"),
+ (" MY_KEY ", "prefix my_key suffix", "prefix my key suffix"),
+ (" GROWTH ", "Report growth figures", "Report figures"),
+ ],
+)
+def test_list_skills_keyword_matches_literal_substrings(
+ field: str, keyword: str, matching: str, nonmatching: str
+) -> None:
+ service = SkillManagementService(tool_file_manager=_FakeToolFileManager())
+ for name, value in [("matching-skill", matching), ("other-skill", nonmatching)]:
+ service.create_skill(
+ tenant_id=TENANT,
+ user_id=USER,
+ payload=SkillCreatePayload.model_validate({"name": name, field: value}),
+ )
+
+ result = service.list_skills(tenant_id=TENANT, keyword=keyword, limit=1)
+
+ assert [item["name"] for item in result["data"]] == ["matching-skill"]
+ assert result["total"] == 1
+ assert result["has_more"] is False
+
+
def test_list_skills_returns_pagination_metadata() -> None:
service = SkillManagementService(tool_file_manager=_FakeToolFileManager())
for name in ["alpha-skill", "beta-skill", "gamma-skill"]: