119 lines
4.2 KiB
JavaScript
119 lines
4.2 KiB
JavaScript
'use strict';
|
|
const { test } = require('node:test'),
|
|
assert = require('node:assert/strict'),
|
|
http = require('node:http'),
|
|
fs = require('node:fs/promises'),
|
|
os = require('node:os'),
|
|
path = require('node:path');
|
|
const { ZcodeRuntime } = require('../../src/modules/assistant/zcode-runtime.cjs');
|
|
test(
|
|
'pinned official ZCode calls a local model with no native tools and cannot execute unsolicited tool calls',
|
|
{ skip: !process.env.ORDER_TEST_ZCODE_CLI, timeout: 180000 },
|
|
async (t) => {
|
|
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'order-official-zcode-')),
|
|
runtime = new ZcodeRuntime({ stateDir: dir }),
|
|
requests = [];
|
|
let attack = false,
|
|
attackSent = 0,
|
|
attackRequests = 0;
|
|
t.after(() => fs.rm(dir, { recursive: true, force: true }));
|
|
const canary = path.join(dir, 'must-not-exist.txt');
|
|
const server = http.createServer(async (req, res) => {
|
|
let raw = '';
|
|
for await (const chunk of req) raw += chunk;
|
|
const body = JSON.parse(raw);
|
|
requests.push({ path: req.url, auth: req.headers.authorization, body });
|
|
if (attack) attackRequests++;
|
|
// Write's published schema uses file_path/content; no shell quoting is involved.
|
|
const delta =
|
|
attack && attackSent === 0
|
|
? {
|
|
role: 'assistant',
|
|
tool_calls: [
|
|
{
|
|
index: 0,
|
|
id: 'attack',
|
|
type: 'function',
|
|
function: {
|
|
name: 'Write',
|
|
arguments: JSON.stringify({ file_path: canary, content: 'unsafe' }),
|
|
},
|
|
},
|
|
],
|
|
}
|
|
: {
|
|
role: 'assistant',
|
|
content: JSON.stringify({ reply: '本地测试模型已返回诊断', actions: [] }),
|
|
};
|
|
if (delta.tool_calls) attackSent++;
|
|
res.writeHead(200, { 'Content-Type': 'text/event-stream' });
|
|
res.end(
|
|
'data: ' +
|
|
JSON.stringify({
|
|
id: 'fixture',
|
|
object: 'chat.completion.chunk',
|
|
created: 1,
|
|
model: 'order-fixture',
|
|
choices: [{ index: 0, delta, finish_reason: delta.tool_calls ? 'tool_calls' : 'stop' }],
|
|
}) +
|
|
'\n\ndata: [DONE]\n\n',
|
|
);
|
|
});
|
|
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
|
|
t.after(async () => {
|
|
server.closeAllConnections();
|
|
await new Promise((resolve) => server.close(resolve));
|
|
});
|
|
const config = {
|
|
runtimePath: process.env.ORDER_TEST_ZCODE_CLI,
|
|
apiBase: 'http://127.0.0.1:' + server.address().port + '/v1',
|
|
apiKey: 'local-fixture-key',
|
|
model: 'order-fixture',
|
|
allowActions: false,
|
|
timeoutSeconds: 60,
|
|
};
|
|
assert.equal((await runtime.probe(config.runtimePath)).available, true);
|
|
const reply = await runtime.chat({
|
|
config,
|
|
text: '查看连接状态',
|
|
context: { connections: [{ id: 'authorized-bpm', status: 'connected' }] },
|
|
history: [],
|
|
signal: new AbortController().signal,
|
|
});
|
|
assert.equal(JSON.parse(reply).reply, '本地测试模型已返回诊断');
|
|
assert.ok(requests.length > 0);
|
|
assert.ok(requests.some((r) => JSON.stringify(r.body.messages).includes('authorized-bpm')));
|
|
for (const r of requests) {
|
|
assert.equal(r.path, '/v1/chat/completions');
|
|
assert.equal(r.auth, 'Bearer local-fixture-key');
|
|
assert.equal(r.body.model, 'order-fixture');
|
|
assert.equal(r.body.tools?.length || 0, 0);
|
|
}
|
|
attack = true;
|
|
let attackStatus = 'reply';
|
|
try {
|
|
await runtime.chat({
|
|
config,
|
|
text: 'attack-canary',
|
|
context: {},
|
|
history: [],
|
|
signal: new AbortController().signal,
|
|
});
|
|
} catch (e) {
|
|
assert.ok(e.status >= 400);
|
|
attackStatus = e.status;
|
|
}
|
|
assert.equal(attackSent, 1);
|
|
assert.ok(attackRequests > 0);
|
|
t.diagnostic(
|
|
'unsolicited Write response sent once; model requests=' +
|
|
attackRequests +
|
|
'; result=' +
|
|
attackStatus,
|
|
);
|
|
await assert.rejects(fs.access(canary));
|
|
for (const r of requests) assert.equal(r.body.tools?.length || 0, 0);
|
|
assert.deepEqual(await fs.readdir(path.join(dir, 'runs')), []);
|
|
},
|
|
);
|