-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwac.test.js
More file actions
284 lines (233 loc) · 9.79 KB
/
Copy pathwac.test.js
File metadata and controls
284 lines (233 loc) · 9.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* WAC (Web Access Control) tests
*/
import { describe, it, before, after } from 'node:test';
import assert from 'node:assert';
import {
startTestServer,
stopTestServer,
request,
createTestPod,
assertStatus,
assertHeader,
getBaseUrl
} from './helpers.js';
import { parseAcl, AccessMode, generateOwnerAcl, serializeAcl } from '../src/wac/parser.js';
import { checkAccess, getRequiredMode } from '../src/wac/checker.js';
describe('WAC Parser', () => {
describe('parseAcl', () => {
it('should parse a simple ACL', async () => {
const acl = {
'@context': { 'acl': 'http://www.w3.org/ns/auth/acl#' },
'@graph': [{
'@id': '#owner',
'@type': 'acl:Authorization',
'acl:agent': { '@id': 'https://alice.example/#me' },
'acl:accessTo': { '@id': 'https://alice.example/resource' },
'acl:mode': [{ '@id': 'acl:Read' }, { '@id': 'acl:Write' }]
}]
};
const auths = await parseAcl(JSON.stringify(acl), 'https://alice.example/.acl');
assert.strictEqual(auths.length, 1);
assert.ok(auths[0].agents.includes('https://alice.example/#me'));
assert.ok(auths[0].modes.includes(AccessMode.READ));
assert.ok(auths[0].modes.includes(AccessMode.WRITE));
});
it('should parse public access', async () => {
const acl = {
'@context': { 'acl': 'http://www.w3.org/ns/auth/acl#', 'foaf': 'http://xmlns.com/foaf/0.1/' },
'@graph': [{
'@id': '#public',
'@type': 'acl:Authorization',
'acl:agentClass': { '@id': 'foaf:Agent' },
'acl:accessTo': { '@id': 'https://alice.example/public/' },
'acl:mode': [{ '@id': 'acl:Read' }]
}]
};
const auths = await parseAcl(JSON.stringify(acl), 'https://alice.example/public/.acl');
assert.strictEqual(auths.length, 1);
assert.ok(auths[0].agentClasses.includes('foaf:Agent'));
assert.ok(auths[0].modes.includes(AccessMode.READ));
});
it('should parse default authorizations for containers', async () => {
const acl = {
'@context': { 'acl': 'http://www.w3.org/ns/auth/acl#' },
'@graph': [{
'@id': '#default',
'@type': 'acl:Authorization',
'acl:agent': { '@id': 'https://alice.example/#me' },
'acl:default': { '@id': 'https://alice.example/folder/' },
'acl:mode': [{ '@id': 'acl:Read' }]
}]
};
const auths = await parseAcl(JSON.stringify(acl), 'https://alice.example/folder/.acl');
assert.strictEqual(auths.length, 1);
assert.ok(auths[0].default.includes('https://alice.example/folder/'));
});
it('should handle invalid JSON gracefully', async () => {
const auths = await parseAcl('not valid json', 'https://example.com/.acl');
assert.strictEqual(auths.length, 0);
});
it('should parse Turtle ACL format', async () => {
const turtleAcl = `
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
<#owner>
a acl:Authorization;
acl:agent <did:nostr:abc123>;
acl:accessTo <https://example.com/resource>;
acl:mode acl:Read, acl:Write.
`;
const auths = await parseAcl(turtleAcl, 'https://example.com/.acl');
assert.strictEqual(auths.length, 1);
assert.ok(auths[0].agents.includes('did:nostr:abc123'));
assert.ok(auths[0].modes.includes(AccessMode.READ));
assert.ok(auths[0].modes.includes(AccessMode.WRITE));
});
it('should resolve relative accessTo URLs', async () => {
const acl = {
'@context': { 'acl': 'http://www.w3.org/ns/auth/acl#' },
'@id': '#owner',
'@type': 'acl:Authorization',
'acl:agent': { '@id': 'https://alice.example/#me' },
'acl:accessTo': { '@id': './' },
'acl:mode': [{ '@id': 'acl:Read' }]
};
const auths = await parseAcl(JSON.stringify(acl), 'https://alice.example/folder/.acl');
assert.strictEqual(auths.length, 1);
assert.ok(auths[0].accessTo.includes('https://alice.example/folder/'),
`Expected accessTo to include 'https://alice.example/folder/', got: ${auths[0].accessTo}`);
});
it('should resolve relative default URLs', async () => {
const acl = {
'@context': { 'acl': 'http://www.w3.org/ns/auth/acl#' },
'@id': '#owner',
'@type': 'acl:Authorization',
'acl:agent': { '@id': 'https://alice.example/#me' },
'acl:accessTo': { '@id': './' },
'acl:default': { '@id': './' },
'acl:mode': [{ '@id': 'acl:Read' }]
};
const auths = await parseAcl(JSON.stringify(acl), 'https://alice.example/folder/.acl');
assert.strictEqual(auths.length, 1);
assert.ok(auths[0].default.includes('https://alice.example/folder/'),
`Expected default to include 'https://alice.example/folder/', got: ${auths[0].default}`);
});
it('should resolve parent-relative URLs like ../other/', async () => {
const acl = {
'@context': { 'acl': 'http://www.w3.org/ns/auth/acl#' },
'@id': '#owner',
'@type': 'acl:Authorization',
'acl:agent': { '@id': 'https://alice.example/#me' },
'acl:accessTo': { '@id': '../other/' },
'acl:mode': [{ '@id': 'acl:Read' }]
};
const auths = await parseAcl(JSON.stringify(acl), 'https://alice.example/folder/.acl');
assert.strictEqual(auths.length, 1);
assert.ok(auths[0].accessTo.includes('https://alice.example/other/'),
`Expected accessTo to include 'https://alice.example/other/', got: ${auths[0].accessTo}`);
});
it('should keep absolute URLs unchanged', async () => {
const acl = {
'@context': { 'acl': 'http://www.w3.org/ns/auth/acl#' },
'@id': '#owner',
'@type': 'acl:Authorization',
'acl:agent': { '@id': 'https://alice.example/#me' },
'acl:accessTo': { '@id': 'https://other.example/resource' },
'acl:mode': [{ '@id': 'acl:Read' }]
};
const auths = await parseAcl(JSON.stringify(acl), 'https://alice.example/folder/.acl');
assert.strictEqual(auths.length, 1);
assert.ok(auths[0].accessTo.includes('https://other.example/resource'),
`Expected accessTo to include 'https://other.example/resource', got: ${auths[0].accessTo}`);
});
});
describe('generateOwnerAcl', () => {
it('should generate owner ACL with public read', () => {
const acl = generateOwnerAcl('https://alice.example/', 'https://alice.example/#me', true);
assert.ok(acl['@graph'].length >= 2);
// Find owner auth
const ownerAuth = acl['@graph'].find(a => a['@id'] === '#owner');
assert.ok(ownerAuth);
assert.strictEqual(ownerAuth['acl:agent']['@id'], 'https://alice.example/#me');
// Find public auth
const publicAuth = acl['@graph'].find(a => a['@id'] === '#public');
assert.ok(publicAuth);
});
});
});
describe('WAC Checker', () => {
describe('getRequiredMode', () => {
it('should return READ for GET', () => {
assert.strictEqual(getRequiredMode('GET'), AccessMode.READ);
});
it('should return READ for HEAD', () => {
assert.strictEqual(getRequiredMode('HEAD'), AccessMode.READ);
});
it('should return APPEND for POST', () => {
assert.strictEqual(getRequiredMode('POST'), AccessMode.APPEND);
});
it('should return WRITE for PUT', () => {
assert.strictEqual(getRequiredMode('PUT'), AccessMode.WRITE);
});
it('should return WRITE for DELETE', () => {
assert.strictEqual(getRequiredMode('DELETE'), AccessMode.WRITE);
});
});
});
describe('WAC Integration', () => {
let baseUrl;
before(async () => {
const result = await startTestServer();
baseUrl = result.baseUrl;
await createTestPod('wactest');
});
after(async () => {
await stopTestServer();
});
describe('ACL Files', () => {
it('should create root .acl on pod creation', async () => {
// ACL files require Control permission - must be authenticated as pod owner
const res = await request('/wactest/.acl', { auth: 'wactest' });
assertStatus(res, 200);
const content = await res.json();
assert.ok(content['@graph'], 'Should be JSON-LD');
});
it('should deny unauthenticated access to .acl files', async () => {
// Security: ACL files must require authentication
const res = await request('/wactest/.acl');
assertStatus(res, 401);
});
it('should create private folder .acl', async () => {
const res = await request('/wactest/private/.acl', { auth: 'wactest' });
assertStatus(res, 200);
const content = await res.json();
assert.ok(content['@graph']);
// Should only have owner, no public
const hasPublic = content['@graph'].some(a =>
a['acl:agentClass'] && a['acl:agentClass']['@id'] === 'foaf:Agent'
);
assert.ok(!hasPublic, 'Private folder should not have public access');
});
it('should create inbox .acl with public append', async () => {
const res = await request('/wactest/inbox/.acl', { auth: 'wactest' });
assertStatus(res, 200);
const content = await res.json();
// Should have public append
const publicAuth = content['@graph'].find(a =>
a['acl:agentClass'] && a['acl:agentClass']['@id'] === 'foaf:Agent'
);
assert.ok(publicAuth, 'Inbox should have public access');
const modes = publicAuth['acl:mode'].map(m => m['@id']);
assert.ok(modes.includes('acl:Append'), 'Public should have Append');
assert.ok(!modes.includes('acl:Read'), 'Public should not have Read');
});
});
describe('WAC-Allow Header', () => {
it('should return WAC-Allow header for public container', async () => {
const res = await request('/wactest/public/');
assertHeader(res, 'WAC-Allow');
const wacAllow = res.headers.get('WAC-Allow');
assert.ok(wacAllow.includes('public='), 'Should have public permissions');
});
});
});