hfs 3.1.0-alpha3.1 → 3.1.0-beta4

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/src/webdav.js CHANGED
@@ -23,7 +23,7 @@ const lodash_1 = __importDefault(require("lodash"));
23
23
  const forceWebdavLogin = (0, config_1.defineConfig)(cross_1.CFG.force_webdav_login, true, compileWebdavAgentRegex);
24
24
  const webdavInitialAuth = (0, config_1.defineConfig)(cross_1.CFG.webdav_initial_auth, 'WebDAVFS', compileWebdavAgentRegex);
25
25
  const webdavPrompted = (0, expiringCache_1.expiringCache)(cross_1.DAY);
26
- const webdavDetectedAgents = new Set();
26
+ const webdavDetectedAgents = (0, expiringCache_1.expiringCache)(cross_1.DAY);
27
27
  const TOKEN_HEADER = 'lock-token';
28
28
  const WEBDAV_METHODS = new Set(['PROPFIND', 'MKCOL', 'MOVE', 'LOCK', 'UNLOCK']);
29
29
  const WEBDAV_HINT_HEADERS = ['depth', 'destination', 'overwrite', 'translate', 'if', TOKEN_HEADER, 'x-expected-entity-length'];
@@ -50,17 +50,16 @@ function hasToken(header, token) {
50
50
  return header.includes(`<${token}>`) || header.split(/[,;\s]+/).includes(token);
51
51
  }
52
52
  async function handledWebdav(ctx) {
53
- const { path } = ctx;
54
- const isWebdavAuthRequest = WEBDAV_METHODS.has(ctx.method) || WEBDAV_HINT_HEADERS.some(h => !!ctx.get(h));
53
+ let { path } = ctx;
54
+ path = path.replace(/^\/+/, '/'); // double-slash is causing empty listing in filezilla-pro
55
55
  const ua = ctx.get('user-agent');
56
- if (isWebdavAuthRequest && (0, auth_1.getCurrentUsername)(ctx)) {
57
- if (ua)
58
- webdavDetectedAgents.add(ua);
59
- }
60
- if (ctx.path.includes('/._') && ua?.startsWith('WebDAVFS')) { // too much spam from Finder for these files that can contain metas
56
+ if (path.includes('/._') && ua?.startsWith('WebDAVFS')) { // too much spam from Finder for these files that can contain metas
61
57
  ctx.state.dontLog = true;
62
58
  return ctx.status = cross_1.HTTP_FORBIDDEN;
63
59
  }
60
+ const isWebdavAuthRequest = WEBDAV_METHODS.has(ctx.method) || WEBDAV_HINT_HEADERS.some(h => ctx.get(h));
61
+ if (isWebdavAuthRequest && ua && (0, auth_1.getCurrentUsername)(ctx))
62
+ webdavDetectedAgents.try(webdavAgentKey(ctx, ua), () => true);
64
63
  if (ctx.method === 'OPTIONS') {
65
64
  if (ctx.get('Access-Control-Request-Method'))
66
65
  return; // it's a preflight cors request, not webdav
@@ -87,7 +86,7 @@ async function handledWebdav(ctx) {
87
86
  }
88
87
  if (x && ctx.length === undefined) // missing length can make PUT fail
89
88
  ctx.req.headers['content-length'] = x;
90
- if (KNOWN_UA.test(ua) || webdavDetectedAgents.has(ua))
89
+ if (KNOWN_UA.test(ua) || webdavDetectedAgents.has(webdavAgentKey(ctx, ua)))
91
90
  ctx.query.existing ??= 'overwrite'; // with webdav this is our default
92
91
  return; // default handling
93
92
  }
@@ -166,23 +165,27 @@ async function handledWebdav(ctx) {
166
165
  }
167
166
  if (ctx.method === 'LOCK') {
168
167
  setWebdavHeaders();
169
- const lockBody = await readRequestBody(ctx);
168
+ const body = ctx.length || ctx.get('content-length') || ctx.get('transfer-encoding') ? await (0, consumers_1.text)(ctx.req) : '';
170
169
  const token = getProvidedLockToken(ctx);
171
170
  let seconds = Number(ctx.get('timeout').split(',').find(x => /^Second-\d+$/i.test(x.trim()))?.trim().split('-', 2)[1]);
172
171
  seconds = lodash_1.default.clamp(seconds || LOCK_DEFAULT_SECONDS, 1, LOCK_MAX_SECONDS);
173
- if (!lockBody) {
172
+ if (!body) {
174
173
  // Finder and similar clients refresh an existing lock by sending LOCK without a body
175
174
  if (!token)
176
175
  return ctx.status = cross_1.HTTP_BAD_REQUEST;
177
176
  const lock = locks.get(path);
178
177
  if (token !== lock?.token)
179
178
  return ctx.status = cross_1.HTTP_PRECONDITION_FAILED;
180
- refreshLock(path, lock, seconds);
179
+ // refresh lock – keep the same token on refresh so clients can continue using the lock they already hold
180
+ clearTimeout(lock.timeout);
181
+ lock.timeout = setTimeout(() => locks.delete(path), seconds * 1000);
182
+ lock.seconds = seconds;
183
+ locks.set(path, lock);
181
184
  ctx.set(TOKEN_HEADER, lock.token);
182
- ctx.body = renderLockResponse(path, lock.token, lock.seconds);
185
+ ctx.body = renderLockResponse(lock.token, lock.seconds);
183
186
  return true;
184
187
  }
185
- const lockinfo = (0, cross_1.try_)(() => xmlParser.parse(lockBody).lockinfo);
188
+ const lockinfo = (0, cross_1.try_)(() => xmlParser.parse(body).lockinfo);
186
189
  const scope = lodash_1.default.keys(lockinfo?.lockscope)[0];
187
190
  const type = lodash_1.default.keys(lockinfo?.locktype)[0];
188
191
  if (!scope || !type)
@@ -197,7 +200,7 @@ async function handledWebdav(ctx) {
197
200
  const timeout = setTimeout(() => locks.delete(path), seconds * 1000);
198
201
  locks.set(path, { token: newToken, timeout, seconds });
199
202
  ctx.set(TOKEN_HEADER, newToken);
200
- ctx.body = renderLockResponse(path, newToken, seconds);
203
+ ctx.body = renderLockResponse(newToken, seconds);
201
204
  return true;
202
205
  function getProvidedLockToken(ctx) {
203
206
  const direct = ctx.get(TOKEN_HEADER).replace(/[<>]/g, '');
@@ -206,22 +209,15 @@ async function handledWebdav(ctx) {
206
209
  const ifHeader = ctx.get('If');
207
210
  return /<([^>]+)>/.exec(ifHeader)?.[1] || '';
208
211
  }
209
- function refreshLock(path, lock, seconds) {
210
- clearTimeout(lock.timeout);
211
- // keep the same token on refresh so clients can continue using the lock they already hold
212
- lock.timeout = setTimeout(() => locks.delete(path), seconds * 1000);
213
- lock.seconds = seconds;
214
- locks.set(path, lock);
215
- }
216
- function renderLockResponse(path, token, seconds) {
212
+ function renderLockResponse(token, seconds) {
217
213
  return `<?xml version="1.0" encoding="utf-8"?><prop xmlns="DAV:"><lockdiscovery><activelock>
218
- <locktype><write/></locktype>
219
- <lockscope><exclusive/></lockscope>
220
- <locktoken><href>${token}</href></locktoken>
221
- <lockroot><href>${path}</href></lockroot>
222
- <depth>0</depth>
223
- <timeout>Second-${seconds}</timeout>
224
- </activelock></lockdiscovery></prop>`;
214
+ <locktype><write/></locktype>
215
+ <lockscope><exclusive/></lockscope>
216
+ <locktoken><href>${token}</href></locktoken>
217
+ <lockroot><href>${path}</href></lockroot>
218
+ <depth>0</depth>
219
+ <timeout>Second-${seconds}</timeout>
220
+ </activelock></lockdiscovery></prop>`;
225
221
  }
226
222
  }
227
223
  if (ctx.method === 'PROPFIND') {
@@ -239,7 +235,7 @@ async function handledWebdav(ctx) {
239
235
  }
240
236
  ctx.type = 'xml';
241
237
  ctx.status = 207;
242
- const pathSlash = (0, cross_1.enforceFinal)('/', path);
238
+ const outPath = (0, cross_1.enforceFinal)('/', path.slice(Math.max(0, (ctx.state.root?.length ?? 0) - 1)), true);
243
239
  const res = ctx.body = new stream_1.PassThrough({ encoding: 'utf8' });
244
240
  res.write(`<?xml version="1.0" encoding="utf-8" ?><multistatus xmlns="DAV:">`);
245
241
  await sendEntry(node);
@@ -258,7 +254,7 @@ async function handledWebdav(ctx) {
258
254
  const isDir = await (0, vfs_1.nodeIsFolder)(node);
259
255
  const st = await (0, vfs_1.nodeStats)(node);
260
256
  res.write(`<response>
261
- <href>${pathSlash + (append ? (0, cross_1.pathEncode)(name, true) + (isDir ? '/' : '') : '')}</href>
257
+ <href>${outPath + (append ? (0, cross_1.pathEncode)(name, true) + (isDir ? '/' : '') : '')}</href>
262
258
  <propstat>
263
259
  <status>HTTP/1.1 200 OK</status>
264
260
  <prop>
@@ -305,12 +301,13 @@ async function handledWebdav(ctx) {
305
301
  }
306
302
  }
307
303
  }
308
- async function readRequestBody(ctx) {
309
- return ctx.length || ctx.get('content-length') || ctx.get('transfer-encoding') ? String(await (0, consumers_1.text)(ctx.req)).trim() : '';
310
- }
311
304
  function compileWebdavAgentRegex(v) {
312
305
  return !v ? null : v === true ? /.*/ : new RegExp(v.trim(), 'i');
313
306
  }
307
+ function webdavAgentKey(ctx, ua) {
308
+ // tying detection to source IP avoids promoting one spoofed UA to global WebDAV behavior
309
+ return `${ctx.ip}|${ua}`;
310
+ }
314
311
  // Finder will upload special attributes as files with name ._* that can be merged using system utility "dot_clean"
315
312
  const cleaners = {};
316
313
  function dotClean(path) {