如何在5分钟内向您的网站添加 security.txt 文件(使用生成器)

发布: (2025年12月3日 GMT+8 23:53)
6 min read
原文: Dev.to

Source: Dev.to

如果你在 2026 年运营网站,安全应该是你关心的重点。
你可能已经使用了 HTTPS、HSTS、CSP,甚至还有漏洞赏金计划。
但仍有一个大多数站点会忽略的微小文本文件:

/.well-known/security.txt

它是一个简单的文件,告诉安全研究人员如果在你的网站上发现漏洞,应该如何联系你。

什么是 security.txt

security.txt 是一个提议中的标准(RFC 9116),用于在网站上公布安全联系信息。
它的工作方式类似于 robots.txt

文件用途
robots.txt告诉搜索引擎如何抓取你的网站
security.txt告诉安全研究人员如何报告漏洞

该文件通常位于:

https://example.com/.well-known/security.txt

文件内容可以包括:

  • 联系方式(电子邮件或 URL)
  • 你的安全策略链接
  • 可选:你的 PGP 密钥、致谢页面、招聘信息等

当有人发现漏洞时,他们可以直接访问该 URL 并按照指示报告,而不是猜测该把报告发到哪里。

为什么重要

  • 许多安全问题因为没有明确的报告渠道而未被上报。

  • security.txt 文件的作用是:“如果你发现了问题,请按以下方式告诉我们。”

  • 即使是小型 SaaS、独立项目或副业项目,只要添加一行类似的内容,就会显得更专业:

    Policy: https://example.com/security-policy
    Contact: mailto:security@example.com
  • 这只是一个小文本文件,使用 HTTPS 提供服务,偶尔更新一次——属于“现在花 10 分钟,日后可能救命”的任务。

可包含的字段

标准定义了多个字段。并不需要全部使用,最常用的有:

字段示例
ContactContact: mailto:security@example.com
Contact: https://example.com/security-contact
ExpiresExpires: 2026-01-01T00:00:00Z
PolicyPolicy: https://example.com/security-policy
AcknowledgmentsAcknowledgments: https://example.com/security-acknowledgments
EncryptionEncryption: https://example.com/pgp-key.txt
HiringHiring: https://example.com/careers
CanonicalCanonical: https://example.com/.well-known/security.txt

你也可以使用 # 添加注释:

# Example Corp 的安全联系人
Contact: mailto:security@example.com
Policy: https://example.com/security-policy
Expires: 2027-01-01T00:00:00Z

就是这样——一个结构化的文本文件而已。

手动创建 security.txt

  1. 阅读 RFC(或博客文章)以了解语法。
  2. 决定需要哪些字段。
  3. 复制/粘贴示例并根据自己站点进行调整。
  4. 确保 Expires 日期是有效的未来时间戳。
  5. 将文件保存为 security.txt,放入 /.well-known/,部署并测试。

使用生成器

网上有多个在线生成器。下面以 CodersTool security.txt 生成器 为例:

  1. 在浏览器中打开生成器。
  2. 填写关键字段(Contact、Policy、Expires,可选字段)。
  3. 点击 Generate
  4. 复制输出或下载文件。
  5. 将其保存为 security.txt 并部署到 /.well-known/

生成的片段类似:

# security.txt for Example SaaS
Contact: mailto:security@example.com
Policy: https://example.com/security-policy
Acknowledgments: https://example.com/security-acknowledgments
Expires: 2027-01-01T00:00:00Z
Canonical: https://example.com/.well-known/security.txt

无需登录、无需项目设置、无需 CLI。

在不同平台上部署 security.txt

目标始终相同:让文件可以通过

https://yourdomain.com/.well-known/security.txt

访问。具体实现方式取决于你的技术栈。

静态站点(HTML/CSS/JS)

在站点根目录创建 .well-known 文件夹:

/your-site-root/
  index.html

  /.well-known/
    security.txt

像平常一样上传该文件夹并测试:

https://yourdomain.com/.well-known/security.txt

静态站点生成器(Hugo、Jekyll 等)

.well-known/security.txt 放入 staticpublic 文件夹,使其在最终构建产物中出现。

Laravel / PHP

方案 A – 静态文件

/public/
  index.php

  /.well-known/
    security.txt

Laravel 会让 Web 服务器直接提供该文件。

方案 B – 动态路由(如果想实时生成)

// routes/web.php
Route::get('/.well-known/security.txt', function () {
    return response(file_get_contents(public_path('.well-known/security.txt')))
        ->header('Content-Type', 'text/plain');
});

.NET / IIS

  1. 在站点根目录创建 .well-known 目录。
  2. security.txt 放进去。
  3. 确认 IIS 已允许提供 .txt 文件(默认配置通常已允许)。
/wwwroot/
  web.config

  /.well-known/
    security.txt

ASP.NET Core(Kestrel + 反向代理)

  • .well-known/security.txt 放入 wwwroot
  • 映射一个返回该文本文件的路由。

CDN / Cloudflare / Fastly

  • 确保源站提供 /.well-known/security.txt
  • 检查 CDN 是否拦截或改写了该路径。
  • 若 CDN 本身提供 security.txt 功能,决定是使用它还是让源站文件直接通过。

验证部署

  1. 在浏览器打开 https://yourdomain.com/.well-known/security.txt

  2. 你应该能看到自己创建的纯文本内容。

  3. 也可以使用 curl 检查:

    curl -I https://yourdomain.com/.well-known/security.txt

    确认返回状态为 200 OK,且 Content-Typetext/plain

验证通过后,任务完成——你的站点现在为安全研究人员提供了明确、标准化的联系渠道。

Back to Blog

相关文章

阅读更多 »

SaaS IA 新闻

SaaS IA 新闻的封面图片 https://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazon...

从混沌到代码:ALPHALABS

让我彻夜难眠的问题 我想要构建一个平台,让任何人都能创建 AI trading agents、backtest strategies,并证明其 performance……