@preventive/triage 1.0.0-alpha.4 → 1.0.0-alpha.40
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/LICENSE-APACHE +202 -0
- package/README.md +16 -0
- package/api/reap.ts +17 -0
- package/cli.js +6 -0
- package/client/dependency-paths.d.ts +2 -0
- package/client/dependency-paths.js +24 -0
- package/client/finding-link.js +305 -0
- package/client/linked-findings.d.ts +1 -0
- package/client/linked-findings.js +112 -0
- package/common/bundle-commit.d.ts +1 -0
- package/common/bundle-commit.js +4 -0
- package/common/bundle-compare-diff.d.ts +3 -0
- package/common/bundle-compare-diff.js +522 -0
- package/common/bundle-compare-inputs.d.ts +2 -0
- package/common/bundle-compare-inputs.js +65 -0
- package/common/bundle-metadata.d.ts +14 -0
- package/common/bundle-metadata.js +226 -0
- package/common/bundle-pkg-of.d.ts +1 -0
- package/common/bundle-pkg-of.js +90 -0
- package/common/bundle-reasons.d.ts +2 -0
- package/common/bundle-reasons.js +21 -0
- package/common/bundle-source-links.d.ts +6 -0
- package/common/bundle-source-links.js +216 -0
- package/common/bundle-sourcemap.d.ts +10 -0
- package/common/bundle-sourcemap.js +100 -0
- package/common/bundle-sources.d.ts +7 -0
- package/common/bundle-sources.js +310 -0
- package/common/bundle-stats.js +41 -0
- package/common/bundle-tabs.js +1 -0
- package/common/code-language.js +65 -0
- package/common/default-scan-models.ts +31 -0
- package/common/dependency-reviews.d.ts +23 -0
- package/common/dependency-reviews.js +81 -0
- package/common/finding-duplicates.js +58 -0
- package/common/finding-filters.js +274 -0
- package/common/finding-id.js +47 -0
- package/common/github-issue-labels.d.ts +1 -0
- package/common/github-issue-labels.js +11 -0
- package/common/github-pr.ts +56 -0
- package/common/gzip.js +59 -0
- package/common/line-diff.d.ts +8 -0
- package/common/line-diff.js +56 -0
- package/common/linked-workspace-groups.js +63 -0
- package/common/managed/comments.ts +34 -0
- package/common/managed/npm-packages.d.ts +5 -0
- package/common/managed/npm-packages.js +34 -0
- package/common/managed/permissions.ts +31 -0
- package/common/managed/report-content.ts +72 -0
- package/common/managed/report-filter.ts +108 -0
- package/common/managed/repository-alias.ts +65 -0
- package/common/managed/roles.ts +28 -0
- package/common/managed/routes.d.ts +2 -0
- package/common/managed/routes.js +254 -0
- package/common/managed/scan-models.ts +6 -0
- package/common/managed/team-app.d.ts +2 -0
- package/common/managed/team-app.js +31 -0
- package/common/managed/triage.ts +83 -0
- package/common/managed/upload-seal.ts +126 -0
- package/common/pretty-print.d.ts +5 -0
- package/common/pretty-print.js +24 -0
- package/common/revalidation-conflicts.js +48 -0
- package/common/revalidation-input-groups.js +56 -0
- package/common/save-error-reason.ts +20 -7
- package/common/scan-server.ts +13 -0
- package/common/server-info.ts +35 -0
- package/common/utf8.d.ts +3 -0
- package/common/utf8.js +45 -0
- package/common/workspace-app.js +23 -0
- package/common/workspace-groups.js +176 -0
- package/out/brotli-fallback.js +3 -3
- package/out/chunk-2KBF5TZ7.js +2 -0
- package/out/chunk-2XUEZHIP.js +28 -0
- package/out/chunk-35VE6CDH.js +2 -0
- package/out/chunk-3BZ7JERB.js +1 -0
- package/out/chunk-4F6JSF57.js +3 -0
- package/out/chunk-4XCDFGDP.js +10 -0
- package/out/chunk-AQ75QITE.js +1 -0
- package/out/chunk-B2RQY6OP.js +2 -0
- package/out/chunk-C4Z6ZBXV.js +10 -0
- package/out/chunk-CMQA4DM6.js +1 -0
- package/out/chunk-CVVJAXVY.js +2 -0
- package/out/chunk-DIJJNTRH.js +10 -0
- package/out/chunk-GJ4IUCNS.js +1 -0
- package/out/chunk-GNXOKIGP.js +80 -0
- package/out/chunk-GZ4MS3TE.js +13 -0
- package/out/chunk-IISEF2BZ.js +1 -0
- package/out/chunk-IU3CCINR.js +3 -0
- package/out/chunk-K3X5EJBQ.js +2 -0
- package/out/chunk-KEX2ATEW.js +2 -0
- package/out/chunk-KYCNJVB7.js +1 -0
- package/out/chunk-MYDDTA7W.js +225 -0
- package/out/chunk-NRPC74AM.js +1 -0
- package/out/chunk-OOJM4CTU.js +1 -0
- package/out/chunk-PNHBZGDD.js +1 -0
- package/out/chunk-R5O6FAW4.js +2 -0
- package/out/chunk-RFZQQXSG.js +17 -0
- package/out/chunk-RSHMVYNS.js +1 -0
- package/out/chunk-TGANXVTA.js +1 -0
- package/out/chunk-TUII34K2.js +1 -0
- package/out/chunk-TZ5KB2Y5.js +10 -0
- package/out/chunk-UN5ZSYLE.js +1 -0
- package/out/chunk-UNTRGE26.js +2 -0
- package/out/chunk-UXNUAIIC.js +33 -0
- package/out/chunk-UYAXVRVL.js +1 -0
- package/out/chunk-V3ME6FI5.js +2 -0
- package/out/chunk-V5O7M2YT.js +1 -0
- package/out/chunk-VIHGO77Y.js +1 -0
- package/out/chunk-VQJCXGIY.js +7 -0
- package/out/chunk-W4J3MGLB.js +1 -0
- package/out/chunk-WGG7KQUH.js +1 -0
- package/out/chunk-WZDTZE64.js +1 -0
- package/out/chunk-XYDGU6BM.js +51 -0
- package/out/chunk-XYFXXB2G.js +10 -0
- package/out/chunk-YVZH5SHP.js +1 -0
- package/out/chunk-ZBBOBQ43.js +1 -0
- package/out/chunk-ZMQQCGF6.js +2 -0
- package/out/client-managed-import.js +4 -0
- package/out/client-managed.js +37 -0
- package/out/client-sync.js +10 -13
- package/out/github-issue-authorized.html +3 -0
- package/out/github-issue-authorized.js +1 -0
- package/out/graph.js +17 -4
- package/out/index.html +90 -8
- package/out/prism.js +6 -2
- package/out/stasis.svg +20 -0
- package/out/terminal.js +365 -39
- package/out/view.css +1 -1
- package/out/view.js +83 -117
- package/package.json +297 -59
- package/risk/files/characters.js +196 -0
- package/risk/files/hosts.js +205 -0
- package/risk/files/readability.js +171 -0
- package/risk/files/runs-code.js +358 -0
- package/risk/files/script.js +114 -0
- package/risk/files/stamps.js +46 -0
- package/risk/index.d.ts +142 -0
- package/risk/index.js +7 -0
- package/risk/npm/assess.js +219 -0
- package/risk/npm/combined.js +65 -0
- package/risk/npm/compare.js +228 -0
- package/risk/npm/dependencies.js +153 -0
- package/risk/npm/diff.js +43 -0
- package/risk/npm/downloads.js +84 -0
- package/risk/npm/files.js +34 -0
- package/risk/npm/held.js +60 -0
- package/risk/npm/install-scripts.js +34 -0
- package/risk/npm/malware.js +36 -0
- package/risk/npm/manifest.js +65 -0
- package/risk/npm/new-dependency.js +30 -0
- package/risk/npm/old-version.js +108 -0
- package/risk/npm/registry.js +282 -0
- package/risk/npm/release.js +85 -0
- package/risk/npm/specs.js +124 -0
- package/risk/npm/tarball.js +113 -0
- package/server-common/database-config.ts +16 -0
- package/server-common/initialize.ts +18 -0
- package/server-common/npm-advisories.ts +101 -0
- package/{server → server-common}/origin.ts +5 -5
- package/server-common/reap.ts +48 -0
- package/server-common/scan-config.ts +19 -0
- package/server-common/standalone.ts +29 -0
- package/server-common/storage-crypto.ts +182 -0
- package/server-common/storage-log.ts +38 -0
- package/server-common/vercel-blob.ts +103 -0
- package/server-e2e/app.ts +486 -0
- package/{server → server-e2e}/auth.ts +5 -1
- package/{server → server-e2e}/bus-receiver.ts +2 -1
- package/{server → server-e2e}/cli.js +9 -4
- package/{server → server-e2e}/config.ts +56 -35
- package/{server → server-e2e}/db-neon.ts +2 -2
- package/{server → server-e2e}/db-revision-sql.ts +1 -1
- package/{server → server-e2e}/db-stmt.ts +2 -2
- package/{server → server-e2e}/db.ts +7 -7
- package/{server → server-e2e}/http.ts +116 -18
- package/{server → server-e2e}/hub.ts +40 -9
- package/server-e2e/index.ts +17 -0
- package/server-e2e/lifecycle.ts +95 -0
- package/{server → server-e2e}/neon-driver.ts +2 -2
- package/{server → server-e2e}/npm-proxy.ts +12 -145
- package/{server → server-e2e}/objstore/blob-vercel.ts +10 -114
- package/{server → server-e2e}/objstore/blob.ts +1 -1
- package/server-e2e/objstore/fetch-mint-guard.ts +74 -0
- package/{server → server-e2e}/objstore/handlers.ts +9 -8
- package/{server → server-e2e}/objstore/init.ts +35 -18
- package/{server → server-e2e}/objstore/reaper.ts +1 -1
- package/server-e2e/objstore/rest-deny.ts +28 -0
- package/server-e2e/objstore/rest-mint.ts +224 -0
- package/{server → server-e2e}/objstore/rest.ts +27 -26
- package/{server → server-e2e}/objstore/sign.ts +105 -0
- package/{server → server-e2e}/objstore/store-neon.ts +6 -10
- package/{server → server-e2e}/objstore/store.ts +7 -7
- package/{server → server-e2e}/objstore/tokens.ts +2 -2
- package/{server → server-e2e}/pubsub.ts +12 -9
- package/{server → server-e2e}/sign.ts +4 -4
- package/{server → server-e2e}/sse-server.ts +102 -68
- package/{server → server-e2e}/sse-session.ts +28 -8
- package/{server → server-e2e}/static.ts +31 -21
- package/server-e2e/sync-handlers.ts +408 -0
- package/{server → server-e2e}/ws-server.ts +20 -9
- package/server-managed/activity.ts +245 -0
- package/server-managed/avatar-store.ts +12 -0
- package/server-managed/blob-store.ts +17 -0
- package/server-managed/brotli.ts +10 -0
- package/server-managed/bundle-advisories.ts +120 -0
- package/server-managed/bundle-advisory-inventory.ts +77 -0
- package/server-managed/bundle-build-diagnostics.d.ts +10 -0
- package/server-managed/bundle-build-diagnostics.js +23 -0
- package/server-managed/bundle-build-leases.ts +41 -0
- package/server-managed/bundle-build-worker.js +111 -0
- package/server-managed/bundle-build.ts +198 -0
- package/server-managed/bundle-cache.ts +199 -0
- package/server-managed/bundle-catalog.ts +29 -0
- package/server-managed/bundle-commits.ts +140 -0
- package/server-managed/bundle-store.ts +24 -0
- package/server-managed/bundle-summary-cache.ts +130 -0
- package/server-managed/bundle.ts +120 -0
- package/server-managed/cache-storage.ts +19 -0
- package/server-managed/cli.js +21 -0
- package/server-managed/combined.ts +46 -0
- package/server-managed/comments.ts +151 -0
- package/server-managed/config.ts +158 -0
- package/server-managed/content-access.ts +15 -0
- package/server-managed/crypto.ts +25 -0
- package/server-managed/db-methods.ts +1725 -0
- package/server-managed/db-neon.ts +367 -0
- package/server-managed/db-schema.ts +227 -0
- package/server-managed/db-table-names.ts +22 -0
- package/server-managed/db.ts +132 -0
- package/server-managed/dependency-reviews.ts +248 -0
- package/server-managed/github-app.ts +391 -0
- package/server-managed/github-errors.ts +68 -0
- package/server-managed/github-issue-links.ts +127 -0
- package/server-managed/github-issue-oauth.ts +47 -0
- package/server-managed/github-issues.ts +152 -0
- package/server-managed/github-metadata.ts +213 -0
- package/server-managed/github-oauth.ts +235 -0
- package/server-managed/github-pulls.ts +227 -0
- package/server-managed/http-response.ts +50 -0
- package/server-managed/http.ts +3472 -0
- package/server-managed/import-triage.ts +51 -0
- package/server-managed/index.ts +187 -0
- package/server-managed/kept.ts +28 -0
- package/server-managed/link-reports.ts +158 -0
- package/server-managed/locked-packages.ts +85 -0
- package/server-managed/lru.ts +30 -0
- package/server-managed/managed-issues.ts +75 -0
- package/server-managed/management-catalog.ts +45 -0
- package/server-managed/management.ts +99 -0
- package/server-managed/npm-insights.ts +440 -0
- package/server-managed/npm-kept.ts +65 -0
- package/server-managed/npm-loads.ts +167 -0
- package/server-managed/npm-manifest.ts +115 -0
- package/server-managed/npm-packages.ts +378 -0
- package/server-managed/npm-provenance.ts +123 -0
- package/server-managed/npm-risk.ts +177 -0
- package/server-managed/object-storage-disk.ts +148 -0
- package/server-managed/object-storage-vercel.ts +67 -0
- package/server-managed/object-storage.ts +74 -0
- package/server-managed/package-entry-points.ts +51 -0
- package/server-managed/pretty-print.ts +352 -0
- package/server-managed/public-workspace.ts +220 -0
- package/server-managed/rename-diff-worker.js +26 -0
- package/server-managed/rename-diff.ts +270 -0
- package/server-managed/repo-path.ts +21 -0
- package/server-managed/report-migration.ts +35 -0
- package/server-managed/report-query.ts +10 -0
- package/server-managed/report-response.ts +16 -0
- package/server-managed/report-sources.ts +178 -0
- package/server-managed/report-store.ts +47 -0
- package/server-managed/repository-aliases.ts +91 -0
- package/server-managed/repository-browser.ts +153 -0
- package/server-managed/repository-discovery.ts +82 -0
- package/server-managed/repository-policy.ts +27 -0
- package/server-managed/repository-risk.ts +418 -0
- package/server-managed/revisions.ts +35 -0
- package/server-managed/session.ts +92 -0
- package/server-managed/slugs.ts +38 -0
- package/server-managed/solidity-entry-points.ts +69 -0
- package/server-managed/sql-postgres.ts +31 -0
- package/server-managed/sql.ts +76 -0
- package/server-managed/static.ts +28 -0
- package/server-managed/storage-command.ts +16 -0
- package/server-managed/storage-db.ts +175 -0
- package/server-managed/storage-encryption.ts +279 -0
- package/server-managed/storage-maintenance.ts +256 -0
- package/server-managed/storage-payload.ts +103 -0
- package/server-managed/storage-stores.ts +85 -0
- package/server-managed/storage.ts +78 -0
- package/server-managed/team-app.ts +94 -0
- package/server-managed/team-catalog.ts +7 -0
- package/server-managed/team-feed.ts +173 -0
- package/server-managed/team-npm-scopes.ts +77 -0
- package/server-managed/team-reports.ts +228 -0
- package/server-managed/triage-response.ts +16 -0
- package/server-managed/type-only-worker.js +19 -0
- package/server-managed/type-only.ts +103 -0
- package/server-managed/uploads.ts +86 -0
- package/server-managed/upstream-cache.ts +215 -0
- package/server-managed/view-as.ts +58 -0
- package/server-managed/workspace-shares.ts +166 -0
- package/server.ts +50 -0
- package/server/index.ts +0 -477
- package/server/lifecycle.ts +0 -204
- package/server/sync-handlers.ts +0 -320
- /package/{LICENSE → LICENSE-MIT} +0 -0
- /package/{server → server-e2e}/config.example.json +0 -0
- /package/{server → server-e2e}/objstore/blob-fs.ts +0 -0
- /package/{server → server-e2e}/objstore/fs.ts +0 -0
- /package/{server → server-e2e}/peer.ts +0 -0
- /package/{server → server-e2e}/util.ts +0 -0
- /package/{server → server-e2e}/validation.ts +0 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Staging is shared storage, never process memory. Each part is bound to the
|
|
2
|
+
// authenticated session, destination and random upload ID. No bearer URL is
|
|
3
|
+
// exposed; all reads/writes still pass the managed role/origin/CSRF gates.
|
|
4
|
+
import { Buffer } from 'node:buffer'
|
|
5
|
+
import { createHash, generateKeyPairSync } from 'node:crypto'
|
|
6
|
+
import type { IncomingMessage } from 'node:http'
|
|
7
|
+
import type { BlobStore } from './blob-store.ts'
|
|
8
|
+
import { maxSealedBytes, openUpload } from '../common/managed/upload-seal.ts'
|
|
9
|
+
|
|
10
|
+
export const UPLOAD_CHUNK_BYTES = 3 * 1024 * 1024
|
|
11
|
+
export const MAX_UPLOAD_BYTES = 1024 * 1024 * 1024
|
|
12
|
+
// Staged parts may carry a sealed body, slightly larger than its content.
|
|
13
|
+
const MAX_STAGED_BYTES = maxSealedBytes(MAX_UPLOAD_BYTES)
|
|
14
|
+
const MAX_UPLOAD_PARTS = Math.ceil(MAX_STAGED_BYTES / UPLOAD_CHUNK_BYTES)
|
|
15
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/u
|
|
16
|
+
export type UploadKind = 'reports' | 'bundles'
|
|
17
|
+
|
|
18
|
+
function partId(session: string, kind: UploadKind, id: string, index: number): string {
|
|
19
|
+
const hash = createHash('sha256').update(JSON.stringify([session, kind, id, index])).digest('hex').slice(0, 32)
|
|
20
|
+
return `${hash.slice(0, 8)}-${hash.slice(8, 12)}-${hash.slice(12, 16)}-${hash.slice(16, 20)}-${hash.slice(20)}`
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function validUploadPart(id: string, index: number, maxBytes: number): boolean {
|
|
24
|
+
return UUID.test(id) && Number.isSafeInteger(index) && index >= 0 && index < Math.ceil(Math.min(maxSealedBytes(maxBytes), MAX_STAGED_BYTES) / UPLOAD_CHUNK_BYTES)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function validUpload(id: string, count: number): boolean {
|
|
28
|
+
return UUID.test(id) && Number.isSafeInteger(count) && count > 0
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Limit cleanup to indices that any configured server could have accepted,
|
|
32
|
+
// even if the request advertises an enormous count or the limit was lowered.
|
|
33
|
+
export async function deleteUpload(store: BlobStore, session: string, kind: UploadKind, id: string, count: number): Promise<void> {
|
|
34
|
+
if (!validUpload(id, count)) throw new Error('bad-upload')
|
|
35
|
+
const end = Math.min(count, MAX_UPLOAD_PARTS), errors = []
|
|
36
|
+
for (let start = 0; start < end; start += 8) {
|
|
37
|
+
const results = await Promise.allSettled(Array.from({ length: Math.min(8, end - start) },
|
|
38
|
+
(_, index) => store.delete(partId(session, kind, id, start + index))))
|
|
39
|
+
for (const result of results) if (result.status === 'rejected') errors.push(result.reason)
|
|
40
|
+
}
|
|
41
|
+
if (errors.length > 0) throw new AggregateError(errors, 'Upload cleanup failed')
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function putUploadPart(store: BlobStore, session: string, kind: UploadKind, id: string, index: number, bytes: Buffer): Promise<void> {
|
|
45
|
+
await store.put(partId(session, kind, id, index), bytes)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function readUpload(store: BlobStore, req: IncomingMessage, session: string, kind: UploadKind, maxBytes: number): Promise<Buffer> {
|
|
49
|
+
const id = req.headers['x-upload-id']
|
|
50
|
+
const count = Number(req.headers['x-upload-parts']), size = Number(req.headers['x-upload-size'])
|
|
51
|
+
if (typeof id !== 'string' || !validUpload(id, count)) throw new Error('bad-upload')
|
|
52
|
+
try {
|
|
53
|
+
if (!Number.isSafeInteger(size) || size <= 0 || count !== Math.ceil(size / UPLOAD_CHUNK_BYTES)) throw new Error('bad-upload')
|
|
54
|
+
if (size > maxBytes || size > MAX_STAGED_BYTES) throw new Error('too-large')
|
|
55
|
+
const parts: Buffer[] = []
|
|
56
|
+
for (let index = 0; index < count; index++) {
|
|
57
|
+
const bytes = await store.get(partId(session, kind, id, index))
|
|
58
|
+
const expected = Math.min(UPLOAD_CHUNK_BYTES, size - index * UPLOAD_CHUNK_BYTES)
|
|
59
|
+
if (!bytes || bytes.length !== expected) throw new Error('bad-upload')
|
|
60
|
+
parts.push(bytes)
|
|
61
|
+
}
|
|
62
|
+
return Buffer.concat(parts, size)
|
|
63
|
+
} finally {
|
|
64
|
+
// These bytes can no longer be finalized. Failed/abandoned chunk uploads
|
|
65
|
+
// are also covered by automatic staging maintenance.
|
|
66
|
+
await deleteUpload(store, session, kind, id, count).catch(err => console.warn('managed: upload cleanup failed:', err))
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// A session's upload key pair, stored as its private JWK.
|
|
71
|
+
export function newUploadKey(): string {
|
|
72
|
+
const { privateKey } = generateKeyPairSync('x25519')
|
|
73
|
+
const { kty, crv, x, d } = privateKey.export({ format: 'jwk' })
|
|
74
|
+
return JSON.stringify({ kty, crv, x, d })
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// The raw public key (base64url) browsers seal this session's uploads to.
|
|
78
|
+
export function uploadPublicKey(key: string): string {
|
|
79
|
+
return (JSON.parse(key) as { x: string }).x
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function openSessionUpload(key: string | null | undefined, sealed: Buffer, maxBytes: number): Promise<Buffer> {
|
|
83
|
+
if (!key) throw new Error('bad-upload')
|
|
84
|
+
const bytes = await openUpload(new Uint8Array(sealed.buffer as ArrayBuffer, sealed.byteOffset, sealed.byteLength), JSON.parse(key) as JsonWebKey, maxBytes)
|
|
85
|
+
return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
86
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer'
|
|
2
|
+
import { constants } from 'node:fs'
|
|
3
|
+
import { mkdir, open, rename, rm, writeFile } from 'node:fs/promises'
|
|
4
|
+
import { dirname, join } from 'node:path'
|
|
5
|
+
import { promisify } from 'node:util'
|
|
6
|
+
import { gunzip, gzip } from 'node:zlib'
|
|
7
|
+
import type { CacheStore, Package } from '@preventive/upstream/advisories.js'
|
|
8
|
+
import type { ManagedSql } from './sql.ts'
|
|
9
|
+
|
|
10
|
+
// Records @preventive/upstream would otherwise keep in its disk cache, shared
|
|
11
|
+
// by every instance: each repository's published advisory listing, written by
|
|
12
|
+
// dependency audits that ask repositories, keyed `github/advisories/owner/name`.
|
|
13
|
+
// Upstream stamps, validates and expires each value (90 minutes for a listing);
|
|
14
|
+
// a refresh replaces the row, and there is one row per repository, so rows are
|
|
15
|
+
// not evicted. Only public repositories publish advisories: rows are shared
|
|
16
|
+
// across viewers.
|
|
17
|
+
export const UPSTREAM_CACHE_SCHEMA = `
|
|
18
|
+
CREATE TABLE IF NOT EXISTS managed_upstream_cache (
|
|
19
|
+
cache_key TEXT PRIMARY KEY,
|
|
20
|
+
value TEXT NOT NULL,
|
|
21
|
+
cached_at INTEGER NOT NULL
|
|
22
|
+
) STRICT;
|
|
23
|
+
`
|
|
24
|
+
|
|
25
|
+
export interface UpstreamCacheStore {
|
|
26
|
+
getUpstreamCacheEntry(key: string): Promise<string | null>
|
|
27
|
+
// Key → value for those of `keys` that are kept.
|
|
28
|
+
getUpstreamCacheEntries(keys: string[]): Promise<Map<string, string>>
|
|
29
|
+
setUpstreamCacheEntry(key: string, value: string, cachedAt: number): Promise<void>
|
|
30
|
+
// Removes those under `prefix` (no LIKE wildcard in it) kept before `before`;
|
|
31
|
+
// how many.
|
|
32
|
+
deleteUpstreamCacheEntries(prefix: string, before: number): Promise<number>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function upstreamCacheMethods(db: ManagedSql): UpstreamCacheStore {
|
|
36
|
+
const get = db.prepare('SELECT value FROM managed_upstream_cache WHERE cache_key = ?')
|
|
37
|
+
const getMany = db.prepare('SELECT cache_key, value FROM managed_upstream_cache WHERE cache_key IN (SELECT value FROM json_each(?))')
|
|
38
|
+
// A slower concurrent audit cannot replace a newer listing with an older one.
|
|
39
|
+
const set = db.prepare(`INSERT INTO managed_upstream_cache (cache_key, value, cached_at) VALUES (?, ?, ?)
|
|
40
|
+
ON CONFLICT (cache_key) DO UPDATE SET value = excluded.value, cached_at = excluded.cached_at
|
|
41
|
+
WHERE managed_upstream_cache.cached_at <= excluded.cached_at`)
|
|
42
|
+
const remove = db.prepare('DELETE FROM managed_upstream_cache WHERE cache_key LIKE ? AND cached_at < ?')
|
|
43
|
+
return {
|
|
44
|
+
async getUpstreamCacheEntry(key) {
|
|
45
|
+
const row = await get.get(key) as { value: string } | undefined
|
|
46
|
+
return row?.value ?? null
|
|
47
|
+
},
|
|
48
|
+
async getUpstreamCacheEntries(keys) {
|
|
49
|
+
if (keys.length === 0) return new Map()
|
|
50
|
+
const rows = await getMany.all(JSON.stringify(keys)) as { cache_key: string; value: string }[]
|
|
51
|
+
return new Map(rows.map(row => [row.cache_key, row.value]))
|
|
52
|
+
},
|
|
53
|
+
async setUpstreamCacheEntry(key, value, cachedAt) {
|
|
54
|
+
await set.run(key, value, cachedAt)
|
|
55
|
+
},
|
|
56
|
+
async deleteUpstreamCacheEntries(prefix, before) {
|
|
57
|
+
return Number((await remove.run(`${prefix}%`, before)).changes)
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// An audit's `cache`: where the server has an upstream disk cache (off Vercel,
|
|
63
|
+
// set at startup), that keeps everything; else this store keeps listings.
|
|
64
|
+
export function auditCache(cacheDir: string | null | undefined, db: UpstreamCacheStore, signal: AbortSignal, debug = false, repos: Iterable<string> = []): CacheStore | undefined {
|
|
65
|
+
return cacheDir ? undefined : upstreamCache(db, signal, debug, repos)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The repositories whose listings an audit of `packages` asks for, where they
|
|
69
|
+
// are already known: each `github` package, each `soldeer` package's given
|
|
70
|
+
// repository, and with `repoAdvisories`, every other package's. Upstream looks
|
|
71
|
+
// up the rest itself.
|
|
72
|
+
export function auditedRepos(packages: Iterable<Package>, repoAdvisories: boolean): string[] {
|
|
73
|
+
const repos = [...packages].map(pkg => (pkg.ecosystem === 'github' ? pkg.name
|
|
74
|
+
: pkg.ecosystem === 'soldeer' || repoAdvisories ? pkg.github : undefined))
|
|
75
|
+
return [...new Set(repos.filter(repo => repo !== undefined))]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Only listings are kept: upstream's other records (npm version documents,
|
|
79
|
+
// registries' repository lookups) read as misses and are not written.
|
|
80
|
+
const CACHED_TYPE = 'github/advisories'
|
|
81
|
+
|
|
82
|
+
// The cache is an optimization: a database failure is a miss, never a failed
|
|
83
|
+
// audit. Upstream keeps listing after an abandoned audit; once `signal` aborts,
|
|
84
|
+
// the request's database work is over and the store is left alone.
|
|
85
|
+
// Upstream reads one listing at a time per repository, and a request's queries
|
|
86
|
+
// share one connection (Neon), so a recheck of hundreds of packages would wait
|
|
87
|
+
// on as many round trips: the first read asks for every listing of `repos`
|
|
88
|
+
// at once. Any other key, or all of them if that read fails, is read alone.
|
|
89
|
+
export function upstreamCache(db: UpstreamCacheStore, signal: AbortSignal, debug = false, repos: Iterable<string> = []): CacheStore {
|
|
90
|
+
// Upstream asks for a listing by its repository's lowercased name.
|
|
91
|
+
const batched = new Set([...repos].map(repo => `${CACHED_TYPE}/${repo.toLowerCase()}`))
|
|
92
|
+
let batch: Promise<Map<string, string> | null> | undefined
|
|
93
|
+
const readBatch = () => batch ??= db.getUpstreamCacheEntries([...batched]).catch((error: unknown) => {
|
|
94
|
+
if (debug) console.warn('managed: upstream cache read failed:', error)
|
|
95
|
+
return null
|
|
96
|
+
})
|
|
97
|
+
return {
|
|
98
|
+
async read(type, key) {
|
|
99
|
+
if (type !== CACHED_TYPE || signal.aborted) return null
|
|
100
|
+
const cacheKey = `${type}/${key}`
|
|
101
|
+
try {
|
|
102
|
+
const found = batched.has(cacheKey) ? await readBatch() : null
|
|
103
|
+
const value = found ? found.get(cacheKey) ?? null : await db.getUpstreamCacheEntry(cacheKey)
|
|
104
|
+
return value === null ? null : JSON.parse(value) as unknown
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (debug) console.warn('managed: upstream cache read failed:', error)
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
async write(type, key, value) {
|
|
111
|
+
if (type !== CACHED_TYPE || signal.aborted) return
|
|
112
|
+
try { await db.setUpstreamCacheEntry(`${type}/${key}`, JSON.stringify(value), Date.now()) } catch (error) {
|
|
113
|
+
if (debug) console.warn('managed: upstream cache write failed:', error)
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// The file upstream keeps `file` in, under `dir` of its disk cache
|
|
120
|
+
// (setCacheDir), as its cachePath names it: `@babel+core@7.29.7.tgz`, a
|
|
121
|
+
// capital written `!` and the letter, and a Windows device name's first
|
|
122
|
+
// letter escaped.
|
|
123
|
+
export function upstreamCachePath(root: string, dir: string, file: string): string {
|
|
124
|
+
const name = encodeURIComponent(file.replaceAll(/[!A-Z]/gu, char => `!${char.toLowerCase()}`))
|
|
125
|
+
.replaceAll('%40', '@').replaceAll('%2F', '+')
|
|
126
|
+
.replace(/^(?=(?:con|prn|aux|nul|com\d|lpt\d)(?:\.|$))./u, char => `%${char.codePointAt(0)!.toString(16).toUpperCase()}`)
|
|
127
|
+
return join(root, dir, name)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// A kept file, where it is a regular file of at most `max` bytes, checked on
|
|
131
|
+
// the file as opened (a FIFO or device would block or never end), and read
|
|
132
|
+
// to the size it had then.
|
|
133
|
+
export async function readKeptFile(path: string, max: number): Promise<Uint8Array | null> {
|
|
134
|
+
const handle = await open(path, constants.O_RDONLY | constants.O_NONBLOCK).catch(() => null)
|
|
135
|
+
if (!handle) return null
|
|
136
|
+
try {
|
|
137
|
+
const stats = await handle.stat()
|
|
138
|
+
if (!stats.isFile() || stats.size > max) return null
|
|
139
|
+
const bytes = Buffer.allocUnsafe(stats.size)
|
|
140
|
+
for (let at = 0; at < bytes.length;) {
|
|
141
|
+
const { bytesRead } = await handle.read(bytes, at, bytes.length - at, at)
|
|
142
|
+
if (bytesRead === 0) return null
|
|
143
|
+
at += bytesRead
|
|
144
|
+
}
|
|
145
|
+
return bytes
|
|
146
|
+
} catch { return null }
|
|
147
|
+
finally { await handle.close() }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// `bytes` kept at `path`, written whole beside it first and then moved in, so a
|
|
151
|
+
// reader finds the old file or the new one, never part of one; a failure
|
|
152
|
+
// keeps nothing.
|
|
153
|
+
let tmpSeq = 0
|
|
154
|
+
export async function keepFile(path: string, bytes: Uint8Array) {
|
|
155
|
+
const tmp = `${path}.${process.pid}.${++tmpSeq}.tmp`
|
|
156
|
+
try {
|
|
157
|
+
await mkdir(dirname(path), { recursive: true })
|
|
158
|
+
await writeFile(tmp, bytes)
|
|
159
|
+
await rename(tmp, path)
|
|
160
|
+
} catch { await rm(tmp, { force: true }).catch(() => {}) }
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const gunzipAsync = promisify(gunzip)
|
|
164
|
+
const gzipAsync = promisify(gzip)
|
|
165
|
+
|
|
166
|
+
// An upstream's JSON object kept with its ETag, gzipped, as keepJson keeps it:
|
|
167
|
+
// `{ etag, json }`, or null where none is kept, or what is can't be read
|
|
168
|
+
// within `limit` bytes.
|
|
169
|
+
export async function readKeptJson(path: string, limit: number): Promise<{ etag: string; json: Record<string, unknown> } | null> {
|
|
170
|
+
const bytes = await readKeptFile(path, limit)
|
|
171
|
+
if (!bytes) return null
|
|
172
|
+
const object = (value: unknown): value is Record<string, unknown> => Boolean(value) && typeof value === 'object' && !Array.isArray(value)
|
|
173
|
+
try {
|
|
174
|
+
const kept: unknown = JSON.parse((await gunzipAsync(bytes, { maxOutputLength: limit + 1024 })).toString('utf8'))
|
|
175
|
+
return object(kept) && typeof kept['etag'] === 'string' && object(kept['json']) ? { etag: kept['etag'], json: kept['json'] } : null
|
|
176
|
+
} catch { return null }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// `body`, an upstream's JSON object as it wrote it, kept with its `etag`.
|
|
180
|
+
export async function keepJson(path: string, etag: string, body: Buffer) {
|
|
181
|
+
await keepFile(path, await gzipAsync(Buffer.concat([Buffer.from(`{"etag":${JSON.stringify(etag)},"json":`), body, Buffer.from('}')])))
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// A kept listing is read to no more than this, gzipped or not: one runs to
|
|
185
|
+
// some hundreds of KiB.
|
|
186
|
+
const LISTING_BYTES = 64 * 1024 * 1024
|
|
187
|
+
|
|
188
|
+
// A repository's listing as last kept where audits keep them (auditCache),
|
|
189
|
+
// however old, for when GitHub can't be reached to refresh it: `at`, when it
|
|
190
|
+
// was kept, and `cache`, a store answering it to upstream as kept just now,
|
|
191
|
+
// so that upstream takes it rather than ask GitHub, and keeping nothing.
|
|
192
|
+
// Upstream checks it as it checks its own. Null where none is kept, or it
|
|
193
|
+
// can't be read.
|
|
194
|
+
export async function lastListing(cacheDir: string | null | undefined, db: UpstreamCacheStore, repo: string, debug = false): Promise<{ at: number; cache: CacheStore } | null> {
|
|
195
|
+
const key = repo.toLowerCase()
|
|
196
|
+
let kept: { at?: unknown } | null
|
|
197
|
+
try {
|
|
198
|
+
if (cacheDir) {
|
|
199
|
+
const bytes = await readKeptFile(upstreamCachePath(cacheDir, CACHED_TYPE, `${key}.json.gz`), LISTING_BYTES)
|
|
200
|
+
kept = bytes && JSON.parse((await gunzipAsync(bytes, { maxOutputLength: LISTING_BYTES })).toString('utf8'))
|
|
201
|
+
} else {
|
|
202
|
+
const value = await db.getUpstreamCacheEntry(`${CACHED_TYPE}/${key}`)
|
|
203
|
+
kept = value === null ? null : JSON.parse(value)
|
|
204
|
+
}
|
|
205
|
+
} catch (error) {
|
|
206
|
+
if (debug) console.warn('managed: kept listing read failed:', error)
|
|
207
|
+
return null
|
|
208
|
+
}
|
|
209
|
+
const at = kept?.at
|
|
210
|
+
if (typeof at !== 'number') return null
|
|
211
|
+
return { at, cache: {
|
|
212
|
+
read: (type, name) => Promise.resolve(type === CACHED_TYPE && name === key ? { ...kept, at: Date.now() } : null),
|
|
213
|
+
write: () => Promise.resolve(),
|
|
214
|
+
} }
|
|
215
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// An administrator's read-only view as another user, for checking what that
|
|
2
|
+
// account can access. The view is a session row for the viewed user (so every
|
|
3
|
+
// access check applies that user's role, teams and permissions unchanged) tied
|
|
4
|
+
// to the admin's own session, carried in its own cookie beside the session
|
|
5
|
+
// cookie. Ending the admin session, or the admin role, ends the view.
|
|
6
|
+
//
|
|
7
|
+
// The router refuses writes for any request that carries the view cookie, and
|
|
8
|
+
// such requests never act with the viewed user's GitHub credentials: GitHub
|
|
9
|
+
// data that needs them is left out instead.
|
|
10
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
11
|
+
import type { ManagedConfig } from './config.ts'
|
|
12
|
+
import type { ManagedDb, ManagedSession } from './db.ts'
|
|
13
|
+
import { hashToken, randomToken } from './crypto.ts'
|
|
14
|
+
import { VIEW_COOKIE, buildCookie, clearCookie, cookieName, parseCookies } from './session.ts'
|
|
15
|
+
|
|
16
|
+
export const VIEW_AS_PATH = '/api/auth/view-as'
|
|
17
|
+
export const VIEW_ONLY_ERROR = 'view-only'
|
|
18
|
+
|
|
19
|
+
function viewCookieName(config: ManagedConfig): string {
|
|
20
|
+
return cookieName(config, VIEW_COOKIE)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// The raw view token, or null when the request carries no view.
|
|
24
|
+
export function viewToken(config: ManagedConfig, cookieHeader: string | undefined): string | null {
|
|
25
|
+
const token = parseCookies(cookieHeader).get(viewCookieName(config))
|
|
26
|
+
return token == null || token === '' ? null : token
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function clearViewCookie(config: ManagedConfig): string {
|
|
30
|
+
return clearCookie(viewCookieName(config), config.cookieSecure)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Open a view as `userId` from the admin's `session`, replacing any earlier
|
|
34
|
+
// view of that session. Null when the database refuses it (see createViewSession).
|
|
35
|
+
export async function startViewSession(config: ManagedConfig, db: ManagedDb, session: ManagedSession, userId: string, now: number): Promise<string | null> {
|
|
36
|
+
const token = randomToken()
|
|
37
|
+
const ok = await db.createViewSession({ id: hashToken(token), viewerSessionId: session.id, userId, csrfToken: randomToken() }, now)
|
|
38
|
+
if (!ok) return null
|
|
39
|
+
return buildCookie(viewCookieName(config), token, {
|
|
40
|
+
maxAgeS: Math.max(0, Math.floor((session.expiresAt - now) / 1000)), secure: config.cookieSecure, sameSite: 'Lax',
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function endViewSession(config: ManagedConfig, db: ManagedDb, cookieHeader: string | undefined): Promise<void> {
|
|
45
|
+
const token = viewToken(config, cookieHeader)
|
|
46
|
+
if (token != null) await db.deleteSession(hashToken(token))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const viewing = new AsyncLocalStorage<boolean>()
|
|
50
|
+
|
|
51
|
+
export function runViewing<T>(active: boolean, work: () => T): T {
|
|
52
|
+
return active ? viewing.run(true, work) : work()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Whether the current request is an admin's view as another user.
|
|
56
|
+
export function isViewing(): boolean {
|
|
57
|
+
return viewing.getStore() === true
|
|
58
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { linkRevision } from './link-reports.ts'
|
|
2
|
+
import type { ManagedSql } from './sql.ts'
|
|
3
|
+
import type { BundleBuildConditions, ManagedBundle, TeamReportAccessSnapshot, UserTeam } from './db.ts'
|
|
4
|
+
import { type TeamUserPermissions, parseTeamUserPermissions } from '../common/managed/permissions.ts'
|
|
5
|
+
|
|
6
|
+
export const WORKSPACE_SHARE_SCHEMA = `
|
|
7
|
+
CREATE TABLE IF NOT EXISTS managed_workspace_share (
|
|
8
|
+
token_hash TEXT PRIMARY KEY,
|
|
9
|
+
team_id TEXT NOT NULL REFERENCES managed_team(id) ON DELETE CASCADE,
|
|
10
|
+
created_by TEXT NOT NULL REFERENCES managed_user(id) ON DELETE CASCADE,
|
|
11
|
+
created_at INTEGER NOT NULL,
|
|
12
|
+
dependencies INTEGER NOT NULL DEFAULT 0,
|
|
13
|
+
security INTEGER NOT NULL DEFAULT 0
|
|
14
|
+
) STRICT;
|
|
15
|
+
CREATE INDEX IF NOT EXISTS managed_workspace_share_team_idx ON managed_workspace_share(team_id);
|
|
16
|
+
`
|
|
17
|
+
|
|
18
|
+
export interface WorkspaceShareSnapshot extends TeamReportAccessSnapshot {
|
|
19
|
+
team: UserTeam
|
|
20
|
+
permissions: TeamUserPermissions
|
|
21
|
+
repositories: { repoId: number; github: string; path: string }[]
|
|
22
|
+
bundles: ManagedBundle[]
|
|
23
|
+
}
|
|
24
|
+
export interface WorkspaceShareInfo {
|
|
25
|
+
id: string
|
|
26
|
+
createdAt: number
|
|
27
|
+
createdBy: string
|
|
28
|
+
permissions: TeamUserPermissions
|
|
29
|
+
}
|
|
30
|
+
export interface ManagedWorkspaceShare extends WorkspaceShareInfo {
|
|
31
|
+
teamId: string
|
|
32
|
+
teamName: string
|
|
33
|
+
teamSlug: string
|
|
34
|
+
}
|
|
35
|
+
export type WorkspaceShareFeedState = { grant: string; catalog: number; annotations: number }
|
|
36
|
+
export interface WorkspaceShareStore {
|
|
37
|
+
createWorkspaceShare(sessionId: string, now: number, teamId: string, tokenHash: string, permissions?: TeamUserPermissions): Promise<boolean>
|
|
38
|
+
listWorkspaceShares(sessionId: string, now: number, teamId: string): Promise<WorkspaceShareInfo[] | null>
|
|
39
|
+
listManagedWorkspaceShares(sessionId: string, now: number): Promise<ManagedWorkspaceShare[] | null>
|
|
40
|
+
updateWorkspaceShare(sessionId: string, now: number, teamId: string, id: string, permissions: TeamUserPermissions): Promise<boolean>
|
|
41
|
+
revokeWorkspaceShares(sessionId: string, now: number, teamId: string, id?: string): Promise<boolean>
|
|
42
|
+
getWorkspaceShare(tokenHash: string): Promise<WorkspaceShareSnapshot | null>
|
|
43
|
+
getWorkspaceShareFeedState(tokenHash: string): Promise<WorkspaceShareFeedState | null>
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// A share is a capability, never a user session. Its issuer must still manage
|
|
47
|
+
// this team on every read. No union of the issuer's other memberships applies.
|
|
48
|
+
function shareQueries(db: ManagedSql) {
|
|
49
|
+
const manager = db.prepare(`SELECT 1 FROM managed_session s
|
|
50
|
+
JOIN managed_user u ON u.id = s.user_id
|
|
51
|
+
JOIN managed_team t ON t.id = ? AND t.hidden = 0
|
|
52
|
+
WHERE s.id = ? AND s.expires_at > ? AND (u.role = 'admin' OR (u.role = 'manage'
|
|
53
|
+
AND EXISTS (SELECT 1 FROM managed_team_user tu WHERE tu.user_id = u.id AND tu.team_id = t.id)))`)
|
|
54
|
+
const insert = db.prepare(`INSERT INTO managed_workspace_share (token_hash, team_id, created_by, created_at, dependencies, security)
|
|
55
|
+
SELECT ?, ?, user_id, ?, ?, ? FROM managed_session WHERE id = ?`)
|
|
56
|
+
const remove = db.prepare('DELETE FROM managed_workspace_share WHERE team_id = ? AND (CAST(? AS TEXT) IS NULL OR token_hash = ?)')
|
|
57
|
+
const update = db.prepare('UPDATE managed_workspace_share SET dependencies = ?, security = ? WHERE team_id = ? AND token_hash = ?')
|
|
58
|
+
// The management ID is a hash, never a bearer token. Existing URLs remain
|
|
59
|
+
// unchanged when their permissions are edited; plaintext tokens aren't kept.
|
|
60
|
+
const list = db.prepare(`SELECT s.token_hash AS id, s.created_at AS createdAt, u.login AS createdBy, s.dependencies, s.security
|
|
61
|
+
FROM managed_workspace_share s JOIN managed_user u ON u.id = s.created_by WHERE s.team_id = ? ORDER BY s.created_at DESC, s.token_hash`)
|
|
62
|
+
const session = db.prepare(`SELECT u.id, u.role FROM managed_session s JOIN managed_user u ON u.id = s.user_id
|
|
63
|
+
WHERE s.id = ? AND s.expires_at > ? AND u.role IN ('manage', 'admin')`)
|
|
64
|
+
const all = db.prepare(`SELECT s.token_hash AS id, s.created_at AS createdAt, u.login AS createdBy, s.dependencies, s.security,
|
|
65
|
+
t.id AS teamId, t.slug AS teamSlug, t.name AS teamName FROM managed_workspace_share s
|
|
66
|
+
JOIN managed_user u ON u.id = s.created_by JOIN managed_team t ON t.id = s.team_id AND t.hidden = 0
|
|
67
|
+
WHERE ? = 'admin' OR EXISTS (SELECT 1 FROM managed_team_user tu WHERE tu.user_id = ? AND tu.team_id = s.team_id)
|
|
68
|
+
ORDER BY t.name, t.id, s.created_at DESC, s.token_hash`)
|
|
69
|
+
const shareGrant = `FROM managed_workspace_share s
|
|
70
|
+
JOIN managed_user u ON u.id = s.created_by
|
|
71
|
+
JOIN managed_team t ON t.id = s.team_id AND t.hidden = 0
|
|
72
|
+
WHERE s.token_hash = ? AND (u.role = 'admin' OR (u.role = 'manage'
|
|
73
|
+
AND EXISTS (SELECT 1 FROM managed_team_user tu WHERE tu.user_id = u.id AND tu.team_id = s.team_id)))`
|
|
74
|
+
const share = db.prepare(`SELECT t.id, t.slug, t.name, s.dependencies, s.security ${shareGrant}`)
|
|
75
|
+
// Validate the capability and its issuer every poll; global counters are
|
|
76
|
+
// internal hints that let unchanged feeds skip rebuilding the full catalog.
|
|
77
|
+
const feed = db.prepare(`SELECT t.id, s.dependencies, s.security,
|
|
78
|
+
(SELECT catalog FROM managed_change_revision WHERE id = 1) AS catalog,
|
|
79
|
+
(SELECT annotations FROM managed_change_revision WHERE id = 1) AS annotations ${shareGrant}`)
|
|
80
|
+
const repositories = db.prepare(`SELECT tr.repo_id AS repoId, sr.full_name AS github, tr.path
|
|
81
|
+
FROM managed_team_repo tr JOIN managed_selected_repo sr ON sr.repo_id = tr.repo_id
|
|
82
|
+
WHERE tr.team_id = ? ORDER BY tr.repo_id, tr.path`)
|
|
83
|
+
const reports = db.prepare(`SELECT DISTINCT r.id, r.slug, r.filename, r.analyzer, r.byte_size AS byteSize,
|
|
84
|
+
r.sha256, r.repo_directory AS directory, sr.full_name AS github
|
|
85
|
+
FROM managed_team_repo tr JOIN managed_report r ON r.repo_id = tr.repo_id
|
|
86
|
+
JOIN managed_selected_repo sr ON sr.repo_id = r.repo_id
|
|
87
|
+
WHERE tr.team_id = ? AND r.visible = 1 AND (tr.path = '' OR r.repo_directory = tr.path
|
|
88
|
+
OR substr(r.repo_directory, 1, length(tr.path) + 1) = tr.path || '/') ORDER BY r.id`)
|
|
89
|
+
// A raw bundle is visible only when its declared root is inside the team scope.
|
|
90
|
+
// Root bundles remain hidden from directory-only grants.
|
|
91
|
+
const bundles = db.prepare(`SELECT DISTINCT b.id, b.slug, b.integrity, b.filename, b.kind, b.byte_size AS byteSize,
|
|
92
|
+
b.uploaded_by AS uploadedBy, b.repo_id AS repoId, b.repo_directory AS repoDirectory, b.uploaded_at AS uploadedAt, b.provenance, b.build_conditions AS buildConditions, sr.full_name AS repoFullName
|
|
93
|
+
FROM managed_team_repo tr JOIN managed_bundle b ON b.repo_id = tr.repo_id
|
|
94
|
+
JOIN managed_selected_repo sr ON sr.repo_id = b.repo_id
|
|
95
|
+
WHERE tr.team_id = ? AND b.visible = 1 AND (tr.path = '' OR b.repo_directory = tr.path
|
|
96
|
+
OR substr(b.repo_directory, 1, length(tr.path) + 1) = tr.path || '/') ORDER BY b.id`)
|
|
97
|
+
return { manager, insert, remove, update, list, session, all, share, feed, repositories, reports, bundles }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function shareInfo<T extends { dependencies: number; security: number }>(row: T) {
|
|
101
|
+
const { dependencies, security, ...rest } = row
|
|
102
|
+
return { ...rest, permissions: { dependencies: dependencies === 1, security: security === 1 } }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function workspaceShareMethods(db: ManagedSql): WorkspaceShareStore {
|
|
106
|
+
const q = shareQueries(db)
|
|
107
|
+
return {
|
|
108
|
+
async createWorkspaceShare(sessionId, now, teamId, tokenHash, permissions) {
|
|
109
|
+
if (!await q.manager.get(teamId, sessionId, now)) return false
|
|
110
|
+
const p = parseTeamUserPermissions(permissions)
|
|
111
|
+
await q.insert.run(tokenHash, teamId, now, +p.dependencies, +p.security, sessionId)
|
|
112
|
+
return true
|
|
113
|
+
},
|
|
114
|
+
async listWorkspaceShares(sessionId, now, teamId) {
|
|
115
|
+
if (!await q.manager.get(teamId, sessionId, now)) return null
|
|
116
|
+
const rows = await q.list.all(teamId) as { id: string; createdAt: number; createdBy: string; dependencies: number; security: number }[]
|
|
117
|
+
return rows.map(shareInfo)
|
|
118
|
+
},
|
|
119
|
+
async listManagedWorkspaceShares(sessionId, now) {
|
|
120
|
+
const viewer = await q.session.get(sessionId, now) as { id: string; role: string } | undefined
|
|
121
|
+
if (!viewer) return null
|
|
122
|
+
const rows = await q.all.all(viewer.role, viewer.id) as (Omit<ManagedWorkspaceShare, 'permissions'> & { dependencies: number; security: number })[]
|
|
123
|
+
return rows.map(shareInfo)
|
|
124
|
+
},
|
|
125
|
+
async updateWorkspaceShare(sessionId, now, teamId, id, permissions) {
|
|
126
|
+
if (!await q.manager.get(teamId, sessionId, now)) return false
|
|
127
|
+
const p = parseTeamUserPermissions(permissions)
|
|
128
|
+
return (await q.update.run(+p.dependencies, +p.security, teamId, id)).changes > 0
|
|
129
|
+
},
|
|
130
|
+
async revokeWorkspaceShares(sessionId, now, teamId, id) {
|
|
131
|
+
if (!await q.manager.get(teamId, sessionId, now)) return false
|
|
132
|
+
const result = await q.remove.run(teamId, id ?? null, id ?? null)
|
|
133
|
+
return id === undefined || result.changes > 0
|
|
134
|
+
},
|
|
135
|
+
async getWorkspaceShareFeedState(tokenHash) {
|
|
136
|
+
const row = await q.feed.get(tokenHash) as { id: string; dependencies: number; security: number; catalog: number; annotations: number } | undefined
|
|
137
|
+
return row ? { grant: JSON.stringify([row.id, row.dependencies, row.security]), catalog: row.catalog, annotations: row.annotations } : null
|
|
138
|
+
},
|
|
139
|
+
async getWorkspaceShare(tokenHash) {
|
|
140
|
+
const grant = await q.share.get(tokenHash) as { id: string; slug: string; name: string; dependencies: number; security: number } | undefined
|
|
141
|
+
if (!grant) return null
|
|
142
|
+
const { dependencies, security, ...team } = grant
|
|
143
|
+
const permissions = { dependencies: dependencies === 1, security: security === 1 }
|
|
144
|
+
const rows = await q.reports.all(team.id) as { id: string; slug: string; filename: string; analyzer: string | null; byteSize: number; sha256: string; directory: string; github: string }[]
|
|
145
|
+
const links = await linkRevision(db)
|
|
146
|
+
const bundleRows = (await q.bundles.all(team.id) as (Omit<ManagedBundle, 'buildConditions'> & { buildConditions: string | null; repoFullName: string })[])
|
|
147
|
+
.map(row => ({ ...row, buildConditions: row.buildConditions == null ? null : JSON.parse(row.buildConditions) as BundleBuildConditions }))
|
|
148
|
+
return {
|
|
149
|
+
user: { id: `share:${tokenHash}`, login: 'public', name: 'Public workspace', avatarUrl: null, role: 'view' },
|
|
150
|
+
teamId: team.id,
|
|
151
|
+
linkRevision: links,
|
|
152
|
+
permissions,
|
|
153
|
+
repositories: await q.repositories.all(team.id) as WorkspaceShareSnapshot['repositories'],
|
|
154
|
+
reports: rows.map(row => ({ id: row.id, filename: row.filename, byteSize: row.byteSize, sha256: row.sha256,
|
|
155
|
+
repo: { github: row.github, directory: row.directory }, permissions })),
|
|
156
|
+
team: { ...team, permissions, cacheKey: links,
|
|
157
|
+
reports: rows.map(row => ({ id: row.id, slug: row.slug, filename: row.filename, visible: true, analyzer: row.analyzer, repoFullName: row.github, repoDirectory: row.directory,
|
|
158
|
+
cacheKey: JSON.stringify([row.sha256, row.github, row.directory, row.filename, permissions, links]) })),
|
|
159
|
+
bundles: bundleRows.map(row => ({ id: row.id, slug: row.slug, integrity: row.integrity,
|
|
160
|
+
filename: row.filename, visible: true, kind: row.kind, byteSize: row.byteSize, repoId: row.repoId!, repoDirectory: row.repoDirectory, repoFullName: row.repoFullName, provenance: row.provenance })),
|
|
161
|
+
},
|
|
162
|
+
bundles: bundleRows.map(row => ({ ...row, visible: true })),
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
}
|
package/server.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util'
|
|
2
|
+
import type { Server } from 'node:http'
|
|
3
|
+
import { startServer } from './server-common/standalone.ts'
|
|
4
|
+
|
|
5
|
+
const modes = ['e2e', 'managed', 'managed-e2e', 'e2e-managed'] as const
|
|
6
|
+
export type ServerMode = typeof modes[number]
|
|
7
|
+
|
|
8
|
+
function parseMode(mode: string): ServerMode {
|
|
9
|
+
for (const candidate of modes) if (mode === candidate) return candidate
|
|
10
|
+
throw new Error(`Unknown mode: ${mode}. Expected ${modes.join(', ')}.`)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Assemble the selected server without binding a port. Hosts can reuse its
|
|
14
|
+
// request/upgrade listeners or call listen() themselves.
|
|
15
|
+
export async function init(mode: ServerMode = 'e2e'): Promise<Server> {
|
|
16
|
+
const selected = parseMode(mode)
|
|
17
|
+
if (selected === 'e2e') {
|
|
18
|
+
const { createE2eApp } = await import('./server-e2e/app.ts')
|
|
19
|
+
return (await createE2eApp()).httpServer
|
|
20
|
+
}
|
|
21
|
+
if (selected === 'managed') {
|
|
22
|
+
const managed = await import('./server-managed/index.ts')
|
|
23
|
+
return managed.init()
|
|
24
|
+
}
|
|
25
|
+
const combined = await import('./server-managed/combined.ts')
|
|
26
|
+
return combined.init(selected === 'managed-e2e' ? 'managed+e2e' : 'e2e+managed')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function start(): Promise<void> {
|
|
30
|
+
const { values } = parseArgs({ options: {
|
|
31
|
+
mode: { type: 'string', default: 'e2e' },
|
|
32
|
+
help: { type: 'boolean', short: 'h' },
|
|
33
|
+
} })
|
|
34
|
+
const mode = parseMode(values.mode)
|
|
35
|
+
if (values.help) {
|
|
36
|
+
console.log(`Usage: node cli.js --mode <${modes.join('|')}>
|
|
37
|
+
Defaults to e2e. Combined modes share HOST/PORT; the first mode is the client default.
|
|
38
|
+
Managed modes require GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET and OAUTH_CALLBACK_URL.
|
|
39
|
+
Combined storage: DATABASE_URL for a shared Neon DB, or E2E_DATABASE_URL and
|
|
40
|
+
MANAGED_DATABASE_URL for separate Neon DBs. Do not mix shared and per-mode URLs.
|
|
41
|
+
Without URLs: SQLite at DB_PATH for e2e and MANAGED_DB_PATH for managed.
|
|
42
|
+
Combined mode does not support mixing Neon and SQLite.`)
|
|
43
|
+
} else {
|
|
44
|
+
const config = mode === 'managed'
|
|
45
|
+
? (await import('./server-managed/config.ts')).loadManagedConfig()
|
|
46
|
+
: (await import('./server-e2e/config.ts')).loadConfig()
|
|
47
|
+
const server = await init(mode)
|
|
48
|
+
startServer(server, config)
|
|
49
|
+
}
|
|
50
|
+
}
|