fix(运行时): 将插件路径匹配移到宿主

新增宿主侧精确路径匹配和插件级 match.paths 配置。\n按请求路径固定插件会话链,移除 Lua 插件内的路径判断与 bypass 状态,并迁移现有插件配置和测试。
This commit is contained in:
starrrLight committed 2026-10-15 15:49:05 +08:00
1 parent f905aa22cc
commit d1f5f69454
12 files changed
+126 -200

No files matched your search

+20 -1
View File
@@ -3,7 +3,7 @@ from pathlib import Path
import pytest
from pydantic import ValidationError
from middleware.config import NetworkRule, Settings, load_settings
from middleware.config import NetworkRule, PluginConfig, PluginMatch, Settings, load_settings
def test_configuration_and_paths(tmp_path, monkeypatch):
@@ -73,6 +73,25 @@ def test_duplicate_ids_and_missing_file(tmp_path):
)
def test_plugin_match_paths_are_exact_and_legacy_paths_are_rejected(tmp_path):
plugin = tmp_path / "plugin.lua"
plugin.write_text("return {}", encoding="utf-8")
match = PluginMatch(paths=["/chat/stream"])
assert match.matches("/chat/stream")
assert not match.matches("/chat/stream/")
assert not match.matches("/chat/stream?token=x")
with pytest.raises(ValidationError):
PluginMatch(paths=[])
for invalid in ["chat/stream", "/chat/*", "/chat/stream?x=1", "/chat/stream#part"]:
with pytest.raises(ValidationError):
PluginMatch(paths=[invalid])
with pytest.raises(ValidationError, match="config.paths.*match.paths"):
PluginConfig(id="legacy", file=plugin, config={"paths": ["/chat/stream"]})
@pytest.mark.parametrize("domain", ["*", "bad/example.com", "foo..com", "127.0.0.1", "*.com*"])
def test_bad_domains(domain):
with pytest.raises(ValueError):
+31
View File
@@ -57,6 +57,37 @@ async def test_chain_isolation_json_and_disabled(tmp_path):
assert not manager.sessions
async def test_host_selects_plugins_once_by_exact_path(tmp_path):
first = tmp_path / "first.lua"
second = tmp_path / "second.lua"
first.write_text(
"return {on_request=function(ctx,r) r.body=r.body..' first'; return {action='replace',value=r} end}",
encoding="utf-8",
)
second.write_text(
"return {on_request=function(ctx,r) r.body=r.body..' second'; return {action='replace',value=r} end}",
encoding="utf-8",
)
settings = Settings(
upstream="http://agent:8000",
plugins=[
PluginConfig(id="first", file=first, match={"paths": ["/first"]}),
PluginConfig(id="second", file=second, match={"paths": ["/second"]}),
],
)
manager = PluginManager(settings)
first_session = await manager.open(path="/first")
try:
assert await first_session.apply("on_request", {"body": "x"}) == [{"body": "x first"}]
finally:
await first_session.aclose()
second_session = await manager.open(path="/second?ignored=true")
try:
assert await second_session.apply("on_request", {"body": "x"}) == [{"body": "x"}]
finally:
await second_session.aclose()
async def test_hold_flush_runs_downstream(tmp_path):
manager = manager_for(
tmp_path,