@c9up/archive 0.1.13 → 0.1.15

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.
Files changed (41) hide show
  1. package/dist/ArchiveProvider.d.ts +1 -0
  2. package/dist/ArchiveProvider.d.ts.map +1 -1
  3. package/dist/ArchiveProvider.js +53 -17
  4. package/dist/ArchiveProvider.js.map +1 -1
  5. package/dist/DriveManager.js +2 -2
  6. package/dist/DriveManager.js.map +1 -1
  7. package/dist/GcsDriver.js +5 -5
  8. package/dist/GcsDriver.js.map +1 -1
  9. package/dist/S3Driver.js +6 -6
  10. package/dist/S3Driver.js.map +1 -1
  11. package/dist/StaticMiddleware.d.ts.map +1 -1
  12. package/dist/StaticMiddleware.js +31 -11
  13. package/dist/StaticMiddleware.js.map +1 -1
  14. package/dist/StorageManager.d.ts +16 -8
  15. package/dist/StorageManager.d.ts.map +1 -1
  16. package/dist/StorageManager.js +176 -115
  17. package/dist/StorageManager.js.map +1 -1
  18. package/dist/etag.d.ts +32 -0
  19. package/dist/etag.d.ts.map +1 -0
  20. package/dist/etag.js +41 -0
  21. package/dist/etag.js.map +1 -0
  22. package/dist/expiry.d.ts +2 -2
  23. package/dist/expiry.js +6 -6
  24. package/dist/expiry.js.map +1 -1
  25. package/dist/services/main.d.ts +11 -0
  26. package/dist/services/main.d.ts.map +1 -1
  27. package/dist/services/main.js +21 -0
  28. package/dist/services/main.js.map +1 -1
  29. package/dist/testing/FakeStorage.js +7 -7
  30. package/dist/testing/FakeStorage.js.map +1 -1
  31. package/package.json +1 -1
  32. package/src/ArchiveProvider.ts +64 -24
  33. package/src/DriveManager.ts +2 -2
  34. package/src/GcsDriver.ts +5 -5
  35. package/src/S3Driver.ts +6 -6
  36. package/src/StaticMiddleware.ts +32 -11
  37. package/src/StorageManager.ts +182 -118
  38. package/src/etag.ts +44 -0
  39. package/src/expiry.ts +6 -6
  40. package/src/services/main.ts +21 -0
  41. package/src/testing/FakeStorage.ts +7 -7
@@ -10,10 +10,10 @@
10
10
  * @implements MISS-25
11
11
  */
12
12
 
13
- import { createHash } from "node:crypto";
14
13
  import * as fs from "node:fs";
15
14
  import * as fsp from "node:fs/promises";
16
15
  import * as path from "node:path";
16
+ import { matchesIfNoneMatch, statTag } from "./etag.js";
17
17
  import { DEFAULT_EXTENSIONS, inferMimeType } from "./mime-types.js";
18
18
  import { VISIBILITY_SIDECAR_SUFFIX } from "./StorageManager.js";
19
19
 
@@ -142,13 +142,14 @@ export class StaticMiddleware {
142
142
  return next();
143
143
  }
144
144
 
145
- const etag = createHash("md5")
146
- .update(`${stat.size}-${stat.mtimeMs}`)
147
- .digest("hex");
145
+ // Quoted and weak (RFC 9110 §8.8.3): the bare md5 hex this used to send
146
+ // is not a valid entity-tag, and a strict cache may ignore it outright.
147
+ const etag = statTag(stat);
148
148
 
149
- // ETag check — 304 Not Modified
150
- const ifNoneMatch = ctx.request.header("if-none-match");
151
- if (ifNoneMatch === etag) {
149
+ // ETag check — 304 Not Modified. The header is a list compared weakly
150
+ // (§13.1.2); raw equality missed a client sending two tags, or one
151
+ // sending back `W/"x"` for a tag we wrote as `"x"`.
152
+ if (matchesIfNoneMatch(ctx.request.header("if-none-match"), etag)) {
152
153
  ctx.response.status(304);
153
154
  return;
154
155
  }
@@ -164,10 +165,30 @@ export class StaticMiddleware {
164
165
  return;
165
166
  }
166
167
 
167
- // Read the canonicalised path — closes the symlink TOCTOU window
168
- // between the realpath check above and the read. `filePath` could
169
- // resolve through a symlink that was swapped after we validated it.
170
- const content = await fsp.readFile(realPath);
168
+ // Opened with O_NOFOLLOW rather than read by name.
169
+ //
170
+ // Reading the canonicalised path narrowed the window but did not close
171
+ // it: between `realpath` and the read, the last component of that path
172
+ // can be replaced by a symlink pointing anywhere, and a read by name
173
+ // follows it. Refusing to follow a link at open time takes the swap off
174
+ // the table — the open fails instead of serving a file outside the root.
175
+ //
176
+ // A parent directory swapped in the same window is not covered; that
177
+ // needs openat-relative-to-a-descriptor, which Node does not expose.
178
+ let content: Buffer;
179
+ try {
180
+ const handle = await fsp.open(
181
+ realPath,
182
+ fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW,
183
+ );
184
+ try {
185
+ content = await handle.readFile();
186
+ } finally {
187
+ await handle.close();
188
+ }
189
+ } catch {
190
+ return next();
191
+ }
171
192
  // Send the raw Buffer — NOT content.toString() which would destroy binary
172
193
  // data (PNG, PDF, ZIP, etc.) by interpreting it as UTF-8.
173
194
  ctx.response.status(200).sendBuffer(content);