Share links: per-device AI limits and an MCP "Share links" switch
All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 28s

- Each MCP server gets a "Share links" switch (off by default). Shared Explain and Ask use
  only the servers that have it on.
- Visitor AI requests are limited per device instead of per IP, so one campus network
  doesn't share an allowance. Each browser gets a random id in a signed, HTTP-only cookie
  (20 requests per 10 minutes per link). Forged ids are replaced. A link-wide cap of 500
  requests per hour stays as a cost backstop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Elijah 2026-11-15 16:12:45 -07:00
parent 031f7bec31
commit 60cf2322f7
7 changed files with 128 additions and 45 deletions

View file

@ -317,7 +317,10 @@ describe('AI on share links', () => {
const share = (await t.api('POST', '/api/shares', { kind: 'quiz', targetId: quiz.id })).body.share;
return { quiz, question: questions[0], other, otherQ, share };
}
const visitor = (method: string, url: string, body?: unknown) => t.raw(method, url, { cookie: null, headers: { 'content-type': 'application/json' }, body: body === undefined ? undefined : JSON.stringify(body) });
/** A visitor without a session; `device` is the visitor cookie a browser would send back. */
const visitor = (method: string, url: string, body?: unknown, device?: string) =>
t.raw(method, url, { cookie: device ?? null, headers: { 'content-type': 'application/json' }, body: body === undefined ? undefined : JSON.stringify(body) });
const deviceCookie = (res: Response) => (res.headers.get('set-cookie') ?? '').split(';')[0];
it('is off until the owner turns it on, and then only explains what was shared', async () => {
resetShareAiLimits();
@ -363,13 +366,58 @@ describe('AI on share links', () => {
// The last message must come from the visitor.
expect((await visitor('POST', `/api/public/${share.token}/chat`, { messages: [{ role: 'assistant', text: 'x' }] })).status).toBe(422);
let limited = 0;
for (let i = 0; i < 22; i += 1) {
const r = await visitor('POST', `/api/public/${share.token}/chat`, { messages: [{ role: 'user', text: `Question ${i}` }] });
});
it('limits each device, not each network: others on the same IP keep going', async () => {
resetShareAiLimits();
const { share } = await sharedQuiz();
await t.api('PATCH', `/api/shares/${share.id}`, { ai: { assistant: true } });
const ask = (device?: string) => visitor('POST', `/api/public/${share.token}/chat`, { messages: [{ role: 'user', text: 'Explain halos' }] }, device);
const first = await ask();
await first.text();
const device = deviceCookie(first);
expect(device).toMatch(/^sd_visitor=[\w-]+\.[\w-]+$/);
// The same device gets 20 requests per 10 minutes, then is told to wait.
const statuses: number[] = [];
for (let i = 0; i < 20; i += 1) {
const r = await ask(device);
await r.text();
if (r.status === 429) limited += 1;
statuses.push(r.status);
}
expect(limited).toBeGreaterThan(0);
expect(statuses.slice(0, 19).every((s) => s === 200)).toBe(true);
expect(statuses[19]).toBe(429);
// Another device (same test client, so the same IP) is not affected.
const other = await ask();
expect(other.status).toBe(200);
await other.text();
// A forged cookie is not trusted: it gets a new device id instead of borrowing one.
const forged = await ask(`sd_visitor=${device.split('=')[1].split('.')[0]}.forged`);
expect(forged.status).toBe(200);
expect(deviceCookie(forged)).not.toBe(device);
await forged.text();
});
it('offers an MCP server on share links only when it is allowed there', async () => {
resetShareAiLimits();
const { share } = await sharedQuiz();
await t.api('PATCH', `/api/shares/${share.id}`, { ai: { assistant: true } });
const server = await addServer();
expect(server.areas.share).toBe(false);
const ask = async () => {
mock.requests.length = 0;
const r = await visitor('POST', `/api/public/${share.token}/chat`, { messages: [{ role: 'user', text: 'Use the mcp drug reference for furosemide' }] });
expect(r.status).toBe(200);
await r.text();
return chatCalls();
};
expect((await ask()).every((r) => !toolNames(r).some((n) => n.startsWith('mcp_')))).toBe(true);
expect(mcpServer.calls.some((c) => c.method === 'tools/call')).toBe(false);
await t.api('PATCH', `/api/ai/mcp/${server.id}`, { areas: { share: true } });
const calls = await ask();
expect(toolNames(calls[0]!)).toContain('mcp_drug_ref__lookup_drug');
expect(mcpServer.calls.some((c) => c.method === 'tools/call')).toBe(true);
expect(JSON.stringify(calls.at(-1)!.body.messages)).toContain('Monograph for furosemide');
});
});