uniwind-pro 1.3.0 → 1.5.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uniwind-pro",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "type": "module",
5
5
  "description": "Uniwind Pro CLI",
6
6
  "keywords": [
@@ -51,10 +51,10 @@
51
51
  },
52
52
  "peerDependenciesMeta": {
53
53
  "@expo/metro-config": {
54
- "optional": true
54
+ "optional": true
55
55
  },
56
56
  "metro-transform-worker": {
57
- "optional": true
57
+ "optional": true
58
58
  }
59
59
  }
60
60
  }
@@ -1,8 +1,10 @@
1
- import { mkdirSync, writeFileSync, readdirSync, cpSync, rmSync, existsSync } from 'node:fs'
1
+ import { mkdirSync, writeFileSync, readdirSync, cpSync, rmSync, existsSync, mkdtempSync } from 'node:fs'
2
2
  import { join } from 'node:path'
3
- import { homedir } from 'node:os'
3
+ import { homedir, tmpdir } from 'node:os'
4
+ import { x as extract } from 'tar'
5
+ import { hashFile } from './integrity.js'
4
6
 
5
- const CACHE_ROOT = join(homedir(), '.uniwind', 'cache', 'pro')
7
+ const CACHE_ROOT = process.env.UNIWIND_CACHE_DIR || join(homedir(), '.uniwind', 'cache', 'pro')
6
8
 
7
9
  const getVersionCacheRoot = (packageVersion) => {
8
10
  return join(CACHE_ROOT, packageVersion)
@@ -10,6 +12,8 @@ const getVersionCacheRoot = (packageVersion) => {
10
12
 
11
13
  const getCacheDir = (packageVersion) => getVersionCacheRoot(packageVersion)
12
14
 
15
+ const getTarballName = (packageVersion) => `uniwind-pro-${packageVersion}.tgz`
16
+
13
17
  export const copyDirectoryContents = (fromDir, toDir) => {
14
18
  for (const file of readdirSync(fromDir)) {
15
19
  cpSync(join(fromDir, file), join(toDir, file), { recursive: true })
@@ -38,30 +42,56 @@ export const enforceCachePolicy = (packageVersion) => {
38
42
  } catch {}
39
43
  }
40
44
 
41
- export const restoreFromCache = (packageVersion, targetDir) => {
45
+ // The cache stores the verified tarball (plus an informational .sha256
46
+ // sidecar), not extracted files, so every restore re-verifies the bytes
47
+ // against the checksum published on the CDN before extracting them.
48
+ export const saveToCache = (packageVersion, tarballPath, sha256Hex) => {
49
+ try {
50
+ const cacheDir = getCacheDir(packageVersion)
51
+ const tarballName = getTarballName(packageVersion)
52
+
53
+ rmSync(cacheDir, { recursive: true, force: true })
54
+ mkdirSync(cacheDir, { recursive: true })
55
+ cpSync(tarballPath, join(cacheDir, tarballName))
56
+ writeFileSync(
57
+ join(cacheDir, `${tarballName}.sha256`),
58
+ `${sha256Hex} ${tarballName}\n`,
59
+ 'utf8'
60
+ )
61
+ } catch {}
62
+ }
63
+
64
+ export const restoreFromCache = async (packageVersion, targetDir, expectedChecksum) => {
42
65
  const cacheDir = getCacheDir(packageVersion)
66
+ const tarballPath = join(cacheDir, getTarballName(packageVersion))
43
67
 
44
- if (!existsSync(cacheDir)) {
68
+ // pre-1.4.0 cache entries (extracted files, no tarball) also land here
69
+ // and get replaced by saveToCache after the fresh verified download
70
+ if (!existsSync(tarballPath)) {
45
71
  return false
46
72
  }
47
73
 
48
- const cacheFiles = readdirSync(cacheDir)
74
+ try {
75
+ const actualChecksum = await hashFile(tarballPath)
49
76
 
50
- if (cacheFiles.length === 0) {
51
- return false
52
- }
77
+ if (actualChecksum !== expectedChecksum) {
78
+ // stale or tampered entry: drop it and fall back to a fresh download
79
+ rmSync(cacheDir, { recursive: true, force: true })
53
80
 
54
- copyDirectoryContents(cacheDir, targetDir)
81
+ return false
82
+ }
55
83
 
56
- return true
57
- }
84
+ const extractDir = mkdtempSync(join(tmpdir(), 'uniwind-pro-'))
58
85
 
59
- export const saveToCache = (packageVersion, sourceDir) => {
60
- try {
61
- const cacheDir = getCacheDir(packageVersion)
86
+ try {
87
+ await extract({ file: tarballPath, cwd: extractDir, strip: 1 })
88
+ copyDirectoryContents(extractDir, targetDir)
89
+ } finally {
90
+ rmSync(extractDir, { recursive: true, force: true })
91
+ }
62
92
 
63
- rmSync(cacheDir, { recursive: true, force: true })
64
- mkdirSync(cacheDir, { recursive: true })
65
- copyDirectoryContents(sourceDir, cacheDir)
66
- } catch {}
93
+ return true
94
+ } catch {
95
+ return false
96
+ }
67
97
  }
@@ -61,6 +61,22 @@ const main = async () => {
61
61
  }
62
62
 
63
63
  main().catch((error) => {
64
+ // Integrity failure: the payload (or its published checksum) could not be
65
+ // verified. Deliberately fails closed - never install unverified bytes.
66
+ if (error.integrity) {
67
+ console.log(chalk.red.bold('Installation aborted: integrity verification failed.\n'))
68
+ console.log(chalk.red(`${error.message}\n`))
69
+ console.log(
70
+ chalk.yellow(
71
+ 'This can be caused by a corrupted download or by a tampered package.\n' +
72
+ 'No files were installed. Please retry, and if the problem persists,\n' +
73
+ `contact ${chalk.bold.underline(HELP_EMAIL)} immediately.\n`
74
+ )
75
+ )
76
+
77
+ process.exit(1)
78
+ }
79
+
64
80
  // Non-HTTP failure (filesystem, tar extraction, missing project, DNS/connection):
65
81
  // the "Access denied" guidance would be misleading, so show the stacktrace instead.
66
82
  if (!error.http) {
@@ -1,5 +1,5 @@
1
1
  import chalk from 'chalk'
2
- import { readFileSync, mkdirSync, readdirSync, rmSync, existsSync } from 'node:fs'
2
+ import { readFileSync, mkdirSync, readdirSync, rmSync, existsSync, createWriteStream } from 'node:fs'
3
3
  import { join, dirname } from 'node:path'
4
4
  import { fileURLToPath } from 'node:url'
5
5
  import { pipeline } from 'node:stream'
@@ -14,6 +14,7 @@ import {
14
14
  } from './cache.js'
15
15
  import { checkLatestVersion } from './checkLatestVersion.js'
16
16
  import { buildDownloadError } from './errors.js'
17
+ import { buildIntegrityError, fetchExpectedChecksum, hashFile } from './integrity.js'
17
18
 
18
19
  const __dirname = dirname(fileURLToPath(import.meta.url))
19
20
  const PKG_ROOT = join(__dirname, '../../')
@@ -122,7 +123,9 @@ export const downloadFromCI = async (licenseUUID) => {
122
123
 
123
124
  enforceCachePolicy(packageVersion)
124
125
 
125
- if (restoreFromCache(packageVersion, PKG_ROOT)) {
126
+ const expectedChecksum = await fetchExpectedChecksum(packageVersion)
127
+
128
+ if (await restoreFromCache(packageVersion, PKG_ROOT, expectedChecksum)) {
126
129
  console.log(chalk.gray(`Using cached v${packageVersion} package...\n`))
127
130
  await warnIfOutdated(versionCheck, packageVersion)
128
131
 
@@ -152,7 +155,7 @@ export const downloadFromCI = async (licenseUUID) => {
152
155
  throw await buildDownloadError(response, cdnUrl)
153
156
  }
154
157
 
155
- await install(response.body, packageVersion)
158
+ await install(response.body, packageVersion, expectedChecksum)
156
159
  await warnIfOutdated(versionCheck, packageVersion)
157
160
  }
158
161
 
@@ -163,7 +166,9 @@ export const downloadFromCLI = async ({ account: handle, password: token }) => {
163
166
 
164
167
  enforceCachePolicy(packageVersion)
165
168
 
166
- if (restoreFromCache(packageVersion, PKG_ROOT)) {
169
+ const expectedChecksum = await fetchExpectedChecksum(packageVersion)
170
+
171
+ if (await restoreFromCache(packageVersion, PKG_ROOT, expectedChecksum)) {
167
172
  console.log(chalk.gray(`Using cached v${packageVersion} for project "${projectName}".\n`))
168
173
  await warnIfOutdated(versionCheck, packageVersion)
169
174
 
@@ -176,7 +181,7 @@ export const downloadFromCLI = async ({ account: handle, password: token }) => {
176
181
  const response = await fetch(cdnUrl)
177
182
 
178
183
  if (response.ok) {
179
- await install(response.body, packageVersion)
184
+ await install(response.body, packageVersion, expectedChecksum)
180
185
  await warnIfOutdated(versionCheck, packageVersion)
181
186
 
182
187
  return
@@ -208,21 +213,42 @@ export const downloadFromCLI = async ({ account: handle, password: token }) => {
208
213
  throw await buildDownloadError(response, cdnUrl)
209
214
  }
210
215
 
211
- const install = async (stream, packageVersion) => {
216
+ // The payload is staged as a tarball and verified against the checksum
217
+ // published on the CDN before any byte of it is extracted or copied.
218
+ const install = async (stream, packageVersion, expectedChecksum) => {
212
219
  const TMP_DIR = join(PKG_ROOT, '_package')
213
220
 
214
- mkdirSync(TMP_DIR, { recursive: true })
221
+ try {
222
+ mkdirSync(TMP_DIR, { recursive: true })
223
+
224
+ const tarballPath = join(TMP_DIR, `uniwind-pro-${packageVersion}.tgz`)
225
+
226
+ await streamPipeline(stream, createWriteStream(tarballPath))
227
+
228
+ const actualChecksum = await hashFile(tarballPath)
215
229
 
216
- await streamPipeline(
217
- stream,
218
- extract({
219
- cwd: TMP_DIR,
230
+ if (actualChecksum !== expectedChecksum) {
231
+ throw buildIntegrityError({
232
+ version: packageVersion,
233
+ reason: 'the downloaded payload does not match the published checksum',
234
+ expected: expectedChecksum,
235
+ actual: actualChecksum,
236
+ })
237
+ }
238
+
239
+ const EXTRACT_DIR = join(TMP_DIR, 'extracted')
240
+
241
+ mkdirSync(EXTRACT_DIR, { recursive: true })
242
+
243
+ await extract({
244
+ file: tarballPath,
245
+ cwd: EXTRACT_DIR,
220
246
  strip: 1,
221
247
  })
222
- )
223
-
224
- copyDirectoryContents(TMP_DIR, PKG_ROOT)
225
- saveToCache(packageVersion, TMP_DIR)
226
248
 
227
- rmSync(TMP_DIR, { recursive: true, force: true })
249
+ copyDirectoryContents(EXTRACT_DIR, PKG_ROOT)
250
+ saveToCache(packageVersion, tarballPath, actualChecksum)
251
+ } finally {
252
+ rmSync(TMP_DIR, { recursive: true, force: true })
253
+ }
228
254
  }
@@ -0,0 +1,69 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { createReadStream } from 'node:fs'
3
+ import { INSTALLATION_URL } from '../consts.js'
4
+
5
+ const SHA256_HEX_REGEX = /^[a-f0-9]{64}$/
6
+
7
+ // First whitespace-delimited token of a sha256sum-style line
8
+ // ("<hex> uniwind-pro-<version>.tgz"), or a bare hex digest.
9
+ export const parseChecksumBody = (body) => {
10
+ const token = (body ?? '').trim().split(/\s+/).at(0)?.toLowerCase() ?? ''
11
+
12
+ return SHA256_HEX_REGEX.test(token) ? token : null
13
+ }
14
+
15
+ // Marks the error via `error.integrity` so the top-level handler can show
16
+ // security guidance instead of the generic access-denied / stacktrace output.
17
+ export const buildIntegrityError = ({ version, reason, expected, actual }) => {
18
+ const details = expected ? `\nExpected sha256: ${expected}\nActual sha256: ${actual}` : ''
19
+ const error = new Error(
20
+ `Integrity verification failed for Uniwind Pro v${version}: ${reason}.${details}`
21
+ )
22
+
23
+ error.integrity = { version, reason, expected, actual }
24
+
25
+ return error
26
+ }
27
+
28
+ // Verification is mandatory: a payload without a reachable, well-formed
29
+ // checksum must not install, otherwise a compromised CDN could bypass
30
+ // verification by stripping the checksum.
31
+ export const fetchExpectedChecksum = async (version) => {
32
+ const checksumUrl = `${INSTALLATION_URL}/checksums/${version}`
33
+ const response = await fetch(checksumUrl, { signal: AbortSignal.timeout(10_000) }).catch(
34
+ (error) => {
35
+ throw buildIntegrityError({
36
+ version,
37
+ reason: `could not reach the checksum endpoint (${error.cause?.code || error.message})`,
38
+ })
39
+ }
40
+ )
41
+
42
+ if (!response.ok) {
43
+ throw buildIntegrityError({
44
+ version,
45
+ reason: `checksum endpoint responded with ${response.status} ${response.statusText}`.trim(),
46
+ })
47
+ }
48
+
49
+ const checksum = parseChecksumBody(await response.text().catch(() => ''))
50
+
51
+ if (!checksum) {
52
+ throw buildIntegrityError({
53
+ version,
54
+ reason: 'checksum endpoint returned a malformed response',
55
+ })
56
+ }
57
+
58
+ return checksum
59
+ }
60
+
61
+ export const hashFile = (path) =>
62
+ new Promise((resolve, reject) => {
63
+ const hash = createHash('sha256')
64
+ const stream = createReadStream(path)
65
+
66
+ stream.on('error', reject)
67
+ stream.on('data', (chunk) => hash.update(chunk))
68
+ stream.on('end', () => resolve(hash.digest('hex')))
69
+ })