核心要点为 `@scope` 映射受控注册表;强制 `https` 与白名单域名。包名与注册表一致性校验;跨作用域访问默认拒绝并记录审计。实现示例type ScopeMap = { [scope: string]: string }

const allowRegistries = new Set<string>(['https://registry.npmjs.org','https://registry.example.com'])

function validRegistry(u: string): boolean {

try {

const url = new URL(u)

return url.protocol === 'https:' && allowRegistries.has(url.origin)

} catch {

return false

}

}

function parseName(name: string): { scope?: string; pkg: string } {

if (name.startsWith('@')) {

const parts = name.split('/')

return { scope: parts[0], pkg: parts[1] || '' }

}

return { pkg: name }

}

function allowedPackage(name: string, registry: string, map: ScopeMap): boolean {

if (!validRegistry(registry)) return false

const { scope } = parseName(name)

if (!scope) return allowRegistries.has(new URL(registry).origin)

const target = map[scope]

return !!target && new URL(target).origin === new URL(registry).origin

}

审计与运行治理审计记录映射与访问结果;变更需审批并进行回归校验与回退。生产环境锁定映射文件并启用只读令牌与严格 SSL。* Add File: 构建可重复性与位级一致性治理(Hermetic-环境锁定-哈希)最佳实践.md---title: 构建可重复性与位级一致性治理(Hermetic-环境锁定-哈希)最佳实践categories:安全治理DevSecOps构建治理keywords:可重复性Hermetic位级一致性哈希环境锁定description: 通过环境与工具版本锁定、依赖冻结与产物哈希比对,确保构建可重复与位级一致,提升发布可信度。---核心要点锁定工具链与依赖版本;禁用非确定性输入与环境漂移。构建产物哈希比对;不一致时阻断并输出差异与证据。实现示例type EnvSpec = { node: string; npm: string; os: string; arch: string }

type Artifact = { path: string; sha256: string }

function semverValid(v: string): boolean {

return /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/.test(v)

}

function envLocked(e: EnvSpec): boolean {

return semverValid(e.node) && semverValid(e.npm) && !!e.os && !!e.arch

}

async function sha256Hex(buf: Uint8Array): Promise<string> {

const d = await crypto.subtle.digest('SHA-256', buf)

return Buffer.from(d).toString('hex')

}

function compareArtifacts(actual: Artifact[], expected: Artifact[]): { ok: boolean; diffs: string[] } {

const map = new Map<string, string>()

for (const e of expected) map.set(e.path, e.sha256.toLowerCase())

const diffs: string[] = []

for (const a of actual) {

const exp = map.get(a.path)

if (!exp || exp !== a.sha256.toLowerCase()) diffs.push(a.path)

}

return { ok: diffs.length === 0, diffs }

}

function reproducible(env: EnvSpec, actual: Artifact[], expected: Artifact[]): boolean {

if (!envLocked(env)) return false

const cmp = compareArtifacts(actual, expected)

return cmp.ok

}

审计与CI门禁记录环境规范、哈希比对结果与差异清单;不一致阻断并触发回滚或重建。禁止未锁定环境构建;对工具链与缓存进行隔离与校验。

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部