← 中文资源

简体中文版本

为移动 API 客户端固定 TLS 证书

嵌入期望的证书哈希,使被入侵的 CA 或 MITM 代理即使链技术上有效也被拒。

阅读英文原文

关闭攻击面

先测量问题。嵌入期望的证书哈希,使被入侵的 CA 或 MITM 代理即使链技术上有效也被拒。 最常见的错误是未确认门禁能捕获你关心的问题就先加上。在接入 CI 之前,先对当前代码库运行 const expected = ‘sha256/abc123…’; if (cert.fingerprint !== expected) throw new Error(‘pin mismatch’); 并检查输出。

const expected = 'sha256/abc123...'; if (cert.fingerprint !== expected) throw new Error('pin mismatch');

验证与监控

门禁就位后,让失败可操作。const expected = ‘sha256/abc123…’; if (cert.fingerprint !== expected) throw new Error(‘pin mismatch’); 失败时应指向具体文件和行号,而非泛泛的错误。在锁文件中固定工具版本,防止上游发布悄悄改变行为。每次拉取请求都应像审查测试结果一样审查门禁输出。

验证

在本地运行 const expected = ‘sha256/abc123…’; if (cert.fingerprint !== expected) throw new Error(‘pin mismatch’); 确认通过后推送。CI 中门禁应阻断不合规的拉取请求。若人为绕过门禁直接推送,应在下次定期扫描中被发现。

注意事项

  • The gate only runs on events that trigger the workflow; locally pushed commits bypass it.
  • Configuration drift is possible if someone edits settings outside version control.