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.
@@ -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
- // Check if resource exists and get current ETag
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: queued within this process, and under a lock file on disk across processes
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
- const previous = patchQueues.get(storagePath) ?? Promise.resolve();
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/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
 
@@ -333,6 +334,8 @@ export function createServer(options = {}) {
333
334
  fastify.decorateRequest('connegEnabled', null);
334
335
  fastify.decorateRequest('notificationsEnabled', null);
335
336
  fastify.decorateRequest('idpEnabled', null);
337
+ // read by the NIP-98 verifier: the lenient git rules apply only while git is on (#643)
338
+ fastify.decorate('gitEnabled', gitEnabled);
336
339
  fastify.decorateRequest('subdomainsEnabled', null);
337
340
  fastify.decorateRequest('baseDomain', null);
338
341
  fastify.decorateRequest('podName', null);
@@ -571,7 +574,6 @@ export function createServer(options = {}) {
571
574
  // Security: Block access to dotfiles except allowed Solid-specific ones
572
575
  // This prevents exposure of .git/, .env, .htpasswd, etc.
573
576
  // Git protocol requests bypass this check when git is enabled
574
- const ALLOWED_DOTFILES = ['.well-known', '.acl', '.meta', '.pods', '.notifications', '.account'];
575
577
  fastify.addHook('onRequest', async (request, reply) => {
576
578
  // Allow git protocol requests through when git is enabled
577
579
  if (gitEnabled && isGitRequest(request.url)) {
@@ -609,14 +611,18 @@ export function createServer(options = {}) {
609
611
  // legitimate proxy requests for upstream URLs that happen to
610
612
  // contain dotfile-like path segments. The dotfile guard is about
611
613
  // *this* pod's filesystem, not what the URL looks like.
612
- const segments = request.url.split('?')[0].split('/');
613
- const hasForbiddenDotfile = segments.some(seg =>
614
- seg.startsWith('.') &&
615
- seg.length > 1 &&
616
- !ALLOWED_DOTFILES.includes(seg)
617
- );
618
-
619
- if (hasForbiddenDotfile) {
614
+ //
615
+ // Segments are percent-decoded before the check, the same way the
616
+ // storage lookup decodes them (#639): `%2Egit` is `.git` on disk.
617
+ // A path that cannot be decoded is refused rather than passed on.
618
+ let forbidden;
619
+ try {
620
+ forbidden = findForbiddenDotfile(urlNoQuery);
621
+ } catch (err) {
622
+ return reply.code(400).send({ error: 'Bad Request', message: err.message });
623
+ }
624
+
625
+ if (forbidden) {
620
626
  return reply.code(403).send({ error: 'Forbidden', message: 'Dotfile access is not allowed' });
621
627
  }
622
628
  });
@@ -780,6 +786,19 @@ export function createServer(options = {}) {
780
786
  const accept = request.headers.accept || '';
781
787
  const wantsAP = accept.includes('activity+json') || accept.includes('ld+json; profile="https://www.w3.org/ns/activitystreams"');
782
788
  const isProfileAP = activitypubEnabled && wantsAP && (request.url === '/profile/card.jsonld' || request.url.startsWith('/profile/card.jsonld?'));
789
+ // An app-owned subtree (appPaths from createServer or a plugin prefix,
790
+ // #582; reservePath claims, #602) skips WAC because the app owns auth
791
+ // there. The LDP wildcards ('/*') sit beneath every app route, so a
792
+ // path the app did NOT register would otherwise fall through to pod
793
+ // storage with WAC already skipped: an unauthenticated read or write
794
+ // (#611). Mark the request; the wildcard handlers answer 404 for it,
795
+ // and OPTIONS answers a bare 204 without a storage lookup. This check
796
+ // runs before the generic OPTIONS skip below so preflights are marked.
797
+ if (appPaths.some(p => request.url === p || request.url.startsWith(p + '/') || request.url.startsWith(p + '?')) ||
798
+ appPathPatterns.some(m => m.methods.has(request.method) && m.re.test(request.url))) {
799
+ request.appPathExempt = true;
800
+ return;
801
+ }
783
802
  if (request.url === '/.pods' ||
784
803
  request.url === '/.notifications' ||
785
804
  request.method === 'OPTIONS' ||
@@ -799,8 +818,6 @@ export function createServer(options = {}) {
799
818
  (webrtcEnabled && (request.url === webrtcPath || request.url.startsWith(webrtcPath + '?'))) ||
800
819
  (terminalEnabled && (request.url === '/.terminal' || request.url.startsWith('/.terminal?'))) ||
801
820
  (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
821
  mashlibPaths.some(p => request.url === p || request.url.startsWith(p + '.'))) {
805
822
  return;
806
823
  }
@@ -965,15 +982,34 @@ export function createServer(options = {}) {
965
982
 
966
983
  // LDP routes - using wildcard routing
967
984
  // Read operations - no rate limit (handled by bodyLimit)
968
- fastify.get('/*', handleGet);
969
- fastify.head('/*', handleHead);
970
- fastify.options('/*', handleOptions);
985
+ // The LDP wildcards never serve an app-owned subtree (#611): a request
986
+ // the WAC hook exempted as app-owned (request.appPathExempt) reaches
987
+ // these only because the app registered no route for it, so it is a 404
988
+ // inside the app's namespace, not a storage read or write.
989
+ const notAppRoute = async (request, reply) => {
990
+ if (!request.appPathExempt) return;
991
+ reply.code(404).send({ error: 'Not Found', message: 'no route in application namespace' });
992
+ return reply;
993
+ };
994
+ fastify.get('/*', { preHandler: notAppRoute }, handleGet);
995
+ fastify.head('/*', { preHandler: notAppRoute }, handleHead);
996
+ // A preflight under an app path must keep answering 204 (the CORS
997
+ // headers are already set by the onRequest hook) or browsers cannot
998
+ // call an app route that registered no OPTIONS handler. But it must not
999
+ // run handleOptions, whose storage.stat and Link headers would disclose
1000
+ // whether something exists in pod storage below the prefix.
1001
+ const appOptions = async (request, reply) => {
1002
+ if (!request.appPathExempt) return;
1003
+ reply.code(204).send();
1004
+ return reply;
1005
+ };
1006
+ fastify.options('/*', { preHandler: appOptions }, handleOptions);
971
1007
 
972
1008
  // Write operations - rate limited
973
- fastify.put('/*', writeRateLimit, handlePut);
974
- fastify.delete('/*', writeRateLimit, handleDelete);
975
- fastify.post('/*', writeRateLimit, handlePost);
976
- fastify.patch('/*', writeRateLimit, handlePatch);
1009
+ fastify.put('/*', { ...writeRateLimit, preHandler: notAppRoute }, handlePut);
1010
+ fastify.delete('/*', { ...writeRateLimit, preHandler: notAppRoute }, handleDelete);
1011
+ fastify.post('/*', { ...writeRateLimit, preHandler: notAppRoute }, handlePost);
1012
+ fastify.patch('/*', { ...writeRateLimit, preHandler: notAppRoute }, handlePatch);
977
1013
 
978
1014
  // Root route
979
1015
  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. A lock older than LOCK_STALE_MS belongs to a crashed process and
141
- // is broken.
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
- export async function withLock(urlPath, fn) {
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 > LOCK_STALE_MS) {
163
- await fs.remove(lockPath).catch(() => {});
187
+ if (stat && Date.now() - stat.mtimeMs > staleMs) {
188
+ await breakStaleLock(lockPath, stat);
164
189
  continue;
165
190
  }
166
- if (Date.now() - started > LOCK_WAIT_MS) {
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,89 @@
1
+ /**
2
+ * Read a fetch Response body under a byte cap AND an abort signal.
3
+ *
4
+ * Pass the same AbortSignal that was handed to `fetch()` and keep its
5
+ * timer alive until this resolves. That is what bounds a server that
6
+ * sends headers promptly and then trickles the body (#641): a timeout
7
+ * that is cleared as soon as `fetch()` resolves only covers the
8
+ * connection and headers, and the body read would otherwise wait for
9
+ * as long as the remote chooses to keep sending.
10
+ *
11
+ * On abort the reader is cancelled (which releases the upstream
12
+ * socket) and the read rejects with a timeout error, never with a
13
+ * partial body.
14
+ *
15
+ * @param {Response} res
16
+ * @param {object} opts
17
+ * @param {number} opts.maxBytes - reject once more than this many bytes arrive
18
+ * @param {AbortSignal} [opts.signal] - the fetch's signal; abort ends the read
19
+ * @param {string} [opts.label='response'] - noun used in error messages
20
+ * @returns {Promise<string>} body as UTF-8 text
21
+ */
22
+ export async function readBodyWithCap(res, { maxBytes, signal, label = 'response' }) {
23
+ const tooLarge = () => new Error(`${label} too large (>${maxBytes} bytes)`);
24
+ const timedOut = () => new Error(`${label} body read timed out`);
25
+
26
+ if (signal?.aborted) throw timedOut();
27
+
28
+ const reader = res.body?.getReader?.();
29
+ if (!reader) {
30
+ // Non-streaming Response (polyfills, hand-built stubs): race the
31
+ // text read against the signal so an abort still ends the wait.
32
+ const text = await raceAbort(res.text(), signal, timedOut);
33
+ if (Buffer.byteLength(text, 'utf8') > maxBytes) throw tooLarge();
34
+ return text;
35
+ }
36
+
37
+ // A real fetch body errors on abort by itself; cancelling here also
38
+ // covers stream implementations that do not watch the signal.
39
+ const onAbort = () => { reader.cancel().catch(() => { /* noop */ }); };
40
+ signal?.addEventListener('abort', onAbort, { once: true });
41
+ try {
42
+ const chunks = [];
43
+ let total = 0;
44
+ for (;;) {
45
+ let result;
46
+ try {
47
+ result = await reader.read();
48
+ } catch (err) {
49
+ if (signal?.aborted) throw timedOut();
50
+ throw err;
51
+ }
52
+ // Check abort before honouring `done`: a cancelled reader resolves
53
+ // `{ done: true }`, which must not pass off a partial body as complete.
54
+ if (signal?.aborted) throw timedOut();
55
+ const { value, done } = result;
56
+ if (done) break;
57
+ total += value.byteLength;
58
+ if (total > maxBytes) {
59
+ try { await reader.cancel(); } catch { /* noop */ }
60
+ throw tooLarge();
61
+ }
62
+ chunks.push(value);
63
+ }
64
+ return Buffer.concat(chunks.map((c) => Buffer.from(c.buffer, c.byteOffset, c.byteLength))).toString('utf8');
65
+ } finally {
66
+ signal?.removeEventListener('abort', onAbort);
67
+ }
68
+ }
69
+
70
+ function raceAbort(promise, signal, makeError) {
71
+ if (!signal) return promise;
72
+ return new Promise((resolve, reject) => {
73
+ const onAbort = () => reject(makeError());
74
+ signal.addEventListener('abort', onAbort, { once: true });
75
+ promise.then(resolve, reject).finally(() => signal.removeEventListener('abort', onAbort));
76
+ });
77
+ }
78
+
79
+ /**
80
+ * Release a Response whose body will not be read (redirect hop, error
81
+ * status, unexpected content-type, oversize, abort). Without this the
82
+ * upstream socket stays busy until the Response is garbage-collected,
83
+ * which on a trickling host means well past the deadline. Fire and
84
+ * forget: cancel() on undici tears the connection down at once.
85
+ */
86
+ export function discardBody(res) {
87
+ if (!res?.body || res.bodyUsed) return;
88
+ try { res.body.cancel().catch(() => { /* locked or already closed */ }); } catch { /* noop */ }
89
+ }
@@ -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
- // Normalize: strip all leading slashes (#131 — `//foo` from bot probes
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
- // Normalize: strip all leading slashes (#131 — see urlToPath for context).
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;