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.

refactor(api): enable strict typing for core helper tests (#44390)

Eddy ZHANG8197cb65038a
diff --git a/api/tests/unit_tests/core/helper/code_executor/javascript/test_javascript_transformer.py b/api/tests/unit_tests/core/helper/code_executor/javascript/test_javascript_transformer.py
index 03f37756d74..8a90fe903b3 100644
--- a/api/tests/unit_tests/core/helper/code_executor/javascript/test_javascript_transformer.py
+++ b/api/tests/unit_tests/core/helper/code_executor/javascript/test_javascript_transformer.py
@@ -2,7 +2,7 @@ from core.helper.code_executor.javascript.javascript_code_provider import Javasc
from core.helper.code_executor.javascript.javascript_transformer import NodeJsTemplateTransformer
-def test_get_runner_script():
+def test_get_runner_script() -> None:
code = JavascriptCodeProvider.get_default_code()
inputs = {"arg1": "hello, ", "arg2": "world!"}
script = NodeJsTemplateTransformer.assemble_runner_script(code, inputs)
diff --git a/api/tests/unit_tests/core/helper/code_executor/jinja2/test_jinja2_sandbox.py b/api/tests/unit_tests/core/helper/code_executor/jinja2/test_jinja2_sandbox.py
index bb80c9c9800..9230d5c5b71 100644
--- a/api/tests/unit_tests/core/helper/code_executor/jinja2/test_jinja2_sandbox.py
+++ b/api/tests/unit_tests/core/helper/code_executor/jinja2/test_jinja2_sandbox.py
@@ -29,7 +29,7 @@ MALICIOUS_TEMPLATES = [
def _exec_scripts(runner: str, preload: str) -> str:
"""Execute preload then runner in a shared namespace, return captured stdout."""
- ns: dict = {}
+ ns: dict[str, object] = {}
exec(compile(preload, "<preload>", "exec"), ns) # noqa: S102
captured = io.StringIO()
old_stdout = sys.stdout
@@ -47,7 +47,7 @@ class TestJinja2TransformCallerSandbox:
@pytest.mark.parametrize("malicious_template", MALICIOUS_TEMPLATES)
def test_blocks_unsafe_template(self, malicious_template: str) -> None:
runner, preload = Jinja2TemplateTransformer.transform_caller(malicious_template, {})
- ns: dict = {}
+ ns: dict[str, object] = {}
exec(compile(preload, "<preload>", "exec"), ns) # noqa: S102
with pytest.raises(
Exception,
diff --git a/api/tests/unit_tests/core/helper/code_executor/python3/test_python3_transformer.py b/api/tests/unit_tests/core/helper/code_executor/python3/test_python3_transformer.py
index 99bd369c142..fab1181da5a 100644
--- a/api/tests/unit_tests/core/helper/code_executor/python3/test_python3_transformer.py
+++ b/api/tests/unit_tests/core/helper/code_executor/python3/test_python3_transformer.py
@@ -2,7 +2,7 @@ from core.helper.code_executor.python3.python3_code_provider import Python3CodeP
from core.helper.code_executor.python3.python3_transformer import Python3TemplateTransformer
-def test_get_runner_script():
+def test_get_runner_script() -> None:
code = Python3CodeProvider.get_default_code()
inputs = {"arg1": "hello, ", "arg2": "world!"}
script = Python3TemplateTransformer.assemble_runner_script(code, inputs)
@@ -12,7 +12,7 @@ def test_get_runner_script():
assert script_lines[: len(code_lines)] == code_lines
-def test_inputs_placeholder_inside_user_code_is_preserved():
+def test_inputs_placeholder_inside_user_code_is_preserved() -> None:
# User code contains a string literal with `{{inputs}}` — it must survive
# the runner assembly untouched (regression for #43435: the inputs
# substitution used to run after code insertion and corrupted the literal).
diff --git a/api/tests/unit_tests/core/helper/test_credential_utils.py b/api/tests/unit_tests/core/helper/test_credential_utils.py
index 1921ed99ae8..9b09b6f3bb9 100644
--- a/api/tests/unit_tests/core/helper/test_credential_utils.py
+++ b/api/tests/unit_tests/core/helper/test_credential_utils.py
@@ -1,4 +1,4 @@
-from typing import cast
+from typing import NoReturn, cast
import pytest
from pytest_mock import MockerFixture
@@ -172,7 +172,7 @@ def test_is_credential_exists_uses_configured_session_factory_without_flask_app_
) -> None:
class RaisingDB:
@property
- def engine(self):
+ def engine(self) -> NoReturn:
raise RuntimeError("Working outside of application context.")
_persist_credential(sqlite_session, PluginCredentialType.MODEL)
diff --git a/api/tests/unit_tests/core/helper/test_encrypter.py b/api/tests/unit_tests/core/helper/test_encrypter.py
index b99d599d108..d05b837d2b6 100644
--- a/api/tests/unit_tests/core/helper/test_encrypter.py
+++ b/api/tests/unit_tests/core/helper/test_encrypter.py
@@ -1,6 +1,6 @@
import base64
import binascii
-from unittest.mock import patch
+from unittest.mock import MagicMock, patch
import pytest
from Crypto.PublicKey import RSA
@@ -35,6 +35,15 @@ def rsa_key() -> RSA.RsaKey:
return RSA.generate(2048)
[email protected](scope="module")
+def other_rsa_key() -> RSA.RsaKey:
+ return RSA.generate(2048)
+
+
+def _public_pem(key: RSA.RsaKey) -> str:
+ return key.publickey().export_key().decode()
+
+
def _persist_tenant(session: Session, *, public_key: str = "mock_public_key") -> Tenant:
tenant = Tenant(name="Test tenant", encrypt_public_key=public_key)
tenant.id = "tenant-123"
@@ -54,11 +63,11 @@ class TestObfuscatedToken:
("abc!@#$%^&*()def", "abc!@#" + "*" * 12 + "ef"), # Special chars
],
)
- def test_obfuscation_logic(self, token, expected):
+ def test_obfuscation_logic(self, token: str, expected: str) -> None:
"""Test core obfuscation logic for various token lengths"""
assert obfuscated_token(token) == expected
- def test_sensitive_data_protection(self):
+ def test_sensitive_data_protection(self) -> None:
"""Ensure obfuscation never reveals full sensitive data"""
token = "api_key_secret_12345"
obfuscated = obfuscated_token(token)
@@ -68,7 +77,7 @@ class TestObfuscatedToken:
class TestEncryptToken:
@patch("libs.rsa.encrypt")
- def test_successful_encryption(self, mock_encrypt, sqlite_session: Session):
+ def test_successful_encryption(self, mock_encrypt: MagicMock, sqlite_session: Session) -> None:
"""Test successful token encryption"""
_persist_tenant(sqlite_session)
mock_encrypt.return_value = b"encrypted_data"
@@ -78,7 +87,7 @@ class TestEncryptToken:
assert result == base64.b64encode(b"encrypted_data").decode()
mock_encrypt.assert_called_with("test_token", "mock_public_key")
- def test_tenant_not_found(self):
+ def test_tenant_not_found(self) -> None:
"""Test error when tenant doesn't exist"""
with pytest.raises(ValueError, match="Tenant with id invalid\\-tenant not found") as exc_info:
encrypt_token("invalid-tenant", "test_token")
@@ -88,7 +97,7 @@ class TestEncryptToken:
class TestDecryptToken:
@patch("libs.rsa.decrypt")
- def test_successful_decryption(self, mock_decrypt):
+ def test_successful_decryption(self, mock_decrypt: MagicMock) -> None:
"""Test successful token decryption"""
mock_decrypt.return_value = "decrypted_token"
encrypted_data = base64.b64encode(b"encrypted_data").decode()
@@ -98,7 +107,7 @@ class TestDecryptToken:
assert result == "decrypted_token"
mock_decrypt.assert_called_once_with(b"encrypted_data", "tenant-123")
- def test_invalid_base64(self):
+ def test_invalid_base64(self) -> None:
"""Test handling of invalid base64 input"""
with pytest.raises(binascii.Error):
decrypt_token("tenant-123", "invalid_base64!!!")
@@ -106,7 +115,7 @@ class TestDecryptToken:
class TestBatchDecryptToken:
@patch("libs.rsa.get_decrypt_decoding")
- def test_batch_decryption(self, mock_get_decoding, sqlite_session: Session, rsa_key: RSA.RsaKey):
+ def test_batch_decryption(self, mock_get_decoding: MagicMock, sqlite_session: Session, rsa_key: RSA.RsaKey) -> None:
"""Decrypt actual tenant ciphertext while loading the key only once."""
_persist_tenant(sqlite_session, public_key=rsa_key.publickey().export_key().decode())
mock_get_decoding.return_value = (rsa_key, gmpy2_pkcs10aep_cipher.new(rsa_key))
@@ -120,7 +129,7 @@ class TestBatchDecryptToken:
class TestGetDecryptDecoding:
@patch("extensions.ext_redis.redis_client.get")
@patch("extensions.ext_storage.storage.load")
- def test_private_key_not_found(self, mock_storage_load, mock_redis_get):
+ def test_private_key_not_found(self, mock_storage_load: MagicMock, mock_redis_get: MagicMock) -> None:
"""Test error when private key file doesn't exist"""
mock_redis_get.return_value = None
mock_storage_load.side_effect = FileNotFoundError()
@@ -134,7 +143,9 @@ class TestGetDecryptDecoding:
class TestEncryptDecryptIntegration:
@patch("libs.rsa.encrypt")
@patch("libs.rsa.decrypt")
- def test_should_encrypt_and_decrypt_consistently(self, mock_decrypt, mock_encrypt, sqlite_session: Session):
+ def test_should_encrypt_and_decrypt_consistently(
+ self, mock_decrypt: MagicMock, mock_encrypt: MagicMock, sqlite_session: Session
+ ) -> None:
"""Test that encryption and decryption are consistent"""
_persist_tenant(sqlite_session)
@@ -155,44 +166,40 @@ class TestEncryptDecryptIntegration:
class TestSecurity:
"""Critical security tests for encryption system"""
- @patch("libs.rsa.encrypt")
- def test_cross_tenant_isolation(self, mock_encrypt, sqlite_session: Session):
- """Ensure tokens encrypted for one tenant cannot be used by another"""
- _persist_tenant(sqlite_session, public_key="tenant1_public_key")
- mock_encrypt.return_value = b"encrypted_for_tenant1"
-
- # Encrypt token for tenant1
+ @patch("libs.rsa.get_decrypt_decoding")
+ def test_cross_tenant_isolation(
+ self, mock_get_decoding: MagicMock, sqlite_session: Session, rsa_key: RSA.RsaKey, other_rsa_key: RSA.RsaKey
+ ) -> None:
+ """Ensure tokens encrypted for one tenant cannot be decrypted with another tenant's private key"""
+ _persist_tenant(sqlite_session, public_key=_public_pem(rsa_key))
encrypted = encrypt_token("tenant-123", "sensitive_data")
+ mock_get_decoding.return_value = (other_rsa_key, gmpy2_pkcs10aep_cipher.new(other_rsa_key))
- # Attempt to decrypt with different tenant should fail
- with patch("libs.rsa.decrypt") as mock_decrypt:
- mock_decrypt.side_effect = Exception("Invalid tenant key")
+ with pytest.raises(ValueError, match="Incorrect decryption"):
+ decrypt_token("different-tenant", encrypted)
+ mock_get_decoding.assert_called_once_with("different-tenant")
- with pytest.raises(Exception, match="Invalid tenant key"):
- decrypt_token("different-tenant", encrypted)
-
- @patch("libs.rsa.decrypt")
- def test_tampered_ciphertext_rejection(self, mock_decrypt):
+ @patch("libs.rsa.get_decrypt_decoding")
+ def test_tampered_ciphertext_rejection(
+ self, mock_get_decoding: MagicMock, sqlite_session: Session, rsa_key: RSA.RsaKey
+ ) -> None:
"""Detect and reject tampered ciphertext"""
- valid_encrypted = base64.b64encode(b"valid_data").decode()
+ _persist_tenant(sqlite_session, public_key=_public_pem(rsa_key))
+ mock_get_decoding.return_value = (rsa_key, gmpy2_pkcs10aep_cipher.new(rsa_key))
+ valid_encrypted = encrypt_token("tenant-123", "valid_data")
- # Tamper with ciphertext
+ # Flip the last byte, which belongs to the AES-EAX ciphertext the tag authenticates
tampered_bytes = bytearray(base64.b64decode(valid_encrypted))
- tampered_bytes[0] ^= 0xFF
+ tampered_bytes[-1] ^= 0xFF
tampered = base64.b64encode(bytes(tampered_bytes)).decode()
- mock_decrypt.side_effect = Exception("Decryption error")
-
- with pytest.raises(Exception, match="Decryption error"):
+ assert decrypt_token("tenant-123", valid_encrypted) == "valid_data"
+ with pytest.raises(ValueError, match="MAC check failed"):
decrypt_token("tenant-123", tampered)
- @patch("libs.rsa.encrypt")
- def test_encryption_randomness(self, mock_encrypt, sqlite_session: Session):
+ def test_encryption_randomness(self, sqlite_session: Session, rsa_key: RSA.RsaKey) -> None:
"""Ensure same plaintext produces different ciphertext"""
- _persist_tenant(sqlite_session, public_key="key")
-
- # Different outputs for same input
- mock_encrypt.side_effect = [b"enc1", b"enc2", b"enc3"]
+ _persist_tenant(sqlite_session, public_key=_public_pem(rsa_key))
results = [encrypt_token("tenant-123", "token") for _ in range(3)]
@@ -203,13 +210,13 @@ class TestSecurity:
class TestEdgeCases:
"""Additional security-focused edge case tests"""
- def test_should_handle_empty_string_in_obfuscation(self):
+ def test_should_handle_empty_string_in_obfuscation(self) -> None:
"""Test handling of empty string in obfuscation"""
# Test empty string (which is a valid str type)
assert obfuscated_token("") == ""
@patch("libs.rsa.encrypt")
- def test_should_handle_empty_token_encryption(self, mock_encrypt, sqlite_session: Session):
+ def test_should_handle_empty_token_encryption(self, mock_encrypt: MagicMock, sqlite_session: Session) -> None:
"""Test encryption of empty token"""
_persist_tenant(sqlite_session)
mock_encrypt.return_value = b"encrypted_empty"
@@ -220,7 +227,7 @@ class TestEdgeCases:
mock_encrypt.assert_called_with("", "mock_public_key")
@patch("libs.rsa.encrypt")
- def test_should_handle_special_characters_in_token(self, mock_encrypt, sqlite_session: Session):
+ def test_should_handle_special_characters_in_token(self, mock_encrypt: MagicMock, sqlite_session: Session) -> None:
"""Test tokens containing special/unicode characters"""
_persist_tenant(sqlite_session)
mock_encrypt.return_value = b"encrypted_special"
@@ -239,25 +246,24 @@ class TestEdgeCases:
assert result == base64.b64encode(b"encrypted_special").decode()
mock_encrypt.assert_called_with(token, "mock_public_key")
- @patch("libs.rsa.encrypt")
- def test_should_handle_rsa_size_limits(self, mock_encrypt, sqlite_session: Session):
- """Test behavior when token exceeds RSA encryption limits"""
- _persist_tenant(sqlite_session)
-
- # RSA 2048-bit can only encrypt ~245 bytes
- # The actual limit depends on padding scheme
- mock_encrypt.side_effect = ValueError("Message too long for RSA key size")
+ @patch("libs.rsa.get_decrypt_decoding")
+ def test_should_handle_rsa_size_limits(
+ self, mock_get_decoding: MagicMock, sqlite_session: Session, rsa_key: RSA.RsaKey
+ ) -> None:
+ """Tokens longer than RSA-OAEP can encrypt directly still round-trip through the hybrid scheme"""
+ _persist_tenant(sqlite_session, public_key=_public_pem(rsa_key))
+ mock_get_decoding.return_value = (rsa_key, gmpy2_pkcs10aep_cipher.new(rsa_key))
- # Create a token that would exceed RSA limits
+ # RSA-OAEP with a 2048-bit key and SHA-1 takes at most 214 bytes; libs.rsa encrypts the
+ # token with AES and only the AES key with RSA.
long_token = "x" * 300
- with pytest.raises(ValueError, match="Message too long for RSA key size"):
- encrypt_token("tenant-123", long_token)
+ assert decrypt_token("tenant-123", encrypt_token("tenant-123", long_token)) == long_token
@patch("libs.rsa.get_decrypt_decoding")
def test_batch_decrypt_loads_key_only_once(
- self, mock_get_decoding, sqlite_session: Session, rsa_key: RSA.RsaKey, mocker: MockerFixture
- ):
+ self, mock_get_decoding: MagicMock, sqlite_session: Session, rsa_key: RSA.RsaKey, mocker: MockerFixture
+ ) -> None:
"""Reuse one decoding context across every actual ciphertext in the batch."""
_persist_tenant(sqlite_session, public_key=rsa_key.publickey().export_key().decode())
cipher_rsa = gmpy2_pkcs10aep_cipher.new(rsa_key)
diff --git a/api/tests/unit_tests/pyrefly.toml b/api/tests/unit_tests/pyrefly.toml
index 8245c53b4ba..eebf7861ce3 100644
--- a/api/tests/unit_tests/pyrefly.toml
+++ b/api/tests/unit_tests/pyrefly.toml
@@ -249,13 +249,7 @@ project-excludes = [
"core/extension/test_api_based_extension_requestor.py",
"core/external_data_tool/test_external_data_fetch.py",
"core/external_data_tool/test_factory.py",
- "core/helper/code_executor/javascript/test_javascript_transformer.py",
- "core/helper/code_executor/jinja2/test_jinja2_sandbox.py",
- "core/helper/code_executor/python3/test_python3_transformer.py",
"core/helper/code_executor/test_code_executor.py",
- "core/helper/test_creators.py",
- "core/helper/test_credential_utils.py",
- "core/helper/test_encrypter.py",
"core/helper/test_ssrf_proxy.py",
"core/llm_generator/output_parser/test_rule_config_generator.py",
"core/llm_generator/output_parser/test_structured_output.py",