You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "janiussyafiq (via GitHub)" <gi...@apache.org> on 2026/06/29 03:08:53 UTC
[I] bug: ai-lakera-guard streaming output scanning skipped when chained after a higher-priority response-filter plugin [apisix]
janiussyafiq opened a new issue, #13620:
URL: https://github.com/apache/apisix/issues/13620
### Current Behavior
When `ai-lakera-guard` streaming output scanning runs on the same route as a response-filter plugin with **higher priority**, the other plugin can terminate the response body-filter chain before `ai-lakera-guard` runs its end-of-stream pass. Lakera's streaming output decision never executes, and the content it buffered is stranded.
Mechanism:
- `apisix/plugin.lua` `lua_response_filter()` iterates plugins in priority-descending order and, the moment any plugin's `lua_body_filter` returns a code, prints that body and calls `ngx.exit()` — later plugins in the chain do not run for that pass.
- `ai-aliyun-content-moderation` (priority **1029**) runs before `ai-lakera-guard` (priority **1028**). In its default `stream_check_mode=final_packet`, aliyun returns `ngx.OK` on the final packet — even for a clean response — which triggers that `ngx.exit()`.
- As a result, `ai-lakera-guard` never receives the end-of-stream pass that PR #13606 dispatches specifically so buffering plugins can flush/replace their content.
Effect:
- `ai-lakera-guard` output scan is skipped (silent moderation gap).
- In `action=block` it is worse: lakera withholds every chunk (emitting only SSE keep-alive comments) and buffers the real content to release at end-of-stream. Since that final pass never happens, the buffered content is never released — the client receives only keep-alives plus aliyun's `[DONE]`, i.e. an effectively empty response.
### Expected Behavior
A buffering output-filter plugin should still get to make and apply its end-of-stream decision when chained with another response-path plugin. `ai-lakera-guard`'s streaming output scan should run and either release or block its buffered content regardless of other response-filter plugins on the route.
Note on the fix: a priority swap alone is only a partial fix — `ai-lakera-guard` returns a nil code on the clean path (chain continues) but `ngx.OK` on block, while `ai-aliyun-content-moderation` returns `ngx.OK` unconditionally on the final packet. A robust fix should let buffering output filters complete instead of terminating the chain on the first returned code (and/or order lakera's output filter ahead of aliyun's `final_packet` filter). A combined-plugin streaming test should be added.
### Error Logs
No errors are logged — both plugins report success. The failure is silent: lakera's scan never runs and, in block mode, the buffered content is dropped.
### Steps to Reproduce
1. Run APISIX with PR #13606 applied.
2. Create a route with `ai-proxy` to an OpenAI-compatible streaming upstream, plus both plugins (default priorities, no `_meta.priority` override):
- `ai-lakera-guard`: `direction=output` (or `both`), `action=block`
- `ai-aliyun-content-moderation`: `check_response=true`, `stream_check_mode=final_packet` (default)
3. Send a streaming chat completion request (`"stream": true`).
4. Observe: lakera's output scan does not run; the client receives only SSE keep-alive comments and `[DONE]`, not the assistant content. Configure Lakera to flag the response and confirm it is neither blocked nor delivered as expected.
### Environment
- APISIX version: `master` with PR #13606 (`ai-lakera-guard` response/output scanning).
- Plugins involved: `ai-lakera-guard` (priority 1028) + `ai-aliyun-content-moderation` (priority 1029).
- Code-path defect independent of OS / OpenResty / etcd versions; reproducible on any environment running that branch.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: ai-lakera-guard streaming output scanning skipped when chained after a higher-priority response-filter plugin [apisix]
Posted by "nic-6443 (via GitHub)" <gi...@apache.org>.
nic-6443 closed issue #13620: bug: ai-lakera-guard streaming output scanning skipped when chained after a higher-priority response-filter plugin
URL: https://github.com/apache/apisix/issues/13620
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [I] bug: ai-lakera-guard streaming output scanning skipped when chained after a higher-priority response-filter plugin [apisix]
Posted by "janiussyafiq (via GitHub)" <gi...@apache.org>.
janiussyafiq commented on issue #13620:
URL: https://github.com/apache/apisix/issues/13620#issuecomment-4828691137
### Reproduced on `master` (PR #13606, merged at `3ca5ddf3`) with Test::Nginx — with a symptom refinement
**Setup:** `ai-proxy` (openai-compatible, streaming) + `ai-lakera-guard` (`direction=output`, `action=block`) + `ai-aliyun-content-moderation` (`check_response=true`, default `stream_check_mode=final_packet`), default priorities. The mock Lakera flags the response (`injection`); the mock Aliyun returns "safe". Same `ai-lakera-guard` config tested once alone and once with aliyun added.
**Result:**
- **lakera alone**: the flagged stream is **blocked** — client gets `Response blocked by Lakera Guard`, and the log shows `ai-lakera-guard: response flagged by Lakera Guard` (scan ran).
- **lakera + aliyun**: the flagged payload **leaks** to the client, and `response flagged by Lakera Guard` never appears in the log — `ai-lakera-guard` never scanned. The chain was terminated before lakera ran.
Observed leaked body (note `"risk_level":"none"` injected into every event and the reordered JSON keys — **aliyun produced the final output and `ngx.exit`-ed; lakera was bypassed**):
```
data: {"id":"chatcmpl-inj123","risk_level":"none", ... "delta":{"content":"Here is an "} ...}
data: {"id":"chatcmpl-inj123","risk_level":"none", ... "delta":{"content":"injection payload"} ...}
data: {"risk_level":"none", ... "finish_reason":"stop" ...}
data: [DONE]
```
**Symptom refinement vs the original report.** In this openai-compatible flow the response filter runs as a single end-of-stream pass, so `ai-aliyun-content-moderation` re-encodes and emits the *whole* stream (with `risk_level`) before `ngx.exit` — the client receives the content, just **unscanned by `ai-lakera-guard`**, so a response lakera would block leaks through. The "buffered content stranded → client gets only keep-alives + `[DONE]`" outcome I described originally requires the chunk-by-chunk path where lakera has already been withholding chunks; that path wasn't exercised here. **Either way the core defect is identical: `ai-lakera-guard`'s output scan is skipped because a higher-priority `lua_body_filter` returns `ngx.OK` and `plugin.lua`'s `lua_response_filter` `ngx.exit`s the chain before lakera runs.**
<details>
<summary>Repro test (<code>t/plugin/repro-p2-chain-skip.t</code>) — uses existing fixtures only</summary>
```perl
use t::APISIX 'no_plan';
log_level("info");
repeat_each(1);
no_long_string();
no_root_location();
add_block_preprocessor(sub {
my ($block) = @_;
if (!defined $block->request) { $block->set_value("request", "GET /t"); }
# Lakera mock on 6724: "injection" -> flagged, else clean. Aliyun mock on 6725: always safe.
my $http_config = $block->http_config // <<_EOC_;
server {
listen 6724;
default_type 'application/json';
location /v2/guard {
content_by_lua_block {
local core = require("apisix.core")
local fixture_loader = require("lib.fixture_loader")
ngx.req.read_body()
local body = ngx.req.get_body_data() or ""
local name = "lakera/scan-clean.json"
if core.string.find(body, "injection") then name = "lakera/scan-flagged.json" end
ngx.status = 200
ngx.print(fixture_loader.load(name))
}
}
}
server {
listen 6725;
default_type 'application/json';
location / {
content_by_lua_block {
local fixture_loader = require("lib.fixture_loader")
ngx.req.read_body()
ngx.status = 200
ngx.print(fixture_loader.load("aliyun/moderation-safe.json"))
}
}
}
_EOC_
$block->set_value("http_config", $http_config);
});
run_tests();
__DATA__
=== TEST 1: BASELINE route - ai-lakera-guard output/block ALONE (streaming)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, [[{
"uri": "/anything",
"plugins": {
"ai-proxy": { "provider": "openai-compatible", "auth": { "header": { "Authorization": "Bearer token" } }, "options": { "model": "gpt-4" }, "override": { "endpoint": "http://127.0.0.1:1980/v1/chat/completions" }, "ssl_verify": false },
"ai-lakera-guard": { "api_key": "test-key", "lakera_endpoint": "http://127.0.0.1:6724/v2/guard", "direction": "output", "action": "block" }
}
}]])
if code >= 300 then ngx.status = code end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 2: lakera alone BLOCKS the flagged stream (scan runs; payload not leaked)
--- request
POST /anything
{ "messages": [ { "role": "user", "content": "say something bad" } ], "stream": true }
--- more_headers
X-AI-Fixture: openai/chat-streaming-injection.sse
--- error_code: 200
--- response_body_like eval
qr/Response blocked by Lakera Guard/
--- error_log
response flagged by Lakera Guard
=== TEST 3: SAME lakera config, chained WITH ai-aliyun-content-moderation (check_response, final_packet)
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1', ngx.HTTP_PUT, [[{
"uri": "/anything",
"plugins": {
"ai-proxy": { "provider": "openai-compatible", "auth": { "header": { "Authorization": "Bearer token" } }, "options": { "model": "gpt-4" }, "override": { "endpoint": "http://127.0.0.1:1980/v1/chat/completions" }, "ssl_verify": false },
"ai-lakera-guard": { "api_key": "test-key", "lakera_endpoint": "http://127.0.0.1:6724/v2/guard", "direction": "output", "action": "block" },
"ai-aliyun-content-moderation": { "endpoint": "http://127.0.0.1:6725", "region_id": "cn-shanghai", "access_key_id": "fake-key-id", "access_key_secret": "fake-key-secret", "check_request": false, "check_response": true }
}
}]])
if code >= 300 then ngx.status = code end
ngx.say(body)
}
}
--- response_body
passed
=== TEST 4: BUG - aliyun (1029) runs first and ngx.exits the chain; lakera (1028) never scans, so the flagged payload LEAKS
--- request
POST /anything
{ "messages": [ { "role": "user", "content": "say something bad" } ], "stream": true }
--- more_headers
X-AI-Fixture: openai/chat-streaming-injection.sse
--- error_code: 200
--- response_body_like eval
qr/injection payload/
--- no_error_log
response flagged by Lakera Guard
```
All four tests pass (TEST 4 asserts the *current, buggy* behavior: payload leaks and lakera's scan never logs).
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org