javascript-solid-server 0.0.223 → 0.0.225
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/docs/configuration.md +8 -0
- package/docs/security.md +26 -0
- package/package.json +1 -1
- package/src/handlers/resource.js +66 -23
- package/src/nostr/relay.js +50 -29
- package/src/server.js +52 -18
- package/src/storage/filesystem.js +39 -6
- package/src/utils/dotfiles.js +70 -0
- package/src/utils/url.js +40 -23
- package/test/app-paths.test.js +87 -5
- package/test/conditional-race.test.js +102 -0
- package/test/dotfile-guard.test.js +144 -0
- package/test/nostr-relay-nip11.test.js +65 -0
- package/test/plugin-reservepath.test.js +11 -0
- package/test/withlock.test.js +81 -0
package/docs/configuration.md
CHANGED
|
@@ -372,6 +372,14 @@ built-in `/storage/` and `/db/` routes. Everything outside the declared
|
|
|
372
372
|
prefixes keeps full WAC enforcement. Entries must start with `/` and be
|
|
373
373
|
longer than `/`; anything else is ignored.
|
|
374
374
|
|
|
375
|
+
A path under an app prefix that the application did **not** register is a
|
|
376
|
+
404 inside the app's namespace
|
|
377
|
+
([#611](https://github.com/JavaScriptSolidServer/JavaScriptSolidServer/issues/611)):
|
|
378
|
+
the LDP handlers never read or write pod storage below an app path, since
|
|
379
|
+
WAC has already been skipped there. Files an app wants served from the pod
|
|
380
|
+
belong outside its prefix (for example `/public/apps/<name>/`), where WAC
|
|
381
|
+
applies.
|
|
382
|
+
|
|
375
383
|
Because the WAC hook is also what populates `request.webId`, requests under
|
|
376
384
|
an app path never carry it — don't rely on `request.webId` in app handlers.
|
|
377
385
|
Resolve identity with the public accessor
|
package/docs/security.md
CHANGED
|
@@ -43,6 +43,32 @@ Save this as `data/.acl` (replacing `your-domain.com` with your actual domain).
|
|
|
43
43
|
|
|
44
44
|
See [Issue #32](https://github.com/JavaScriptSolidServer/JavaScriptSolidServer/issues/32) for background.
|
|
45
45
|
|
|
46
|
+
### Dotfiles
|
|
47
|
+
|
|
48
|
+
Paths whose segments start with a dot are refused with `403` unless the
|
|
49
|
+
segment is one of the Solid-specific names (`.well-known`, `.acl`, `.meta`,
|
|
50
|
+
`.pods`, `.notifications`, `.account`). This keeps `.git/`, `.env`,
|
|
51
|
+
`.htpasswd` and similar files out of reach over HTTP even when they sit
|
|
52
|
+
inside a pod. Git smart-HTTP, the pay routes and app plugins mounted at a
|
|
53
|
+
dot prefix are allowed through on their own terms.
|
|
54
|
+
|
|
55
|
+
The check runs on the percent-decoded path, segment by segment, and the
|
|
56
|
+
storage lookup applies the same rule, so `%2Egit` is `.git` on both sides
|
|
57
|
+
and neither can be talked past the other (#639). A dot has to be literal
|
|
58
|
+
in the request: a segment that only starts with a dot after decoding is
|
|
59
|
+
refused even for the allowed names. A path that cannot be decoded, or that
|
|
60
|
+
hides a path separator behind `%2F`, is refused with `400` instead of
|
|
61
|
+
being passed on.
|
|
62
|
+
|
|
63
|
+
### Conditional writes are atomic
|
|
64
|
+
|
|
65
|
+
`If-Match` and `If-None-Match` on PUT, DELETE and PATCH are checked and acted on as one
|
|
66
|
+
step. Writes to a resource are queued within the process and held under a lock file next
|
|
67
|
+
to the resource (`.<name>.lock`, never listed or served) across processes that share the
|
|
68
|
+
data directory. Two clients racing with the same, still valid, ETag get one `2xx` and one
|
|
69
|
+
`412`; the loser's write is never silently overwritten. A request that cannot take the lock
|
|
70
|
+
within 30 seconds gets `503` with `Retry-After: 1`.
|
|
71
|
+
|
|
46
72
|
## Subdomain Mode (XSS Protection)
|
|
47
73
|
|
|
48
74
|
By default, JSS uses **path-based pods** (`/alice/`, `/bob/`). This is simple but has a security limitation: all pods share the same origin, making cross-site scripting (XSS) attacks possible between pods.
|
package/package.json
CHANGED
package/src/handlers/resource.js
CHANGED
|
@@ -911,6 +911,35 @@ export async function handleHead(request, reply) {
|
|
|
911
911
|
/**
|
|
912
912
|
* Handle PUT request
|
|
913
913
|
*/
|
|
914
|
+
// Writes to one resource run one at a time. A conditional write (If-Match,
|
|
915
|
+
// If-None-Match) is stat → compare → write; two of them racing with the same, still
|
|
916
|
+
// valid, ETag would both pass the check and the loser's write would vanish with a 2xx
|
|
917
|
+
// (#600). PATCH is read-modify-write and was already serialised this way. So PUT,
|
|
918
|
+
// DELETE and PATCH on a path are queued within this process and run under a lock file
|
|
919
|
+
// on disk across processes that share the data directory (storage.withLock).
|
|
920
|
+
const writeQueues = new Map(); // resolved lock path -> tail promise
|
|
921
|
+
|
|
922
|
+
async function serialised(storagePath, reply, fn) {
|
|
923
|
+
// Key the queue by the same resolved file the disk lock uses, so two spellings of
|
|
924
|
+
// one resource share a queue as well as a lock.
|
|
925
|
+
const key = storage.lockPathFor(storagePath);
|
|
926
|
+
const previous = writeQueues.get(key) ?? Promise.resolve();
|
|
927
|
+
let release;
|
|
928
|
+
const mine = new Promise((resolve) => { release = resolve; });
|
|
929
|
+
const tail = previous.then(() => mine);
|
|
930
|
+
writeQueues.set(key, tail);
|
|
931
|
+
await previous;
|
|
932
|
+
try {
|
|
933
|
+
return await storage.withLock(storagePath, fn);
|
|
934
|
+
} catch (err) {
|
|
935
|
+
if (err.code === 'ELOCKED') return reply.code(503).header('Retry-After', '1').send({ error: 'Service Unavailable', message: 'The resource is being written by another request; retry shortly' });
|
|
936
|
+
throw err;
|
|
937
|
+
} finally {
|
|
938
|
+
release();
|
|
939
|
+
if (writeQueues.get(key) === tail) writeQueues.delete(key);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
914
943
|
export async function handlePut(request, reply) {
|
|
915
944
|
// Read-only mode - block all writes
|
|
916
945
|
if (request.config?.readOnly) {
|
|
@@ -1010,6 +1039,25 @@ export async function handlePut(request, reply) {
|
|
|
1010
1039
|
});
|
|
1011
1040
|
}
|
|
1012
1041
|
|
|
1042
|
+
// If-Match on a resource that is not there is 412 whatever happens next. Answer
|
|
1043
|
+
// before taking the lock, so that a failed conditional PUT into a container that does
|
|
1044
|
+
// not exist yet never creates it (the lock file lives next to the resource). This is
|
|
1045
|
+
// consistent under contention: if another writer creates the resource between this
|
|
1046
|
+
// probe and the lock, our 412 is the answer this request would have got had it run
|
|
1047
|
+
// first, and the client's ETag cannot match a resource it has never seen. The check
|
|
1048
|
+
// is repeated under the lock for the ordinary case.
|
|
1049
|
+
const ifMatchEarly = request.headers['if-match'];
|
|
1050
|
+
if (ifMatchEarly && !await storage.stat(storagePath)) {
|
|
1051
|
+
const check = checkIfMatch(ifMatchEarly, null);
|
|
1052
|
+
if (!check.ok) return reply.code(check.status).send({ error: check.error });
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
// From here on the request holds the resource: the precondition check and the
|
|
1056
|
+
// write must not be split by another writer (#600).
|
|
1057
|
+
return serialised(storagePath, reply, () => writeResource(request, reply, { storagePath, resourceUrl, contentType, connegEnabled }));
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
async function writeResource(request, reply, { storagePath, resourceUrl, contentType, connegEnabled }) {
|
|
1013
1061
|
// Check if resource already exists and get current ETag
|
|
1014
1062
|
const stats = await storage.stat(storagePath);
|
|
1015
1063
|
const existed = stats !== null;
|
|
@@ -1114,15 +1162,27 @@ export async function handleDelete(request, reply) {
|
|
|
1114
1162
|
|
|
1115
1163
|
const { storagePath, resourceUrl } = getRequestPaths(request);
|
|
1116
1164
|
|
|
1117
|
-
|
|
1118
|
-
const stats = await storage.stat(storagePath);
|
|
1119
|
-
if (!stats) {
|
|
1165
|
+
const notFound = () => {
|
|
1120
1166
|
const origin = request.headers.origin;
|
|
1121
1167
|
const connegEnabled = request.connegEnabled || false;
|
|
1122
1168
|
const headers = getNotFoundHeaders({ resourceUrl, origin, connegEnabled });
|
|
1123
1169
|
Object.entries(headers).forEach(([k, v]) => reply.header(k, v));
|
|
1124
1170
|
return reply.code(404).send({ error: 'Not Found' });
|
|
1125
|
-
}
|
|
1171
|
+
};
|
|
1172
|
+
|
|
1173
|
+
// A missing resource is 404 before taking the lock, so that a DELETE of nothing
|
|
1174
|
+
// never creates the lock's directory. Under contention this is the answer the
|
|
1175
|
+
// request would have got had it run first; the stat is repeated under the lock.
|
|
1176
|
+
if (!await storage.stat(storagePath)) return notFound();
|
|
1177
|
+
|
|
1178
|
+
// The precondition check and the removal must not be split by another writer (#600).
|
|
1179
|
+
return serialised(storagePath, reply, () => removeResource(request, reply, { storagePath, resourceUrl, notFound }));
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
async function removeResource(request, reply, { storagePath, resourceUrl, notFound }) {
|
|
1183
|
+
// Stat again under the lock: the resource may have gone while we waited
|
|
1184
|
+
const stats = await storage.stat(storagePath);
|
|
1185
|
+
if (!stats) return notFound();
|
|
1126
1186
|
|
|
1127
1187
|
// Check If-Match header (for safe deletes)
|
|
1128
1188
|
const ifMatch = request.headers['if-match'];
|
|
@@ -1191,27 +1251,10 @@ export async function handleOptions(request, reply) {
|
|
|
1191
1251
|
// PATCH is read-modify-write. Two patches to the same resource at once (two people
|
|
1192
1252
|
// posting in a chat) must not interleave, or one of them is lost and the file can be
|
|
1193
1253
|
// left with the tail of a longer earlier write. Patches to one resource run one at a
|
|
1194
|
-
// time
|
|
1195
|
-
// that share the data directory (storage.withLock).
|
|
1196
|
-
const patchQueues = new Map(); // storagePath -> tail promise
|
|
1197
|
-
|
|
1254
|
+
// time, through the same queue and lock as PUT and DELETE (see serialised above).
|
|
1198
1255
|
export async function handlePatch(request, reply) {
|
|
1199
1256
|
const { storagePath } = getRequestPaths(request);
|
|
1200
|
-
|
|
1201
|
-
let release;
|
|
1202
|
-
const mine = new Promise((resolve) => { release = resolve; });
|
|
1203
|
-
const tail = previous.then(() => mine);
|
|
1204
|
-
patchQueues.set(storagePath, tail);
|
|
1205
|
-
await previous;
|
|
1206
|
-
try {
|
|
1207
|
-
return await storage.withLock(storagePath, () => applyPatchRequest(request, reply));
|
|
1208
|
-
} catch (err) {
|
|
1209
|
-
if (err.code === 'ELOCKED') return reply.code(503).header('Retry-After', '1').send({ error: 'Service Unavailable', message: 'The resource is being written by another request; retry shortly' });
|
|
1210
|
-
throw err;
|
|
1211
|
-
} finally {
|
|
1212
|
-
release();
|
|
1213
|
-
if (patchQueues.get(storagePath) === tail) patchQueues.delete(storagePath);
|
|
1214
|
-
}
|
|
1257
|
+
return serialised(storagePath, reply, () => applyPatchRequest(request, reply));
|
|
1215
1258
|
}
|
|
1216
1259
|
|
|
1217
1260
|
async function applyPatchRequest(request, reply) {
|
package/src/nostr/relay.js
CHANGED
|
@@ -233,34 +233,11 @@ export async function registerNostrRelay(fastify, options = {}) {
|
|
|
233
233
|
await fastify.register(websocket);
|
|
234
234
|
}
|
|
235
235
|
|
|
236
|
-
//
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
try {
|
|
242
|
-
const message = JSON.parse(data.toString());
|
|
243
|
-
const [type, value, ...rest] = message;
|
|
244
|
-
await processMessage(type, value, rest, socket);
|
|
245
|
-
} catch (e) {
|
|
246
|
-
socket.send(JSON.stringify(['NOTICE', `Error: ${e.message}`]));
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
socket.on('close', () => {
|
|
251
|
-
subscribers.delete(socket);
|
|
252
|
-
rateLimits.delete(socket);
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
socket.on('error', () => {
|
|
256
|
-
subscribers.delete(socket);
|
|
257
|
-
rateLimits.delete(socket);
|
|
258
|
-
});
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
// NIP-11: Relay Information Document at /relay/info
|
|
262
|
-
fastify.get(path + '/info', (request, reply) => {
|
|
263
|
-
const relayInfo = {
|
|
236
|
+
// NIP-11 relay information document. Served at the relay URL itself when
|
|
237
|
+
// the client sends `Accept: application/nostr+json` (what the spec and
|
|
238
|
+
// tools such as `nak relay` expect), and at <path>/info for older callers.
|
|
239
|
+
function relayInfo() {
|
|
240
|
+
return {
|
|
264
241
|
name: 'JSS Nostr Relay',
|
|
265
242
|
description: 'Nostr relay integrated with JavaScript Solid Server',
|
|
266
243
|
pubkey: '',
|
|
@@ -269,11 +246,55 @@ export async function registerNostrRelay(fastify, options = {}) {
|
|
|
269
246
|
software: 'https://github.com/JavaScriptSolidServer/JavaScriptSolidServer',
|
|
270
247
|
version: '0.0.1'
|
|
271
248
|
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function wantsRelayInfo(request) {
|
|
252
|
+
return (request.headers.accept || '').toLowerCase().includes('application/nostr+json');
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Register the relay route: WebSocket upgrades go to wsHandler, plain HTTP
|
|
256
|
+
// GETs to handler (NIP-11 when asked for, 404 otherwise, as before).
|
|
257
|
+
fastify.route({
|
|
258
|
+
method: 'GET',
|
|
259
|
+
url: path,
|
|
260
|
+
handler: (request, reply) => {
|
|
261
|
+
if (!wantsRelayInfo(request)) return reply.code(404).send();
|
|
262
|
+
return reply
|
|
263
|
+
.header('Access-Control-Allow-Origin', '*')
|
|
264
|
+
.header('Content-Type', 'application/nostr+json')
|
|
265
|
+
.send(JSON.stringify(relayInfo()));
|
|
266
|
+
},
|
|
267
|
+
wsHandler: (connection, request) => {
|
|
268
|
+
const socket = connection.socket;
|
|
269
|
+
|
|
270
|
+
socket.on('message', async (data) => {
|
|
271
|
+
try {
|
|
272
|
+
const message = JSON.parse(data.toString());
|
|
273
|
+
const [type, value, ...rest] = message;
|
|
274
|
+
await processMessage(type, value, rest, socket);
|
|
275
|
+
} catch (e) {
|
|
276
|
+
socket.send(JSON.stringify(['NOTICE', `Error: ${e.message}`]));
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
socket.on('close', () => {
|
|
281
|
+
subscribers.delete(socket);
|
|
282
|
+
rateLimits.delete(socket);
|
|
283
|
+
});
|
|
272
284
|
|
|
285
|
+
socket.on('error', () => {
|
|
286
|
+
subscribers.delete(socket);
|
|
287
|
+
rateLimits.delete(socket);
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
// NIP-11 at <path>/info, kept for existing callers
|
|
293
|
+
fastify.get(path + '/info', (request, reply) => {
|
|
273
294
|
return reply
|
|
274
295
|
.header('Access-Control-Allow-Origin', '*')
|
|
275
296
|
.header('Content-Type', 'application/json')
|
|
276
|
-
.send(relayInfo);
|
|
297
|
+
.send(relayInfo());
|
|
277
298
|
});
|
|
278
299
|
|
|
279
300
|
return {
|
package/src/server.js
CHANGED
|
@@ -34,6 +34,7 @@ import { webrtcPlugin } from './webrtc/index.js';
|
|
|
34
34
|
import { tunnelPlugin } from './tunnel/index.js';
|
|
35
35
|
import { terminalPlugin } from './terminal/index.js';
|
|
36
36
|
import { registerErrorHandler } from './utils/error-handler.js';
|
|
37
|
+
import { findForbiddenDotfile } from './utils/dotfiles.js';
|
|
37
38
|
import { seedServerRoot } from './ui/server-root.js';
|
|
38
39
|
import { assertProvisionKeysCompatible } from './keys/provision.js';
|
|
39
40
|
|
|
@@ -571,7 +572,6 @@ export function createServer(options = {}) {
|
|
|
571
572
|
// Security: Block access to dotfiles except allowed Solid-specific ones
|
|
572
573
|
// This prevents exposure of .git/, .env, .htpasswd, etc.
|
|
573
574
|
// Git protocol requests bypass this check when git is enabled
|
|
574
|
-
const ALLOWED_DOTFILES = ['.well-known', '.acl', '.meta', '.pods', '.notifications', '.account'];
|
|
575
575
|
fastify.addHook('onRequest', async (request, reply) => {
|
|
576
576
|
// Allow git protocol requests through when git is enabled
|
|
577
577
|
if (gitEnabled && isGitRequest(request.url)) {
|
|
@@ -609,14 +609,18 @@ export function createServer(options = {}) {
|
|
|
609
609
|
// legitimate proxy requests for upstream URLs that happen to
|
|
610
610
|
// contain dotfile-like path segments. The dotfile guard is about
|
|
611
611
|
// *this* pod's filesystem, not what the URL looks like.
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
612
|
+
//
|
|
613
|
+
// Segments are percent-decoded before the check, the same way the
|
|
614
|
+
// storage lookup decodes them (#639): `%2Egit` is `.git` on disk.
|
|
615
|
+
// A path that cannot be decoded is refused rather than passed on.
|
|
616
|
+
let forbidden;
|
|
617
|
+
try {
|
|
618
|
+
forbidden = findForbiddenDotfile(urlNoQuery);
|
|
619
|
+
} catch (err) {
|
|
620
|
+
return reply.code(400).send({ error: 'Bad Request', message: err.message });
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
if (forbidden) {
|
|
620
624
|
return reply.code(403).send({ error: 'Forbidden', message: 'Dotfile access is not allowed' });
|
|
621
625
|
}
|
|
622
626
|
});
|
|
@@ -780,6 +784,19 @@ export function createServer(options = {}) {
|
|
|
780
784
|
const accept = request.headers.accept || '';
|
|
781
785
|
const wantsAP = accept.includes('activity+json') || accept.includes('ld+json; profile="https://www.w3.org/ns/activitystreams"');
|
|
782
786
|
const isProfileAP = activitypubEnabled && wantsAP && (request.url === '/profile/card.jsonld' || request.url.startsWith('/profile/card.jsonld?'));
|
|
787
|
+
// An app-owned subtree (appPaths from createServer or a plugin prefix,
|
|
788
|
+
// #582; reservePath claims, #602) skips WAC because the app owns auth
|
|
789
|
+
// there. The LDP wildcards ('/*') sit beneath every app route, so a
|
|
790
|
+
// path the app did NOT register would otherwise fall through to pod
|
|
791
|
+
// storage with WAC already skipped: an unauthenticated read or write
|
|
792
|
+
// (#611). Mark the request; the wildcard handlers answer 404 for it,
|
|
793
|
+
// and OPTIONS answers a bare 204 without a storage lookup. This check
|
|
794
|
+
// runs before the generic OPTIONS skip below so preflights are marked.
|
|
795
|
+
if (appPaths.some(p => request.url === p || request.url.startsWith(p + '/') || request.url.startsWith(p + '?')) ||
|
|
796
|
+
appPathPatterns.some(m => m.methods.has(request.method) && m.re.test(request.url))) {
|
|
797
|
+
request.appPathExempt = true;
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
783
800
|
if (request.url === '/.pods' ||
|
|
784
801
|
request.url === '/.notifications' ||
|
|
785
802
|
request.method === 'OPTIONS' ||
|
|
@@ -799,8 +816,6 @@ export function createServer(options = {}) {
|
|
|
799
816
|
(webrtcEnabled && (request.url === webrtcPath || request.url.startsWith(webrtcPath + '?'))) ||
|
|
800
817
|
(terminalEnabled && (request.url === '/.terminal' || request.url.startsWith('/.terminal?'))) ||
|
|
801
818
|
(tunnelEnabled && (request.url === tunnelPath || request.url.startsWith(tunnelPath + '?') || request.url.startsWith('/tunnel/'))) ||
|
|
802
|
-
appPaths.some(p => request.url === p || request.url.startsWith(p + '/') || request.url.startsWith(p + '?')) ||
|
|
803
|
-
appPathPatterns.some(m => m.methods.has(request.method) && m.re.test(request.url)) ||
|
|
804
819
|
mashlibPaths.some(p => request.url === p || request.url.startsWith(p + '.'))) {
|
|
805
820
|
return;
|
|
806
821
|
}
|
|
@@ -965,15 +980,34 @@ export function createServer(options = {}) {
|
|
|
965
980
|
|
|
966
981
|
// LDP routes - using wildcard routing
|
|
967
982
|
// Read operations - no rate limit (handled by bodyLimit)
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
983
|
+
// The LDP wildcards never serve an app-owned subtree (#611): a request
|
|
984
|
+
// the WAC hook exempted as app-owned (request.appPathExempt) reaches
|
|
985
|
+
// these only because the app registered no route for it, so it is a 404
|
|
986
|
+
// inside the app's namespace, not a storage read or write.
|
|
987
|
+
const notAppRoute = async (request, reply) => {
|
|
988
|
+
if (!request.appPathExempt) return;
|
|
989
|
+
reply.code(404).send({ error: 'Not Found', message: 'no route in application namespace' });
|
|
990
|
+
return reply;
|
|
991
|
+
};
|
|
992
|
+
fastify.get('/*', { preHandler: notAppRoute }, handleGet);
|
|
993
|
+
fastify.head('/*', { preHandler: notAppRoute }, handleHead);
|
|
994
|
+
// A preflight under an app path must keep answering 204 (the CORS
|
|
995
|
+
// headers are already set by the onRequest hook) or browsers cannot
|
|
996
|
+
// call an app route that registered no OPTIONS handler. But it must not
|
|
997
|
+
// run handleOptions, whose storage.stat and Link headers would disclose
|
|
998
|
+
// whether something exists in pod storage below the prefix.
|
|
999
|
+
const appOptions = async (request, reply) => {
|
|
1000
|
+
if (!request.appPathExempt) return;
|
|
1001
|
+
reply.code(204).send();
|
|
1002
|
+
return reply;
|
|
1003
|
+
};
|
|
1004
|
+
fastify.options('/*', { preHandler: appOptions }, handleOptions);
|
|
971
1005
|
|
|
972
1006
|
// Write operations - rate limited
|
|
973
|
-
fastify.put('/*', writeRateLimit, handlePut);
|
|
974
|
-
fastify.delete('/*', writeRateLimit, handleDelete);
|
|
975
|
-
fastify.post('/*', writeRateLimit, handlePost);
|
|
976
|
-
fastify.patch('/*', writeRateLimit, handlePatch);
|
|
1007
|
+
fastify.put('/*', { ...writeRateLimit, preHandler: notAppRoute }, handlePut);
|
|
1008
|
+
fastify.delete('/*', { ...writeRateLimit, preHandler: notAppRoute }, handleDelete);
|
|
1009
|
+
fastify.post('/*', { ...writeRateLimit, preHandler: notAppRoute }, handlePost);
|
|
1010
|
+
fastify.patch('/*', { ...writeRateLimit, preHandler: notAppRoute }, handlePatch);
|
|
977
1011
|
|
|
978
1012
|
// Root route
|
|
979
1013
|
fastify.get('/', handleGet);
|
|
@@ -137,8 +137,9 @@ export async function write(urlPath, content, options = {}) {
|
|
|
137
137
|
// in-process queue in the PATCH handler covers one Node process; this lock file covers
|
|
138
138
|
// several processes sharing the data directory (PM2 cluster, instances behind a proxy).
|
|
139
139
|
// The lock is `.<name>.lock` next to the file (a dotfile, so never listed or served),
|
|
140
|
-
// created with O_EXCL.
|
|
141
|
-
//
|
|
140
|
+
// created with O_EXCL. Staleness is measured from the lock file's mtime: the holder
|
|
141
|
+
// touches it every LOCK_STALE_MS / 3 while it works, so a lock older than LOCK_STALE_MS
|
|
142
|
+
// belongs to a crashed process, not a slow one, and is broken.
|
|
142
143
|
const LOCK_STALE_MS = 15000;
|
|
143
144
|
const LOCK_WAIT_MS = 30000;
|
|
144
145
|
|
|
@@ -147,7 +148,31 @@ export function lockPathFor(urlPath) {
|
|
|
147
148
|
return path.join(path.dirname(filePath), `.${path.basename(filePath)}.lock`);
|
|
148
149
|
}
|
|
149
150
|
|
|
150
|
-
|
|
151
|
+
// Break a lock that was observed stale, and only that lock. Two contenders can both see
|
|
152
|
+
// the same dead lock; the faster one breaks it and takes a fresh lock, and a plain
|
|
153
|
+
// unlink by the slower one would then remove the fresh lock. So the stale file is first
|
|
154
|
+
// moved aside with rename (atomic: one contender wins, the other gets ENOENT and looks
|
|
155
|
+
// again) and its inode compared with the one observed. If the file moved is the stale one,
|
|
156
|
+
// it is deleted. If it is somebody's fresh lock, caught in the moment between their break
|
|
157
|
+
// and our rename, it is put back with link, which fails rather than overwrite a third
|
|
158
|
+
// contender's lock.
|
|
159
|
+
async function breakStaleLock(lockPath, observed) {
|
|
160
|
+
const aside = `${lockPath}.${process.pid}.${crypto.randomBytes(4).toString('hex')}`;
|
|
161
|
+
try {
|
|
162
|
+
await fs.rename(lockPath, aside);
|
|
163
|
+
} catch {
|
|
164
|
+
return; // already broken by someone else
|
|
165
|
+
}
|
|
166
|
+
const moved = await fs.stat(aside).catch(() => null);
|
|
167
|
+
if (!moved || moved.ino === observed.ino) {
|
|
168
|
+
await fs.remove(aside).catch(() => {});
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
await fs.link(aside, lockPath).catch(() => {});
|
|
172
|
+
await fs.remove(aside).catch(() => {});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function withLock(urlPath, fn, { staleMs = LOCK_STALE_MS, waitMs = LOCK_WAIT_MS } = {}) {
|
|
151
176
|
const lockPath = lockPathFor(urlPath);
|
|
152
177
|
await fs.ensureDir(path.dirname(lockPath));
|
|
153
178
|
const started = Date.now();
|
|
@@ -159,11 +184,11 @@ export async function withLock(urlPath, fn) {
|
|
|
159
184
|
} catch (err) {
|
|
160
185
|
if (err.code !== 'EEXIST') throw err;
|
|
161
186
|
const stat = await fs.stat(lockPath).catch(() => null);
|
|
162
|
-
if (stat && Date.now() - stat.mtimeMs >
|
|
163
|
-
await
|
|
187
|
+
if (stat && Date.now() - stat.mtimeMs > staleMs) {
|
|
188
|
+
await breakStaleLock(lockPath, stat);
|
|
164
189
|
continue;
|
|
165
190
|
}
|
|
166
|
-
if (Date.now() - started >
|
|
191
|
+
if (Date.now() - started > waitMs) {
|
|
167
192
|
const e = new Error('Resource is locked by another writer');
|
|
168
193
|
e.code = 'ELOCKED';
|
|
169
194
|
throw e;
|
|
@@ -171,9 +196,17 @@ export async function withLock(urlPath, fn) {
|
|
|
171
196
|
await new Promise((resolve) => setTimeout(resolve, 10 + Math.random() * 40));
|
|
172
197
|
}
|
|
173
198
|
}
|
|
199
|
+
// Heartbeat: keep the lock's mtime fresh while fn runs, so a long write is never
|
|
200
|
+
// mistaken for a dead holder and has its lock broken from under it.
|
|
201
|
+
const beat = setInterval(() => {
|
|
202
|
+
const now = new Date();
|
|
203
|
+
fs.utimes(lockPath, now, now).catch(() => {});
|
|
204
|
+
}, Math.max(20, Math.floor(staleMs / 3)));
|
|
205
|
+
beat.unref?.();
|
|
174
206
|
try {
|
|
175
207
|
return await fn();
|
|
176
208
|
} finally {
|
|
209
|
+
clearInterval(beat);
|
|
177
210
|
await fs.remove(lockPath).catch(() => {});
|
|
178
211
|
}
|
|
179
212
|
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dotfile policy, shared by the request guard (src/server.js) and the
|
|
3
|
+
* path lookup (src/utils/url.js) so the two can never disagree (#639).
|
|
4
|
+
*
|
|
5
|
+
* The guard used to inspect the raw request URL while the lookup
|
|
6
|
+
* percent-decoded it, so `%2Egit` passed the guard and resolved to
|
|
7
|
+
* `.git` on disk. Both sides now decode the same way, segment by
|
|
8
|
+
* segment, and both fail closed on anything they cannot decode.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// Solid-specific dotfiles that are part of the protocol surface.
|
|
12
|
+
export const ALLOWED_DOTFILES = ['.well-known', '.acl', '.meta', '.pods', '.notifications', '.account'];
|
|
13
|
+
|
|
14
|
+
export function isDotSegment(segment) {
|
|
15
|
+
return segment.startsWith('.') && segment.length > 1;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A leading dot has to be literal in the request. Decoding may never
|
|
20
|
+
* introduce one: `%2Egit` is refused, and so is `%2Eacl`, because the
|
|
21
|
+
* allow-list is for the literal Solid paths clients actually use.
|
|
22
|
+
*/
|
|
23
|
+
export function introducesDot(raw, decoded) {
|
|
24
|
+
return isDotSegment(decoded) && !raw.startsWith('.');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function isForbiddenDotSegment(raw, decoded) {
|
|
28
|
+
return isDotSegment(decoded) && (!raw.startsWith('.') || !ALLOWED_DOTFILES.includes(decoded));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function badRequest(message) {
|
|
32
|
+
const err = new Error(message);
|
|
33
|
+
err.statusCode = 400;
|
|
34
|
+
return err;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Split a URL path (no query string) into segments and percent-decode
|
|
39
|
+
* each one. Throws a 400 error on malformed percent-encoding and on an
|
|
40
|
+
* encoded path separator (`%2F`), since a segment that decodes into two
|
|
41
|
+
* segments cannot be checked as one.
|
|
42
|
+
* @param {string} urlPath
|
|
43
|
+
* @returns {Array<{ raw: string, decoded: string }>}
|
|
44
|
+
*/
|
|
45
|
+
export function decodeSegments(urlPath) {
|
|
46
|
+
return urlPath.split('/').map((raw) => {
|
|
47
|
+
let decoded;
|
|
48
|
+
try {
|
|
49
|
+
decoded = decodeURIComponent(raw);
|
|
50
|
+
} catch {
|
|
51
|
+
throw badRequest('Malformed percent-encoding in path');
|
|
52
|
+
}
|
|
53
|
+
if (decoded !== raw && (decoded.includes('/') || decoded.includes('\\'))) {
|
|
54
|
+
throw badRequest('Encoded path separator in path');
|
|
55
|
+
}
|
|
56
|
+
return { raw, decoded };
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The first decoded segment of the path that is a forbidden dotfile, or
|
|
62
|
+
* null when the path is clean. Throws a 400 error when the path cannot
|
|
63
|
+
* be decoded, so the caller fails closed.
|
|
64
|
+
* @param {string} urlPath
|
|
65
|
+
* @returns {string|null}
|
|
66
|
+
*/
|
|
67
|
+
export function findForbiddenDotfile(urlPath) {
|
|
68
|
+
const hit = decodeSegments(urlPath).find(({ raw, decoded }) => isForbiddenDotSegment(raw, decoded));
|
|
69
|
+
return hit ? hit.decoded : null;
|
|
70
|
+
}
|
package/src/utils/url.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import * as mime from 'mime-types';
|
|
3
|
+
import { decodeSegments, introducesDot } from './dotfiles.js';
|
|
3
4
|
|
|
4
5
|
// Base directory for storing all pods
|
|
5
6
|
// Use a getter function to read env var at runtime (not import time)
|
|
@@ -16,6 +17,43 @@ export function updateDataRoot() {
|
|
|
16
17
|
DATA_ROOT = getDataRoot();
|
|
17
18
|
}
|
|
18
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Decode and sanitise a URL path into a relative filesystem path.
|
|
22
|
+
*
|
|
23
|
+
* - Leading slashes are stripped (#131 — `//foo` from bot probes would
|
|
24
|
+
* otherwise leave `/foo`, and path.resolve(root, '/foo') would treat the
|
|
25
|
+
* second arg as absolute, escape dataRoot, and trip the traversal guard
|
|
26
|
+
* with a 500 instead of resolving cleanly to a 404).
|
|
27
|
+
* - Each segment is percent-decoded on its own; malformed encoding and
|
|
28
|
+
* encoded separators are rejected with a 400 error (see dotfiles.js).
|
|
29
|
+
* - `..` is removed from every segment (multiple passes for the `....//`
|
|
30
|
+
* bypass).
|
|
31
|
+
* - A segment that only became a dotfile by decoding (`%2Egit`, `%2Eacl`)
|
|
32
|
+
* is rejected with a 403 error, so this lookup can never reach a dotfile
|
|
33
|
+
* that the request guard in server.js would have refused (#639).
|
|
34
|
+
* Internal callers pass dotfile names literally (`.acl`, `.quota.json`,
|
|
35
|
+
* lock files) and are unaffected: their raw and decoded forms agree.
|
|
36
|
+
* @param {string} urlPath
|
|
37
|
+
* @returns {string} relative path, '/'-joined
|
|
38
|
+
*/
|
|
39
|
+
function normalizeUrlPath(urlPath) {
|
|
40
|
+
const stripped = urlPath.replace(/^\/+/, '');
|
|
41
|
+
return decodeSegments(stripped).map(({ raw, decoded }) => {
|
|
42
|
+
let segment = decoded;
|
|
43
|
+
let previous;
|
|
44
|
+
do {
|
|
45
|
+
previous = segment;
|
|
46
|
+
segment = segment.replace(/\.\./g, '');
|
|
47
|
+
} while (segment !== previous);
|
|
48
|
+
if (introducesDot(raw, segment)) {
|
|
49
|
+
const err = new Error('Dotfile access is not allowed');
|
|
50
|
+
err.statusCode = 403;
|
|
51
|
+
throw err;
|
|
52
|
+
}
|
|
53
|
+
return segment;
|
|
54
|
+
}).join('/');
|
|
55
|
+
}
|
|
56
|
+
|
|
19
57
|
/**
|
|
20
58
|
* Convert URL path to filesystem path
|
|
21
59
|
* @param {string} urlPath - The URL path (e.g., /alice/profile/)
|
|
@@ -23,19 +61,7 @@ export function updateDataRoot() {
|
|
|
23
61
|
* @throws {Error} - If path traversal is detected
|
|
24
62
|
*/
|
|
25
63
|
export function urlToPath(urlPath) {
|
|
26
|
-
|
|
27
|
-
// would otherwise leave `/foo`, and path.resolve(root, '/foo') would
|
|
28
|
-
// treat the second arg as absolute, escape dataRoot, and trip the
|
|
29
|
-
// traversal guard with a 500 instead of resolving cleanly to a 404).
|
|
30
|
-
let normalized = urlPath.replace(/^\/+/, '');
|
|
31
|
-
normalized = decodeURIComponent(normalized);
|
|
32
|
-
|
|
33
|
-
// Security: remove path traversal attempts (multiple passes for ....// bypass)
|
|
34
|
-
let previous;
|
|
35
|
-
do {
|
|
36
|
-
previous = normalized;
|
|
37
|
-
normalized = normalized.replace(/\.\./g, '');
|
|
38
|
-
} while (normalized !== previous);
|
|
64
|
+
const normalized = normalizeUrlPath(urlPath);
|
|
39
65
|
|
|
40
66
|
// Resolve to absolute path and verify it's within DATA_ROOT
|
|
41
67
|
const dataRoot = path.resolve(getDataRoot());
|
|
@@ -58,16 +84,7 @@ export function urlToPath(urlPath) {
|
|
|
58
84
|
* @throws {Error} - If path traversal is detected
|
|
59
85
|
*/
|
|
60
86
|
export function urlToPathWithPod(urlPath, podName) {
|
|
61
|
-
|
|
62
|
-
let normalized = urlPath.replace(/^\/+/, '');
|
|
63
|
-
normalized = decodeURIComponent(normalized);
|
|
64
|
-
|
|
65
|
-
// Security: remove path traversal attempts (multiple passes for ....// bypass)
|
|
66
|
-
let previous;
|
|
67
|
-
do {
|
|
68
|
-
previous = normalized;
|
|
69
|
-
normalized = normalized.replace(/\.\./g, '');
|
|
70
|
-
} while (normalized !== previous);
|
|
87
|
+
const normalized = normalizeUrlPath(urlPath);
|
|
71
88
|
|
|
72
89
|
// Also sanitize podName (multiple passes for ....// bypass)
|
|
73
90
|
let safePodName = podName;
|
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,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
|
+
});
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nostr relay NIP-11 tests
|
|
3
|
+
*
|
|
4
|
+
* NIP-11 says the relay information document is served at the relay's own
|
|
5
|
+
* URL when the client sends `Accept: application/nostr+json`. Tools such as
|
|
6
|
+
* `nak relay` only ask there, so serving it at <path>/info alone made the
|
|
7
|
+
* relay look non-compliant. The WebSocket relay on the same path must keep
|
|
8
|
+
* working.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, it, before, after } from 'node:test';
|
|
12
|
+
import assert from 'node:assert';
|
|
13
|
+
import { mkdtempSync } from 'node:fs';
|
|
14
|
+
import { tmpdir } from 'node:os';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import { WebSocket } from 'ws';
|
|
17
|
+
import { createServer } from '../src/server.js';
|
|
18
|
+
|
|
19
|
+
describe('Nostr relay NIP-11', () => {
|
|
20
|
+
let fastify;
|
|
21
|
+
let base;
|
|
22
|
+
|
|
23
|
+
before(async () => {
|
|
24
|
+
const root = mkdtempSync(join(tmpdir(), 'jss-nip11-'));
|
|
25
|
+
fastify = createServer({ root, port: 0, logger: false, nostr: true });
|
|
26
|
+
await fastify.listen({ port: 0, host: '127.0.0.1' });
|
|
27
|
+
base = `127.0.0.1:${fastify.server.address().port}`;
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
after(async () => {
|
|
31
|
+
await fastify.close();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('serves the info document at the relay URL for Accept: application/nostr+json', async () => {
|
|
35
|
+
const res = await fetch(`http://${base}/relay`, { headers: { Accept: 'application/nostr+json' } });
|
|
36
|
+
assert.strictEqual(res.status, 200);
|
|
37
|
+
assert.match(res.headers.get('content-type'), /application\/nostr\+json/);
|
|
38
|
+
assert.strictEqual(res.headers.get('access-control-allow-origin'), '*');
|
|
39
|
+
const info = await res.json();
|
|
40
|
+
assert.ok(info.supported_nips.includes(11));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('still returns 404 for a plain GET of the relay URL', async () => {
|
|
44
|
+
const res = await fetch(`http://${base}/relay`);
|
|
45
|
+
assert.strictEqual(res.status, 404);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('keeps serving the document at /relay/info', async () => {
|
|
49
|
+
const res = await fetch(`http://${base}/relay/info`);
|
|
50
|
+
assert.strictEqual(res.status, 200);
|
|
51
|
+
const info = await res.json();
|
|
52
|
+
assert.ok(info.supported_nips.includes(11));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('still accepts WebSocket subscriptions on the relay URL', async () => {
|
|
56
|
+
const ws = new WebSocket(`ws://${base}/relay`);
|
|
57
|
+
const msg = await new Promise((resolve, reject) => {
|
|
58
|
+
ws.on('open', () => ws.send(JSON.stringify(['REQ', 'sub1', { kinds: [1], limit: 1 }])));
|
|
59
|
+
ws.on('message', (data) => resolve(JSON.parse(data.toString())));
|
|
60
|
+
ws.on('error', reject);
|
|
61
|
+
});
|
|
62
|
+
ws.close();
|
|
63
|
+
assert.deepStrictEqual(msg, ['EOSE', 'sub1']);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
@@ -175,6 +175,17 @@ describe('api.reservePath (#602)', () => {
|
|
|
175
175
|
assert.ok([401, 403].includes(put.status), `expected WAC rejection, got ${put.status}`);
|
|
176
176
|
});
|
|
177
177
|
|
|
178
|
+
// #611 residual after #607: a widened method at a path the shim did NOT
|
|
179
|
+
// register must not fall through to the WAC-skipped LDP wildcard.
|
|
180
|
+
it('a widened method at an unhandled reserved path is 404, not a storage write', async () => {
|
|
181
|
+
await start([{ id: 'writer', module: path.join(FIXTURE_DIR, 'write-shim.js'), prefix: '/w' }]);
|
|
182
|
+
await fs.ensureDir(path.join(TEST_DATA_DIR, 'api'));
|
|
183
|
+
const res = await fetch(`${baseUrl}/api/unhandled`, { method: 'POST', headers: { 'Content-Type': 'text/plain' }, body: 'planted' });
|
|
184
|
+
assert.strictEqual(res.status, 404);
|
|
185
|
+
const planted = await fs.readdir(path.join(TEST_DATA_DIR, 'api'));
|
|
186
|
+
assert.deepStrictEqual(planted, [], `nothing may be written: ${planted.join(', ')}`);
|
|
187
|
+
});
|
|
188
|
+
|
|
178
189
|
it('WAC still guards unreserved paths', async () => {
|
|
179
190
|
await start(MAIN());
|
|
180
191
|
const res = await fetch(`${baseUrl}/somepod/private/x`, { method: 'PUT', body: 'data' });
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* storage.withLock: a slow holder keeps its lock, a dead holder loses it (#600)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, it, before, after } from 'node:test';
|
|
6
|
+
import assert from 'node:assert';
|
|
7
|
+
import fs from 'fs-extra';
|
|
8
|
+
import os from 'os';
|
|
9
|
+
import path from 'path';
|
|
10
|
+
import { withLock, lockPathFor } from '../src/storage/filesystem.js';
|
|
11
|
+
|
|
12
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
13
|
+
let ROOT, originalDataRoot;
|
|
14
|
+
|
|
15
|
+
describe('withLock', () => {
|
|
16
|
+
before(async () => {
|
|
17
|
+
originalDataRoot = process.env.DATA_ROOT;
|
|
18
|
+
ROOT = await fs.mkdtemp(path.join(os.tmpdir(), 'jss-lock-'));
|
|
19
|
+
process.env.DATA_ROOT = ROOT;
|
|
20
|
+
await fs.ensureDir(path.join(ROOT, 'pod'));
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
after(async () => {
|
|
24
|
+
if (originalDataRoot === undefined) delete process.env.DATA_ROOT; else process.env.DATA_ROOT = originalDataRoot;
|
|
25
|
+
await fs.remove(ROOT);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('a holder slower than the stale timeout keeps the lock: the second writer waits for it', async () => {
|
|
29
|
+
const log = [];
|
|
30
|
+
const opts = { staleMs: 100, waitMs: 5000 };
|
|
31
|
+
const a = withLock('/pod/slow.json', async () => { await sleep(400); log.push('A done'); }, opts);
|
|
32
|
+
await sleep(20);
|
|
33
|
+
const b = withLock('/pod/slow.json', async () => { log.push('B start'); }, opts);
|
|
34
|
+
await Promise.all([a, b]);
|
|
35
|
+
assert.deepStrictEqual(log, ['A done', 'B start']);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('a lock left by a dead holder is broken after the stale timeout', async () => {
|
|
39
|
+
const lockPath = lockPathFor('/pod/dead.json');
|
|
40
|
+
await fs.writeFile(lockPath, '');
|
|
41
|
+
const old = new Date(Date.now() - 60_000);
|
|
42
|
+
await fs.utimes(lockPath, old, old);
|
|
43
|
+
const started = Date.now();
|
|
44
|
+
let ran = false;
|
|
45
|
+
await withLock('/pod/dead.json', async () => { ran = true; }, { staleMs: 100, waitMs: 5000 });
|
|
46
|
+
assert.ok(ran, 'fn ran');
|
|
47
|
+
assert.ok(Date.now() - started < 1000, 'acquired without waiting out the timeout');
|
|
48
|
+
assert.ok(!await fs.pathExists(lockPath), 'lock removed afterwards');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('many contenders on a dead lock: all run, never two at once, no lock left behind', async () => {
|
|
52
|
+
const lockPath = lockPathFor('/pod/crowd.json');
|
|
53
|
+
await fs.writeFile(lockPath, '');
|
|
54
|
+
const old = new Date(Date.now() - 60_000);
|
|
55
|
+
await fs.utimes(lockPath, old, old);
|
|
56
|
+
let inside = 0, peak = 0, ran = 0;
|
|
57
|
+
const opts = { staleMs: 100, waitMs: 5000 };
|
|
58
|
+
await Promise.all(Array.from({ length: 8 }, () => withLock('/pod/crowd.json', async () => {
|
|
59
|
+
inside++; peak = Math.max(peak, inside); ran++;
|
|
60
|
+
await sleep(30);
|
|
61
|
+
inside--;
|
|
62
|
+
}, opts)));
|
|
63
|
+
assert.strictEqual(ran, 8, 'every contender ran');
|
|
64
|
+
assert.strictEqual(peak, 1, 'never two holders at once');
|
|
65
|
+
assert.ok(!await fs.pathExists(lockPath), 'lock removed afterwards');
|
|
66
|
+
const leftovers = (await fs.readdir(path.join(ROOT, 'pod'))).filter((n) => n.startsWith('.crowd.json.lock'));
|
|
67
|
+
assert.deepStrictEqual(leftovers, [], 'no moved-aside lock files left');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('a live lock that is never released ends in ELOCKED after waitMs', async () => {
|
|
71
|
+
const lockPath = lockPathFor('/pod/held.json');
|
|
72
|
+
await fs.writeFile(lockPath, '');
|
|
73
|
+
const keep = setInterval(() => { const n = new Date(); fs.utimes(lockPath, n, n).catch(() => {}); }, 20);
|
|
74
|
+
try {
|
|
75
|
+
await assert.rejects(withLock('/pod/held.json', async () => {}, { staleMs: 100, waitMs: 300 }), (e) => e.code === 'ELOCKED');
|
|
76
|
+
} finally {
|
|
77
|
+
clearInterval(keep);
|
|
78
|
+
await fs.remove(lockPath);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|