---
title: Dockerfile依赖拉取安全治理(pin digest-apt来源-校验)最佳实践
keywords:
- Dockerfile
- digest pin
- apt来源
- 校验
- 受控拉取
description: 针对 Dockerfile 的基础镜像与 apt 来源执行 digest 固定与来源白名单、校验策略,保障容器构建安全。
categories:
- 文章资讯
- 技术教程
---
实现示例
type Base = { image: string; digest?: string }
type Apt = { source: string; packages: string[] }
const allowRegistries = new Set<string>(['docker.io','ghcr.io','registry.example.com'])
function validImage(i: Base): boolean { const m = /^(\w[\w.-]+)\/(\w[\w.-]+):([\w.-]+)$/.exec(i.image); return !!m }
function hasDigest(i: Base): boolean { return !!i.digest && /^[A-Fa-f0-9]{64}$/.test(i.digest) }
function validApt(a: Apt): boolean { return /^https:\/\/.+/.test(a.source) }
function evaluate(base: Base, apt: Apt): { ok: boolean; errors: string[] } { const errors: string[] = []; if (!validImage(base)) errors.push('image'); if (!hasDigest(base)) errors.push('digest'); try { const u = new URL(apt.source); if (!allowRegistries.has(u.host)) errors.push('apt-host') } catch { errors.push('apt-source') } if (!validApt(apt)) errors.push('apt-url'); return { ok: errors.length === 0, errors } }
审计与CI门禁
- 审计基础镜像与
digest固定、apt 来源域;不合规阻断并回退。 - 构建时仅允许受控来源与校验通过的包。

发表评论 取消回复