fix(security): 修复模板预览任意文件读取 - #29
Open
PGwind wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
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
themeto a safe directory-name pattern and restrictsfileto an allowlist of known template filenames. - Uses
realpath()plus directory boundary checks to ensure the resolved theme directory and template file stay within the intendedtemplate/subtree. - Escapes
theme/filein 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题说明
template/preview.php直接使用请求参数theme和file拼接本地文件路径,并将结果传给file_get_contents()。由于缺少文件名限制和路径边界检查,可能导致预览接口读取模板目录之外的本地文件。
此外,
theme和file会直接输出到 HTML 标题中,存在未转义输出问题。修复内容
owner.htmlguest.htmlnotice.htmlrealpath()获取规范路径template目录内htmlspecialchars()转义页面标题兼容性影响
不涉及数据库、插件配置和模板格式变更。
原有模板预览链接无需修改。
基础检查
php -l template/preview.php通过git diff --check通过为避免进一步扩散,PR 中不提供具体利用地址和攻击参数。