Skip to content

Commit 20eac0a

Browse files
[Agent: Claude] fix(lws): Control gate on sidecar writes at the applyLwsWrite choke point
Three surfaces could write an .acl for a sibling with only container Append/Write: HTTP POST+Slug (upstream's b9b38ed, unpatched here), MCP create_resource, MCP write_resource. The last bypassed write_acl's Control gate entirely via wac()'s parent-container fallback for non-existent targets. Guard lives at the choke point every write surface shares — the same reasoning that put writeTypeConsistency here (review #2/JavaScriptSolidServer#10), applied to authorization, which was the one concern left outside it. Fails closed. Deliberately NOT --lws-gated: a gated auth check is worthless and upstream's is unconditional. .meta keeps Control-on-create / Write-on-update per spec; .lwstypes/.lwsprov remain 405 downstream. Two brief/reality corrections made during implementation: - sidecarSubject() (src/utils/url.js) only covers .meta/.lwstypes/.lwsprov, not .acl (SIDECAR_SUFFIX excludes it by design — authorizeAclAccess in src/auth/middleware.js resolves .acl's subject separately). The guard derives the subject from AUX_SUFFIX directly instead. - --public deployments never had a sidecar WAC gate: authorize() returns authorized:true/webId:null for every method before reaching the .acl/.meta branches, so request.webId is always null there. Threading agentWebId alone would have made every sidecar write on a --public pod fail closed, breaking ~30 existing tests. The 4 HTTP call sites now also pass internal: !!request.config?.public, mirroring the existing --public bypass pattern already used at resource.js:699. MCP call sites don't need this — wac() never special-cased public mode and already enforces real WAC there. Co-Authored-By: Claude Opus 4.8 <[email protected]>
1 parent 4312e65 commit 20eac0a

5 files changed

Lines changed: 145 additions & 2 deletions

File tree

‎src/handlers/container.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ export async function handlePost(request, reply) {
162162
content, contentType: postContentType,
163163
declaredTypes: isCreatingContainer ? [] : declared,
164164
lwsEnabled: request.lwsEnabled,
165+
agentWebId: request.webId ?? null,
166+
// --public mode has no WAC to enforce (same rationale as the S1 listing
167+
// filter above and resource.js's authorizeAclAccess-free 699 branch):
168+
// authorize() short-circuits to authorized:true/webId:null for every
169+
// method under `--public`, including .acl/.meta, before it ever reaches
170+
// the suffix-specific branches — so a public deployment never had a
171+
// Control/Write gate on sidecars to begin with. Gating them here for
172+
// the first time would be new-to-Task-6 breakage of an intentional,
173+
// pre-existing bypass, not a security fix (no privilege tiers exist to
174+
// escalate between when the whole pod is world-writable by config).
175+
internal: !!request.config?.public,
165176
});
166177
if (!w.ok) {
167178
if (w.problem) {

‎src/handlers/resource.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,11 @@ export async function handlePut(request, reply) {
26012601
? RDF_TYPES.JSON_LD : (request.headers['content-type'] || ''),
26022602
declaredTypes: declared,
26032603
lwsEnabled: request.lwsEnabled,
2604+
agentWebId: request.webId ?? null,
2605+
// --public mode never had a sidecar WAC gate (authorize() short-circuits
2606+
// before the .acl/.meta branches) — see container.js:160 for the full
2607+
// rationale. Mirrored at all 3 sidecar-guard call sites in this file.
2608+
internal: !!request.config?.public,
26042609
});
26052610
if (!w.ok) {
26062611
if (w.problem) {
@@ -2840,6 +2845,8 @@ async function patchTurtleFamilyResource(request, reply, { storagePath, resource
28402845
contentType: storedType,
28412846
declaredTypes: [],
28422847
lwsEnabled: request.lwsEnabled,
2848+
agentWebId: request.webId ?? null,
2849+
internal: !!request.config?.public,
28432850
});
28442851
if (!w.ok) {
28452852
if (w.problem) {
@@ -3216,6 +3223,8 @@ export async function handlePatch(request, reply) {
32163223
contentType: RDF_TYPES.JSON_LD,
32173224
declaredTypes: [],
32183225
lwsEnabled: request.lwsEnabled,
3226+
agentWebId: request.webId ?? null,
3227+
internal: !!request.config?.public,
32193228
});
32203229
if (!w.ok) {
32213230
if (w.problem) {

‎src/lws/write.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { writeTypeConsistency } from './write-consistency.js';
44
import { subjectTypesFromBody } from './subject-types.js';
55
import { conformsToTargets } from './constraint.js';
66
import { AUX_SUFFIX } from '../storage/filesystem.js';
7+
import { checkAccess as defaultCheckAccess } from '../wac/checker.js';
8+
import { AccessMode } from '../wac/parser.js';
9+
10+
function refuse(instance, detail) {
11+
return { ok: false, problem: { status: 403, title: 'Sidecar write requires authorization', detail, instance } };
12+
}
713

814
/**
915
* Shared LWS write pipeline: name/type gate → SHACL admission → storage.write
@@ -14,7 +20,43 @@ import { AUX_SUFFIX } from '../storage/filesystem.js';
1420
*/
1521
export async function applyLwsWrite({
1622
storage, storagePath, resourceUrl, content, contentType, declaredTypes = [], lwsEnabled,
23+
agentWebId = null, internal = false, checkAccessFn = defaultCheckAccess,
1724
}) {
25+
// SIDECAR AUTHZ GUARD (2026-07-21). Every write surface funnels through here, which is why
26+
// it is the right place: three separate surfaces (HTTP POST+Slug, MCP create_resource, MCP
27+
// write_resource) each reached storage.write for an `.acl` with only container Append/Write.
28+
// Deliberately NOT --lws-gated: an auth check that only fires under --lws is worthless, and
29+
// upstream's own b9b38ed is unconditional. Fails closed — no WebID and not internal = deny.
30+
if (AUX_SUFFIX.test(storagePath) && !internal) {
31+
// sidecarSubject() (src/utils/url.js) only covers .meta/.lwstypes/.lwsprov
32+
// (SIDECAR_SUFFIX) — .acl is deliberately excluded there because its
33+
// authorization has always been resolved separately (see
34+
// authorizeAclAccess in src/auth/middleware.js, which strips `.acl` by
35+
// hand). AUX_SUFFIX covers all four suffixes, so resolve the subject
36+
// directly here rather than calling sidecarSubject on a path it doesn't
37+
// recognize (it would return null for every `.acl`).
38+
const subject = storagePath.replace(AUX_SUFFIX, '');
39+
const sc = subject === storagePath ? null : { subject, isContainer: subject.endsWith('/') };
40+
if (!sc) return refuse(resourceUrl, 'sidecar subject could not be resolved');
41+
// .acl always needs Control. .meta needs Control to CREATE (the escalation: wac() falls
42+
// back to the parent container for non-existent targets) but only Write to UPDATE a
43+
// subject whose ACL you already satisfy. `.lwstypes`/`.lwsprov` are refused downstream
44+
// by writeTypeConsistency (405, System-Managed) and never reach a mode decision here.
45+
const isMeta = /\.meta$/.test(storagePath);
46+
const exists = await storage.exists(storagePath);
47+
const mode = (!isMeta || !exists) ? AccessMode.CONTROL : AccessMode.WRITE;
48+
if (!agentWebId) return refuse(resourceUrl, `${mode} required on ${sc.subject} (no authenticated agent)`);
49+
const subjectUrl = resourceUrl.replace(/\.(acl|meta|lwstypes|lwsprov)$/, '');
50+
const { allowed } = await checkAccessFn({
51+
resourceUrl: subjectUrl,
52+
resourcePath: sc.subject,
53+
isContainer: sc.isContainer,
54+
agentWebId,
55+
requiredMode: mode,
56+
});
57+
if (!allowed) return refuse(resourceUrl, `${mode} required on ${sc.subject}`);
58+
}
59+
1860
// #2 (review 2026-07-12): the gate runs at THE choke point every write
1961
// surface shares (HTTP PUT/POST + all MCP write tools) — no surface can
2062
// store a name/type lie or an admission-skipped body at an RDF name.

‎src/mcp/tools.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ async function write_resource({ path, content, contentType, types }, ctx) {
7070
content: Buffer.from(content, 'utf8'),
7171
contentType: contentType || 'text/plain',
7272
declaredTypes: Array.isArray(types) ? types : [],
73-
lwsEnabled: ctx.lwsEnabled
73+
lwsEnabled: ctx.lwsEnabled,
74+
agentWebId: ctx.webId ?? null,
7475
});
7576
if (!w.ok) return w.problem ? toolError(w.problem.detail) : admissionError(path, { violations: w.violations, shapeUrl: w.shapeUrl });
7677
if (!w.wrote) return toolError(`write failed: ${path}`);
@@ -105,7 +106,8 @@ async function create_resource({ container, slug, content, contentType, isContai
105106
content: Buffer.from(content || '', 'utf8'),
106107
contentType: contentType || 'text/plain',
107108
declaredTypes: Array.isArray(types) ? types : [],
108-
lwsEnabled: ctx.lwsEnabled
109+
lwsEnabled: ctx.lwsEnabled,
110+
agentWebId: ctx.webId ?? null,
109111
});
110112
if (!w.ok) return w.problem ? toolError(w.problem.detail) : admissionError(childPath, { violations: w.violations, shapeUrl: w.shapeUrl });
111113
if (!w.wrote) return toolError(`write failed: ${childPath}`);
@@ -387,6 +389,7 @@ async function put_typed_resource({ path, content, contentType, types, described
387389
storage, storagePath: path, resourceUrl: buildUrl(ctx, path),
388390
content: Buffer.from(content, 'utf8'), contentType: contentType || 'text/plain',
389391
declaredTypes: Array.isArray(types) ? types : [], lwsEnabled: ctx.lwsEnabled,
392+
agentWebId: ctx.webId ?? null,
390393
});
391394
if (!w.ok || !w.wrote) {
392395
if (metaSnapshot !== undefined) { // roll the .meta back

‎test/sidecar-authz.test.js‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ import { createToken } from '../src/auth/token.js';
2727
// against a correctly patched server. storage.exists() is the only reliable
2828
// "no sidecar" oracle.
2929
import * as storage from '../src/storage/filesystem.js';
30+
import { applyLwsWrite } from '../src/lws/write.js';
31+
32+
// Fake storage: records writes, reports nothing pre-existing.
33+
function fakeStorage() {
34+
const writes = [];
35+
return {
36+
writes,
37+
exists: async () => false,
38+
read: async () => null,
39+
write: async (p, c) => { writes.push([p, c]); return true; },
40+
remove: async () => true,
41+
};
42+
}
3043

3144
const ATTACKER = 'http://attacker.example/profile/card#me';
3245

@@ -116,3 +129,68 @@ describe('sidecar privilege escalation', () => {
116129
assert.equal(sidecarExists, false, 'no .acl sidecar may exist after a refused POST');
117130
});
118131
});
132+
133+
describe('applyLwsWrite sidecar guard', () => {
134+
test('refuses an .acl write when the caller lacks Control and never touches storage', async () => {
135+
const storage = fakeStorage();
136+
const r = await applyLwsWrite({
137+
storage,
138+
storagePath: '/foo/victim.acl',
139+
resourceUrl: 'http://localhost/foo/victim.acl',
140+
content: Buffer.from('{}', 'utf8'),
141+
contentType: 'application/ld+json',
142+
lwsEnabled: true,
143+
agentWebId: 'http://localhost/attacker/profile/card#me',
144+
checkAccessFn: async () => ({ allowed: false }),
145+
});
146+
assert.equal(r.ok, false);
147+
assert.equal(r.problem.status, 403);
148+
assert.equal(storage.writes.length, 0, 'storage.write must not be reached');
149+
});
150+
151+
test('fails closed when no agentWebId is supplied and internal is not set', async () => {
152+
const storage = fakeStorage();
153+
const r = await applyLwsWrite({
154+
storage,
155+
storagePath: '/foo/victim.acl',
156+
resourceUrl: 'http://localhost/foo/victim.acl',
157+
content: Buffer.from('{}', 'utf8'),
158+
contentType: 'application/ld+json',
159+
lwsEnabled: true,
160+
checkAccessFn: async () => ({ allowed: true }),
161+
});
162+
assert.equal(r.ok, false);
163+
assert.equal(storage.writes.length, 0);
164+
});
165+
166+
test('allows an .acl write when the caller holds Control', async () => {
167+
const storage = fakeStorage();
168+
const r = await applyLwsWrite({
169+
storage,
170+
storagePath: '/foo/victim.acl',
171+
resourceUrl: 'http://localhost/foo/victim.acl',
172+
content: Buffer.from('{}', 'utf8'),
173+
contentType: 'application/ld+json',
174+
lwsEnabled: true,
175+
agentWebId: 'http://localhost/owner/profile/card#me',
176+
checkAccessFn: async () => ({ allowed: true }),
177+
});
178+
assert.equal(r.ok, true);
179+
assert.equal(storage.writes.length, 1);
180+
});
181+
182+
test('non-sidecar writes are unaffected and need no webid', async () => {
183+
const storage = fakeStorage();
184+
const r = await applyLwsWrite({
185+
storage,
186+
storagePath: '/foo/note.jsonld',
187+
resourceUrl: 'http://localhost/foo/note.jsonld',
188+
content: Buffer.from('{}', 'utf8'),
189+
contentType: 'application/ld+json',
190+
lwsEnabled: false,
191+
checkAccessFn: async () => { throw new Error('must not be called'); },
192+
});
193+
assert.equal(r.ok, true);
194+
assert.equal(storage.writes.length, 1);
195+
});
196+
});

0 commit comments

Comments
 (0)