Skip to content

fix(security): 修复模板预览任意文件读取 - #29

Open
PGwind wants to merge 1 commit into
jrotty:mainfrom
PGwind:fix/secure-template-preview
Open

fix(security): 修复模板预览任意文件读取#29
PGwind wants to merge 1 commit into
jrotty:mainfrom
PGwind:fix/secure-template-preview

Conversation

@PGwind

Copy link
Copy Markdown

问题说明

template/preview.php 直接使用请求参数 themefile 拼接本地文件路径,并将结果传给 file_get_contents()

由于缺少文件名限制和路径边界检查,可能导致预览接口读取模板目录之外的本地文件。

此外,themefile 会直接输出到 HTML 标题中,存在未转义输出问题。

修复内容

  • 限制主题目录名称只能包含字母、数字、下划线和连字符
  • 将可预览文件限制为:
    • owner.html
    • guest.html
    • notice.html
  • 使用 realpath() 获取规范路径
  • 确认主题目录位于 template 目录内
  • 确认模板文件位于选定的主题目录内
  • 使用 htmlspecialchars() 转义页面标题
  • 增加 CSP、禁止缓存等基础安全响应头
  • 保留原有模板预览地址和使用方式

兼容性影响

不涉及数据库、插件配置和模板格式变更。

原有模板预览链接无需修改。

基础检查

  • php -l template/preview.php 通过
  • git diff --check 通过

为避免进一步扩散,PR 中不提供具体利用地址和攻击参数。

CopilotAI review requested due to automatic review settings July 31, 2026 15:17

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the template preview endpoint (template/preview.php) against local file read and unescaped output issues by strictly constraining the previewable theme/file inputs and adding basic defensive response headers.

Changes:

  • Restricts theme to a safe directory-name pattern and restricts file to an allowlist of known template filenames.
  • Uses realpath() plus directory boundary checks to ensure the resolved theme directory and template file stay within the intended template/ subtree.
  • Escapes theme/file in the HTML <title> and adds baseline security/no-cache response headers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@PGwind