-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathauth.test.js
More file actions
292 lines (242 loc) · 9.61 KB
/
Copy pathauth.test.js
File metadata and controls
292 lines (242 loc) · 9.61 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
/**
* Authentication and Authorization tests
*/
import { describe, it, before, after } from 'node:test';
import assert from 'node:assert';
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 deny POST-created .acl/.meta sidecars without Control on the protected resource', async () => {
// Regression for the POST .acl sidecar injection: an agent holding only
// acl:Append on a container (here, the public-append inbox) must not be
// able to plant an .acl sidecar, which the WAC checker would then treat
// as the authorization policy for the sibling resource. .meta is not a
// WAC input, but is gated the same way as a protected Solid sidecar.
await createTestPod('sidecarvictim');
const aclBody = JSON.stringify({
'@context': { acl: 'http://www.w3.org/ns/auth/acl#' },
'@graph': []
});
// Append-only (unauthenticated public append) agent tries to plant victim.acl
const attackAcl = await request('/sidecarvictim/inbox/', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Slug': 'victim.acl' },
body: aclBody
});
assertStatus(attackAcl, 403);
// The same trick with a .meta sidecar must also be blocked
const attackMeta = await request('/sidecarvictim/inbox/', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Slug': 'victim.meta' },
body: aclBody
});
assertStatus(attackMeta, 403);
// A normal (non-sidecar) POST to the public inbox still works
const legit = await request('/sidecarvictim/inbox/', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Slug': 'note' },
body: JSON.stringify({ type: 'note' })
});
assertStatus(legit, 201);
});
it('should allow the owner (Control) to POST an .acl sidecar', async () => {
// Owners hold acl:Control, so the sidecar guard must not block them.
await createTestPod('sidecarowner');
const res = await request('/sidecarowner/', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Slug': 'owned.acl' },
body: JSON.stringify({
'@context': { acl: 'http://www.w3.org/ns/auth/acl#' },
'@graph': []
}),
auth: 'sidecarowner'
});
assertStatus(res, 201);
});
});
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');
});
});
});