官方纰漏bug修复和安全能力提升

This commit is contained in:
Weiming committed 2026-12-02 14:12:35 +08:00
1 parent 459339719d
commit ce00640ea4
148 files changed
+9524 -1701

No files matched your search

+250
View File
@@ -0,0 +1,250 @@
import { createHash } from 'node:crypto'
import {
existsSync,
lstatSync,
readFileSync,
readdirSync,
realpathSync,
statSync,
writeFileSync,
} from 'node:fs'
import path from 'node:path'
export const ARTIFACT_MANIFEST_FILENAME = 'artifact-manifest.json'
export const SOURCE_INPUTS = [
'src',
'scripts',
'public',
'index.html',
'package.json',
'package-lock.json',
'vite.config.ts',
'tsconfig.json',
'tsconfig.node.json',
'postcss.config.js',
'tailwind.config.js',
]
function isWithin(root, candidate) {
const relative = path.relative(root, candidate)
return relative === '' || (!relative.startsWith(`..${path.sep}`) && relative !== '..' && !path.isAbsolute(relative))
}
function canonicalizeCandidate(candidate) {
let existingAncestor = path.resolve(candidate)
const missingSegments = []
while (!existsSync(existingAncestor)) {
const parent = path.dirname(existingAncestor)
if (parent === existingAncestor) {
break
}
missingSegments.unshift(path.basename(existingAncestor))
existingAncestor = parent
}
const canonicalAncestor = existsSync(existingAncestor)
? realpathSync.native(existingAncestor)
: existingAncestor
return path.resolve(canonicalAncestor, ...missingSegments)
}
export function parseArtifactOutDir(argv) {
let outDir
for (let index = 0; index < argv.length; index += 1) {
const argument = argv[index]
if (argument === '--outDir') {
if (outDir !== undefined || index + 1 >= argv.length || argv[index + 1].startsWith('--')) {
throw new Error('Exactly one non-empty --outDir value is required')
}
outDir = argv[index + 1]
index += 1
continue
}
if (argument.startsWith('--outDir=')) {
if (outDir !== undefined) {
throw new Error('Exactly one non-empty --outDir value is required')
}
outDir = argument.slice('--outDir='.length)
continue
}
throw new Error(`Unsupported build:artifact argument: ${argument}`)
}
if (!outDir || outDir.trim() === '') {
throw new Error('A non-empty --outDir is required; release artifacts must not use financial_system_frontend/dist')
}
return outDir
}
export function resolveArtifactOutDir(rawOutDir, {
cwd,
frontendRoot,
repoRoot,
ciArtifactRoot,
}) {
const resolvedFrontendRoot = canonicalizeCandidate(frontendRoot)
const resolvedRepoRoot = canonicalizeCandidate(repoRoot)
const target = canonicalizeCandidate(path.resolve(cwd, rawOutDir))
const filesystemRoot = path.parse(target).root
if (target === filesystemRoot) {
throw new Error('Artifact output cannot be a filesystem root')
}
const frontendDist = canonicalizeCandidate(path.join(resolvedFrontendRoot, 'dist'))
if (isWithin(frontendDist, target)) {
throw new Error('Artifact output cannot be financial_system_frontend/dist or any path below it')
}
if (!isWithin(resolvedRepoRoot, target)) {
return target
}
if (!ciArtifactRoot || ciArtifactRoot.trim() === '') {
throw new Error('Artifact output inside the repository requires an explicit CI_ARTIFACT_DIR')
}
const resolvedCiRoot = canonicalizeCandidate(path.resolve(cwd, ciArtifactRoot))
const protectedRoots = [
path.join(resolvedRepoRoot, '.git'),
path.join(resolvedFrontendRoot, 'src'),
path.join(resolvedFrontendRoot, 'scripts'),
path.join(resolvedFrontendRoot, 'public'),
path.join(resolvedFrontendRoot, 'dist'),
].map(canonicalizeCandidate)
if (!isWithin(resolvedRepoRoot, resolvedCiRoot)
|| protectedRoots.some((protectedRoot) => isWithin(protectedRoot, resolvedCiRoot))) {
throw new Error('CI_ARTIFACT_DIR must be a dedicated, unprotected directory inside the repository')
}
if (!isWithin(resolvedCiRoot, target)) {
throw new Error('Artifact output must stay within CI_ARTIFACT_DIR')
}
return target
}
export function assertArtifactDirectoryReady(outDir) {
if (!existsSync(outDir)) {
return
}
if (!statSync(outDir).isDirectory()) {
throw new Error(`Artifact output already exists and is not a directory: ${outDir}`)
}
if (readdirSync(outDir).length > 0) {
throw new Error(`Artifact output directory must be empty: ${outDir}`)
}
}
export function sha256File(filePath) {
return createHash('sha256').update(readFileSync(filePath)).digest('hex')
}
function collectFiles(directory, relativeDirectory = '') {
const files = []
const absoluteDirectory = path.join(directory, relativeDirectory)
for (const entry of readdirSync(absoluteDirectory, { withFileTypes: true })) {
const relativePath = path.join(relativeDirectory, entry.name)
const absolutePath = path.join(directory, relativePath)
const fileInfo = lstatSync(absolutePath)
if (fileInfo.isSymbolicLink()) {
throw new Error(`Artifact output must not contain symbolic links: ${relativePath}`)
}
if (fileInfo.isDirectory()) {
files.push(...collectFiles(directory, relativePath))
continue
}
if (!fileInfo.isFile()) {
throw new Error(`Artifact output contains an unsupported entry: ${relativePath}`)
}
if (relativeDirectory === '' && entry.name === ARTIFACT_MANIFEST_FILENAME) {
continue
}
files.push({
path: relativePath.split(path.sep).join('/'),
sha256: sha256File(absolutePath),
size: fileInfo.size,
})
}
return files
}
function collectSourcePath(frontendRoot, relativePath) {
const absolutePath = path.join(frontendRoot, relativePath)
if (!existsSync(absolutePath)) return []
const info = lstatSync(absolutePath)
if (info.isSymbolicLink()) throw new Error(`Source input must not be a symbolic link: ${relativePath}`)
if (info.isDirectory()) {
return readdirSync(absolutePath, { withFileTypes: true })
.flatMap(entry => collectSourcePath(frontendRoot, path.join(relativePath, entry.name)))
}
if (!info.isFile()) throw new Error(`Source input is not a regular file: ${relativePath}`)
return [{
path: `financial_system_frontend/${relativePath.split(path.sep).join('/')}`,
sha256: sha256File(absolutePath),
size: info.size,
}]
}
export function collectSourceInputs(frontendRoot, inputs = SOURCE_INPUTS) {
return inputs.flatMap(relativePath => collectSourcePath(frontendRoot, relativePath))
.sort((left, right) => left.path === right.path ? 0 : left.path < right.path ? -1 : 1)
}
export function sourceTreeSha256(files) {
const canonical = files.map(file => `${file.path}\0${file.size}\0${file.sha256}\n`).join('')
return createHash('sha256').update(canonical, 'utf8').digest('hex')
}
export const gitStatusIsDirty = (porcelainOutput) => porcelainOutput.trim().length > 0
export function createArtifactManifest({
outDir,
packageLockPath,
sourceRevision,
buildUtc,
nodeVersion,
npmVersion,
sourceDirty,
sourceFiles,
}) {
const normalizedRevision = sourceRevision?.trim()
if (!normalizedRevision) {
throw new Error('A source revision is required for a release artifact')
}
return {
schemaVersion: 1,
sourceRevision: normalizedRevision,
source: {
dirty: sourceDirty === true,
treeSha256: sourceTreeSha256(sourceFiles),
files: sourceFiles,
},
packageLock: {
path: 'financial_system_frontend/package-lock.json',
sha256: sha256File(packageLockPath),
},
build: {
utc: buildUtc,
nodeVersion,
npmVersion,
},
files: collectFiles(outDir).sort((left, right) => {
if (left.path === right.path) return 0
return left.path < right.path ? -1 : 1
}),
}
}
export function serializeArtifactManifest(manifest) {
return `${JSON.stringify(manifest, null, 2)}\n`
}
export function writeArtifactManifest(outDir, manifest) {
const manifestPath = path.join(outDir, ARTIFACT_MANIFEST_FILENAME)
writeFileSync(manifestPath, serializeArtifactManifest(manifest), { encoding: 'utf8', flag: 'wx' })
return manifestPath
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.