Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

refactor(memory): remove unused URI allowlist (#4536)

bot-of-qin-ctxcb7fe2ad8d6e
diff --git a/openviking/session/memory/__init__.py b/openviking/session/memory/__init__.py
index 719d3b33f..16170c25a 100644
--- a/openviking/session/memory/__init__.py
+++ b/openviking/session/memory/__init__.py
@@ -46,7 +46,6 @@ from openviking.session.memory.tools import (
from openviking.session.memory.utils import (
detect_language_from_conversation,
generate_uri,
- is_uri_allowed,
pretty_print_messages,
validate_uri_template,
)
@@ -93,5 +92,4 @@ __all__ = [
# URI utilities
"generate_uri",
"validate_uri_template",
- "is_uri_allowed",
]
diff --git a/openviking/session/memory/utils/__init__.py b/openviking/session/memory/utils/__init__.py
index e7289612d..761917e67 100644
--- a/openviking/session/memory/utils/__init__.py
+++ b/openviking/session/memory/utils/__init__.py
@@ -63,7 +63,6 @@ __all__ = [
# URI
"generate_uri",
"validate_uri_template",
- "is_uri_allowed",
# JSON Parser
"extract_json_content",
"parse_json_with_stability",
@@ -84,16 +83,14 @@ def __getattr__(name: str):
from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils
return MemoryFileUtils
- if name in {"generate_uri", "is_uri_allowed", "validate_uri_template"}:
+ if name in {"generate_uri", "validate_uri_template"}:
from openviking.session.memory.utils.uri import (
generate_uri,
- is_uri_allowed,
validate_uri_template,
)
return {
"generate_uri": generate_uri,
- "is_uri_allowed": is_uri_allowed,
"validate_uri_template": validate_uri_template,
}[name]
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
diff --git a/openviking/session/memory/utils/uri.py b/openviking/session/memory/utils/uri.py
index feb724efc..947c64743 100644
--- a/openviking/session/memory/utils/uri.py
+++ b/openviking/session/memory/utils/uri.py
@@ -8,7 +8,7 @@ from __future__ import annotations
import hashlib
import re
-from typing import Any, Dict, Set
+from typing import Any, Dict
from openviking.session.memory.dataclass import MemoryTypeSchema
from openviking.session.memory.utils.model import model_to_dict
@@ -218,69 +218,6 @@ def validate_uri_template(memory_type: MemoryTypeSchema) -> bool:
return True
-def _pattern_matches_uri(pattern: str, uri: str) -> bool:
- """
- Check if a URI matches a pattern with variables like {{ topic }}, {{ tool_name }}, etc.
-
- The pattern matching is flexible:
- - {{ variable }} matches any sequence of characters except '/'
- - * matches any sequence of characters except '/' (shell-style)
- - ** matches any sequence of characters including '/' (shell-style)
-
- Args:
- pattern: The pattern to match against (may contain {{ variables }} or * wildcards)
- uri: The URI to check
-
- Returns:
- True if the URI matches the pattern
- """
- import re
-
- # First, convert the pattern to a regex
- # Escape regex special chars except {, }, *, /
- pattern = re.escape(pattern)
- # Unescape {, }, * that we need to handle specially
- pattern = pattern.replace(r"\{", "{").replace(r"\}", "}").replace(r"\*", "*")
- # Convert {{ variable }} to [^/]+
- pattern = re.sub(r"\{\{\s*[^}]+\s*\}\}", r"[^/]+", pattern)
- # Also support legacy {variable} format
- pattern = re.sub(r"\{[^}]+\}", r"[^/]+", pattern)
- # Convert ** to .* and * to [^/]*
- pattern = pattern.replace("**", ".*")
- pattern = pattern.replace("*", "[^/]*")
- # Anchor the pattern
- pattern = "^" + pattern + "$"
-
- return bool(re.match(pattern, uri))
-
-
-def is_uri_allowed(
- uri: str,
- allowed_directories: Set[str],
- allowed_patterns: Set[str],
-) -> bool:
- """
- Check if a URI is allowed based on allowed directories and patterns.
-
- Args:
- uri: The URI to check
- allowed_directories: Set of allowed directory paths
- allowed_patterns: Set of allowed path patterns
-
- Returns:
- True if the URI is allowed
- """
- # Check if URI starts with any allowed directory
- for dir_path in allowed_directories:
- if uri == dir_path or uri.startswith(dir_path + "/"):
- return True
- # Check if URI matches any allowed pattern
- for pattern in allowed_patterns:
- if _pattern_matches_uri(pattern, uri):
- return True
- return False
-
-
def extract_uri_fields_from_flat_model(model: Any, schema: MemoryTypeSchema) -> Dict[str, Any]:
"""
Extract URI-friendly fields from a flat model, ignoring patch objects.
diff --git a/tests/session/memory/test_memory_utils.py b/tests/session/memory/test_memory_utils.py
index 1b7a3f61c..dcd3cce60 100644
--- a/tests/session/memory/test_memory_utils.py
+++ b/tests/session/memory/test_memory_utils.py
@@ -14,7 +14,6 @@ from openviking.session.memory.dataclass import (
from openviking.session.memory.merge_op.base import FieldType, MergeOp
from openviking.session.memory.utils import (
generate_uri,
- is_uri_allowed,
parse_memory_file_with_fields,
validate_uri_template,
)
@@ -304,86 +303,6 @@ class TestUriGeneration:
assert validate_uri_template(memory_type) is False
-class TestUriValidation:
- """Tests for URI validation."""
-
- def test_is_uri_allowed_by_directory(self):
- """Test URI allowed by matching directory prefix."""
- allowed_dirs = {
- "viking://user/default/memories/preferences",
- "viking://user/default/memories/tools",
- }
- allowed_patterns = set()
-
- assert (
- is_uri_allowed(
- "viking://user/default/memories/preferences/test.md",
- allowed_dirs,
- allowed_patterns,
- )
- is True
- )
-
- assert (
- is_uri_allowed(
- "viking://user/default/memories/preferences",
- allowed_dirs,
- allowed_patterns,
- )
- is True
- )
-
- assert (
- is_uri_allowed(
- "viking://user/default/memories/preferences/subdir/test.md",
- allowed_dirs,
- allowed_patterns,
- )
- is True
- )
-
- def test_is_uri_allowed_by_pattern(self):
- """Test URI allowed by matching pattern."""
- allowed_dirs = set()
- allowed_patterns = {
- "viking://user/default/memories/preferences/{{ topic }}.md",
- }
-
- assert (
- is_uri_allowed(
- "viking://user/default/memories/preferences/Python code style.md",
- allowed_dirs,
- allowed_patterns,
- )
- is True
- )
-
- def test_is_uri_disallowed(self):
- """Test URI not allowed."""
- allowed_dirs = {
- "viking://user/default/memories/preferences",
- }
- allowed_patterns = set()
-
- assert (
- is_uri_allowed(
- "viking://user/default/memories/other/test.md",
- allowed_dirs,
- allowed_patterns,
- )
- is False
- )
-
- assert (
- is_uri_allowed(
- "viking://user/other/memories/preferences/test.md",
- allowed_dirs,
- allowed_patterns,
- )
- is False
- )
-
-
class TestParseMemoryFileWithFields:
"""Tests for parse_memory_file_with_fields function."""