forked from JavaScriptSolidServer/JavaScriptSolidServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.test.js
More file actions
340 lines (287 loc) · 11.3 KB
/
Copy pathauth.test.js
File metadata and controls
340 lines (287 loc) · 11.3 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* Authentication and Authorization tests
*/
import { describe, it, before, after } from 'node:test';
import assert from 'node:assert';
import fs from 'fs-extra';
import path from 'path';
import {
startTestServer,
stopTestServer,
request,
createTestPod,
getPodToken,
getBaseUrl,
assertStatus,
assertHeader
} from './helpers.js';
describe('Authentication', () => {
before(async () => {
await startTestServer();
});
after(async () => {
await stopTestServer();
});
describe('Token Authentication', () => {
it('should return token on pod creation', async () => {
const result = await createTestPod('authtest');
assert.ok(result.token, 'Should return a token');
assert.ok(result.token.includes('.'), 'Token should have signature');
});
it('should allow authenticated access to private resources', async () => {
await createTestPod('privatetest');
// Should succeed with auth
const res = await request('/privatetest/private/', { auth: 'privatetest' });
assertStatus(res, 200);
});
it('should deny unauthenticated access to private resources', async () => {
await createTestPod('denytest');
// Should fail without auth
const res = await request('/denytest/private/');
assertStatus(res, 401);
});
it('should return 403 for wrong user accessing private resources', async () => {
await createTestPod('user1');
await createTestPod('user2');
// User2 trying to access User1's private folder
const res = await request('/user1/private/', { auth: 'user2' });
assertStatus(res, 403);
});
it('should accept Bearer token format', async () => {
await createTestPod('bearertest');
const token = getPodToken('bearertest');
const res = await fetch(`${getBaseUrl()}/bearertest/private/`, {
headers: { 'Authorization': `Bearer ${token}` }
});
assertStatus(res, 200);
});
it('should reject invalid tokens', async () => {
await createTestPod('invalidtest');
const res = await fetch(`${getBaseUrl()}/invalidtest/private/`, {
headers: { 'Authorization': 'Bearer invalid.token' }
});
assertStatus(res, 401);
});
});
describe('WAC Enforcement', () => {
it('should allow public read on pod root', async () => {
await createTestPod('publicread');
// Public folder should be readable without auth
const res = await request('/publicread/public/');
assertStatus(res, 200);
});
it('should allow public read on explicit public folders', async () => {
await createTestPod('explicitpublic');
// Root ACL has public read default
const res = await request('/explicitpublic/');
assertStatus(res, 200);
});
it('should allow authenticated write to owned resources', async () => {
await createTestPod('writetest');
const res = await request('/writetest/public/test.txt', {
method: 'PUT',
body: 'test content',
auth: 'writetest'
});
assertStatus(res, 201);
});
it('should deny unauthenticated write', async () => {
await createTestPod('nowrite');
const res = await request('/nowrite/public/test.txt', {
method: 'PUT',
body: 'test content'
});
assertStatus(res, 401);
});
it('should deny other user write to owned resources', async () => {
await createTestPod('owner1');
await createTestPod('attacker');
const res = await request('/owner1/public/test.txt', {
method: 'PUT',
body: 'malicious content',
auth: 'attacker'
});
assertStatus(res, 403);
});
it('should allow public append to inbox', async () => {
await createTestPod('inboxtest');
// POST to inbox should work for anyone (public append)
const res = await request('/inboxtest/inbox/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Slug': 'notification'
},
body: JSON.stringify({ type: 'notification' })
});
assertStatus(res, 201);
});
it('should deny public read on inbox', async () => {
await createTestPod('inboxread');
// GET inbox should fail for unauthenticated
const res = await request('/inboxread/inbox/');
assertStatus(res, 401);
});
it('should allow any authenticated user with acl:AuthenticatedAgent', async () => {
await createTestPod('authuser1');
await createTestPod('authuser2');
// Create a test resource with acl:AuthenticatedAgent ACL
const baseUrl = getBaseUrl();
// First, create a resource (this will create parent containers)
await request('/authuser1/authenticated-only/test.txt', {
method: 'PUT',
headers: { 'Content-Type': 'text/plain' },
body: 'authenticated content',
auth: 'authuser1'
});
// Now create a custom ACL for the container with acl:AuthenticatedAgent
// Include owner with Control so they can manage the ACL
const acl = {
'@context': { 'acl': 'http://www.w3.org/ns/auth/acl#' },
'@graph': [
{
'@id': '#owner',
'@type': 'acl:Authorization',
'acl:agent': { '@id': `${baseUrl}/authuser1/profile/card.jsonld#me` },
'acl:accessTo': { '@id': `${baseUrl}/authuser1/authenticated-only/` },
'acl:default': { '@id': `${baseUrl}/authuser1/authenticated-only/` },
'acl:mode': [
{ '@id': 'acl:Read' },
{ '@id': 'acl:Write' },
{ '@id': 'acl:Control' }
]
},
{
'@id': '#authenticated',
'@type': 'acl:Authorization',
'acl:agentClass': { '@id': 'acl:AuthenticatedAgent' },
'acl:accessTo': { '@id': `${baseUrl}/authuser1/authenticated-only/` },
'acl:default': { '@id': `${baseUrl}/authuser1/authenticated-only/` },
'acl:mode': [{ '@id': 'acl:Read' }]
}
]
};
await request('/authuser1/authenticated-only/.acl', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(acl),
auth: 'authuser1'
});
// Test 1: Anonymous access should be denied
const res1 = await request('/authuser1/authenticated-only/test.txt');
assertStatus(res1, 401);
// Test 2: Owner should have access
const res2 = await request('/authuser1/authenticated-only/test.txt', { auth: 'authuser1' });
assertStatus(res2, 200);
// Test 3: Different authenticated user should also have access (key test!)
const res3 = await request('/authuser1/authenticated-only/test.txt', { auth: 'authuser2' });
assertStatus(res3, 200);
});
it('should allow owner to edit ACL even without acl:Control', async () => {
await createTestPod('aclowner1');
const baseUrl = getBaseUrl();
// Initial ACL update while owner still has Control via inherited defaults.
const noControlAcl = {
'@context': { acl: 'http://www.w3.org/ns/auth/acl#' },
'@graph': [
{
'@id': '#owner-no-control',
'@type': 'acl:Authorization',
'acl:agent': { '@id': `${baseUrl}/aclowner1/profile/card.jsonld#me` },
'acl:accessTo': { '@id': `${baseUrl}/aclowner1/public/` },
'acl:default': { '@id': `${baseUrl}/aclowner1/public/` },
'acl:mode': [
{ '@id': 'acl:Read' },
{ '@id': 'acl:Write' }
]
}
]
};
const setNoControl = await request('/aclowner1/public/.acl', {
method: 'PUT',
headers: { 'Content-Type': 'application/ld+json' },
body: JSON.stringify(noControlAcl),
auth: 'aclowner1'
});
assert.ok(setNoControl.status < 300, `Initial ACL write failed: ${setNoControl.status}`);
// Second edit would normally fail (no acl:Control), but owner fallback should allow it.
const updatedAcl = {
'@context': { acl: 'http://www.w3.org/ns/auth/acl#' },
'@graph': [
{
'@id': '#owner-updated',
'@type': 'acl:Authorization',
'acl:agent': { '@id': `${baseUrl}/aclowner1/profile/card.jsonld#me` },
'acl:accessTo': { '@id': `${baseUrl}/aclowner1/public/` },
'acl:default': { '@id': `${baseUrl}/aclowner1/public/` },
'acl:mode': [
{ '@id': 'acl:Read' },
{ '@id': 'acl:Write' }
]
}
]
};
const secondEdit = await request('/aclowner1/public/.acl', {
method: 'PUT',
headers: { 'Content-Type': 'application/ld+json' },
body: JSON.stringify(updatedAcl),
auth: 'aclowner1'
});
assert.ok(secondEdit.status < 300, `Owner should edit ACL without Control, got ${secondEdit.status}`);
// Owner should also be able to read ACL even without Control.
const readAcl = await request('/aclowner1/public/.acl', {
method: 'GET',
auth: 'aclowner1'
});
assert.ok(readAcl.status < 300, `Owner should read ACL without Control, got ${readAcl.status}`);
});
it('should allow owner to repair a broken ACL document', async () => {
await createTestPod('aclowner2');
const baseUrl = getBaseUrl();
// Corrupt the ACL on disk to simulate an invalid/unparseable ACL document.
const aclPath = path.join('data', 'aclowner2', 'public', '.acl');
await fs.writeFile(aclPath, 'this is not valid ACL content', 'utf8');
// Owner must still be able to repair the ACL afterwards.
const repairedAcl = {
'@context': { acl: 'http://www.w3.org/ns/auth/acl#' },
'@graph': [
{
'@id': '#owner',
'@type': 'acl:Authorization',
'acl:agent': { '@id': `${baseUrl}/aclowner2/profile/card.jsonld#me` },
'acl:accessTo': { '@id': `${baseUrl}/aclowner2/public/` },
'acl:default': { '@id': `${baseUrl}/aclowner2/public/` },
'acl:mode': [
{ '@id': 'acl:Read' },
{ '@id': 'acl:Write' },
{ '@id': 'acl:Control' }
]
}
]
};
const repair = await request('/aclowner2/public/.acl', {
method: 'PUT',
headers: { 'Content-Type': 'application/ld+json' },
body: JSON.stringify(repairedAcl),
auth: 'aclowner2'
});
assert.ok(repair.status < 300, `Owner should repair broken ACL, got ${repair.status}`);
});
});
describe('WAC-Allow Header', () => {
it('should include user permissions for authenticated requests', async () => {
await createTestPod('wacallow');
const res = await request('/wacallow/public/', { auth: 'wacallow' });
const wacAllow = res.headers.get('WAC-Allow');
assert.ok(wacAllow, 'Should have WAC-Allow header');
assert.ok(wacAllow.includes('user='), 'Should include user permissions');
});
it('should include public permissions', async () => {
await createTestPod('wacpublic');
const res = await request('/wacpublic/public/');
const wacAllow = res.headers.get('WAC-Allow');
assert.ok(wacAllow, 'Should have WAC-Allow header');
assert.ok(wacAllow.includes('public='), 'Should include public permissions');
});
});
});