@preventive/triage 1.0.0-alpha.72 → 1.0.0-alpha.73
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/common/bundle-sources.d.ts +2 -0
- package/common/bundle-sources.js +19 -2
- package/common/finding-duplicates.js +5 -0
- package/common/finding-filters.js +45 -4
- package/common/line-diff.d.ts +2 -2
- package/common/line-diff.js +20 -8
- package/common/managed/finding-detail.js +1 -1
- package/common/managed/review-view-routes.js +23 -0
- package/common/managed/risk-routes.js +30 -0
- package/common/managed/routes.js +11 -6
- package/common/managed/team-app.d.ts +8 -6
- package/common/managed/team-app.js +15 -3
- package/common/stasis-files.d.ts +5 -0
- package/common/stasis-files.js +20 -0
- package/common/stasis-tgz.d.ts +6 -0
- package/common/stasis-tgz.js +27 -0
- package/common/workspace-app.js +20 -0
- package/out/activity.js +2 -4
- package/out/{chunk-2F3CYNPZ.js → chunk-2IHCVA3Y.js} +1 -1
- package/out/{chunk-HAZKDK4M.js → chunk-2V6Q4ZOZ.js} +1 -1
- package/out/{chunk-RRBIJL2B.js → chunk-4IW3VD3H.js} +1 -1
- package/out/chunk-4YZOXAXR.js +2 -0
- package/out/chunk-5NUDNLJA.js +6 -0
- package/out/chunk-5YP7P5KA.js +1 -0
- package/out/chunk-6VOB5WXX.js +2 -0
- package/out/chunk-A4BBBHQU.js +1 -0
- package/out/{chunk-4ISO5F7Y.js → chunk-ADMVRLNP.js} +1 -1
- package/out/chunk-BIWPZT4E.js +52 -0
- package/out/{chunk-HETCVNO4.js → chunk-BLPHP4U3.js} +1 -1
- package/out/{chunk-Y5ZHFNBD.js → chunk-BXQCCW7F.js} +1 -1
- package/out/{chunk-JFL7WLRV.js → chunk-CU3FPB3P.js} +1 -1
- package/out/chunk-CXVBTUK3.js +4 -0
- package/out/{chunk-M37X4LCF.js → chunk-EXQQ5VQJ.js} +1 -1
- package/out/{chunk-NXATZJQ2.js → chunk-GSNSP3EF.js} +1 -1
- package/out/chunk-GV5U7OLP.js +40 -0
- package/out/{chunk-JOXLZXMI.js → chunk-GX5UXWT4.js} +1 -1
- package/out/{chunk-YF4M5OCI.js → chunk-I6UQQKAH.js} +1 -1
- package/out/{chunk-UCP3JRE2.js → chunk-LVW7WWV4.js} +1 -1
- package/out/chunk-NEFCZAIY.js +11 -0
- package/out/{chunk-TOXMUOIW.js → chunk-OQS2USZM.js} +1 -1
- package/out/chunk-PX5DLFSD.js +1 -0
- package/out/chunk-QBFT2DDR.js +1 -0
- package/out/{chunk-WKFBZYJX.js → chunk-RGNF4OH6.js} +1 -1
- package/out/chunk-RLLA4GHZ.js +1 -0
- package/out/chunk-ROHACH7V.js +3 -0
- package/out/chunk-ROWUGPID.js +1 -0
- package/out/{chunk-QMVOEBWC.js → chunk-S2ZIKK45.js} +1 -1
- package/out/chunk-SSPDMA6D.js +1 -0
- package/out/chunk-TD7RVUTT.js +2 -0
- package/out/{chunk-4XIH7YTX.js → chunk-UH6NZBSU.js} +1 -1
- package/out/{chunk-M3Y6SLC4.js → chunk-UJP4PQJA.js} +1 -1
- package/out/{chunk-PAD2JHZT.js → chunk-UJSJMXEG.js} +1 -1
- package/out/{chunk-E3LFSUDJ.js → chunk-VYYUMYVW.js} +1 -1
- package/out/{chunk-VRX2APNA.js → chunk-XVKDPU4D.js} +1 -1
- package/out/chunk-Y2XGV3RC.js +8 -0
- package/out/chunk-YCEMQETS.js +255 -0
- package/out/client-managed-import.js +3 -3
- package/out/client-managed.js +30 -28
- package/out/compare.js +16 -23
- package/out/dialogs.js +38 -38
- package/out/graph.js +14 -14
- package/out/index.html +31 -51
- package/out/scan.js +2 -2
- package/out/terminal.js +239 -240
- package/out/view.css +1 -1
- package/out/view.js +15 -15
- package/package.json +20 -13
- package/risk/actions/resolve.js +47 -16
- package/risk/actions/uses.js +27 -9
- package/risk/actions/workflow.js +75 -2
- package/risk/files/hosts.js +4 -4
- package/risk/files/readability.js +5 -2
- package/risk/files/runs-code.js +12 -25
- package/risk/files/script.js +141 -19
- package/risk/index.d.ts +24 -7
- package/risk/index.js +1 -0
- package/risk/npm/assess.js +1 -1
- package/risk/npm/compare.js +5 -4
- package/risk/npm/diff.js +1 -1
- package/risk/npm/held.js +6 -2
- package/risk/npm/registry.js +5 -2
- package/risk/npm/tarball.js +79 -22
- package/server-managed/bundle-advisories.ts +12 -36
- package/server-managed/bundle-build-worker.js +29 -11
- package/server-managed/bundle-build.ts +4 -3
- package/server-managed/bundle-cache.ts +53 -13
- package/server-managed/bundle.ts +45 -13
- package/server-managed/comparison.ts +3 -3
- package/server-managed/db-methods.ts +54 -17
- package/server-managed/db-neon.ts +9 -2
- package/server-managed/db-schema.ts +12 -2
- package/server-managed/db.ts +4 -1
- package/server-managed/dependency-reviews.ts +8 -5
- package/server-managed/github-errors.ts +22 -0
- package/server-managed/github-requests.ts +4 -2
- package/server-managed/http.ts +231 -22
- package/server-managed/management.ts +10 -4
- package/server-managed/npm-delta.ts +1 -1
- package/server-managed/npm-packages.ts +30 -29
- package/server-managed/npm-patch.ts +9 -9
- package/server-managed/package-entry-points.ts +54 -6
- package/server-managed/rename-diff-worker.js +11 -7
- package/server-managed/rename-diff.ts +5 -4
- package/server-managed/report-opening.ts +77 -0
- package/server-managed/report-sources.ts +2 -1
- package/server-managed/repository-aliases.ts +34 -0
- package/server-managed/repository-browser.ts +87 -32
- package/server-managed/repository-code.ts +6 -3
- package/server-managed/repository-dependency-reviews.ts +21 -0
- package/server-managed/repository-risk-read.ts +50 -4
- package/server-managed/repository-risk.ts +9 -2
- package/server-managed/stasis-config.ts +97 -5
- package/server-managed/team-reports.ts +5 -3
- package/server-managed/type-only-proof.js +26 -4
- package/server-managed/upstream-cache.ts +18 -12
- package/server-managed/workspace-shares.ts +4 -4
- package/out/chunk-27Y75UQR.js +0 -4
- package/out/chunk-4RS3NDOG.js +0 -1
- package/out/chunk-4XSH6ZLT.js +0 -1
- package/out/chunk-7CHIFYQW.js +0 -42
- package/out/chunk-CZ3U2LI4.js +0 -2
- package/out/chunk-ETL5ZPPR.js +0 -259
- package/out/chunk-L6VIWL2Y.js +0 -1
- package/out/chunk-OSHR3N4S.js +0 -3
- package/out/chunk-UD33S2MG.js +0 -52
- package/out/chunk-VG5RH6KN.js +0 -2
- package/out/chunk-WFDXHPT2.js +0 -12
- package/out/chunk-WMFDQS2L.js +0 -11
- package/out/chunk-WREU7JAG.js +0 -1
- /package/out/{chunk-4LZK6C6N.js → chunk-74JZTGTO.js} +0 -0
- /package/out/{chunk-JMG5DXII.js → chunk-7HP2VYIC.js} +0 -0
- /package/out/{chunk-HOB3YPS5.js → chunk-K4XJHM2T.js} +0 -0
- /package/out/{chunk-4EMM6UCD.js → chunk-QI66YA4F.js} +0 -0
- /package/out/{chunk-G3LY2FV4.js → chunk-WCDYSIKG.js} +0 -0
|
@@ -5,14 +5,14 @@ import { mkdir, readFile, readdir, rename, rm, stat, statfs, writeFile } from 'n
|
|
|
5
5
|
import { join, posix } from 'node:path'
|
|
6
6
|
import { brotliCompressSync } from 'node:zlib'
|
|
7
7
|
import { isEvidenceFile } from '@exodus/stasis/audit-corrections'
|
|
8
|
-
import { buildGitHubBundle, setCacheDir } from '@exodus/stasis/vfs-bundle'
|
|
8
|
+
import { buildGitHubBundle, bundleFileBytes, setCacheDir } from '@exodus/stasis/vfs-bundle'
|
|
9
9
|
import { brotliOptions } from '@exodus/stasis-core/brotli'
|
|
10
10
|
import { LOCK_FILE, dependencyEcosystem, integrityHex } from '@exodus/stasis-core/util'
|
|
11
11
|
import { HttpError, createClient } from '@preventive/upstream/github.js'
|
|
12
12
|
import { githubBundleFilename } from './bundle-build.ts'
|
|
13
13
|
import { isDirectoryPackage } from '../common/managed/directory-packages.ts'
|
|
14
14
|
import { bundleBuildDiagnostic } from './bundle-build-diagnostics.js'
|
|
15
|
-
import { MAX_STASIS_CONFIG_BYTES, STASIS_CONFIG, namesLockfiles, parseStasisConfig } from './stasis-config.ts'
|
|
15
|
+
import { MAX_STASIS_CONFIG_BYTES, STASIS_CONFIG, namesEntry, namesLockfiles, parseStasisConfig } from './stasis-config.ts'
|
|
16
16
|
|
|
17
17
|
// A bundle's JSON, before compression, at most: a project producing it is
|
|
18
18
|
// what a build's heap is sized for (bundle-build.ts buildHeapLimitMib).
|
|
@@ -64,6 +64,17 @@ async function directoryConfig(configText, directory) {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
// Whether the stasis.config.json in `directory`, whose entries are `config`,
|
|
68
|
+
// names `entry`: by its path, or by a pattern matching it. One naming it twice
|
|
69
|
+
// Stasis refuses, config-invalid.
|
|
70
|
+
function namesSelected(config, directory, entry) {
|
|
71
|
+
try {
|
|
72
|
+
return config !== null && namesEntry(config, posix.relative(`/${directory}`, `/${entry}`))
|
|
73
|
+
} catch (error) {
|
|
74
|
+
throw new Error('config-invalid', { cause: error })
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
67
78
|
// The directory of the stasis.config.json listing the selected entries: for
|
|
68
79
|
// each entry, the nearest one at or above it whose `entries` names it. null
|
|
69
80
|
// where none lists any. Stasis reads the one in the directory it builds from
|
|
@@ -72,8 +83,7 @@ async function directoryConfig(configText, directory) {
|
|
|
72
83
|
async function listingConfig(input, configText) {
|
|
73
84
|
const found = await Promise.all(input.entries.map(async entry => {
|
|
74
85
|
for (let directory = parentOf(entry); ; directory = parentOf(directory)) {
|
|
75
|
-
|
|
76
|
-
if (config?.has(posix.relative(`/${directory}`, `/${entry}`))) return { entry, directory }
|
|
86
|
+
if (namesSelected(await directoryConfig(configText, directory), directory, entry)) return { entry, directory }
|
|
77
87
|
if (!directory) return { entry, directory: null }
|
|
78
88
|
}
|
|
79
89
|
}))
|
|
@@ -158,8 +168,11 @@ export async function buildStasisBundle({ input, github, token, maxBytes, scopes
|
|
|
158
168
|
const named = packageManager !== undefined && !namesLockfiles(await configText(project) ?? '{}')
|
|
159
169
|
note('project', { project, configured: configured !== null, ...(named && { packageManager }) })
|
|
160
170
|
// The branch the commit was picked by, where it was, Stasis asks first of
|
|
161
|
-
// whether the commit is the repo's own (bundle-build.ts).
|
|
162
|
-
|
|
171
|
+
// whether the commit is the repo's own (bundle-build.ts). It tells the
|
|
172
|
+
// format `stasis github-bundle` writes the bundle in, by the
|
|
173
|
+
// stasis.config.json it builds from: a stasis.code.br but where that names
|
|
174
|
+
// tgz, a .stasis.tgz.
|
|
175
|
+
const { bundle, format } = await buildGitHubBundle({
|
|
163
176
|
github, sha: input.commit, ...(input.branch !== undefined && { branch: input.branch }), directory: project || undefined,
|
|
164
177
|
entries: input.entries.map(entry => posix.relative(project || '.', entry)),
|
|
165
178
|
...options, ...(named && { packageManager }), client: buildClient, ...await cacheOf(cacheDir, instanceDir),
|
|
@@ -171,7 +184,8 @@ export async function buildStasisBundle({ input, github, token, maxBytes, scopes
|
|
|
171
184
|
if (!allowed(directory)) throw new Error('build-scope')
|
|
172
185
|
progress('serialize')
|
|
173
186
|
// Its JSON as bytes, a module at a time, never one string on the heap; a
|
|
174
|
-
// module too large for V8 to hold as one is too large too.
|
|
187
|
+
// module too large for V8 to hold as one is too large too. A .stasis.tgz's
|
|
188
|
+
// too: its contents are served as this JSON (bundle-cache.ts).
|
|
175
189
|
let serialized
|
|
176
190
|
try { serialized = bundle.serializedBytes() } catch (error) {
|
|
177
191
|
// A failure's code, as buildErrorCode reads it, not a value of the wrong type.
|
|
@@ -181,10 +195,11 @@ export async function buildStasisBundle({ input, github, token, maxBytes, scopes
|
|
|
181
195
|
}
|
|
182
196
|
if (serialized.length > MAX_SERIALIZED_BYTES) throw new Error('too-large')
|
|
183
197
|
progress('compress')
|
|
184
|
-
|
|
198
|
+
// As Stasis writes the bundle's file in its format (bundleFileBytes).
|
|
199
|
+
const bytes = format === 'tgz' ? await bundleFileBytes(bundle, format) : brotliCompressSync(serialized, brotliOptions())
|
|
185
200
|
if (bytes.length > maxBytes) throw new Error('too-large')
|
|
186
201
|
const name = await packageName(input, github, project, bundle, buildClient)
|
|
187
|
-
return { bytes, directory, filename: githubBundleFilename(github, project, input.commit, name) }
|
|
202
|
+
return { bytes, directory, filename: githubBundleFilename(github, project, input.commit, name, format) }
|
|
188
203
|
}
|
|
189
204
|
|
|
190
205
|
// The npm package versions a Stasis lockfile holds code of (an evidence
|
|
@@ -286,7 +301,10 @@ export async function buildStasisLockfile({ input, github, token, cacheDir = nul
|
|
|
286
301
|
// or its excludeModules excludes (an entry in such a package among them), the
|
|
287
302
|
// config's or an entry's, is the policy's, as is one broken at load by
|
|
288
303
|
// imports excludeModules excludes alone; one it can't read, build with
|
|
289
|
-
// its options, or lay out the lockfiles it names of, is the config's
|
|
304
|
+
// its options, or lay out the lockfiles it names of, is the config's, as is
|
|
305
|
+
// one whose scope, lock, bundle, format or fs Stasis's config refuses (its
|
|
306
|
+
// `Invalid <option>: <value>`, naming no file: a server build sets none of
|
|
307
|
+
// them but by its stasis.config.json). A build
|
|
290
308
|
// the repository's stasis.lock.json disagrees with, or that its lock mode
|
|
291
309
|
// holds to one that isn't there, is the stasis.lock.json's. A commit Stasis
|
|
292
310
|
// finds in the history of none of the repo's branches or tags (a fork's,
|
|
@@ -299,7 +317,7 @@ export function buildErrorCode(error, message) {
|
|
|
299
317
|
if (message.endsWith(': bundle either the entries it names or others, not both')) return 'config-mixed'
|
|
300
318
|
if (/\bstasis\.config\.json(?:: entries .*?)?: (?:(?:allowed|disallowed)(?:Paths|Modules) (?:refuses|does not allow|allows none of)|excludeModules excludes) /u.test(message)) return 'config-policy'
|
|
301
319
|
if (brokenByExclusion(message)) return 'config-policy'
|
|
302
|
-
if (/\bstasis\.config\.json(?: is not valid JSON|: |'s lockfiles )/u.test(message)) return 'config-invalid'
|
|
320
|
+
if (/\bstasis\.config\.json(?: is not valid JSON|: |'s lockfiles )|^Invalid (?:scope|lock|bundle|format|fs): /u.test(message)) return 'config-invalid'
|
|
303
321
|
if (/\bstasis\.lock\.json(?::| isn't there,| \(lock = )/u.test(message)) return 'stasis-lock'
|
|
304
322
|
if (/ is in the history of no branch or tag of | is at no tag or branch head of .+ nor in its default branch's history/u.test(message)) return 'commit-not-own'
|
|
305
323
|
return /no packageManager given|lockfile installs|no (?:pnpm-lock\.yaml|yarn\.lock|package-lock\.json|soldeer\.lock) found in /u.test(message) ? 'build-lockfile' : 'build-failed'
|
|
@@ -105,10 +105,11 @@ function packageBase(name: string): string | null {
|
|
|
105
105
|
|
|
106
106
|
// Same portable name and 255-character limit as Stasis's githubBundleFile
|
|
107
107
|
// (src/cmd/github-bundle.js), which is not a public package export, except
|
|
108
|
-
// that a usable `packageName` replaces both the repo and the directory
|
|
109
|
-
|
|
108
|
+
// that a usable `packageName` replaces both the repo and the directory; a
|
|
109
|
+
// .stasis.tgz's (`format` tgz) ends in `.stasis.tgz`, as Stasis's does.
|
|
110
|
+
export function githubBundleFilename(github: string, directory: string, commit: string, packageName: string | null = null, format: 'br' | 'tgz' = 'br'): string {
|
|
110
111
|
const portable = (value: string) => value.replaceAll(/[^\w.-]/gu, '_')
|
|
111
|
-
const tail = `.${portable(commit.slice(0, 7))}.stasis.code.br`
|
|
112
|
+
const tail = `.${portable(commit.slice(0, 7))}.${format === 'tgz' ? 'stasis.tgz' : 'stasis.code.br'}`
|
|
112
113
|
const named = packageName === null ? null : packageBase(packageName)
|
|
113
114
|
if (named !== null) return `${portable(named)}${tail}`
|
|
114
115
|
const repo = portable(github.replace('/', '-'))
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Metadata is cached as Brotli. Contents use the stored Brotli bytes directly:
|
|
2
|
-
// unchanged Stasis uploads or sourcemaps compressed once at upload.
|
|
2
|
+
// unchanged Stasis uploads or sourcemaps compressed once at upload. A
|
|
3
|
+
// .stasis.tgz's are its bundle's JSON, made from it once and cached as Brotli.
|
|
3
4
|
import { Buffer } from 'node:buffer'
|
|
4
5
|
import { brotliDecompress } from 'node:zlib'
|
|
5
6
|
import { promisify } from 'node:util'
|
|
@@ -9,6 +10,8 @@ import { bundleReasons, bundleScopes, scopeLabel } from '../common/bundle-reason
|
|
|
9
10
|
import { sourcemapEdges } from '../common/bundle-sourcemap.js'
|
|
10
11
|
import { type BundleAdvisoryInventory, bundleAdvisoryInventory } from './bundle-advisory-inventory.ts'
|
|
11
12
|
import { decodeUtf8 } from '../common/utf8.js'
|
|
13
|
+
import { isStasisTgzName } from '../common/stasis-files.js'
|
|
14
|
+
import { readStasisTgz } from '../common/stasis-tgz.js'
|
|
12
15
|
import { type OpenedBlob, readBytes } from './blob-store.ts'
|
|
13
16
|
import type { BundleStore } from './bundle-store.ts'
|
|
14
17
|
import { encodeBrotli } from './brotli.ts'
|
|
@@ -28,13 +31,17 @@ export async function readBundleDetails(record: BundleCacheRecord, store: Bundle
|
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
// As readBundleDetails, with the bytes the bundle decoded to: about what
|
|
31
|
-
// holding its details takes.
|
|
34
|
+
// holding its details takes. A Stasis bundle's name tells a .stasis.tgz's.
|
|
32
35
|
export async function readSizedBundleDetails(record: BundleCacheRecord, store: BundleStore) {
|
|
33
36
|
const bytes = await store.get(record.id, record.kind)
|
|
34
37
|
if (!bytes) return null
|
|
38
|
+
if (record.kind === 'stasis' && isStasisTgzName(record.filename)) {
|
|
39
|
+
const { bundle, size } = await readStasisTgz(bytes, { limit: MAX_DECODED_BYTES })
|
|
40
|
+
return { details: { integrity: record.integrity, kind: record.kind, size: record.byteSize, bundle } as BundleDetails, decodedBytes: size, tgz: true }
|
|
41
|
+
}
|
|
35
42
|
const decoded = record.kind === 'stasis' || record.kind === 'sourcemap' ? await decompress(bytes, { maxOutputLength: MAX_DECODED_BYTES }) : bytes
|
|
36
43
|
if (decoded.length > MAX_DECODED_BYTES) throw new Error('Decoded bundle too large')
|
|
37
|
-
return { details: parseBundleContents(decodeUtf8(decoded), { integrity: record.integrity, kind: record.kind, size: record.byteSize }), decodedBytes: decoded.length }
|
|
44
|
+
return { details: parseBundleContents(decodeUtf8(decoded), { integrity: record.integrity, kind: record.kind, size: record.byteSize }), decodedBytes: decoded.length, tgz: false }
|
|
38
45
|
}
|
|
39
46
|
|
|
40
47
|
export interface BundleCacheStorage {
|
|
@@ -46,6 +53,8 @@ export interface BundleCacheStorage {
|
|
|
46
53
|
}
|
|
47
54
|
|
|
48
55
|
const filename = `v${BUNDLE_METADATA_VERSION}-metadata.json.br`
|
|
56
|
+
// A .stasis.tgz's bundle as a stasis.code.br holds it: its JSON, as Brotli.
|
|
57
|
+
const contentsFilename = 'v1-contents.json.br'
|
|
49
58
|
const packagesFilename = 'v8-advisory-inventory.json'
|
|
50
59
|
|
|
51
60
|
// An entry point's inventory is rows of the bundle's own, a package's versions
|
|
@@ -123,13 +132,24 @@ function readSourcemapEdges(details: BundleDetails) {
|
|
|
123
132
|
|
|
124
133
|
// As readBundleDetails, thrown where the bundle's bytes are gone.
|
|
125
134
|
async function storedDetails(record: BundleCacheRecord, store: BundleStore) {
|
|
126
|
-
const
|
|
127
|
-
if (!
|
|
128
|
-
return
|
|
135
|
+
const read = await readSizedBundleDetails(record, store)
|
|
136
|
+
if (!read) throw new Error('Bundle bytes unavailable')
|
|
137
|
+
return read
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// A .stasis.tgz's contents (contentsFilename), of its bundle as read.
|
|
141
|
+
const encodeContents = (details: BundleDetails) => encodeBrotli((details.bundle as { serializedBytes(): Uint8Array }).serializedBytes())
|
|
142
|
+
|
|
143
|
+
// What a build wrote is removed where another instance deleted the row
|
|
144
|
+
// meanwhile, so its cleanup cannot be undone by us.
|
|
145
|
+
async function reconcile(record: BundleCacheRecord, storage: BundleCacheStorage, db: ManagedDb) {
|
|
146
|
+
if (await db.getBundle(record.id)) return
|
|
147
|
+
await storage.delete(record.id)
|
|
148
|
+
throw new Error('Bundle deleted')
|
|
129
149
|
}
|
|
130
150
|
|
|
131
151
|
async function build(record: BundleCacheRecord, storage: BundleCacheStorage, db: ManagedDb, store: BundleStore) {
|
|
132
|
-
const details = await storedDetails(record, store)
|
|
152
|
+
const { details, tgz } = await storedDetails(record, store)
|
|
133
153
|
readSourcemapEdges(details)
|
|
134
154
|
const metadata = { ...await createBundleMetadata(details), id: record.id, filename: record.filename }
|
|
135
155
|
const body = await encodeBrotli(Buffer.from(JSON.stringify(metadata)))
|
|
@@ -140,14 +160,12 @@ async function build(record: BundleCacheRecord, storage: BundleCacheStorage, db:
|
|
|
140
160
|
const written = await Promise.allSettled([
|
|
141
161
|
storage.put(record.id, filename, body),
|
|
142
162
|
...record.kind === 'stasis' ? [storage.put(record.id, packagesFilename, encodePackageInventory(details))] : [],
|
|
163
|
+
...tgz ? [encodeContents(details).then(contents => storage.put(record.id, contentsFilename, contents))] : [],
|
|
143
164
|
storage.put(record.id, SUMMARY_FILENAME, Buffer.from(JSON.stringify(summary))),
|
|
144
165
|
])
|
|
145
166
|
// A different instance may have deleted the row while these writes ran.
|
|
146
167
|
// Reconcile after publishing so its cleanup cannot be undone by us.
|
|
147
|
-
|
|
148
|
-
await storage.delete(record.id)
|
|
149
|
-
throw new Error('Bundle deleted')
|
|
150
|
-
}
|
|
168
|
+
await reconcile(record, storage, db)
|
|
151
169
|
const failed = written.find(result => result.status === 'rejected')
|
|
152
170
|
if (failed) throw failed.reason
|
|
153
171
|
return summary
|
|
@@ -157,7 +175,7 @@ export function createBundleCache(storage: BundleCacheStorage, db: ManagedDb, st
|
|
|
157
175
|
const pending = new Map<string, Promise<void>>()
|
|
158
176
|
// The database deduplicates by integrity, so each hash has one persistent
|
|
159
177
|
// bundle/cache directory, shared across teams and repeated uploads.
|
|
160
|
-
const summaries = createBundleSummaryCache(storage, async record => bundleSummary(await storedDetails(record, store)), id => db.getBundle(id))
|
|
178
|
+
const summaries = createBundleSummaryCache(storage, async record => bundleSummary((await storedDetails(record, store)).details), id => db.getBundle(id))
|
|
161
179
|
let queue = Promise.resolve()
|
|
162
180
|
async function ensure(record: BundleCacheRecord): Promise<void> {
|
|
163
181
|
const existing = pending.get(record.id)
|
|
@@ -171,6 +189,26 @@ export function createBundleCache(storage: BundleCacheStorage, db: ManagedDb, st
|
|
|
171
189
|
pending.set(record.id, job)
|
|
172
190
|
try { await job } finally { if (pending.get(record.id) === job) pending.delete(record.id) }
|
|
173
191
|
}
|
|
192
|
+
// A .stasis.tgz's contents as cached, else made from it, as a build of
|
|
193
|
+
// the rest makes them beside its metadata, each made once at a time.
|
|
194
|
+
async function openContents(record: BundleCacheRecord) {
|
|
195
|
+
try { return await storage.open(record.id, contentsFilename) }
|
|
196
|
+
catch (error) { if (!(error instanceof CacheMissError)) throw error }
|
|
197
|
+
const key = `${record.id}\0contents`
|
|
198
|
+
let job = pending.get(key)
|
|
199
|
+
if (!job) {
|
|
200
|
+
job = (async () => {
|
|
201
|
+
const body = await encodeContents((await storedDetails(record, store)).details)
|
|
202
|
+
if (!(await db.getBundle(record.id))) throw new Error('Bundle deleted')
|
|
203
|
+
await storage.put(record.id, contentsFilename, body)
|
|
204
|
+
await reconcile(record, storage, db)
|
|
205
|
+
})()
|
|
206
|
+
pending.set(key, job)
|
|
207
|
+
job.finally(() => { if (pending.get(key) === job) pending.delete(key) }).catch(() => {})
|
|
208
|
+
}
|
|
209
|
+
await job
|
|
210
|
+
return storage.open(record.id, contentsFilename)
|
|
211
|
+
}
|
|
174
212
|
async function openCached(record: BundleCacheRecord, file: string) {
|
|
175
213
|
try { return await storage.open(record.id, file) }
|
|
176
214
|
catch (error) { if (!(error instanceof CacheMissError)) throw error }
|
|
@@ -185,6 +223,8 @@ export function createBundleCache(storage: BundleCacheStorage, db: ManagedDb, st
|
|
|
185
223
|
async open(record: ManagedBundle, part: BundleCachePart) {
|
|
186
224
|
if (part === 'contents') {
|
|
187
225
|
if (record.kind !== 'stasis' && record.kind !== 'sourcemap') throw new Error('Unsupported bundle')
|
|
226
|
+
// A .stasis.tgz's: its bundle's JSON in its place.
|
|
227
|
+
if (record.kind === 'stasis' && isStasisTgzName(record.filename)) return openContents(record)
|
|
188
228
|
const stored = await store.open(record.id, record.kind)
|
|
189
229
|
if (!stored) throw new Error('Bundle bytes unavailable')
|
|
190
230
|
return stored
|
|
@@ -212,7 +252,7 @@ export function createBundleCache(storage: BundleCacheStorage, db: ManagedDb, st
|
|
|
212
252
|
async delete(id: string) {
|
|
213
253
|
// Call after deleting the row. Waiting prevents an in-flight builder
|
|
214
254
|
// from recreating its files after deletion has completed.
|
|
215
|
-
await pending.get(
|
|
255
|
+
await Promise.all([id, `${id}\0contents`].map(key => pending.get(key)?.catch(() => {})))
|
|
216
256
|
await summaries.forget(id)
|
|
217
257
|
await storage.delete(id)
|
|
218
258
|
},
|
package/server-managed/bundle.ts
CHANGED
|
@@ -8,9 +8,12 @@
|
|
|
8
8
|
import { createHash } from 'node:crypto'
|
|
9
9
|
import type { Buffer } from 'node:buffer'
|
|
10
10
|
import { StringDecoder } from 'node:string_decoder'
|
|
11
|
-
import { brotliDecompress, createBrotliDecompress } from 'node:zlib'
|
|
11
|
+
import { brotliDecompress, createBrotliDecompress, createGunzip } from 'node:zlib'
|
|
12
12
|
import { promisify } from 'node:util'
|
|
13
|
+
import { unpackStream } from '@preventive/archive/tar.js'
|
|
13
14
|
import { parseBundleContents } from '../common/bundle-metadata.js'
|
|
15
|
+
import { isStasisBundleName, isStasisTgzName } from '../common/stasis-files.js'
|
|
16
|
+
import { parseStasisTgz } from '../common/stasis-tgz.js'
|
|
14
17
|
import { bundleFilesAsMap } from '../common/bundle-sources.js'
|
|
15
18
|
import { commonFileDirectory } from '../common/managed/repository-alias.ts'
|
|
16
19
|
import { decodeUtf8 } from '../common/utf8.js'
|
|
@@ -25,13 +28,15 @@ export function bundleIntegrity(bytes: Buffer): string {
|
|
|
25
28
|
}
|
|
26
29
|
|
|
27
30
|
// Classify an uploaded bundle by filename — mirrors ui/view/ingest.js
|
|
28
|
-
// `bundleKind`: sourcemap (.map) or stasis (stasis.code.br / .stasis.code.br
|
|
29
|
-
// null for anything else (stored anyway; the kind
|
|
31
|
+
// `bundleKind`: sourcemap (.map) or stasis (stasis.code.br / .stasis.code.br,
|
|
32
|
+
// stasis.tgz / .stasis.tgz). null for anything else (stored anyway; the kind
|
|
33
|
+
// is informational). A stasis bundle's format is told by its name, as Stasis
|
|
34
|
+
// tells it (isStasisTgzName).
|
|
30
35
|
export function bundleKind(filename: string): 'sourcemap' | 'stasis' | null {
|
|
31
36
|
// Browsers insert a duplicate-download counter before the final extension.
|
|
32
37
|
const lower = filename.toLowerCase().replace(/ \(\d+\)(?=\.[^.]*$)/u, '')
|
|
33
38
|
if (lower.endsWith('.map')) return 'sourcemap'
|
|
34
|
-
if (lower
|
|
39
|
+
if (isStasisBundleName(lower)) return 'stasis'
|
|
35
40
|
return null
|
|
36
41
|
}
|
|
37
42
|
|
|
@@ -64,10 +69,7 @@ function headerRepo(text: string): BundleRepo | null | undefined {
|
|
|
64
69
|
depth++
|
|
65
70
|
if (depth === 1) expectKey = true
|
|
66
71
|
} else if (depth === 1 && (c === ',' || c === '}')) {
|
|
67
|
-
if (key === 'repo')
|
|
68
|
-
const repo: unknown = JSON.parse(text.slice(valueStart, i))
|
|
69
|
-
return repo != null && typeof repo === 'object' && !Array.isArray(repo) ? repo as BundleRepo : null
|
|
70
|
-
}
|
|
72
|
+
if (key === 'repo') return plainRepo(JSON.parse(text.slice(valueStart, i)))
|
|
71
73
|
if (c === '}') return null
|
|
72
74
|
expectKey = true
|
|
73
75
|
} else if (c === '}' || c === ']') depth--
|
|
@@ -77,9 +79,38 @@ function headerRepo(text: string): BundleRepo | null | undefined {
|
|
|
77
79
|
return undefined
|
|
78
80
|
}
|
|
79
81
|
|
|
82
|
+
const plainRepo = (repo: unknown) => repo != null && typeof repo === 'object' && !Array.isArray(repo) ? repo as BundleRepo : null
|
|
83
|
+
|
|
84
|
+
// A .stasis.tgz's repo, as its stasis.lock.json records it: the first entry
|
|
85
|
+
// of its tar, as Stasis requires one (Bundle.fromTar refuses a tar it isn't
|
|
86
|
+
// first in), read alone, its gunzipped bytes bounded as the lockfile's own are.
|
|
87
|
+
const MAX_TGZ_LOCKFILE_BYTES = 64 * 1024 * 1024
|
|
88
|
+
async function tgzRepo(bytes: Buffer): Promise<BundleRepo | null> {
|
|
89
|
+
const stream = createGunzip({ chunkSize: 64 * 1024 })
|
|
90
|
+
stream.end(bytes)
|
|
91
|
+
async function* bounded() {
|
|
92
|
+
let size = 0
|
|
93
|
+
for await (const chunk of stream) {
|
|
94
|
+
size += (chunk as Buffer).length
|
|
95
|
+
if (size > MAX_TGZ_LOCKFILE_BYTES) throw new Error('Lockfile too large')
|
|
96
|
+
yield chunk as Buffer
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
for await (const entry of unpackStream(bounded())) {
|
|
101
|
+
if (entry.name !== 'stasis.lock.json') return null
|
|
102
|
+
return plainRepo((JSON.parse(decodeUtf8(entry.data)) as { repo?: unknown } | null)?.repo)
|
|
103
|
+
}
|
|
104
|
+
return null
|
|
105
|
+
} catch { return null }
|
|
106
|
+
finally { stream.destroy() }
|
|
107
|
+
}
|
|
108
|
+
|
|
80
109
|
// Decode only a bounded prefix, stopping as soon as repo is read or the
|
|
81
|
-
// header ends.
|
|
82
|
-
|
|
110
|
+
// header ends: a stasis.code.br's, but where `filename` names a .stasis.tgz,
|
|
111
|
+
// its stasis.lock.json (tgzRepo). Unknown or malformed archives remain uploadable as opaque bytes.
|
|
112
|
+
export async function bundleRepo(bytes: Buffer, filename = ''): Promise<BundleRepo | null> {
|
|
113
|
+
if (isStasisTgzName(filename)) return tgzRepo(bytes)
|
|
83
114
|
const stream = createBrotliDecompress({ chunkSize: 4096 })
|
|
84
115
|
const decoder = new StringDecoder('utf8')
|
|
85
116
|
let size = 0, text = ''
|
|
@@ -100,10 +131,11 @@ export async function bundleRepo(bytes: Buffer): Promise<BundleRepo | null> {
|
|
|
100
131
|
const decompress = promisify(brotliDecompress)
|
|
101
132
|
// Same decoded size bound as the bundle metadata cache; only reached when an
|
|
102
133
|
// alias can use a shared file suffix. Invalid/opaque bundles keep header defaults.
|
|
103
|
-
export async function bundleFilePrefix(bytes: Buffer): Promise<string> {
|
|
134
|
+
export async function bundleFilePrefix(bytes: Buffer, filename = ''): Promise<string> {
|
|
104
135
|
try {
|
|
105
|
-
const
|
|
106
|
-
const details =
|
|
136
|
+
const identity = { integrity: '', kind: 'stasis', size: bytes.length }
|
|
137
|
+
const details = isStasisTgzName(filename) ? { ...identity, bundle: await parseStasisTgz(bytes) }
|
|
138
|
+
: parseBundleContents(decodeUtf8(await decompress(bytes, { maxOutputLength: 512 * 1024 * 1024 })), identity)
|
|
107
139
|
return commonFileDirectory(bundleFilesAsMap(details).keys())
|
|
108
140
|
} catch { return '' }
|
|
109
141
|
}
|
|
@@ -23,13 +23,13 @@ import { opened } from './pretty-print.ts'
|
|
|
23
23
|
export const emptySide = (): BundleDetails => ({ integrity: '(empty)', kind: 'sourcemap', size: 0, json: { version: 3, sources: [], sourcesContent: [] } })
|
|
24
24
|
|
|
25
25
|
// A version's files as the viewer reads them for a comparison (ui/view/npm-package.js
|
|
26
|
-
// npmPackageDetails): its texts, and those that are not text by their size and digest.
|
|
27
|
-
export function npmComparedSide(integrity: string, files: NpmPackageFile[]): BundleDetails & { npmBinaries: Map<string, { size: number; digest: string }> } {
|
|
26
|
+
// npmPackageDetails): its texts, and those that are not text by their size and digest, a symlink's target too.
|
|
27
|
+
export function npmComparedSide(integrity: string, files: NpmPackageFile[]): BundleDetails & { npmBinaries: Map<string, { size: number; digest: string; link?: string }> } {
|
|
28
28
|
const rows = npmFileRows(files)
|
|
29
29
|
return {
|
|
30
30
|
integrity, kind: 'sourcemap', size: 0,
|
|
31
31
|
json: { version: 3, sources: rows.map(([path]) => path), sourcesContent: rows.map(([, , text]) => text) },
|
|
32
|
-
npmBinaries: new Map(rows.flatMap(([path, size, text, digest]) => (text === null ? [[path, { size, digest: digest
|
|
32
|
+
npmBinaries: new Map(rows.flatMap(([path, size, text, digest, link]) => (text === null ? [[path, { size, digest: digest!, ...link === undefined ? {} : { link } }] as const] : []))),
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
@@ -132,6 +132,10 @@ export interface ReportRecord {
|
|
|
132
132
|
analyzer: string | null
|
|
133
133
|
visible: boolean
|
|
134
134
|
bundleId: string | null
|
|
135
|
+
// Whether it may be switched to open on Confirmed (null: not read yet), and
|
|
136
|
+
// whether it is (report-opening.ts).
|
|
137
|
+
confirmedOffered: boolean | null
|
|
138
|
+
confirmedDefault: boolean
|
|
135
139
|
}
|
|
136
140
|
|
|
137
141
|
// One transaction snapshots all metadata needed to authorize, filter and
|
|
@@ -143,6 +147,8 @@ export interface ReportAccessRecord {
|
|
|
143
147
|
sha256: string
|
|
144
148
|
repo: { github: string | null; directory: string }
|
|
145
149
|
permissions: TeamUserPermissions
|
|
150
|
+
// Set where a team snapshot reads a report switched to open on Confirmed.
|
|
151
|
+
confirmedDefault?: true
|
|
146
152
|
}
|
|
147
153
|
export interface TeamReportAccessSnapshot extends ReportAccessSnapshot {
|
|
148
154
|
teamId: string | null
|
|
@@ -179,6 +185,8 @@ export interface ReportRecordInput {
|
|
|
179
185
|
visible: boolean
|
|
180
186
|
bundleId: string | null
|
|
181
187
|
bundleIntegrity: string | null
|
|
188
|
+
// Read of its content as it is uploaded (report-opening.ts); none: not yet.
|
|
189
|
+
confirmedOffered?: boolean | null
|
|
182
190
|
}
|
|
183
191
|
|
|
184
192
|
// A report row for the "Manage reports" list — adds display joins: uploader
|
|
@@ -202,6 +210,8 @@ export interface AdminReport {
|
|
|
202
210
|
bundleFilename: string | null
|
|
203
211
|
bundleIntegrity: string | null
|
|
204
212
|
uploadedAt: number
|
|
213
|
+
confirmedOffered: boolean | null
|
|
214
|
+
confirmedDefault: boolean
|
|
205
215
|
}
|
|
206
216
|
|
|
207
217
|
export interface RepoDataItem {
|
|
@@ -471,6 +481,12 @@ export interface ManagedDb extends DependencyReviewStore, RepositoryDependencyRe
|
|
|
471
481
|
// directory path.
|
|
472
482
|
setReportRepo(id: string, repoId: number | null, repoDirectory?: string): Promise<boolean>
|
|
473
483
|
setReportVisible(id: string, visible: boolean): Promise<boolean>
|
|
484
|
+
// A manager's switch of a report to open on Confirmed, resolving true iff it
|
|
485
|
+
// changed; and, of those not read for it yet, whether each may be
|
|
486
|
+
// (report-opening.ts), all in one write, any another read already told
|
|
487
|
+
// left as it is.
|
|
488
|
+
setReportConfirmedDefault(id: string, on: boolean): Promise<boolean>
|
|
489
|
+
setReportsConfirmedOffered(read: readonly { id: string; offered: boolean }[]): Promise<void>
|
|
474
490
|
// Per-finding triage annotations, keyed by finding id alone (shared by every
|
|
475
491
|
// report carrying the finding). listTriage reads the rows for a set of ids —
|
|
476
492
|
// the endpoint passes a viewer's visible findings of one report; a cleared
|
|
@@ -611,7 +627,7 @@ const USER_TEAM_SCOPES_SQL = (user: string) => `SELECT tr.team_id AS teamId, tr.
|
|
|
611
627
|
// shows under every team whose repo it's attached to).
|
|
612
628
|
const USER_TEAM_REPORTS_SQL = (user: string) => `SELECT DISTINCT tr.team_id AS teamId, r.id AS id, r.slug AS slug, r.filename AS filename, r.analyzer, r.visible, r.uploaded_at,
|
|
613
629
|
r.sha256 AS sha256, r.bundle_id AS bundleId, r.repo_id AS repoId, r.repo_directory AS repoDirectory, sr.full_name AS repoFullName,
|
|
614
|
-
tu.view_dependencies AS dependencies, tu.view_security AS security,
|
|
630
|
+
r.confirmed_default AS confirmedDefault, tu.view_dependencies AS dependencies, tu.view_security AS security,
|
|
615
631
|
(SELECT role FROM managed_user WHERE id = tu.user_id) AS role
|
|
616
632
|
FROM managed_team_user tu
|
|
617
633
|
JOIN managed_team t ON t.id = tu.team_id AND t.hidden = 0
|
|
@@ -644,18 +660,20 @@ type UserTeamRows = {
|
|
|
644
660
|
scopes: { teamId: string; repoId: number; path: string; fullName: string }[]
|
|
645
661
|
reports: {
|
|
646
662
|
teamId: string; id: string; slug: string; filename: string; analyzer: string | null; sha256: string; bundleId: string | null; repoId: number
|
|
647
|
-
repoDirectory: string; repoFullName: string | null; visible: number; dependencies: number; security: number; role: string
|
|
663
|
+
repoDirectory: string; repoFullName: string | null; visible: number; confirmedDefault: number; dependencies: number; security: number; role: string
|
|
648
664
|
}[]
|
|
649
665
|
bundles: (Omit<UserTeamBundle, 'visible'> & { teamId: string; visible: number })[]
|
|
650
666
|
}
|
|
651
667
|
|
|
652
|
-
// A catalog report's content key: its content, where it is,
|
|
653
|
-
// reader may see of it (their grants and role)
|
|
654
|
-
// revision `links`. A team snapshot names it of each
|
|
655
|
-
|
|
668
|
+
// A catalog report's content key: its content, where it is, whether it opens
|
|
669
|
+
// on Confirmed, and what its reader may see of it (their grants and role)
|
|
670
|
+
// under the link reports' revision `links`. A team snapshot names it of each
|
|
671
|
+
// report it reads.
|
|
672
|
+
type CatalogReportRow = Pick<UserTeamRows['reports'][number], 'sha256' | 'bundleId' | 'filename' | 'repoId' | 'repoDirectory' | 'repoFullName' | 'confirmedDefault' | 'dependencies' | 'security'>
|
|
656
673
|
function reportCatalogKey(r: CatalogReportRow, role: string, links: string): string {
|
|
657
674
|
return createHash('sha256').update(JSON.stringify([
|
|
658
675
|
r.sha256, r.bundleId, r.filename, r.repoId, r.repoDirectory, r.repoFullName, r.dependencies, r.security, role, links,
|
|
676
|
+
...r.confirmedDefault === 1 ? ['confirmed'] : [],
|
|
659
677
|
])).digest('base64url')
|
|
660
678
|
}
|
|
661
679
|
|
|
@@ -845,9 +863,9 @@ function prepareStatements(db: ManagedSql) {
|
|
|
845
863
|
selectAllReposStmt: db.prepare(`SELECT ${REPO_COLUMNS} FROM managed_selected_repo ORDER BY full_name ASC`),
|
|
846
864
|
insertReportStmt: db.prepare(
|
|
847
865
|
`WITH candidate(id, slug) AS (VALUES (?, ?))
|
|
848
|
-
INSERT INTO managed_report (id, slug, filename, content_type, byte_size, sha256, uploaded_by, uploaded_by_login, repo_id, repo_directory, repo_embedded, analyzer, visible, bundle_id, bundle_integrity, uploaded_at, data_key, storage_encrypted)
|
|
866
|
+
INSERT INTO managed_report (id, slug, filename, content_type, byte_size, sha256, uploaded_by, uploaded_by_login, repo_id, repo_directory, repo_embedded, analyzer, visible, bundle_id, bundle_integrity, uploaded_at, data_key, storage_encrypted, confirmed_offered)
|
|
849
867
|
SELECT id, CASE WHEN EXISTS (SELECT 1 FROM managed_report WHERE slug = candidate.slug) THEN id ELSE slug END,
|
|
850
|
-
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? FROM candidate`,
|
|
868
|
+
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? FROM candidate`,
|
|
851
869
|
),
|
|
852
870
|
// LEFT JOINs so a report whose uploader / repo / bundle was removed (the FK
|
|
853
871
|
// nulled) still lists, with null display fields.
|
|
@@ -858,7 +876,7 @@ function prepareStatements(db: ManagedSql) {
|
|
|
858
876
|
r.repo_directory AS repoDirectory, r.repo_embedded AS repoEmbedded,
|
|
859
877
|
r.analyzer AS analyzer, r.visible AS visible,
|
|
860
878
|
r.bundle_id AS bundleId, b.filename AS bundleFilename, r.bundle_integrity AS bundleIntegrity,
|
|
861
|
-
r.uploaded_at AS uploadedAt
|
|
879
|
+
r.uploaded_at AS uploadedAt, r.confirmed_offered AS confirmedOffered, r.confirmed_default AS confirmedDefault
|
|
862
880
|
FROM managed_report r
|
|
863
881
|
LEFT JOIN managed_user u ON u.id = r.uploaded_by
|
|
864
882
|
LEFT JOIN managed_selected_repo sr ON sr.repo_id = r.repo_id
|
|
@@ -871,7 +889,8 @@ function prepareStatements(db: ManagedSql) {
|
|
|
871
889
|
`SELECT id, slug, filename, content_type AS contentType, byte_size AS byteSize,
|
|
872
890
|
sha256, uploaded_by AS uploadedBy, uploaded_at AS uploadedAt,
|
|
873
891
|
repo_id AS repoId, repo_directory AS repoDirectory, repo_embedded AS repoEmbedded,
|
|
874
|
-
analyzer AS analyzer, visible AS visible, bundle_id AS bundleId
|
|
892
|
+
analyzer AS analyzer, visible AS visible, bundle_id AS bundleId,
|
|
893
|
+
confirmed_offered AS confirmedOffered, confirmed_default AS confirmedDefault
|
|
875
894
|
FROM managed_report WHERE id = ?`,
|
|
876
895
|
),
|
|
877
896
|
selectReportByHashStmt: db.prepare(
|
|
@@ -917,9 +936,10 @@ function prepareStatements(db: ManagedSql) {
|
|
|
917
936
|
WHERE tu.user_id = v.uid AND tu.team_id = ? AND t.hidden = 0) AS member,
|
|
918
937
|
(SELECT json_group_array(json_object('id', x.id, 'filename', x.filename, 'byteSize', x.byteSize, 'sha256', x.sha256,
|
|
919
938
|
'visible', x.visible, 'bundleId', x.bundleId, 'repoId', x.repoId, 'repoDirectory', x.repoDirectory, 'repoFullName', x.repoFullName,
|
|
920
|
-
'dependencies', x.dependencies, 'security', x.security) ORDER BY x.id)
|
|
939
|
+
'confirmedDefault', x.confirmedDefault, 'dependencies', x.dependencies, 'security', x.security) ORDER BY x.id)
|
|
921
940
|
FROM (SELECT DISTINCT r.id AS id, r.filename AS filename, r.byte_size AS byteSize, r.sha256 AS sha256, r.visible AS visible,
|
|
922
941
|
r.bundle_id AS bundleId, r.repo_id AS repoId, r.repo_directory AS repoDirectory, sr.full_name AS repoFullName,
|
|
942
|
+
r.confirmed_default AS confirmedDefault,
|
|
923
943
|
tu.view_dependencies AS dependencies, tu.view_security AS security
|
|
924
944
|
FROM managed_team_user tu
|
|
925
945
|
JOIN managed_team t ON t.id = tu.team_id AND t.hidden = 0
|
|
@@ -943,6 +963,9 @@ function prepareStatements(db: ManagedSql) {
|
|
|
943
963
|
deleteReportStmt: db.prepare(`DELETE FROM managed_report WHERE id = ?`),
|
|
944
964
|
setReportRepoStmt: db.prepare(`UPDATE managed_report SET repo_id = ?, repo_directory = ? WHERE id = ?`),
|
|
945
965
|
setReportVisibleStmt: db.prepare(`UPDATE managed_report SET visible = ? WHERE id = ?`),
|
|
966
|
+
setReportConfirmedDefaultStmt: db.prepare(`UPDATE managed_report SET confirmed_default = ? WHERE id = ? AND confirmed_default <> ?`),
|
|
967
|
+
setReportsConfirmedOfferedStmt: db.prepare(`UPDATE managed_report SET confirmed_offered = ?
|
|
968
|
+
WHERE confirmed_offered IS NULL AND id IN (SELECT value FROM json_each(?))`),
|
|
946
969
|
upsertTriageStmt: db.prepare(
|
|
947
970
|
`INSERT INTO managed_finding_triage (finding_id, color, triage, comment, fix, flagged, updated_by, updated_by_login, updated_at)
|
|
948
971
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
@@ -1117,7 +1140,7 @@ function prepareStatements(db: ManagedSql) {
|
|
|
1117
1140
|
${ENABLED_LINKS_SQL} AS links,
|
|
1118
1141
|
(SELECT json_group_array(json_object('teamId', x.teamId, 'id', x.id, 'slug', x.slug, 'filename', x.filename, 'analyzer', x.analyzer,
|
|
1119
1142
|
'visible', x.visible, 'sha256', x.sha256, 'bundleId', x.bundleId, 'repoId', x.repoId, 'repoDirectory', x.repoDirectory,
|
|
1120
|
-
'repoFullName', x.repoFullName, 'dependencies', x.dependencies, 'security', x.security, 'role', x.role)
|
|
1143
|
+
'repoFullName', x.repoFullName, 'confirmedDefault', x.confirmedDefault, 'dependencies', x.dependencies, 'security', x.security, 'role', x.role)
|
|
1121
1144
|
ORDER BY x.uploaded_at DESC, x.filename ASC, x.id ASC)
|
|
1122
1145
|
FROM (${USER_TEAM_REPORTS_SQL('v.id')}) AS x) AS reports,
|
|
1123
1146
|
(SELECT json_group_array(json_object('teamId', x.teamId, 'id', x.id, 'slug', x.slug, 'integrity', x.integrity, 'filename', x.filename,
|
|
@@ -1265,24 +1288,28 @@ type ReportListRow = {
|
|
|
1265
1288
|
repoId: number | null; repoFullName: string | null
|
|
1266
1289
|
repoDirectory: string; repoEmbedded: number; analyzer: string | null; visible: number
|
|
1267
1290
|
bundleId: string | null; bundleFilename: string | null; bundleIntegrity: string | null
|
|
1268
|
-
uploadedAt: number
|
|
1291
|
+
uploadedAt: number; confirmedOffered: number | null; confirmedDefault: number
|
|
1269
1292
|
}
|
|
1270
1293
|
type ReportRow = {
|
|
1271
1294
|
id: string; slug: string; filename: string; contentType: string; byteSize: number
|
|
1272
1295
|
sha256: string; uploadedBy: string | null; uploadedAt: number
|
|
1273
1296
|
repoId: number | null; repoDirectory: string; repoEmbedded: number; analyzer: string | null; visible: number
|
|
1274
|
-
bundleId: string | null
|
|
1297
|
+
bundleId: string | null; confirmedOffered: number | null; confirmedDefault: number
|
|
1275
1298
|
}
|
|
1299
|
+
// Its opening's columns as a report gives them.
|
|
1300
|
+
const reportOpening = (row: { confirmedOffered: number | null; confirmedDefault: number }) =>
|
|
1301
|
+
({ confirmedOffered: row.confirmedOffered == null ? null : row.confirmedOffered === 1, confirmedDefault: row.confirmedDefault === 1 })
|
|
1276
1302
|
|
|
1277
1303
|
// A report as an access snapshot read it, and as the snapshot gives it: with
|
|
1278
1304
|
// every permission where the reader's role sees `whole` reports.
|
|
1279
1305
|
type AccessRow = Pick<ReportAccessRecord, 'id' | 'filename' | 'byteSize' | 'sha256'> & {
|
|
1280
|
-
repoDirectory: string; repoFullName: string | null; dependencies: number; security: number
|
|
1306
|
+
repoDirectory: string; repoFullName: string | null; dependencies: number; security: number; confirmedDefault?: number
|
|
1281
1307
|
}
|
|
1282
1308
|
const accessRecord = (row: AccessRow, whole: boolean): ReportAccessRecord => ({
|
|
1283
1309
|
id: row.id, filename: row.filename, byteSize: row.byteSize, sha256: row.sha256,
|
|
1284
1310
|
repo: { github: row.repoFullName, directory: row.repoDirectory },
|
|
1285
1311
|
permissions: { dependencies: whole || row.dependencies === 1, security: whole || row.security === 1 },
|
|
1312
|
+
...row.confirmedDefault === 1 ? { confirmedDefault: true as const } : {},
|
|
1286
1313
|
})
|
|
1287
1314
|
|
|
1288
1315
|
// Byte storage may outlast a logout or grant change. HTTP uploads reauthorize
|
|
@@ -1304,7 +1331,7 @@ function reportMethods(stmts: ReturnType<typeof prepareStatements>, db: ManagedS
|
|
|
1304
1331
|
async function getReport(id: string): Promise<ReportRecord | null> {
|
|
1305
1332
|
const row = (await selectReportStmt.get(id)) as ReportRow | undefined
|
|
1306
1333
|
if (row == null) return null
|
|
1307
|
-
return { ...row, repoEmbedded: row.repoEmbedded === 1, visible: row.visible === 1 }
|
|
1334
|
+
return { ...row, repoEmbedded: row.repoEmbedded === 1, visible: row.visible === 1, ...reportOpening(row) }
|
|
1308
1335
|
}
|
|
1309
1336
|
async function getReportByHash(sha256: string, analyzer: string | null): Promise<ReportRecord | null> {
|
|
1310
1337
|
const row = await stmts.selectReportByHashStmt.get(sha256, analyzer) as { id: string } | undefined
|
|
@@ -1317,6 +1344,7 @@ function reportMethods(stmts: ReturnType<typeof prepareStatements>, db: ManagedS
|
|
|
1317
1344
|
report.sha256, report.uploadedBy, report.uploadedByLogin ?? null, report.repoId,
|
|
1318
1345
|
report.repoDirectory ?? '', report.repoEmbedded ? 1 : 0, report.analyzer ?? null, report.visible ? 1 : 0, report.bundleId ?? null,
|
|
1319
1346
|
report.bundleIntegrity ?? null, now, report.dataKey ?? null, report.dataKey ? 1 : 0,
|
|
1347
|
+
report.confirmedOffered == null ? null : report.confirmedOffered ? 1 : 0,
|
|
1320
1348
|
)
|
|
1321
1349
|
}
|
|
1322
1350
|
return {
|
|
@@ -1335,7 +1363,7 @@ function reportMethods(stmts: ReturnType<typeof prepareStatements>, db: ManagedS
|
|
|
1335
1363
|
},
|
|
1336
1364
|
async listReports(userId?: string): Promise<AdminReport[]> {
|
|
1337
1365
|
const rows = (await selectReportsStmt.all(...asManager(userId))) as ReportListRow[]
|
|
1338
|
-
return rows.map(r => ({ ...r, repoEmbedded: r.repoEmbedded === 1, visible: r.visible === 1 }))
|
|
1366
|
+
return rows.map(r => ({ ...r, repoEmbedded: r.repoEmbedded === 1, visible: r.visible === 1, ...reportOpening(r) }))
|
|
1339
1367
|
},
|
|
1340
1368
|
async listFindingCatalogReports(after: string, limit: number): Promise<Pick<ReportRecord, 'id' | 'byteSize'>[]> {
|
|
1341
1369
|
return await stmts.findingCatalogReportsStmt.all(after, limit) as Pick<ReportRecord, 'id' | 'byteSize'>[]
|
|
@@ -1387,6 +1415,15 @@ function reportMethods(stmts: ReturnType<typeof prepareStatements>, db: ManagedS
|
|
|
1387
1415
|
async setReportVisible(id: string, visible: boolean): Promise<boolean> {
|
|
1388
1416
|
return anyChanged(await setReportVisibleStmt.run(visible ? 1 : 0, id))
|
|
1389
1417
|
},
|
|
1418
|
+
async setReportConfirmedDefault(id: string, on: boolean): Promise<boolean> {
|
|
1419
|
+
return anyChanged(await stmts.setReportConfirmedDefaultStmt.run(on ? 1 : 0, id, on ? 1 : 0))
|
|
1420
|
+
},
|
|
1421
|
+
async setReportsConfirmedOffered(read: readonly { id: string; offered: boolean }[]): Promise<void> {
|
|
1422
|
+
for (const offered of [true, false]) {
|
|
1423
|
+
const ids = read.filter(entry => entry.offered === offered).map(entry => entry.id)
|
|
1424
|
+
if (ids.length > 0) await stmts.setReportsConfirmedOfferedStmt.run(offered ? 1 : 0, JSON.stringify(ids))
|
|
1425
|
+
}
|
|
1426
|
+
},
|
|
1390
1427
|
}
|
|
1391
1428
|
}
|
|
1392
1429
|
|
|
@@ -8,7 +8,7 @@ import { AsyncLocalStorage } from 'node:async_hooks'
|
|
|
8
8
|
import { WebSocket } from 'ws'
|
|
9
9
|
import { type ManagedDb, type ManagedDbOptions, createManagedMethods } from './db-methods.ts'
|
|
10
10
|
import { revisionSchema } from './revisions.ts'
|
|
11
|
-
import { MANAGED_SCHEMA, REPO_UPLOAD_INDEXES, VIEW_SESSION_INDEX } from './db-schema.ts'
|
|
11
|
+
import { MANAGED_SCHEMA, REPORT_OPENING_COLUMNS, REPO_UPLOAD_INDEXES, VIEW_SESSION_INDEX } from './db-schema.ts'
|
|
12
12
|
import { STORAGE_SCHEMA } from './storage-db.ts'
|
|
13
13
|
import { GITHUB_COMMIT_SCHEMA, GITHUB_METADATA_SCHEMA, GITHUB_STATE_REASON_COLUMN } from './github-metadata.ts'
|
|
14
14
|
import { MANAGED_ISSUE_SCHEMA } from './managed-issues.ts'
|
|
@@ -40,7 +40,7 @@ const LOCK = 'SELECT pg_advisory_xact_lock(1937006964, 1835101793)'
|
|
|
40
40
|
// Whether every version up to the latest is recorded, in one round trip at
|
|
41
41
|
// a cold start: a database with no versions table yet (undefined_table) has
|
|
42
42
|
// none. Outside a transaction, so the failed read aborts nothing.
|
|
43
|
-
const SCHEMA_VERSIONS =
|
|
43
|
+
const SCHEMA_VERSIONS = 38
|
|
44
44
|
async function currentSchema(db: PgConnection): Promise<boolean> {
|
|
45
45
|
try {
|
|
46
46
|
const { rows } = await db.query(`SELECT count(*) AS n FROM managed_schema_version WHERE version BETWEEN 1 AND ${SCHEMA_VERSIONS}`)
|
|
@@ -139,6 +139,13 @@ async function migrateFrom12(db: PgConnection): Promise<void> {
|
|
|
139
139
|
// The patch a dependency review's base is of, none of those written
|
|
140
140
|
// before; each author's one draft of a comparison held to it too.
|
|
141
141
|
await migration(db, 36, `ALTER TABLE managed_dependency_review ${addColumns(DEPENDENCY_REVIEW_COLUMNS)}`, DEPENDENCY_REVIEW_INDEXES)
|
|
142
|
+
// Whether a report may open on Confirmed, existing ones read for it as the
|
|
143
|
+
// reports page lists them (report-opening.ts), and none switched yet.
|
|
144
|
+
await migration(db, 37, `ALTER TABLE managed_report ${addColumns(REPORT_OPENING_COLUMNS)}`)
|
|
145
|
+
// When a dependency review was last published to a team, none of those
|
|
146
|
+
// published before: a time in milliseconds, so BIGINT, as postgresSchema
|
|
147
|
+
// makes SQLite's INTEGER.
|
|
148
|
+
await migration(db, 38, postgresSchema(`ALTER TABLE managed_dependency_review ${addColumns(DEPENDENCY_REVIEW_COLUMNS)}`))
|
|
142
149
|
}
|
|
143
150
|
|
|
144
151
|
async function initialize(db: PgConnection): Promise<void> {
|