|
| 1 | +// test/lws-write-gate-chokepoint.test.js |
| 2 | +// Review 2026-07-12 #2/#10: the name/type gate must hold at EVERY write |
| 3 | +// surface (it lived at the 2 HTTP call sites only), and application/json |
| 4 | +// must gate as JSON-LD (the rest of the pipeline already treats it so). |
| 5 | +import { test } from 'node:test'; |
| 6 | +import assert from 'node:assert/strict'; |
| 7 | +import { callTool } from '../src/mcp/tools.js'; |
| 8 | +import { startLwsPod, ownerCtx, request, createTestPod, startTestServer, stopTestServer, assertStatus } from './helpers.js'; |
| 9 | + |
| 10 | +test('MCP write_resource: text/plain body at a .ttl name is refused (gate at choke point)', async (t) => { |
| 11 | + const p = await startLwsPod(t); |
| 12 | + const ctx = { ...ownerCtx(p), lwsEnabled: true }; |
| 13 | + const r = await callTool('write_resource', { |
| 14 | + path: `/${p.podName}/evil.ttl`, content: 'not turtle at all', // contentType omitted -> text/plain |
| 15 | + }, ctx); |
| 16 | + assert.equal(r.isError, true); |
| 17 | + assert.match(r.content[0].text, /name implies text\/turtle/); |
| 18 | +}); |
| 19 | + |
| 20 | +test('MCP write_resource: Turtle body at a .jsonld name is refused', async (t) => { |
| 21 | + const p = await startLwsPod(t); |
| 22 | + const ctx = { ...ownerCtx(p), lwsEnabled: true }; |
| 23 | + const r = await callTool('write_resource', { |
| 24 | + path: `/${p.podName}/lie.jsonld`, content: '<#s> <#p> <#o>.', contentType: 'text/turtle', |
| 25 | + }, ctx); |
| 26 | + assert.equal(r.isError, true); |
| 27 | +}); |
| 28 | + |
| 29 | +test('MCP put_typed_resource: extensionless write still passes (JSS idiom preserved)', async (t) => { |
| 30 | + const p = await startLwsPod(t); |
| 31 | + const ctx = { ...ownerCtx(p), lwsEnabled: true }; |
| 32 | + const r = await callTool('put_typed_resource', { |
| 33 | + path: `/${p.podName}/shape1`, content: JSON.stringify({ '@id': '#it' }), contentType: 'application/ld+json', |
| 34 | + }, ctx); |
| 35 | + assert.equal(r.isError, false); |
| 36 | +}); |
| 37 | + |
| 38 | +test('HTTP PUT: application/json at .ttl name 400s; at .jsonld name passes (#10)', async () => { |
| 39 | + await startTestServer({ lws: true, conneg: true }); |
| 40 | + try { |
| 41 | + await createTestPod('gatejson'); |
| 42 | + const bad = await request('/gatejson/x.ttl', { |
| 43 | + method: 'PUT', auth: 'gatejson', |
| 44 | + headers: { 'Content-Type': 'application/json' }, body: '{"a":1}', |
| 45 | + }); |
| 46 | + assertStatus(bad, 400); |
| 47 | + const ok = await request('/gatejson/x.jsonld', { |
| 48 | + method: 'PUT', auth: 'gatejson', |
| 49 | + headers: { 'Content-Type': 'application/json' }, body: '{"a":1}', |
| 50 | + }); |
| 51 | + assert.ok(ok.status === 201 || ok.status === 204 || ok.status === 200); |
| 52 | + const plain = await request('/gatejson/y.ttl', { |
| 53 | + method: 'PUT', auth: 'gatejson', |
| 54 | + headers: { 'Content-Type': 'text/plain' }, body: 'junk', |
| 55 | + }); |
| 56 | + assertStatus(plain, 400); |
| 57 | + } finally { await stopTestServer(); } |
| 58 | +}); |
0 commit comments