@taggor/contract 0.0.0-stage → 0.2.0

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 (343) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +61 -2
  3. package/dist/.tsbuildinfo +1 -0
  4. package/dist/artifact-keys.d.ts +169 -0
  5. package/dist/artifact-keys.d.ts.map +1 -0
  6. package/dist/artifact-keys.js +195 -0
  7. package/dist/artifact-keys.js.map +1 -0
  8. package/dist/assets.d.ts +72 -0
  9. package/dist/assets.d.ts.map +1 -0
  10. package/dist/assets.js +94 -0
  11. package/dist/assets.js.map +1 -0
  12. package/dist/bounded-path.d.ts +29 -0
  13. package/dist/bounded-path.d.ts.map +1 -0
  14. package/dist/bounded-path.js +57 -0
  15. package/dist/bounded-path.js.map +1 -0
  16. package/dist/bounded.d.ts +47 -0
  17. package/dist/bounded.d.ts.map +1 -0
  18. package/dist/bounded.js +93 -0
  19. package/dist/bounded.js.map +1 -0
  20. package/dist/build-diagnostic.d.ts +89 -0
  21. package/dist/build-diagnostic.d.ts.map +1 -0
  22. package/dist/build-diagnostic.js +91 -0
  23. package/dist/build-diagnostic.js.map +1 -0
  24. package/dist/component-types.d.ts +61 -0
  25. package/dist/component-types.d.ts.map +1 -0
  26. package/dist/component-types.js +277 -0
  27. package/dist/component-types.js.map +1 -0
  28. package/dist/consent-text.d.ts +31 -0
  29. package/dist/consent-text.d.ts.map +1 -0
  30. package/dist/consent-text.js +41 -0
  31. package/dist/consent-text.js.map +1 -0
  32. package/dist/contract-version.d.ts +150 -0
  33. package/dist/contract-version.d.ts.map +1 -0
  34. package/dist/contract-version.js +198 -0
  35. package/dist/contract-version.js.map +1 -0
  36. package/dist/credential-fields.d.ts +31 -0
  37. package/dist/credential-fields.d.ts.map +1 -0
  38. package/dist/credential-fields.js +36 -0
  39. package/dist/credential-fields.js.map +1 -0
  40. package/dist/css-tokens.d.ts +46 -0
  41. package/dist/css-tokens.d.ts.map +1 -0
  42. package/dist/css-tokens.js +189 -0
  43. package/dist/css-tokens.js.map +1 -0
  44. package/dist/css-value.d.ts +42 -0
  45. package/dist/css-value.d.ts.map +1 -0
  46. package/dist/css-value.js +78 -0
  47. package/dist/css-value.js.map +1 -0
  48. package/dist/data-api.d.ts +127 -0
  49. package/dist/data-api.d.ts.map +1 -0
  50. package/dist/data-api.js +167 -0
  51. package/dist/data-api.js.map +1 -0
  52. package/dist/denylist-keys.d.ts +19 -0
  53. package/dist/denylist-keys.d.ts.map +1 -0
  54. package/dist/denylist-keys.js +48 -0
  55. package/dist/denylist-keys.js.map +1 -0
  56. package/dist/diagnostic-catalog.d.ts +1402 -0
  57. package/dist/diagnostic-catalog.d.ts.map +1 -0
  58. package/dist/diagnostic-catalog.js +1311 -0
  59. package/dist/diagnostic-catalog.js.map +1 -0
  60. package/dist/dom-members.snapshot.d.ts +33 -0
  61. package/dist/dom-members.snapshot.d.ts.map +1 -0
  62. package/dist/dom-members.snapshot.js +760 -0
  63. package/dist/dom-members.snapshot.js.map +1 -0
  64. package/dist/early-loading.d.ts +27 -0
  65. package/dist/early-loading.d.ts.map +1 -0
  66. package/dist/early-loading.js +44 -0
  67. package/dist/early-loading.js.map +1 -0
  68. package/dist/error-catalog.d.ts +749 -0
  69. package/dist/error-catalog.d.ts.map +1 -0
  70. package/dist/error-catalog.js +720 -0
  71. package/dist/error-catalog.js.map +1 -0
  72. package/dist/executable-config.d.ts +35 -0
  73. package/dist/executable-config.d.ts.map +1 -0
  74. package/dist/executable-config.js +44 -0
  75. package/dist/executable-config.js.map +1 -0
  76. package/dist/freeze.d.ts +17 -0
  77. package/dist/freeze.d.ts.map +1 -0
  78. package/dist/freeze.js +29 -0
  79. package/dist/freeze.js.map +1 -0
  80. package/dist/gunzip.d.ts +33 -0
  81. package/dist/gunzip.d.ts.map +1 -0
  82. package/dist/gunzip.js +330 -0
  83. package/dist/gunzip.js.map +1 -0
  84. package/dist/hostname.d.ts +74 -0
  85. package/dist/hostname.d.ts.map +1 -0
  86. package/dist/hostname.js +215 -0
  87. package/dist/hostname.js.map +1 -0
  88. package/dist/human-actions.d.ts +49 -0
  89. package/dist/human-actions.d.ts.map +1 -0
  90. package/dist/human-actions.js +272 -0
  91. package/dist/human-actions.js.map +1 -0
  92. package/dist/index.d.ts +73 -0
  93. package/dist/index.d.ts.map +1 -0
  94. package/dist/index.js +73 -0
  95. package/dist/index.js.map +1 -0
  96. package/dist/json-schema.d.ts +108 -0
  97. package/dist/json-schema.d.ts.map +1 -0
  98. package/dist/json-schema.js +240 -0
  99. package/dist/json-schema.js.map +1 -0
  100. package/dist/key-checksum.d.ts +33 -0
  101. package/dist/key-checksum.d.ts.map +1 -0
  102. package/dist/key-checksum.js +67 -0
  103. package/dist/key-checksum.js.map +1 -0
  104. package/dist/libraries.d.ts +60 -0
  105. package/dist/libraries.d.ts.map +1 -0
  106. package/dist/libraries.js +120 -0
  107. package/dist/libraries.js.map +1 -0
  108. package/dist/limit-catalog.d.ts +381 -0
  109. package/dist/limit-catalog.d.ts.map +1 -0
  110. package/dist/limit-catalog.js +429 -0
  111. package/dist/limit-catalog.js.map +1 -0
  112. package/dist/limits.d.ts +167 -0
  113. package/dist/limits.d.ts.map +1 -0
  114. package/dist/limits.js +175 -0
  115. package/dist/limits.js.map +1 -0
  116. package/dist/locale-tag.d.ts +21 -0
  117. package/dist/locale-tag.d.ts.map +1 -0
  118. package/dist/locale-tag.js +21 -0
  119. package/dist/locale-tag.js.map +1 -0
  120. package/dist/make-diagnostic.d.ts +74 -0
  121. package/dist/make-diagnostic.d.ts.map +1 -0
  122. package/dist/make-diagnostic.js +100 -0
  123. package/dist/make-diagnostic.js.map +1 -0
  124. package/dist/manifest-compat-diagnostics.d.ts +23 -0
  125. package/dist/manifest-compat-diagnostics.d.ts.map +1 -0
  126. package/dist/manifest-compat-diagnostics.js +150 -0
  127. package/dist/manifest-compat-diagnostics.js.map +1 -0
  128. package/dist/manifest-compat.d.ts +58 -0
  129. package/dist/manifest-compat.d.ts.map +1 -0
  130. package/dist/manifest-compat.js +377 -0
  131. package/dist/manifest-compat.js.map +1 -0
  132. package/dist/manifest.d.ts +1769 -0
  133. package/dist/manifest.d.ts.map +1 -0
  134. package/dist/manifest.js +421 -0
  135. package/dist/manifest.js.map +1 -0
  136. package/dist/metering.d.ts +126 -0
  137. package/dist/metering.d.ts.map +1 -0
  138. package/dist/metering.js +147 -0
  139. package/dist/metering.js.map +1 -0
  140. package/dist/migrate.d.ts +70 -0
  141. package/dist/migrate.d.ts.map +1 -0
  142. package/dist/migrate.js +159 -0
  143. package/dist/migrate.js.map +1 -0
  144. package/dist/money.d.ts +41 -0
  145. package/dist/money.d.ts.map +1 -0
  146. package/dist/money.js +52 -0
  147. package/dist/money.js.map +1 -0
  148. package/dist/names.d.ts +37 -0
  149. package/dist/names.d.ts.map +1 -0
  150. package/dist/names.js +55 -0
  151. package/dist/names.js.map +1 -0
  152. package/dist/overage-pricing.d.ts +89 -0
  153. package/dist/overage-pricing.d.ts.map +1 -0
  154. package/dist/overage-pricing.js +170 -0
  155. package/dist/overage-pricing.js.map +1 -0
  156. package/dist/plan-ids.d.ts +10 -0
  157. package/dist/plan-ids.d.ts.map +1 -0
  158. package/dist/plan-ids.js +8 -0
  159. package/dist/plan-ids.js.map +1 -0
  160. package/dist/plan-limits.d.ts +138 -0
  161. package/dist/plan-limits.d.ts.map +1 -0
  162. package/dist/plan-limits.js +112 -0
  163. package/dist/plan-limits.js.map +1 -0
  164. package/dist/plan-pricing.d.ts +145 -0
  165. package/dist/plan-pricing.d.ts.map +1 -0
  166. package/dist/plan-pricing.js +172 -0
  167. package/dist/plan-pricing.js.map +1 -0
  168. package/dist/plan-usage.d.ts +112 -0
  169. package/dist/plan-usage.d.ts.map +1 -0
  170. package/dist/plan-usage.js +134 -0
  171. package/dist/plan-usage.js.map +1 -0
  172. package/dist/preview-token.d.ts +52 -0
  173. package/dist/preview-token.d.ts.map +1 -0
  174. package/dist/preview-token.js +106 -0
  175. package/dist/preview-token.js.map +1 -0
  176. package/dist/prop-names.d.ts +51 -0
  177. package/dist/prop-names.d.ts.map +1 -0
  178. package/dist/prop-names.js +150 -0
  179. package/dist/prop-names.js.map +1 -0
  180. package/dist/prop-type-docs.d.ts +20 -0
  181. package/dist/prop-type-docs.d.ts.map +1 -0
  182. package/dist/prop-type-docs.js +89 -0
  183. package/dist/prop-type-docs.js.map +1 -0
  184. package/dist/prop-types.d.ts +97 -0
  185. package/dist/prop-types.d.ts.map +1 -0
  186. package/dist/prop-types.js +299 -0
  187. package/dist/prop-types.js.map +1 -0
  188. package/dist/props.d.ts +996 -0
  189. package/dist/props.d.ts.map +1 -0
  190. package/dist/props.js +183 -0
  191. package/dist/props.js.map +1 -0
  192. package/dist/proto-guard.d.ts +29 -0
  193. package/dist/proto-guard.d.ts.map +1 -0
  194. package/dist/proto-guard.js +79 -0
  195. package/dist/proto-guard.js.map +1 -0
  196. package/dist/runtime-errors.d.ts +42 -0
  197. package/dist/runtime-errors.d.ts.map +1 -0
  198. package/dist/runtime-errors.js +42 -0
  199. package/dist/runtime-errors.js.map +1 -0
  200. package/dist/runtime-pointers.d.ts +58 -0
  201. package/dist/runtime-pointers.d.ts.map +1 -0
  202. package/dist/runtime-pointers.js +84 -0
  203. package/dist/runtime-pointers.js.map +1 -0
  204. package/dist/secret-scan.d.ts +191 -0
  205. package/dist/secret-scan.d.ts.map +1 -0
  206. package/dist/secret-scan.js +542 -0
  207. package/dist/secret-scan.js.map +1 -0
  208. package/dist/semver.d.ts +89 -0
  209. package/dist/semver.d.ts.map +1 -0
  210. package/dist/semver.js +400 -0
  211. package/dist/semver.js.map +1 -0
  212. package/dist/server-key.d.ts +68 -0
  213. package/dist/server-key.d.ts.map +1 -0
  214. package/dist/server-key.js +150 -0
  215. package/dist/server-key.js.map +1 -0
  216. package/dist/site-manifest-keys.d.ts +107 -0
  217. package/dist/site-manifest-keys.d.ts.map +1 -0
  218. package/dist/site-manifest-keys.js +146 -0
  219. package/dist/site-manifest-keys.js.map +1 -0
  220. package/dist/site-manifest.d.ts +286 -0
  221. package/dist/site-manifest.d.ts.map +1 -0
  222. package/dist/site-manifest.js +100 -0
  223. package/dist/site-manifest.js.map +1 -0
  224. package/dist/source-archive.d.ts +119 -0
  225. package/dist/source-archive.d.ts.map +1 -0
  226. package/dist/source-archive.js +644 -0
  227. package/dist/source-archive.js.map +1 -0
  228. package/dist/source-package-corpus.d.ts +55 -0
  229. package/dist/source-package-corpus.d.ts.map +1 -0
  230. package/dist/source-package-corpus.js +303 -0
  231. package/dist/source-package-corpus.js.map +1 -0
  232. package/dist/source-package.d.ts +227 -0
  233. package/dist/source-package.d.ts.map +1 -0
  234. package/dist/source-package.js +361 -0
  235. package/dist/source-package.js.map +1 -0
  236. package/dist/source-secrets.d.ts +21 -0
  237. package/dist/source-secrets.d.ts.map +1 -0
  238. package/dist/source-secrets.js +43 -0
  239. package/dist/source-secrets.js.map +1 -0
  240. package/dist/ssr-cap.d.ts +61 -0
  241. package/dist/ssr-cap.d.ts.map +1 -0
  242. package/dist/ssr-cap.js +107 -0
  243. package/dist/ssr-cap.js.map +1 -0
  244. package/dist/ssr-kv.d.ts +18 -0
  245. package/dist/ssr-kv.d.ts.map +1 -0
  246. package/dist/ssr-kv.js +18 -0
  247. package/dist/ssr-kv.js.map +1 -0
  248. package/dist/ssr-version-ledger.d.ts +53 -0
  249. package/dist/ssr-version-ledger.d.ts.map +1 -0
  250. package/dist/ssr-version-ledger.js +88 -0
  251. package/dist/ssr-version-ledger.js.map +1 -0
  252. package/dist/style-references.d.ts +58 -0
  253. package/dist/style-references.d.ts.map +1 -0
  254. package/dist/style-references.js +241 -0
  255. package/dist/style-references.js.map +1 -0
  256. package/dist/tag-name.d.ts +87 -0
  257. package/dist/tag-name.d.ts.map +1 -0
  258. package/dist/tag-name.js +222 -0
  259. package/dist/tag-name.js.map +1 -0
  260. package/dist/tar-writer.d.ts +74 -0
  261. package/dist/tar-writer.d.ts.map +1 -0
  262. package/dist/tar-writer.js +148 -0
  263. package/dist/tar-writer.js.map +1 -0
  264. package/dist/testing.d.ts +11 -0
  265. package/dist/testing.d.ts.map +1 -0
  266. package/dist/testing.js +11 -0
  267. package/dist/testing.js.map +1 -0
  268. package/dist/text-codec.d.ts +36 -0
  269. package/dist/text-codec.d.ts.map +1 -0
  270. package/dist/text-codec.js +32 -0
  271. package/dist/text-codec.js.map +1 -0
  272. package/dist/url-value.d.ts +41 -0
  273. package/dist/url-value.d.ts.map +1 -0
  274. package/dist/url-value.js +147 -0
  275. package/dist/url-value.js.map +1 -0
  276. package/dist/usage-guard.d.ts +99 -0
  277. package/dist/usage-guard.d.ts.map +1 -0
  278. package/dist/usage-guard.js +104 -0
  279. package/dist/usage-guard.js.map +1 -0
  280. package/dist/usage-mirror.d.ts +55 -0
  281. package/dist/usage-mirror.d.ts.map +1 -0
  282. package/dist/usage-mirror.js +52 -0
  283. package/dist/usage-mirror.js.map +1 -0
  284. package/dist/value-check.d.ts +88 -0
  285. package/dist/value-check.d.ts.map +1 -0
  286. package/dist/value-check.js +279 -0
  287. package/dist/value-check.js.map +1 -0
  288. package/dist/value-patterns.d.ts +9 -0
  289. package/dist/value-patterns.d.ts.map +1 -0
  290. package/dist/value-patterns.js +17 -0
  291. package/dist/value-patterns.js.map +1 -0
  292. package/dist/value-schema.d.ts +796 -0
  293. package/dist/value-schema.d.ts.map +1 -0
  294. package/dist/value-schema.js +231 -0
  295. package/dist/value-schema.js.map +1 -0
  296. package/dist/zod-free.d.ts +60 -0
  297. package/dist/zod-free.d.ts.map +1 -0
  298. package/dist/zod-free.js +59 -0
  299. package/dist/zod-free.js.map +1 -0
  300. package/package.json +75 -3
  301. package/schema/DEPLOYED +12 -0
  302. package/schema/build-diagnostic.v1.json +63 -0
  303. package/schema/component-manifest.v1.json +1000 -0
  304. package/schema/data-api.v1.json +194 -0
  305. package/schema/library-manifest.v1.json +60 -0
  306. package/schema/published/SHA256SUMS +36 -0
  307. package/schema/published/build-diagnostic/v1.10.json +83 -0
  308. package/schema/published/build-diagnostic/v1.3.json +83 -0
  309. package/schema/published/build-diagnostic/v1.4.json +83 -0
  310. package/schema/published/build-diagnostic/v1.5.json +83 -0
  311. package/schema/published/build-diagnostic/v1.6.json +83 -0
  312. package/schema/published/build-diagnostic/v1.7.json +83 -0
  313. package/schema/published/build-diagnostic/v1.8.json +83 -0
  314. package/schema/published/build-diagnostic/v1.9.json +83 -0
  315. package/schema/published/component-manifest/v1.10.json +1443 -0
  316. package/schema/published/component-manifest/v1.3.json +1411 -0
  317. package/schema/published/component-manifest/v1.4.json +1419 -0
  318. package/schema/published/component-manifest/v1.5.json +1437 -0
  319. package/schema/published/component-manifest/v1.6.json +1437 -0
  320. package/schema/published/component-manifest/v1.7.json +1437 -0
  321. package/schema/published/component-manifest/v1.8.json +1437 -0
  322. package/schema/published/component-manifest/v1.9.json +1437 -0
  323. package/schema/published/data-api/v1.10.json +282 -0
  324. package/schema/published/data-api/v1.3.json +281 -0
  325. package/schema/published/data-api/v1.4.json +281 -0
  326. package/schema/published/data-api/v1.5.json +281 -0
  327. package/schema/published/data-api/v1.6.json +281 -0
  328. package/schema/published/data-api/v1.7.json +281 -0
  329. package/schema/published/data-api/v1.8.json +282 -0
  330. package/schema/published/data-api/v1.9.json +282 -0
  331. package/schema/published/library-manifest/v1.10.json +68 -0
  332. package/schema/published/library-manifest/v1.5.json +68 -0
  333. package/schema/published/library-manifest/v1.6.json +68 -0
  334. package/schema/published/library-manifest/v1.7.json +68 -0
  335. package/schema/published/library-manifest/v1.8.json +68 -0
  336. package/schema/published/library-manifest/v1.9.json +68 -0
  337. package/schema/published/taggor-config/v1.10.json +56 -0
  338. package/schema/published/taggor-config/v1.5.json +56 -0
  339. package/schema/published/taggor-config/v1.6.json +56 -0
  340. package/schema/published/taggor-config/v1.7.json +56 -0
  341. package/schema/published/taggor-config/v1.8.json +56 -0
  342. package/schema/published/taggor-config/v1.9.json +56 -0
  343. package/schema/taggor-config.v1.json +39 -0
@@ -0,0 +1,644 @@
1
+ /**
2
+ * Reader of the (already decompressed) source package: a POSIX tar archive. Created in the
3
+ * builder by TAG-401 (ADR 0017 M1); moved to the contract by TAG-407, so the API (first line,
4
+ * before the package is stored) and the build container (second line) read a package the same
5
+ * way and refuse it with the same diagnostic codes.
6
+ *
7
+ * Only POSIX ustar/pax headers are read (what node-tar, bsdtar and `git archive` write); GNU
8
+ * and v7 headers are refused, because the `prefix` field only exists in POSIX ustar. Numeric
9
+ * fields are read as POSIX writes them, and only regular files and pax headers may have a body,
10
+ * so this reader and bsdtar always agree on where each header starts. Only regular files and
11
+ * directories are accepted; symlinks, hardlinks, devices, FIFOs and GNU long-name records are
12
+ * refused, as are absolute paths, `..` segments, backslashes, control, bidi and invisible
13
+ * characters in names (a leading BOM included), files under `node_modules/`, executable config
14
+ * files, macOS metadata files (`._x`, `.DS_Store`), files outside the allowed extensions, paths
15
+ * longer than the limit, two paths a case-insensitive, normalizing disk would merge, and a file
16
+ * that is also a folder (another file's, or a directory entry). pax extended headers are read
17
+ * only for their `path` (long names); the other keys must be on an allowlist of harmless
18
+ * metadata, and pax headers are bounded in bytes and records. The result is the in-memory file
19
+ * map the build runs on, so nothing the customer sends is ever written to a disk.
20
+ *
21
+ * TAG-410: the asset files a component imports (`ASSET_SOURCE_EXTENSIONS`: images and WOFF2 fonts)
22
+ * may be in the package too, under `src/` only, and are kept as bytes (`assets`), never decoded as
23
+ * text; every other check (path, duplicates, limits) is the same for them.
24
+ *
25
+ * Problems with one entry (its path, type, extension) are collected, up to
26
+ * `MAX_SOURCE_PROBLEMS`, so an agent fixes them in one round; a broken archive or a limit stops
27
+ * the read at once.
28
+ *
29
+ * TAG-402: the builder reads the npm tarballs of its allowlist with this same reader, through a
30
+ * `TarEntryPolicy` of its own (strip `package/`, keep only module files, skip the rest); the
31
+ * header, pax, entry-type, limit and duplicate checks are the same for both.
32
+ *
33
+ * Zod-free and pure: it reads the bytes it is given.
34
+ */
35
+ import { assetTypeOfPath } from "./assets.js";
36
+ import { DIAGNOSTIC_TEXT_PATTERN } from "./diagnostic-catalog.js";
37
+ import { executableConfigProblem } from "./executable-config.js";
38
+ import { latin1, strictUtf8, strictUtf8KeepingBom } from "./text-codec.js";
39
+ const BLOCK = 512;
40
+ /** Entry types that carry a body: regular files (`0`, NUL) and pax headers (`x`, `g`). */
41
+ const BODY_TYPES = new Set(["0", "\0", "x", "g"]);
42
+ /**
43
+ * Text extensions a source package may contain; `scss` since TAG-408 (compiled by the builder).
44
+ * The asset extensions (`ASSET_SOURCE_EXTENSIONS`, TAG-410) are accepted too, as bytes.
45
+ */
46
+ export const SOURCE_EXTENSIONS = new Set([
47
+ "ts",
48
+ "tsx",
49
+ "js",
50
+ "jsx",
51
+ "mjs",
52
+ "json",
53
+ "css",
54
+ "scss",
55
+ "md",
56
+ ]);
57
+ /** Most problems one read reports; the rest are dropped (each costs a diagnostic). */
58
+ export const MAX_SOURCE_PROBLEMS = 20;
59
+ /** Headers allowed per regular file when `maxEntries` is not given: room for folders and pax. */
60
+ export const ENTRIES_PER_FILE = 4;
61
+ /**
62
+ * Names, pax keys and pax values keep a leading U+FEFF (TAG-407 security review, M1): a decoder
63
+ * that drops it made `\uFEFFsrc/index.tsx` read as `src/index.tsx` here while bsdtar keeps it
64
+ * outside `src/`, and made a `\uFEFFpath` key act as `path` here while bsdtar and tarfile ignore
65
+ * it. Kept, the BOM reaches `checkArchivePath` (and the key allowlist) and is refused. File
66
+ * contents are text for the bundler, which drops a leading BOM itself.
67
+ */
68
+ const utf8 = strictUtf8KeepingBom;
69
+ /** A NUL-terminated name field decoded as UTF-8 (as bsdtar and tarfile show it), or undefined. */
70
+ function textField(header, offset, length) {
71
+ const bytes = header.subarray(offset, offset + length);
72
+ const end = bytes.indexOf(0);
73
+ try {
74
+ return utf8.decode(end === -1 ? bytes : bytes.subarray(0, end));
75
+ }
76
+ catch {
77
+ return undefined;
78
+ }
79
+ }
80
+ /** The POSIX magic and version (`ustar\0` + `00`) at offsets 257 and 263. */
81
+ const POSIX_MAGIC = [0x75, 0x73, 0x74, 0x61, 0x72, 0x00, 0x30, 0x30];
82
+ /**
83
+ * Whether a header is POSIX ustar (the format pax extends). GNU headers (`ustar \0`) and v7
84
+ * headers (no magic) are refused: in them the bytes at offset 345 are not a path prefix (GNU
85
+ * keeps times there), so reading them as one made the builder see a different tree from bsdtar
86
+ * and node-tar (TAG-401 security review, M2 remainder).
87
+ */
88
+ function isPosixUstar(header) {
89
+ return POSIX_MAGIC.every((byte, index) => header[257 + index] === byte);
90
+ }
91
+ /**
92
+ * A numeric field as POSIX writes it: optional leading spaces, octal digits, then spaces or NULs
93
+ * to the end of the field. Nothing else: `trim()` accepted a leading `\v`, `\n`, `\f`, `\r` or
94
+ * U+00A0, which libarchive reads as 0, so the two disagreed on where the next header starts
95
+ * (TAG-407 security review, A1). GNU's base-256 numbers are not POSIX and are refused too.
96
+ */
97
+ const POSIX_NUMBER = /^ *([0-7]+)[ \0]*$/;
98
+ function octal(header, offset, length) {
99
+ const match = POSIX_NUMBER.exec(latin1.decode(header.subarray(offset, offset + length)));
100
+ return match === null ? undefined : Number.parseInt(match[1] ?? "", 8);
101
+ }
102
+ /**
103
+ * The other numeric fields (mode, uid, gid, mtime, devmajor, devminor) as libarchive accepts
104
+ * them: optional leading spaces, octal digits, then spaces or NULs. The reader does not use their
105
+ * values, but bsdtar refuses an archive whose first header has, say, `000064x` as its mode, and
106
+ * tarfile stops at such a header further in, so both would see another tree (found by the
107
+ * TAG-425 oracle). GNU base-256 values are refused here as everywhere.
108
+ */
109
+ const POSIX_NUMBER_OR_BLANK = /^ *[0-7]*[ \0]*$/;
110
+ const OTHER_NUMBER_FIELDS = [
111
+ [100, 8],
112
+ [108, 8],
113
+ [116, 8],
114
+ [136, 12],
115
+ [329, 8],
116
+ [337, 8],
117
+ ];
118
+ function otherNumbersWellFormed(header) {
119
+ return OTHER_NUMBER_FIELDS.every(([offset, length]) => POSIX_NUMBER_OR_BLANK.test(latin1.decode(header.subarray(offset, offset + length))));
120
+ }
121
+ function checksumMatches(header) {
122
+ const stored = octal(header, 148, 8);
123
+ let sum = 0;
124
+ for (let index = 0; index < BLOCK; index += 1) {
125
+ // The checksum field itself counts as eight spaces.
126
+ sum += index >= 148 && index < 156 ? 32 : (header[index] ?? 0);
127
+ }
128
+ return stored === sum;
129
+ }
130
+ function isZeroBlock(header) {
131
+ return header.every((byte) => byte === 0);
132
+ }
133
+ /**
134
+ * The pax keys a package may carry (TAG-407 security review, B2): `path`, which the reader applies,
135
+ * and metadata that changes neither where an entry's data is nor what it is (times, owners, the
136
+ * node-tar `dev`/`ino`/`nlink`, a `comment` such as `git archive`'s, the creation time, the file
137
+ * flags bsdtar writes from the disk (`SCHILY.fflags`, TAG-2239, only with the values of
138
+ * `ALLOWED_FFLAGS`) and the `com.apple.*` extended attributes macOS tar writes). Any other key is
139
+ * refused: `size`,
140
+ * `linkpath`, `GNU.sparse.*`, `SCHILY.realsize` (bsdtar truncates the file to it), `hdrcharset`
141
+ * (changes how names are read) and every key not known to be harmless.
142
+ */
143
+ const ALLOWED_PAX_KEYS = new Set([
144
+ "path",
145
+ "mtime",
146
+ "atime",
147
+ "ctime",
148
+ "uid",
149
+ "gid",
150
+ "uname",
151
+ "gname",
152
+ "comment",
153
+ "dev",
154
+ "ino",
155
+ "nlink",
156
+ "LIBARCHIVE.creationtime",
157
+ "SCHILY.fflags",
158
+ ]);
159
+ /**
160
+ * Extended attributes macOS tar writes (`SCHILY.xattr.com.apple.provenance`, `…quarantine`,
161
+ * `…metadata:kMDItemWhereFroms`), ignored, since their values are raw bytes. Only `com.apple.*`
162
+ * names with printable ASCII (TAG-407 re-review, B9): a consumer that extracts the package with
163
+ * xattrs on Linux would apply `security.capability`, `trusted.*` or `system.posix_acl_*`, and a
164
+ * NUL would cut a name to whatever follows a harmless-looking prefix.
165
+ */
166
+ const ALLOWED_XATTR_KEY = /^(?:SCHILY|LIBARCHIVE)\.xattr\.com\.apple\.[\x21-\x7e]{1,200}$/;
167
+ /**
168
+ * The file flags a `SCHILY.fflags` value may name (TAG-2239 security review, B1): the ones bsdtar
169
+ * writes for an ordinary file (`nocow` on btrfs; `nodump`, `noatime`, `notail`, `compress` from
170
+ * `chattr` on Linux; `hidden` on macOS), which a tool extracting as root may set on the file but
171
+ * which never stop it from being changed or removed. `schg`, `uchg`, `sappnd`, `uappnd` (immutable
172
+ * and append-only: the file can no longer be removed) and every other flag are refused.
173
+ */
174
+ const ALLOWED_FFLAGS = new Set(["nodump", "nocow", "noatime", "hidden", "notail", "compress"]);
175
+ /** The longest `SCHILY.fflags` value: every allowed flag once, with commas, is 43 bytes. */
176
+ const MAX_FFLAGS_BYTES = 64;
177
+ /**
178
+ * Whether a `SCHILY.fflags` value is a comma-separated list of `ALLOWED_FFLAGS`, as bsdtar writes
179
+ * it: lowercase ASCII, no spaces, no empty item, at most `MAX_FFLAGS_BYTES` bytes.
180
+ */
181
+ function isAllowedFflags(value) {
182
+ if (value.length === 0 || value.length > MAX_FFLAGS_BYTES)
183
+ return false;
184
+ if (!value.every((byte) => byte === 0x2c || (byte >= 0x61 && byte <= 0x7a)))
185
+ return false;
186
+ return String.fromCharCode(...value)
187
+ .split(",")
188
+ .every((flag) => ALLOWED_FFLAGS.has(flag));
189
+ }
190
+ /** How to pack without the file flags bsdtar stores from the disk. */
191
+ const FFLAGS_HINT = "bsdtar stores the file flags of the disk (chattr, chflags). Pack with --no-fflags (bsdtar), clear the flags, or let the Taggor CLI pack the component.";
192
+ function isAllowedPaxKey(key) {
193
+ return ALLOWED_PAX_KEYS.has(key) || ALLOWED_XATTR_KEY.test(key);
194
+ }
195
+ /**
196
+ * Most bytes of one pax header (TAG-407 security review, M2): the only value read is `path`, at
197
+ * most `maxPathLength` characters, so 64 KiB leaves room for every metadata record; 20 MiB of
198
+ * tiny records took 6 s of CPU.
199
+ */
200
+ export const MAX_PAX_HEADER_BYTES = 64 * 1024;
201
+ /** Most records of one pax header (same reason). */
202
+ export const MAX_PAX_RECORDS = 256;
203
+ /**
204
+ * Pax records allowed per entry in the whole archive: the cap is this × `maxEntries` (16,000
205
+ * with the default 2,000, about 40 ms at 2.3 µs a record). One header of 256 records per entry
206
+ * cost 1 to 1.4 s of CPU per read (TAG-425 security review, M2); a flat 4,096 refused a real
207
+ * package, 500 files and 101 folders from macOS bsdtar with `com.apple.quarantine` at 7 records
208
+ * each (re-review).
209
+ */
210
+ export const PAX_RECORDS_PER_ENTRY = 8;
211
+ /** How to pack without the extended attributes Linux bsdtar stores by default. */
212
+ const XATTR_HINT = "Linux bsdtar stores extended attributes by default (on SELinux every file has security.selinux). Pack with --no-xattrs (bsdtar) or let the Taggor CLI pack the component.";
213
+ /**
214
+ * Reads the records of a pax extended header (`<length> <key>=<value>\n`, the length counting
215
+ * bytes of the whole record), over the bytes, as POSIX defines it: the last `path` wins.
216
+ * Refuses a malformed record, a length that does not match, a key that is not UTF-8, a path that
217
+ * is not UTF-8 or empty, more than `MAX_PAX_RECORDS` records and any key outside the allowlist
218
+ * (`isAllowedPaxKey`); a `path` in a global header is refused too. The values of the other
219
+ * allowed keys are ignored and not even decoded. The caller has already bounded the body to
220
+ * `MAX_PAX_HEADER_BYTES`; `recordsLeft` is what remains of the archive's cap, `maxRecords`, and
221
+ * reaching it is a limit (`tooLarge`), not a malformed header.
222
+ */
223
+ function readPax(body, global, recordsLeft, maxRecords) {
224
+ const refuse = (why, hint) => ({
225
+ ok: false,
226
+ message: `malformed pax header: ${why}`,
227
+ ...(hint === undefined ? {} : { hint }),
228
+ });
229
+ let path;
230
+ let offset = 0;
231
+ let records = 0;
232
+ for (; offset < body.length; records += 1) {
233
+ if (records === MAX_PAX_RECORDS)
234
+ return refuse(`more than ${String(MAX_PAX_RECORDS)} records`);
235
+ if (records === recordsLeft) {
236
+ return {
237
+ ok: false,
238
+ message: `more than ${String(maxRecords)} pax records (${String(PAX_RECORDS_PER_ENTRY)} per allowed entry)`,
239
+ tooLarge: true,
240
+ };
241
+ }
242
+ let cursor = offset;
243
+ let length = 0;
244
+ while (cursor < body.length && body[cursor] !== 0x20) {
245
+ const byte = body[cursor] ?? 0;
246
+ if (byte < 0x30 || byte > 0x39 || cursor - offset >= 7)
247
+ return refuse("bad record length");
248
+ length = length * 10 + (byte - 0x30);
249
+ cursor += 1;
250
+ }
251
+ const recordEnd = offset + length;
252
+ if (cursor === offset ||
253
+ cursor >= body.length ||
254
+ length <= cursor - offset + 1 ||
255
+ recordEnd > body.length) {
256
+ return refuse("record length does not match its bytes");
257
+ }
258
+ if (body[recordEnd - 1] !== 0x0a)
259
+ return refuse("record does not end with a newline");
260
+ const record = body.subarray(cursor + 1, recordEnd - 1);
261
+ const equals = record.indexOf(0x3d);
262
+ if (equals <= 0)
263
+ return refuse("record without a key");
264
+ let key;
265
+ try {
266
+ key = utf8.decode(record.subarray(0, equals));
267
+ }
268
+ catch {
269
+ return refuse("record key is not UTF-8");
270
+ }
271
+ if (!isAllowedPaxKey(key)) {
272
+ const xattr = /^(?:SCHILY|LIBARCHIVE)\.xattr\./.test(key);
273
+ return refuse(`key ${JSON.stringify(quotable(key))} is not accepted`, xattr ? XATTR_HINT : undefined);
274
+ }
275
+ if (key === "SCHILY.fflags" && !isAllowedFflags(record.subarray(equals + 1))) {
276
+ return refuse("SCHILY.fflags names a file flag that is not accepted", FFLAGS_HINT);
277
+ }
278
+ if (key === "path") {
279
+ // Only the values the reader uses are decoded: macOS tar writes extended attributes as raw
280
+ // bytes (`SCHILY.xattr.com.apple.provenance`), which are not UTF-8 and are ignored anyway.
281
+ let value;
282
+ try {
283
+ value = utf8.decode(record.subarray(equals + 1));
284
+ }
285
+ catch {
286
+ return refuse("path is not UTF-8");
287
+ }
288
+ if (global || value === "")
289
+ return refuse("path in a global header, or empty");
290
+ path = value;
291
+ }
292
+ offset = recordEnd;
293
+ }
294
+ return { ok: true, path, records };
295
+ }
296
+ /** Characters of a name quoted in a message: a pax path can be megabytes long. */
297
+ const QUOTED_NAME_MAX_LENGTH = 200;
298
+ /** A name cut for a message, never splitting a surrogate pair (escaping is the caller's). */
299
+ function quotable(name) {
300
+ if (name.length <= QUOTED_NAME_MAX_LENGTH)
301
+ return name;
302
+ const high = name.charCodeAt(QUOTED_NAME_MAX_LENGTH - 1);
303
+ const end = high >= 0xd800 && high <= 0xdbff ? QUOTED_NAME_MAX_LENGTH - 1 : QUOTED_NAME_MAX_LENGTH;
304
+ return `${name.slice(0, end)}…`;
305
+ }
306
+ /** A name as a message quotes it: bounded, and JSON-escaped (newlines, quotes, C0 controls). */
307
+ function quoted(name) {
308
+ return JSON.stringify(quotable(name));
309
+ }
310
+ /**
311
+ * Checks one archive path and turns it into a virtual path.
312
+ *
313
+ * Inputs: the path as stored in the archive and the maximum length. Output: `/` + the path
314
+ * without a leading `./`, or a `SOURCE_PATH_NOT_ALLOWED` problem: empty or too long, absolute
315
+ * (`/x`, `C:/x`), a backslash, a control, bidi or invisible character (tab and NUL included;
316
+ * the characters `DIAGNOSTIC_TEXT_PATTERN` refuses, so a name can always be shown as is), an
317
+ * empty, `.` or `..` segment, or a segment `node_modules`. Errors: none. Side effects: none.
318
+ */
319
+ export function checkArchivePath(raw, maxPathLength) {
320
+ const path = raw.startsWith("./") ? raw.slice(2) : raw;
321
+ const refuse = (why) => ({
322
+ ok: false,
323
+ problem: { code: "SOURCE_PATH_NOT_ALLOWED", message: `path ${quoted(raw)} ${why}` },
324
+ });
325
+ if (path === "" || path.length > maxPathLength) {
326
+ return refuse(`is empty or longer than ${String(maxPathLength)} characters`);
327
+ }
328
+ if (path.startsWith("/") || /^[a-z]:/i.test(path))
329
+ return refuse("is absolute");
330
+ if (path.includes("\\"))
331
+ return refuse("has a backslash");
332
+ if (path.includes("\t") || !DIAGNOSTIC_TEXT_PATTERN.test(path)) {
333
+ return refuse("has a control, bidirectional or invisible character");
334
+ }
335
+ const segments = path.replace(/\/$/, "").split("/");
336
+ if (segments.some((segment) => segment === ".." || segment === "." || segment === "")) {
337
+ return refuse('has an empty, "." or ".." segment');
338
+ }
339
+ if (segments.includes("node_modules"))
340
+ return refuse("is inside node_modules");
341
+ return { ok: true, path: `/${path}` };
342
+ }
343
+ /**
344
+ * The extension of a virtual path's file name (`ts` for `/src/a.d.ts`), or `undefined` when the
345
+ * name has none (`/Makefile`, `/.env`).
346
+ */
347
+ function extensionOf(path) {
348
+ const name = path.slice(path.lastIndexOf("/") + 1);
349
+ const dot = name.lastIndexOf(".");
350
+ return dot <= 0 ? undefined : name.slice(dot + 1);
351
+ }
352
+ /**
353
+ * The form two names share when a case-insensitive, normalizing disk (APFS, NTFS) would store
354
+ * them as one file: NFC, then upper and lower case, so `σ`/`ς`, `s`/`ſ` and `ss`/`ß` meet as
355
+ * well as `A`/`a` (TAG-407 security review, B1).
356
+ */
357
+ function foldName(path) {
358
+ return path.normalize("NFC").toUpperCase().toLowerCase().normalize("NFC");
359
+ }
360
+ /** How to pack on macOS without the metadata files its tar adds (`._x`, `.DS_Store`). */
361
+ const MACOS_METADATA_HINT = "macOS tar adds these files for extended attributes. Pack with COPYFILE_DISABLE=1 tar --format=posix -czf component.tgz taggor.json package.json src (and leave .DS_Store out), or let the Taggor CLI pack the component.";
362
+ /** Problems of one regular file's path, before its body is read; an asset is placed as bytes. */
363
+ function fileProblem(stored, limits) {
364
+ const checked = checkArchivePath(stored, limits.maxPathLength);
365
+ if (!checked.ok)
366
+ return { problem: checked.problem };
367
+ const file = checked.path.slice(1);
368
+ const name = file.slice(file.lastIndexOf("/") + 1);
369
+ if (name.startsWith("._") || name === ".DS_Store") {
370
+ return {
371
+ problem: {
372
+ code: "SOURCE_EXTENSION_NOT_ALLOWED",
373
+ message: `file ${quoted(stored)} is macOS metadata (AppleDouble or Finder), not part of the component`,
374
+ file,
375
+ hint: MACOS_METADATA_HINT,
376
+ },
377
+ };
378
+ }
379
+ const config = executableConfigProblem(checked.path, quoted(stored));
380
+ if (config !== undefined)
381
+ return { problem: { ...config, file } };
382
+ if (assetTypeOfPath(checked.path) !== undefined) {
383
+ if (checked.path.startsWith("/src/"))
384
+ return { path: checked.path, asset: true };
385
+ return {
386
+ problem: {
387
+ code: "SOURCE_EXTENSION_NOT_ALLOWED",
388
+ message: `file ${quoted(stored)} is an asset outside src/`,
389
+ file,
390
+ hint: 'Put images and fonts under src/ and import them from the component\'s code (import logo from "./logo.svg").',
391
+ },
392
+ };
393
+ }
394
+ const extension = extensionOf(checked.path);
395
+ if (extension === undefined || !SOURCE_EXTENSIONS.has(extension)) {
396
+ return {
397
+ problem: {
398
+ code: "SOURCE_EXTENSION_NOT_ALLOWED",
399
+ message: `file ${quoted(stored)} has an extension the builder does not accept`,
400
+ file,
401
+ },
402
+ };
403
+ }
404
+ return { path: checked.path };
405
+ }
406
+ /** The source package's policy: every path checked, configs and odd extensions refused. */
407
+ function sourcePolicy(limits) {
408
+ return {
409
+ directory(stored) {
410
+ const checked = checkArchivePath(stored, limits.maxPathLength);
411
+ return checked.ok ? { path: checked.path } : { problem: checked.problem };
412
+ },
413
+ file: (stored) => fileProblem(stored, limits),
414
+ };
415
+ }
416
+ /**
417
+ * Reads a tar archive into a file map.
418
+ *
419
+ * Inputs: the uncompressed archive bytes and the limits. Output: `{ ok: true, files, assets }` with
420
+ * every regular file decoded as UTF-8 under its virtual path (`/src/a.ts`), except the ones the
421
+ * policy places as assets (TAG-410: the source package's images and fonts under `src/`), which are
422
+ * copied as bytes into `assets`; or `{ ok: false, problems }`
423
+ * (at most `MAX_SOURCE_PROBLEMS`). A broken archive stops at its first problem (`SOURCE_INVALID`:
424
+ * bad checksum, size or other numeric field, a GNU or v7 header, a truncated body or header, a
425
+ * malformed pax header or one with no entry after it, a second global pax header, a name that is
426
+ * not UTF-8), and so does a limit (`SOURCE_TOO_LARGE`: more entries, files, bytes or pax records,
427
+ * `PAX_RECORDS_PER_ENTRY` × `maxEntries`, than allowed).
428
+ * Problems of one entry are collected and the read goes on: `SOURCE_PATH_NOT_ALLOWED`
429
+ * (`checkArchivePath`), `SOURCE_ENTRY_NOT_ALLOWED` (symlink, hardlink, device, FIFO, GNU record),
430
+ * `SOURCE_CONFIG_NOT_ALLOWED` (`executableConfigProblem`), `SOURCE_EXTENSION_NOT_ALLOWED`, and
431
+ * `SOURCE_INVALID` for a duplicate path (also by letter case or Unicode normalization), a file that
432
+ * is also a folder (`folderClashes`) or content that is not UTF-8. TAG-402: `policy` replaces the
433
+ * source package's own rules for paths and files (the npm tarballs of the builder's allowlist);
434
+ * the limits, the folder clashes and the pax bounds apply to every policy, on the paths it maps.
435
+ * Errors: never throws. Side effects: none.
436
+ */
437
+ export function readSourceArchive(archive, limits, policy = sourcePolicy(limits)) {
438
+ const files = new Map();
439
+ const assets = new Map();
440
+ /** Folded path (NFC, lower case) → the stored path, to catch names a disk would merge. */
441
+ const folded = new Map();
442
+ /** Folded path of each directory entry, without the trailing slash → the stored path. */
443
+ const folders = new Map();
444
+ const problems = [];
445
+ const stop = (problem) => ({
446
+ ok: false,
447
+ problems: [...problems, problem].slice(0, MAX_SOURCE_PROBLEMS),
448
+ });
449
+ const invalid = (message) => stop({ code: "SOURCE_INVALID", message });
450
+ const maxEntries = limits.maxEntries ?? ENTRIES_PER_FILE * limits.maxFiles;
451
+ const maxPaxRecords = PAX_RECORDS_PER_ENTRY * maxEntries;
452
+ let entries = 0;
453
+ let regularFiles = 0;
454
+ let unpacked = 0;
455
+ let pendingPath;
456
+ let pendingPax = false;
457
+ /** A pax header (`x` or `g`) was read and no entry came after it yet. */
458
+ let paxAwaitsEntry = false;
459
+ let globalSeen = false;
460
+ let paxRecords = 0;
461
+ let offset = 0;
462
+ let ended = false;
463
+ while (offset + BLOCK <= archive.length && problems.length < MAX_SOURCE_PROBLEMS) {
464
+ const header = archive.subarray(offset, offset + BLOCK);
465
+ if (isZeroBlock(header)) {
466
+ ended = true;
467
+ break;
468
+ }
469
+ entries += 1;
470
+ if (entries > maxEntries) {
471
+ return stop({
472
+ code: "SOURCE_TOO_LARGE",
473
+ message: `more than ${String(maxEntries)} entries (files, folders and pax headers)`,
474
+ });
475
+ }
476
+ if (!checksumMatches(header))
477
+ return invalid("corrupt tar header (checksum)");
478
+ if (!isPosixUstar(header)) {
479
+ return invalid("only POSIX ustar/pax archives are accepted (GNU and v7 headers are refused); with GNU tar, pack with tar --format=posix");
480
+ }
481
+ const size = octal(header, 124, 12);
482
+ if (size === undefined)
483
+ return invalid("corrupt tar header (size)");
484
+ if (!otherNumbersWellFormed(header))
485
+ return invalid("corrupt tar header (numeric field)");
486
+ const bodyStart = offset + BLOCK;
487
+ const bodyEnd = bodyStart + size;
488
+ if (bodyEnd > archive.length)
489
+ return invalid("truncated tar archive");
490
+ const body = archive.subarray(bodyStart, bodyEnd);
491
+ offset = bodyStart + Math.ceil(size / BLOCK) * BLOCK;
492
+ const type = String.fromCharCode(header[156] ?? 0);
493
+ // Only regular files and pax headers have a body. bsdtar and tarfile read the body of any
494
+ // other entry (a directory, a link) as the next headers, so a size there would hide entries
495
+ // from this reader that they extract (TAG-407 security review, A1).
496
+ if (size !== 0 && !BODY_TYPES.has(type)) {
497
+ return invalid(`a ${JSON.stringify(type)} entry declares a body of ${String(size)} bytes`);
498
+ }
499
+ if ((type === "x" || type === "g") && size > MAX_PAX_HEADER_BYTES) {
500
+ return invalid(`pax header over ${String(MAX_PAX_HEADER_BYTES)} bytes`);
501
+ }
502
+ const prefix = textField(header, 345, 155);
503
+ const name = textField(header, 0, 100);
504
+ if (prefix === undefined || name === undefined)
505
+ return invalid("tar entry name is not UTF-8");
506
+ const storedPath = pendingPath ?? (prefix === "" ? name : `${prefix}/${name}`);
507
+ if (type === "x" || type === "g") {
508
+ if (type === "x" && pendingPax)
509
+ return invalid("two pax headers in a row");
510
+ // git archive writes one global header, at the start; a flood of them is only CPU (M2).
511
+ if (type === "g" && globalSeen)
512
+ return invalid("more than one pax global header");
513
+ const pax = readPax(body, type === "g", maxPaxRecords - paxRecords, maxPaxRecords);
514
+ if (!pax.ok) {
515
+ return stop({
516
+ code: pax.tooLarge === true ? "SOURCE_TOO_LARGE" : "SOURCE_INVALID",
517
+ message: pax.message,
518
+ ...(pax.hint === undefined ? {} : { hint: pax.hint }),
519
+ });
520
+ }
521
+ paxRecords += pax.records;
522
+ globalSeen ||= type === "g";
523
+ paxAwaitsEntry = true;
524
+ if (type === "x") {
525
+ pendingPax = true;
526
+ pendingPath = pax.path;
527
+ }
528
+ continue;
529
+ }
530
+ pendingPath = undefined;
531
+ pendingPax = false;
532
+ paxAwaitsEntry = false;
533
+ if (type === "5") {
534
+ const checked = policy.directory(storedPath);
535
+ if ("problem" in checked)
536
+ problems.push(checked.problem);
537
+ else if ("path" in checked)
538
+ folders.set(foldName(checked.path.replace(/\/$/, "")), storedPath);
539
+ continue;
540
+ }
541
+ if (type !== "0" && type !== "\0") {
542
+ const where = checkArchivePath(storedPath, limits.maxPathLength);
543
+ problems.push({
544
+ code: "SOURCE_ENTRY_NOT_ALLOWED",
545
+ message: `entry ${quoted(storedPath)} is not a regular file (type ${JSON.stringify(type)})`,
546
+ ...(where.ok ? { file: where.path.slice(1) } : {}),
547
+ });
548
+ continue;
549
+ }
550
+ regularFiles += 1;
551
+ unpacked += size;
552
+ if (regularFiles > limits.maxFiles) {
553
+ return stop({
554
+ code: "SOURCE_TOO_LARGE",
555
+ message: `more than ${String(limits.maxFiles)} files`,
556
+ });
557
+ }
558
+ if (unpacked > limits.maxUnpackedBytes) {
559
+ return stop({
560
+ code: "SOURCE_TOO_LARGE",
561
+ message: `more than ${String(limits.maxUnpackedBytes)} bytes unpacked`,
562
+ });
563
+ }
564
+ const checked = policy.file(storedPath);
565
+ if ("skip" in checked)
566
+ continue;
567
+ if ("problem" in checked) {
568
+ problems.push(checked.problem);
569
+ continue;
570
+ }
571
+ const key = foldName(checked.path);
572
+ const earlier = folded.get(key);
573
+ if (earlier !== undefined) {
574
+ problems.push({
575
+ code: "SOURCE_INVALID",
576
+ message: `duplicate path ${quoted(storedPath)} (same name as ${quoted(earlier)}, ignoring letter case and Unicode normalization)`,
577
+ file: checked.path.slice(1),
578
+ });
579
+ continue;
580
+ }
581
+ folded.set(key, storedPath);
582
+ if ("asset" in checked) {
583
+ // A copy: the archive buffer is not held by the result.
584
+ assets.set(checked.path, body.slice());
585
+ continue;
586
+ }
587
+ try {
588
+ files.set(checked.path, strictUtf8.decode(body));
589
+ }
590
+ catch {
591
+ problems.push({
592
+ code: "SOURCE_INVALID",
593
+ message: `file ${quoted(storedPath)} is not UTF-8 text`,
594
+ file: checked.path.slice(1),
595
+ });
596
+ }
597
+ }
598
+ // bsdtar fails on bytes left that are not a whole header, on a last body without its padding
599
+ // (security review, M1: the offset then passes the end) and on an `x` header with no entry
600
+ // after it, tarfile on a `g` header with none (found by the TAG-425 oracle).
601
+ if (problems.length < MAX_SOURCE_PROBLEMS) {
602
+ if (!ended && offset !== archive.length)
603
+ return invalid("truncated tar archive");
604
+ if (paxAwaitsEntry)
605
+ return invalid("pax header without an entry after it");
606
+ }
607
+ for (const problem of folderClashes(folded, folders)) {
608
+ if (problems.length < MAX_SOURCE_PROBLEMS)
609
+ problems.push(problem);
610
+ }
611
+ return problems.length === 0 ? { ok: true, files, assets } : { ok: false, problems };
612
+ }
613
+ /**
614
+ * Entries that no disk can hold together, after folding (TAG-407 security review, B1; directory
615
+ * entries since the re-review, B7): a file or directory inside a path that is a file
616
+ * (`src/a.ts` and `src/a.ts/b.ts`), and a file that is also a directory entry (`src/a.ts` and
617
+ * `src/A.ts/`). Extracting the package would fail or drop one of them.
618
+ */
619
+ function folderClashes(files, folders) {
620
+ const clashes = [];
621
+ for (const [key, stored] of files) {
622
+ const folder = folders.get(key);
623
+ if (folder !== undefined) {
624
+ clashes.push({
625
+ code: "SOURCE_INVALID",
626
+ message: `file ${quoted(stored)} is also a folder (${quoted(folder)})`,
627
+ });
628
+ }
629
+ }
630
+ for (const [key, stored] of [...files, ...folders]) {
631
+ for (let slash = key.indexOf("/", 1); slash !== -1; slash = key.indexOf("/", slash + 1)) {
632
+ const file = files.get(key.slice(0, slash));
633
+ if (file !== undefined) {
634
+ clashes.push({
635
+ code: "SOURCE_INVALID",
636
+ message: `path ${quoted(stored)} is inside ${quoted(file)}, which is a file`,
637
+ });
638
+ break;
639
+ }
640
+ }
641
+ }
642
+ return clashes;
643
+ }
644
+ //# sourceMappingURL=source-archive.js.map