javascript-solid-server 0.0.224 → 0.0.226
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +38 -0
- package/SECURITY.md +8 -0
- package/docs/authentication.md +6 -0
- package/docs/configuration.md +8 -0
- package/docs/security.md +30 -0
- package/package.json +1 -1
- package/src/auth/cid-doc-fetch.js +44 -51
- package/src/auth/did-nostr.js +35 -42
- package/src/auth/nostr.js +70 -7
- package/src/auth/webid-tls.js +35 -21
- package/src/handlers/resource.js +66 -23
- package/src/server.js +54 -18
- package/src/storage/filesystem.js +39 -6
- package/src/utils/bounded-body.js +89 -0
- package/src/utils/dotfiles.js +70 -0
- package/src/utils/url.js +40 -23
- package/test/app-paths.test.js +87 -5
- package/test/auth-fetch-body-timeout.test.js +253 -0
- package/test/conditional-race.test.js +102 -0
- package/test/dotfile-guard.test.js +144 -0
- package/test/nip98-replay.test.js +145 -0
- package/test/plugin-reservepath.test.js +11 -0
- package/test/withlock.test.js +81 -0
package/test/app-paths.test.js
CHANGED
|
@@ -23,6 +23,8 @@ import { describe, it, before, after, afterEach } from 'node:test';
|
|
|
23
23
|
import assert from 'node:assert';
|
|
24
24
|
import { createServer } from '../src/server.js';
|
|
25
25
|
import fs from 'fs-extra';
|
|
26
|
+
import path from 'path';
|
|
27
|
+
import os from 'os';
|
|
26
28
|
|
|
27
29
|
const TEST_DATA_DIR = './test-data-app-paths';
|
|
28
30
|
|
|
@@ -30,18 +32,38 @@ let server;
|
|
|
30
32
|
let baseUrl;
|
|
31
33
|
let originalDataRoot;
|
|
32
34
|
|
|
33
|
-
|
|
35
|
+
// A plugin that mounts at its prefix but handles ONE path only (#611).
|
|
36
|
+
const PARTIAL_PLUGIN = `export async function activate(api) {
|
|
37
|
+
api.fastify.get(api.prefix + '/echo', async () => ({ app: true, plugin: true }));
|
|
38
|
+
}
|
|
39
|
+
`;
|
|
40
|
+
let fixtureDir; // a fresh temp dir per run, so parallel runs never collide
|
|
41
|
+
|
|
42
|
+
// routes: 'all' mounts the app on the prefix and its whole subtree (the
|
|
43
|
+
// #582 shape); 'partial' registers one route only, leaving the rest of
|
|
44
|
+
// the subtree unhandled (#611); 'plugin' loads PARTIAL_PLUGIN instead.
|
|
45
|
+
async function startWith(appPaths, routes = 'all') {
|
|
34
46
|
await fs.emptyDir(TEST_DATA_DIR);
|
|
47
|
+
if (routes === 'plugin') {
|
|
48
|
+
fixtureDir ??= await fs.mkdtemp(path.join(os.tmpdir(), 'jss-app-paths-'));
|
|
49
|
+
await fs.writeFile(path.join(fixtureDir, 'partial.js'), PARTIAL_PLUGIN);
|
|
50
|
+
}
|
|
35
51
|
server = createServer({
|
|
36
52
|
logger: false,
|
|
37
53
|
forceCloseConnections: true,
|
|
38
54
|
root: TEST_DATA_DIR,
|
|
39
|
-
|
|
55
|
+
...(routes === 'plugin'
|
|
56
|
+
? { plugins: [{ id: 'partial', module: path.join(fixtureDir, 'partial.js'), prefix: '/myapp' }] }
|
|
57
|
+
: { appPaths }),
|
|
40
58
|
});
|
|
41
59
|
// An app in the #206 plugin-zero shape: routes registered on the returned
|
|
42
60
|
// instance, answering with its own status codes (its own "auth").
|
|
43
|
-
|
|
44
|
-
|
|
61
|
+
if (routes === 'all') {
|
|
62
|
+
server.all('/myapp', echo);
|
|
63
|
+
server.all('/myapp/*', echo);
|
|
64
|
+
} else if (routes === 'partial') {
|
|
65
|
+
server.get('/myapp/echo', echo);
|
|
66
|
+
}
|
|
45
67
|
async function echo(request, reply) {
|
|
46
68
|
reply.code(200).send({
|
|
47
69
|
app: true,
|
|
@@ -70,9 +92,10 @@ describe('appPaths application mount points (#582)', () => {
|
|
|
70
92
|
await fs.remove(TEST_DATA_DIR);
|
|
71
93
|
});
|
|
72
94
|
|
|
73
|
-
after(() => {
|
|
95
|
+
after(async () => {
|
|
74
96
|
if (originalDataRoot === undefined) delete process.env.DATA_ROOT;
|
|
75
97
|
else process.env.DATA_ROOT = originalDataRoot;
|
|
98
|
+
if (fixtureDir) await fs.remove(fixtureDir);
|
|
76
99
|
});
|
|
77
100
|
|
|
78
101
|
it('unauthenticated POST below an app path reaches the app handler', async () => {
|
|
@@ -148,4 +171,63 @@ describe('appPaths application mount points (#582)', () => {
|
|
|
148
171
|
assert.ok(res.status === 401 || res.status === 403,
|
|
149
172
|
`expected WAC rejection with no appPaths, got ${res.status}`);
|
|
150
173
|
});
|
|
174
|
+
|
|
175
|
+
// #611: WAC is skipped under an app path, and the LDP wildcards sit
|
|
176
|
+
// beneath every app route. A path the app never registered must be a
|
|
177
|
+
// 404 inside the app's namespace, never an unauthenticated storage
|
|
178
|
+
// read or write.
|
|
179
|
+
describe('unhandled paths under an app subtree (#611)', () => {
|
|
180
|
+
for (const shape of ['partial', 'plugin']) {
|
|
181
|
+
it(`${shape}: anonymous PUT below the prefix is 404 and writes nothing`, async () => {
|
|
182
|
+
await startWith(['/myapp'], shape);
|
|
183
|
+
// The container exists, which is when the fall-through used to 201.
|
|
184
|
+
await fs.ensureDir(path.join(TEST_DATA_DIR, 'myapp'));
|
|
185
|
+
const res = await fetch(`${baseUrl}/myapp/planted.txt`, {
|
|
186
|
+
method: 'PUT',
|
|
187
|
+
headers: { 'Content-Type': 'text/plain' },
|
|
188
|
+
body: 'planted',
|
|
189
|
+
});
|
|
190
|
+
assert.strictEqual(res.status, 404);
|
|
191
|
+
assert.strictEqual(await fs.pathExists(path.join(TEST_DATA_DIR, 'myapp', 'planted.txt')), false);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it(`${shape}: anonymous GET, DELETE and PATCH below the prefix are 404, the app route still answers`, async () => {
|
|
195
|
+
await startWith(['/myapp'], shape);
|
|
196
|
+
await fs.ensureDir(path.join(TEST_DATA_DIR, 'myapp'));
|
|
197
|
+
await fs.writeFile(path.join(TEST_DATA_DIR, 'myapp', 'secret.txt'), 'private');
|
|
198
|
+
for (const method of ['GET', 'HEAD', 'DELETE', 'PATCH']) {
|
|
199
|
+
const res = await fetch(`${baseUrl}/myapp/secret.txt`, {
|
|
200
|
+
method,
|
|
201
|
+
...(method === 'PATCH' ? { headers: { 'Content-Type': 'text/n3' }, body: '' } : {}),
|
|
202
|
+
});
|
|
203
|
+
assert.strictEqual(res.status, 404, `${method} should be 404`);
|
|
204
|
+
}
|
|
205
|
+
assert.strictEqual(await fs.readFile(path.join(TEST_DATA_DIR, 'myapp', 'secret.txt'), 'utf8'), 'private');
|
|
206
|
+
const ok = await fetch(`${baseUrl}/myapp/echo`);
|
|
207
|
+
assert.strictEqual(ok.status, 200);
|
|
208
|
+
assert.strictEqual((await ok.json()).app, true);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
it('OPTIONS below the prefix is a bare 204 for existing and missing paths alike (no existence leak)', async () => {
|
|
213
|
+
await startWith(['/myapp'], 'plugin');
|
|
214
|
+
await fs.ensureDir(path.join(TEST_DATA_DIR, 'myapp', 'dir'));
|
|
215
|
+
await fs.writeFile(path.join(TEST_DATA_DIR, 'myapp', 'secret.txt'), 'private');
|
|
216
|
+
const seen = [];
|
|
217
|
+
for (const p of ['/myapp/secret.txt', '/myapp/missing.txt', '/myapp/dir/', '/myapp/nodir/']) {
|
|
218
|
+
const res = await fetch(`${baseUrl}${p}`, { method: 'OPTIONS', headers: { Origin: 'https://app.example' } });
|
|
219
|
+
assert.strictEqual(res.status, 204, `${p} preflight should be 204`);
|
|
220
|
+
assert.strictEqual(res.headers.get('access-control-allow-origin'), 'https://app.example');
|
|
221
|
+
seen.push([res.headers.get('link'), res.headers.get('accept-patch'), res.headers.get('allow')].join('|'));
|
|
222
|
+
}
|
|
223
|
+
assert.strictEqual(new Set(seen).size, 1, `OPTIONS headers must not vary with storage: ${seen.join(' / ')}`);
|
|
224
|
+
assert.strictEqual(seen[0].includes('Container'), false);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('a sibling LDP path is untouched: PUT is still 401, not 404', async () => {
|
|
228
|
+
await startWith(['/myapp'], 'partial');
|
|
229
|
+
const res = await fetch(`${baseUrl}/elsewhere.txt`, { method: 'PUT', headers: { 'Content-Type': 'text/plain' }, body: 'x' });
|
|
230
|
+
assert.strictEqual(res.status, 401);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
151
233
|
});
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #641 — the auth document fetch deadline must cover the body read.
|
|
3
|
+
*
|
|
4
|
+
* A profile host that sends headers promptly and then trickles the
|
|
5
|
+
* body used to hold the authentication attempt (and the upstream
|
|
6
|
+
* socket) open indefinitely: the abort timer was cleared as soon as
|
|
7
|
+
* fetch() resolved, and the body was read with no clock on it.
|
|
8
|
+
*
|
|
9
|
+
* Three paths are covered:
|
|
10
|
+
* - did-nostr fetchWithRedirectGuard (real HTTP server, validator seam)
|
|
11
|
+
* - WebID-TLS verifyWebIdTls (real HTTP server; no SSRF guard there)
|
|
12
|
+
* - cid-doc-fetch fetchCidDocument (global.fetch stub with a trickling
|
|
13
|
+
* ReadableStream, because the SSRF guard blocks loopback)
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { describe, it, before, after, beforeEach } from 'node:test';
|
|
17
|
+
import assert from 'node:assert';
|
|
18
|
+
import http from 'node:http';
|
|
19
|
+
import { fetchWithRedirectGuard } from '../src/auth/did-nostr.js';
|
|
20
|
+
import { verifyWebIdTls, clearCache as clearTlsCache } from '../src/auth/webid-tls.js';
|
|
21
|
+
import { fetchCidDocument, _clearProfileCacheForTests } from '../src/auth/cid-doc-fetch.js';
|
|
22
|
+
import { readBodyWithCap } from '../src/utils/bounded-body.js';
|
|
23
|
+
|
|
24
|
+
const TIMEOUT_MS = 300;
|
|
25
|
+
const TRICKLE_MS = 50;
|
|
26
|
+
// Generous upper bound: the deadline plus scheduling slack, and far
|
|
27
|
+
// below how long the trickle would run if the timeout did not fire.
|
|
28
|
+
const MAX_ELAPSED_MS = 2500;
|
|
29
|
+
|
|
30
|
+
const allowAll = async () => ({ valid: true });
|
|
31
|
+
|
|
32
|
+
/** Start a server whose /trickle writes headers now and one byte every TRICKLE_MS forever. */
|
|
33
|
+
function startTrickleServer(contentType) {
|
|
34
|
+
const closed = [];
|
|
35
|
+
const server = http.createServer((req, res) => {
|
|
36
|
+
if (req.url.startsWith('/redirect-trickle')) {
|
|
37
|
+
// 302 whose own body trickles forever: the hop must be abandoned
|
|
38
|
+
// AND its socket released before the next hop is fetched.
|
|
39
|
+
res.writeHead(302, { Location: '/ok', 'Content-Type': 'text/plain' });
|
|
40
|
+
res.write('moved');
|
|
41
|
+
const iv = setInterval(() => res.write(' '), TRICKLE_MS);
|
|
42
|
+
const stop = () => { clearInterval(iv); closed.push(req.url); };
|
|
43
|
+
req.on('close', stop);
|
|
44
|
+
res.on('close', stop);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (req.url.startsWith('/error-trickle')) {
|
|
48
|
+
res.writeHead(500, { 'Content-Type': contentType });
|
|
49
|
+
res.write('{');
|
|
50
|
+
const iv = setInterval(() => res.write(' '), TRICKLE_MS);
|
|
51
|
+
const stop = () => { clearInterval(iv); closed.push(req.url); };
|
|
52
|
+
req.on('close', stop);
|
|
53
|
+
res.on('close', stop);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (req.url === '/ok') {
|
|
57
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
58
|
+
res.end('{"ok":true}');
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (req.url.startsWith('/trickle')) {
|
|
62
|
+
res.writeHead(200, { 'Content-Type': contentType });
|
|
63
|
+
res.write('{');
|
|
64
|
+
const iv = setInterval(() => res.write(' '), TRICKLE_MS);
|
|
65
|
+
const stop = () => { clearInterval(iv); closed.push(req.url); };
|
|
66
|
+
req.on('close', stop);
|
|
67
|
+
res.on('close', stop);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
res.writeHead(404).end();
|
|
71
|
+
});
|
|
72
|
+
return new Promise((resolve) => server.listen(0, '127.0.0.1', () => {
|
|
73
|
+
resolve({ server, port: server.address().port, closed });
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function waitFor(pred, ms = 1500) {
|
|
78
|
+
const until = Date.now() + ms;
|
|
79
|
+
while (Date.now() < until) {
|
|
80
|
+
if (pred()) return true;
|
|
81
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
82
|
+
}
|
|
83
|
+
return pred();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
describe('#641 auth document fetch: deadline covers the body read', () => {
|
|
87
|
+
let srv;
|
|
88
|
+
before(async () => { srv = await startTrickleServer('application/json'); });
|
|
89
|
+
after(async () => {
|
|
90
|
+
// Aborted upstream requests may linger as idle keep-alive sockets;
|
|
91
|
+
// close() alone would wait for them.
|
|
92
|
+
srv.server.closeAllConnections();
|
|
93
|
+
await new Promise((resolve) => srv.server.close(resolve));
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('did-nostr fetchWithRedirectGuard rejects a trickling body within the deadline and drops the socket', async () => {
|
|
97
|
+
const started = Date.now();
|
|
98
|
+
await assert.rejects(
|
|
99
|
+
() => fetchWithRedirectGuard(`http://127.0.0.1:${srv.port}/trickle/did`, {
|
|
100
|
+
_validateUrl: allowAll,
|
|
101
|
+
timeout: TIMEOUT_MS,
|
|
102
|
+
}),
|
|
103
|
+
/timed out|abort/i,
|
|
104
|
+
);
|
|
105
|
+
const elapsed = Date.now() - started;
|
|
106
|
+
assert.ok(elapsed < MAX_ELAPSED_MS, `took ${elapsed}ms`);
|
|
107
|
+
assert.ok(await waitFor(() => srv.closed.includes('/trickle/did')), 'upstream request was not closed');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('WebID-TLS verifyWebIdTls gives up on a trickling profile within the deadline and drops the socket', async () => {
|
|
111
|
+
clearTlsCache();
|
|
112
|
+
const cert = { modulus: 'abc123def456', exponent: '10001' };
|
|
113
|
+
const started = Date.now();
|
|
114
|
+
const verified = await verifyWebIdTls(cert, `http://127.0.0.1:${srv.port}/trickle/tls`, { timeout: TIMEOUT_MS });
|
|
115
|
+
const elapsed = Date.now() - started;
|
|
116
|
+
assert.strictEqual(verified, false);
|
|
117
|
+
assert.ok(elapsed < MAX_ELAPSED_MS, `took ${elapsed}ms`);
|
|
118
|
+
assert.ok(await waitFor(() => srv.closed.includes('/trickle/tls')), 'upstream request was not closed');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('did-nostr releases a trickling redirect body before following the hop', async () => {
|
|
122
|
+
const started = Date.now();
|
|
123
|
+
const r = await fetchWithRedirectGuard(`http://127.0.0.1:${srv.port}/redirect-trickle/did`, {
|
|
124
|
+
_validateUrl: allowAll,
|
|
125
|
+
timeout: TIMEOUT_MS,
|
|
126
|
+
});
|
|
127
|
+
assert.strictEqual(r.status, 200);
|
|
128
|
+
assert.strictEqual(r.body, '{"ok":true}');
|
|
129
|
+
assert.ok(Date.now() - started < MAX_ELAPSED_MS);
|
|
130
|
+
assert.ok(await waitFor(() => srv.closed.includes('/redirect-trickle/did')), 'redirect hop socket was not released');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('did-nostr returns an error status at once and releases its trickling body', async () => {
|
|
134
|
+
const started = Date.now();
|
|
135
|
+
const r = await fetchWithRedirectGuard(`http://127.0.0.1:${srv.port}/error-trickle/did`, {
|
|
136
|
+
_validateUrl: allowAll,
|
|
137
|
+
timeout: TIMEOUT_MS,
|
|
138
|
+
});
|
|
139
|
+
assert.strictEqual(r.status, 500);
|
|
140
|
+
assert.strictEqual(r.body, '');
|
|
141
|
+
assert.ok(Date.now() - started < 200, 'should return on the status, not wait for the deadline');
|
|
142
|
+
assert.ok(await waitFor(() => srv.closed.includes('/error-trickle/did')), 'error response socket was not released');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('WebID-TLS releases a trickling error-status body', async () => {
|
|
146
|
+
clearTlsCache();
|
|
147
|
+
const cert = { modulus: 'abc123def456', exponent: '10001' };
|
|
148
|
+
const started = Date.now();
|
|
149
|
+
const verified = await verifyWebIdTls(cert, `http://127.0.0.1:${srv.port}/error-trickle/tls`, { timeout: TIMEOUT_MS });
|
|
150
|
+
assert.strictEqual(verified, false);
|
|
151
|
+
assert.ok(Date.now() - started < 200, 'should fail on the status, not wait for the deadline');
|
|
152
|
+
assert.ok(await waitFor(() => srv.closed.includes('/error-trickle/tls')), 'error response socket was not released');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('cid-doc-fetch fetchCidDocument (stubbed fetch, trickling stream)', () => {
|
|
156
|
+
const realFetch = global.fetch;
|
|
157
|
+
let cancelled = 0;
|
|
158
|
+
|
|
159
|
+
before(() => {
|
|
160
|
+
global.fetch = async (url, init = {}) => {
|
|
161
|
+
const signal = init.signal;
|
|
162
|
+
const status = String(url).includes('/err') ? 404 : 200;
|
|
163
|
+
let stop;
|
|
164
|
+
const stream = new ReadableStream({
|
|
165
|
+
start(controller) {
|
|
166
|
+
controller.enqueue(new TextEncoder().encode('{'));
|
|
167
|
+
const iv = setInterval(() => {
|
|
168
|
+
try { controller.enqueue(new TextEncoder().encode(' ')); } catch { /* errored or closed */ }
|
|
169
|
+
}, TRICKLE_MS);
|
|
170
|
+
stop = () => { clearInterval(iv); cancelled++; };
|
|
171
|
+
// Mirror what a real fetch body does on abort: error the stream.
|
|
172
|
+
signal?.addEventListener('abort', () => {
|
|
173
|
+
stop();
|
|
174
|
+
try { controller.error(new DOMException('This operation was aborted', 'AbortError')); } catch { /* already closed */ }
|
|
175
|
+
}, { once: true });
|
|
176
|
+
},
|
|
177
|
+
cancel() { stop?.(); },
|
|
178
|
+
});
|
|
179
|
+
return new Response(stream, { status, headers: { 'content-type': 'application/ld+json' } });
|
|
180
|
+
};
|
|
181
|
+
});
|
|
182
|
+
after(() => { global.fetch = realFetch; });
|
|
183
|
+
beforeEach(() => { _clearProfileCacheForTests(); cancelled = 0; });
|
|
184
|
+
|
|
185
|
+
it('rejects within the deadline instead of waiting for the body', async () => {
|
|
186
|
+
const started = Date.now();
|
|
187
|
+
await assert.rejects(
|
|
188
|
+
() => fetchCidDocument('https://example.com/profile/trickle.jsonld', { timeoutMs: TIMEOUT_MS }),
|
|
189
|
+
/timed out|abort/i,
|
|
190
|
+
);
|
|
191
|
+
const elapsed = Date.now() - started;
|
|
192
|
+
assert.ok(elapsed < MAX_ELAPSED_MS, `took ${elapsed}ms`);
|
|
193
|
+
assert.ok(cancelled >= 1, 'body stream was not released');
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('releases a trickling body on an error status instead of leaving it to GC', async () => {
|
|
197
|
+
const started = Date.now();
|
|
198
|
+
await assert.rejects(
|
|
199
|
+
() => fetchCidDocument('https://example.com/profile/err.jsonld', { timeoutMs: TIMEOUT_MS }),
|
|
200
|
+
/HTTP 404/,
|
|
201
|
+
);
|
|
202
|
+
assert.ok(Date.now() - started < 200, 'should fail on the status, not wait for the deadline');
|
|
203
|
+
assert.ok(await waitFor(() => cancelled >= 1), 'body stream was not released');
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('caches the timeout as a failure like any other fetch error', async () => {
|
|
207
|
+
await assert.rejects(() => fetchCidDocument('https://example.com/profile/trickle2.jsonld', { timeoutMs: TIMEOUT_MS }));
|
|
208
|
+
const started = Date.now();
|
|
209
|
+
await assert.rejects(() => fetchCidDocument('https://example.com/profile/trickle2.jsonld', { timeoutMs: TIMEOUT_MS }), /timed out/);
|
|
210
|
+
assert.ok(Date.now() - started < 100, 'second call should be served from the failure cache');
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe('readBodyWithCap', () => {
|
|
215
|
+
it('never returns a partial body when the signal fires mid-read', async () => {
|
|
216
|
+
const ac = new AbortController();
|
|
217
|
+
let pull = 0;
|
|
218
|
+
const stream = new ReadableStream({
|
|
219
|
+
pull(controller) {
|
|
220
|
+
pull++;
|
|
221
|
+
controller.enqueue(new TextEncoder().encode('x'));
|
|
222
|
+
if (pull === 3) ac.abort();
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
await assert.rejects(
|
|
226
|
+
() => readBodyWithCap(new Response(stream), { maxBytes: 1024, signal: ac.signal, label: 'thing' }),
|
|
227
|
+
/thing body read timed out/,
|
|
228
|
+
);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('still enforces the byte cap', async () => {
|
|
232
|
+
await assert.rejects(
|
|
233
|
+
() => readBodyWithCap(new Response('x'.repeat(2000)), { maxBytes: 1000, label: 'thing' }),
|
|
234
|
+
/thing too large \(>1000 bytes\)/,
|
|
235
|
+
);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('reads a normal body to completion', async () => {
|
|
239
|
+
const ac = new AbortController();
|
|
240
|
+
const text = await readBodyWithCap(new Response('{"ok":true}'), { maxBytes: 1000, signal: ac.signal });
|
|
241
|
+
assert.strictEqual(text, '{"ok":true}');
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('rejects immediately on an already-aborted signal', async () => {
|
|
245
|
+
const ac = new AbortController();
|
|
246
|
+
ac.abort();
|
|
247
|
+
await assert.rejects(
|
|
248
|
+
() => readBodyWithCap(new Response('x'), { maxBytes: 10, signal: ac.signal }),
|
|
249
|
+
/response body read timed out/,
|
|
250
|
+
);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
});
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Concurrent conditional writes (#600)
|
|
3
|
+
*
|
|
4
|
+
* If-Match is only useful if the precondition check and the write are one step.
|
|
5
|
+
* Several writers racing with the same, currently valid, ETag must end with exactly
|
|
6
|
+
* one of them succeeding; the rest get 412 and the resource holds the winner's body.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, it, before, after } from 'node:test';
|
|
10
|
+
import assert from 'node:assert';
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import { startTestServer, stopTestServer, request, createTestPod, assertStatus } from './helpers.js';
|
|
13
|
+
|
|
14
|
+
const POD = 'racetest';
|
|
15
|
+
const JSON_LD = { 'Content-Type': 'application/ld+json' };
|
|
16
|
+
|
|
17
|
+
const put = (path, body, headers = {}) => request(path, {
|
|
18
|
+
method: 'PUT', headers: { ...JSON_LD, ...headers }, body: JSON.stringify(body), auth: POD
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
async function createAndGetEtag(path, body) {
|
|
22
|
+
assertStatus(await put(path, body), 201);
|
|
23
|
+
const res = await request(path, { auth: POD });
|
|
24
|
+
const etag = res.headers.get('ETag');
|
|
25
|
+
assert.ok(etag, 'resource has an ETag');
|
|
26
|
+
return etag;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe('Concurrent conditional writes (#600)', () => {
|
|
30
|
+
before(async () => {
|
|
31
|
+
await startTestServer();
|
|
32
|
+
await createTestPod(POD);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
after(async () => {
|
|
36
|
+
await stopTestServer();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('racing PUTs with the same If-Match: exactly one wins, the others get 412', async () => {
|
|
40
|
+
const path = `/${POD}/public/race-put.json`;
|
|
41
|
+
const etag = await createAndGetEtag(path, { '@id': '#doc', 'http://example.org/v': 0 });
|
|
42
|
+
|
|
43
|
+
const bodies = [1, 2, 3, 4, 5].map((v) => ({ '@id': '#doc', 'http://example.org/v': v, 'http://example.org/pad': 'x'.repeat(v * 7) }));
|
|
44
|
+
const results = await Promise.all(bodies.map((b) => put(path, b, { 'If-Match': etag })));
|
|
45
|
+
const statuses = results.map((r) => r.status);
|
|
46
|
+
|
|
47
|
+
const winners = statuses.filter((s) => s === 204);
|
|
48
|
+
assert.strictEqual(winners.length, 1, `exactly one 204, got ${statuses.join(',')}`);
|
|
49
|
+
assert.ok(statuses.filter((s) => s === 412).length === bodies.length - 1, `the rest are 412, got ${statuses.join(',')}`);
|
|
50
|
+
|
|
51
|
+
const stored = await (await request(path, { auth: POD })).json();
|
|
52
|
+
const winner = bodies[statuses.indexOf(204)];
|
|
53
|
+
assert.strictEqual(stored['http://example.org/v'], winner['http://example.org/v'], 'the stored body is the winner\'s');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('racing DELETE and PUT with the same If-Match: exactly one wins', async () => {
|
|
57
|
+
const path = `/${POD}/public/race-delete.json`;
|
|
58
|
+
const etag = await createAndGetEtag(path, { '@id': '#doc', 'http://example.org/v': 0 });
|
|
59
|
+
|
|
60
|
+
const [del, upd] = await Promise.all([
|
|
61
|
+
request(path, { method: 'DELETE', headers: { 'If-Match': etag }, auth: POD }),
|
|
62
|
+
put(path, { '@id': '#doc', 'http://example.org/v': 1, 'http://example.org/pad': 'yyyyyyyy' }, { 'If-Match': etag })
|
|
63
|
+
]);
|
|
64
|
+
const ok = [del.status, upd.status].filter((s) => s === 204).length;
|
|
65
|
+
assert.strictEqual(ok, 1, `exactly one of DELETE/PUT succeeds, got ${del.status}/${upd.status}`);
|
|
66
|
+
assert.strictEqual([del.status, upd.status].includes(412), true, `the loser gets 412, got ${del.status}/${upd.status}`);
|
|
67
|
+
|
|
68
|
+
const after = await request(path, { auth: POD });
|
|
69
|
+
if (del.status === 204) assertStatus(after, 404, 'DELETE won, resource is gone');
|
|
70
|
+
else assert.strictEqual((await after.json())['http://example.org/v'], 1, 'PUT won, resource holds its body');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('racing If-None-Match: * creates: exactly one 201', async () => {
|
|
74
|
+
const path = `/${POD}/public/race-create.json`;
|
|
75
|
+
const results = await Promise.all([1, 2, 3].map((v) => put(path, { '@id': '#doc', 'http://example.org/v': v }, { 'If-None-Match': '*' })));
|
|
76
|
+
const statuses = results.map((r) => r.status);
|
|
77
|
+
assert.strictEqual(statuses.filter((s) => s === 201).length, 1, `exactly one 201, got ${statuses.join(',')}`);
|
|
78
|
+
assert.strictEqual(statuses.filter((s) => s === 412).length, 2, `the rest are 412, got ${statuses.join(',')}`);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('a failed If-Match PUT into a container that does not exist leaves nothing behind', async () => {
|
|
82
|
+
const path = `/${POD}/public/race-nodir/x.json`;
|
|
83
|
+
assertStatus(await put(path, { '@id': '#doc' }, { 'If-Match': '"nope"' }), 412);
|
|
84
|
+
assertStatus(await request(`/${POD}/public/race-nodir/`, { auth: POD }), 404, 'no container was created');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('no lock file is left next to the resource', async () => {
|
|
88
|
+
const path = `/${POD}/public/race-lock.json`;
|
|
89
|
+
const etag = await createAndGetEtag(path, { '@id': '#doc', 'http://example.org/v': 0 });
|
|
90
|
+
await Promise.all([1, 2].map((v) => put(path, { '@id': '#doc', 'http://example.org/v': v, 'http://example.org/pad': 'p'.repeat(v * 3) }, { 'If-Match': etag })));
|
|
91
|
+
const listing = await (await request(`/${POD}/public/`, { headers: { Accept: 'application/ld+json' }, auth: POD })).text();
|
|
92
|
+
assert.ok(!listing.includes('race-lock.json.lock'), 'lock sidecar is not listed');
|
|
93
|
+
assert.ok(!fs.existsSync(`./data/${POD}/public/.race-lock.json.lock`), 'lock sidecar is gone from disk');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('the same PUTs in sequence: 204 then 412', async () => {
|
|
97
|
+
const path = `/${POD}/public/race-seq.json`;
|
|
98
|
+
const etag = await createAndGetEtag(path, { '@id': '#doc', 'http://example.org/v': 0 });
|
|
99
|
+
assertStatus(await put(path, { '@id': '#doc', 'http://example.org/v': 1, 'http://example.org/pad': 'zzzz' }, { 'If-Match': etag }), 204);
|
|
100
|
+
assertStatus(await put(path, { '@id': '#doc', 'http://example.org/v': 2, 'http://example.org/pad': 'zzzzzz' }, { 'If-Match': etag }), 412);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dotfile guard vs percent-encoding (#639)
|
|
3
|
+
*
|
|
4
|
+
* The guard in server.js used to look at the raw request URL while the
|
|
5
|
+
* storage lookup decoded it, so `/pod/%2Egit/config` passed the guard
|
|
6
|
+
* and read `.git/config` from disk. Both now decode the same way.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, it, before, after } from 'node:test';
|
|
9
|
+
import assert from 'node:assert';
|
|
10
|
+
import fs from 'fs-extra';
|
|
11
|
+
import path from 'path';
|
|
12
|
+
import { startTestServer, stopTestServer, createTestPod, request, assertStatus } from './helpers.js';
|
|
13
|
+
import { urlToPath, urlToPathWithPod } from '../src/utils/url.js';
|
|
14
|
+
import { decodeSegments, findForbiddenDotfile } from '../src/utils/dotfiles.js';
|
|
15
|
+
|
|
16
|
+
const POD = 'dotguard';
|
|
17
|
+
const CANARY = 'canary-639-do-not-serve';
|
|
18
|
+
const podDir = path.resolve('./data', POD);
|
|
19
|
+
|
|
20
|
+
describe('dotfile guard decodes the path before checking it (#639)', () => {
|
|
21
|
+
before(async () => {
|
|
22
|
+
await startTestServer();
|
|
23
|
+
await createTestPod(POD);
|
|
24
|
+
await fs.outputFile(path.join(podDir, '.git', 'config'), `[canary]\n\tvalue = ${CANARY}\n`);
|
|
25
|
+
await fs.outputFile(path.join(podDir, '.env'), `SECRET=${CANARY}\n`);
|
|
26
|
+
await fs.outputFile(path.join(podDir, 'public', '.htpasswd'), `${CANARY}\n`);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
after(async () => {
|
|
30
|
+
await stopTestServer();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
async function assertBlocked(urlPath, status = 403) {
|
|
34
|
+
const res = await request(urlPath);
|
|
35
|
+
const body = await res.text();
|
|
36
|
+
assertStatus(res, status, `${urlPath} should be ${status}`);
|
|
37
|
+
assert.ok(!body.includes(CANARY), `${urlPath} leaked the file body`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
it('still blocks the literal dotfile paths', async () => {
|
|
41
|
+
await assertBlocked(`/${POD}/.git/config`);
|
|
42
|
+
await assertBlocked(`/${POD}/.env`);
|
|
43
|
+
await assertBlocked(`/${POD}/public/.htpasswd`);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('blocks a real .git/config requested with an encoded dot', async () => {
|
|
47
|
+
await assertBlocked(`/${POD}/%2Egit/config`);
|
|
48
|
+
await assertBlocked(`/${POD}/%2egit/config`);
|
|
49
|
+
await assertBlocked(`/${POD}/%2Egit/%63onfig`);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('blocks encoded forms of the other dotfiles', async () => {
|
|
53
|
+
await assertBlocked(`/${POD}/%2Eenv`);
|
|
54
|
+
await assertBlocked(`/${POD}/public/%2Ehtpasswd`);
|
|
55
|
+
await assertBlocked(`/${POD}/public/%2E%68tpasswd`);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('blocks encoded dotfiles on every method', async () => {
|
|
59
|
+
for (const method of ['HEAD', 'PUT', 'DELETE', 'PATCH', 'POST']) {
|
|
60
|
+
const res = await request(`/${POD}/%2Egit/config`, {
|
|
61
|
+
method,
|
|
62
|
+
headers: { 'Content-Type': 'text/plain' },
|
|
63
|
+
body: method === 'HEAD' ? undefined : 'x',
|
|
64
|
+
auth: POD
|
|
65
|
+
});
|
|
66
|
+
assertStatus(res, 403, `${method} should be 403`);
|
|
67
|
+
}
|
|
68
|
+
assert.strictEqual(await fs.readFile(path.join(podDir, '.git', 'config'), 'utf8'),
|
|
69
|
+
`[canary]\n\tvalue = ${CANARY}\n`, 'the file on disk is untouched');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('does not serve the file for a double-encoded dot either', async () => {
|
|
73
|
+
// Decoded once this is `%2Egit`, which is not a file on disk: 404, never the body.
|
|
74
|
+
const res = await request(`/${POD}/%252Egit/config`, { auth: POD });
|
|
75
|
+
const body = await res.text();
|
|
76
|
+
assertStatus(res, 404);
|
|
77
|
+
assert.ok(!body.includes(CANARY));
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('refuses a path it cannot decode instead of passing it on', async () => {
|
|
81
|
+
await assertBlocked(`/${POD}/%E0%A4%A/config`, 400);
|
|
82
|
+
await assertBlocked(`/${POD}/%2Egit%2Fconfig`, 400);
|
|
83
|
+
await assertBlocked(`/${POD}/public%2F%2Ehtpasswd`, 400);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('allows the Solid dotfiles only when the dot is literal', async () => {
|
|
87
|
+
const literal = await request(`/${POD}/public/.acl`, { auth: POD });
|
|
88
|
+
assert.notStrictEqual(literal.status, 403);
|
|
89
|
+
const encoded = await request(`/${POD}/public/%2Eacl`, { auth: POD });
|
|
90
|
+
assertStatus(encoded, 403, 'a dot that only appears after decoding is refused');
|
|
91
|
+
const wellKnown = await request('/.well-known/openid-configuration');
|
|
92
|
+
assert.notStrictEqual(wellKnown.status, 403);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('leaves ordinary encoded names alone', async () => {
|
|
96
|
+
const put = await request(`/${POD}/public/a%20b.txt`, {
|
|
97
|
+
method: 'PUT', headers: { 'Content-Type': 'text/plain' }, body: 'hello', auth: POD
|
|
98
|
+
});
|
|
99
|
+
assert.ok([201, 204].includes(put.status), `PUT gave ${put.status}`);
|
|
100
|
+
const get = await request(`/${POD}/public/a%20b.txt`);
|
|
101
|
+
assertStatus(get, 200);
|
|
102
|
+
assert.strictEqual(await get.text(), 'hello');
|
|
103
|
+
const trailingDot = await request(`/${POD}/public/name%2E`);
|
|
104
|
+
assert.notStrictEqual(trailingDot.status, 403);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('the path lookup refuses what the guard refuses (#639)', () => {
|
|
109
|
+
it('rejects a segment that only becomes a dotfile by decoding', () => {
|
|
110
|
+
for (const p of ['/alice/%2Egit/config', '/alice/%2eenv', '/alice/%2Eacl', '/alice/pub/%2E%2E%2Egit']) {
|
|
111
|
+
assert.throws(() => urlToPath(p), (err) => err.statusCode === 403, p);
|
|
112
|
+
assert.throws(() => urlToPathWithPod(p, 'alice'), (err) => err.statusCode === 403, p);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('rejects malformed encoding and encoded separators with 400', () => {
|
|
117
|
+
for (const p of ['/alice/%E0%A4%A', '/alice/pub%2Fx', '/alice/pub%5Cx']) {
|
|
118
|
+
assert.throws(() => urlToPath(p), (err) => err.statusCode === 400, p);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('still resolves literal dotfile names for internal callers', () => {
|
|
123
|
+
assert.ok(urlToPath('/alice/.git/config').endsWith(path.join('alice', '.git', 'config')));
|
|
124
|
+
assert.ok(urlToPath('/alice/.quota.json').endsWith('.quota.json'));
|
|
125
|
+
assert.ok(urlToPath('/alice/.%61cl').endsWith(path.join('alice', '.acl')));
|
|
126
|
+
assert.ok(urlToPathWithPod('/public/.acl', 'alice').endsWith(path.join('alice', 'public', '.acl')));
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('keeps the traversal stripping', () => {
|
|
130
|
+
const root = urlToPath('/');
|
|
131
|
+
assert.strictEqual(urlToPath('/alice/%2E%2E/%2E%2E/etc/passwd'), path.join(root, 'alice', 'etc', 'passwd'));
|
|
132
|
+
assert.strictEqual(urlToPath('//alice/....//x'), path.join(root, 'alice', 'x'));
|
|
133
|
+
assert.throws(() => urlToPath('/%2E%2E%2Fetc'), (err) => err.statusCode === 400);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('decodeSegments and findForbiddenDotfile agree with the guard', () => {
|
|
137
|
+
assert.deepStrictEqual(decodeSegments('/a/%2Eb').map((s) => s.decoded), ['', 'a', '.b']);
|
|
138
|
+
assert.strictEqual(findForbiddenDotfile('/alice/%2Egit/config'), '.git');
|
|
139
|
+
assert.strictEqual(findForbiddenDotfile('/alice/.acl'), null);
|
|
140
|
+
assert.strictEqual(findForbiddenDotfile('/alice/%2Eacl'), '.acl');
|
|
141
|
+
assert.strictEqual(findForbiddenDotfile('/.well-known/openid-configuration'), null);
|
|
142
|
+
assert.throws(() => findForbiddenDotfile('/%zz'), (err) => err.statusCode === 400);
|
|
143
|
+
});
|
|
144
|
+
});
|