feat: 上线前安全加固与发布工程化;appmall 由插件迁移为独立应用
安全(P0) - 发布包签名:新增 scripts/package_keygen.php、scripts/package_verify.php、 ywxapp\service\PackageVerifier;FrameworkService::apply() 解压前强制验签 (ywxapp.framework_package_verify,默认开启) - JWT 收紧:config/jwt.php 移除公开默认密钥,JwtService::assertStrongSecret() 校验非空 / 长度≥32 / 已知弱值;.env.production.example 键名由 JWT_SECRET_KEY 修正为 JWT_SECRET - /api/v1/Article:免登录白名单收窄为只读接口,写操作字段白名单 + assertOwner() 越权校验 - 开发者令牌:查找接口不再回显明文、统一失败文案 + 限流 - TemplateInstaller:文件名白名单 + realpath 归一化 + 目录内断言,修复 Zip Slip;补 use ZipArchive - 后台登录:接入验证码、失败锁定与登录限流(config/ratelimit.php) - 缓存 / Session 驱动可由 .env 覆盖(CACHE_DRIVER / SESSION_TYPE),集群部署改 redis - paycenter 支付日志开关改为 PAY_LOG,不再跟随 APP_DEBUG 工程化 / 交付 - 新增 CHANGELOG.md 与 .env.production.example;composer.lock 入库锁定依赖版本 - 插件离线安装 CLI 增加 --uid,后台上传安装透传 uid,付费插件授权校验可通过 - 授权拒绝文案区分「未携带 uid」与「未找到有效授权」,便于排障 - .gitignore 放行 .env.production.example / .env.addon.example / .gitattributes / public/.htaccess 结构 - appmall 由 addon/appmall 迁移为独立应用 app/appmall(市场 / 开发者中心 / 审核 / 分润)
This commit is contained in:
1 parent
1d49e6f5ee
commit
18101cf11a
636 files changed
+58517
-9519
No files matched your search
@@ -45,9 +45,22 @@ class DocsFrontend extends FrontendBase
|
||||
'docsTitle' => $this->docsConfig['site_title'] ?? 'YwxApp 文档中心',
|
||||
'projectList' => DocsProject::enabledList(),
|
||||
'currentYear' => date('Y'),
|
||||
// 前台路由前缀:域名绑定/独立入口模式下为空串,路径模式下为 /docs。
|
||||
// 模板统一用 {$docsBase}/xxx 生成链接,两种访问方式自适应。
|
||||
'docsBase' => $this->docsBase(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前访问是否已把 docs 插件"独立成站"。逻辑统一在 docs_base_url() 全局助手。
|
||||
*
|
||||
* @see addon/docs/common.php
|
||||
*/
|
||||
protected function docsBase(): string
|
||||
{
|
||||
return docs_base_url();
|
||||
}
|
||||
|
||||
/**
|
||||
* 模板变量赋值
|
||||
*
|
||||
|
||||
@@ -14,6 +14,7 @@ use addon\docs\model\DocsDoc;
|
||||
use addon\docs\model\DocsProject;
|
||||
use addon\docs\model\DocsVersion;
|
||||
use addon\docs\service\DocTreeService;
|
||||
use think\facade\Cache;
|
||||
|
||||
/**
|
||||
* 文档阅读页(三栏布局)
|
||||
@@ -44,7 +45,7 @@ class Read extends DocsFrontend
|
||||
return $this->notFound('该项目暂无可阅读的文档');
|
||||
}
|
||||
|
||||
return redirect('/docs/' . $projectModel->name . '/' . $first['name']);
|
||||
return redirect($this->docsBase() . '/' . $projectModel->name . '/' . $first['name']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,7 +92,8 @@ class Read extends DocsFrontend
|
||||
}
|
||||
}
|
||||
|
||||
DocsDoc::addViews((int) $doc->id);
|
||||
// 阅读量去重:同一会话同一文档 5 分钟内不计,避免刷新/刷新/刷新刷虚高
|
||||
$this->bumpViews((int) $doc->id);
|
||||
|
||||
$tree = DocTreeService::buildTree($projectId, $versionId);
|
||||
$siblings = DocTreeService::siblings($projectId, $versionId, (int) $doc->id);
|
||||
@@ -101,7 +103,7 @@ class Read extends DocsFrontend
|
||||
[$content, $toc] = $this->buildToc((string) $doc->content);
|
||||
|
||||
// 默认版本用短地址,非默认版本地址里带版本段
|
||||
$baseUrl = '/docs/' . $projectModel->name;
|
||||
$baseUrl = $this->docsBase() . '/' . $projectModel->name;
|
||||
if ((int) $versionModel->is_default !== 1) {
|
||||
$baseUrl .= '/' . $versionModel->name;
|
||||
}
|
||||
@@ -260,21 +262,51 @@ class Read extends DocsFrontend
|
||||
*/
|
||||
protected function docUrl(DocsProject $project, DocsVersion $version, string $name): string
|
||||
{
|
||||
$base = $this->docsBase();
|
||||
// 默认版本走短地址,非默认版本带上版本段
|
||||
if ((int) $version->is_default === 1) {
|
||||
return '/docs/' . $project->name . '/' . $name;
|
||||
return $base . '/' . $project->name . '/' . $name;
|
||||
}
|
||||
return '/docs/' . $project->name . '/' . $version->name . '/' . $name;
|
||||
return $base . '/' . $project->name . '/' . $version->name . '/' . $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染 404 提示页
|
||||
* 渲染 404 提示页(同时返回 HTTP 404 状态码,避免被索引收录)
|
||||
*
|
||||
* @param string $message 提示语
|
||||
*/
|
||||
protected function notFound(string $message)
|
||||
{
|
||||
$this->assign('message', $message);
|
||||
return $this->fetch('read/notfound');
|
||||
// 关键:使用 HTTP 404 而非 200,搜索引擎不会收录软性 404 页。
|
||||
// 注意:$this->fetch() 返回的是渲染后的 HTML 字符串(不是 Response 对象),
|
||||
// 因此不能链式调用 ->code(),必须用 response($html, 404) 包装成响应。
|
||||
return response($this->fetch('read/notfound'), 404);
|
||||
}
|
||||
|
||||
/**
|
||||
* 阅读量计数(同一访客同一文档 5 分钟内只算 1 次)
|
||||
*
|
||||
* 采用缓存节流而非 Session:前台文档对游客开放,游客请求未必启动 Session
|
||||
* ($this->request->session() 在本框架返回数组而非 Store 对象),
|
||||
* 且阅读量属于统计类数据,不应强依赖会话。
|
||||
*
|
||||
* @param int $docId 文档ID
|
||||
*/
|
||||
protected function bumpViews(int $docId): void
|
||||
{
|
||||
if ($docId <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$fingerprint = md5($this->request->ip() . '|' . (string) $this->request->header('user-agent', ''));
|
||||
$key = 'docs_view:' . $docId . ':' . $fingerprint;
|
||||
|
||||
if (Cache::get($key, 0)) {
|
||||
return; // 5 分钟内已计过
|
||||
}
|
||||
|
||||
DocsDoc::addViews($docId);
|
||||
Cache::set($key, 1, 300);
|
||||
}
|
||||
}
|
||||
@@ -38,13 +38,18 @@ class Search extends DocsFrontend
|
||||
$list = DocSearchService::search($keyword, $projectId, $limit);
|
||||
}
|
||||
|
||||
// 关键词转义:搜索关键词原样输出到模板(input value、正文、提示等),
|
||||
// 不在控制器转义就会形成反射型 XSS(如 ?kw="><script>)。
|
||||
$keywordEsc = htmlspecialchars($keyword, ENT_QUOTES, 'UTF-8');
|
||||
|
||||
$this->assign([
|
||||
'keyword' => $keyword,
|
||||
'projectId' => $projectId,
|
||||
'list' => $list,
|
||||
'total' => count($list),
|
||||
'projects' => DocsProject::enabledList(),
|
||||
'pageTitle' => $keyword !== '' ? '搜索:' . $keyword : '文档搜索',
|
||||
'keyword' => $keyword,
|
||||
'keywordEsc' => $keywordEsc,
|
||||
'projectId' => $projectId,
|
||||
'list' => $list,
|
||||
'total' => count($list),
|
||||
'projects' => DocsProject::enabledList(),
|
||||
'pageTitle' => $keyword !== '' ? '搜索:' . $keywordEsc : '文档搜索',
|
||||
]);
|
||||
|
||||
return $this->fetch('search/index');
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\docs\controller;
|
||||
|
||||
use addon\docs\model\DocsDoc;
|
||||
use addon\docs\model\DocsProject;
|
||||
use addon\docs\model\DocsVersion;
|
||||
|
||||
/**
|
||||
* 站点地图(前台 SEO 用)
|
||||
*
|
||||
* 输出最简版 sitemap.xml:
|
||||
* - 首页 / 文档中心首页
|
||||
* - 所有启用项目的入口
|
||||
* - 所有启用项目下「默认版本」且 status=1 的文档(非默认版本不收录,避免长短 URL 双内容)
|
||||
*
|
||||
* 内容量大可后续改成分片/分项目,本版本供搜索引擎基本覆盖即可。
|
||||
*
|
||||
* @author ywxapp <admin@ywxapp.cn>
|
||||
*/
|
||||
class Sitemap extends DocsFrontend
|
||||
{
|
||||
/**
|
||||
* GET /sitemap.xml
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$base = docs_base_url();
|
||||
|
||||
$urls = [];
|
||||
// 首页
|
||||
$urls[] = ['loc' => $base . '/', 'priority' => '1.0'];
|
||||
// 各项目入口
|
||||
$projects = DocsProject::enabledList();
|
||||
foreach ($projects as $project) {
|
||||
$urls[] = [
|
||||
'loc' => $base . '/' . $project['name'],
|
||||
'priority' => '0.8',
|
||||
'lastmod' => $this->dateOf((int) ($project['update_at'] ?? 0)),
|
||||
];
|
||||
|
||||
// 该项目默认版本的可阅读文档(避免短/长地址双内容)
|
||||
$version = DocsVersion::resolve((int) $project['id'], (string) ($project['default_version'] ?? ''));
|
||||
if (!$version) {
|
||||
continue;
|
||||
}
|
||||
$docs = DocsDoc::where('project_id', $project['id'])
|
||||
->where('version_id', $version->id)
|
||||
->where('status', 1)
|
||||
->where('is_dir', 0)
|
||||
->field('name,update_at')
|
||||
->order('update_at', 'desc')
|
||||
->limit(5000)
|
||||
->select()
|
||||
->toArray();
|
||||
foreach ($docs as $doc) {
|
||||
$urls[] = [
|
||||
'loc' => $base . '/' . $project['name'] . '/' . rawurlencode($doc['name']),
|
||||
'priority' => '0.6',
|
||||
'lastmod' => $this->dateOf((int) ($doc['update_at'] ?? 0)),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
||||
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
|
||||
foreach ($urls as $u) {
|
||||
$xml .= '<url>';
|
||||
$xml .= '<loc>' . htmlspecialchars((string) $u['loc'], ENT_XML1, 'UTF-8') . '</loc>';
|
||||
if (!empty($u['lastmod'])) {
|
||||
$xml .= '<lastmod>' . $u['lastmod'] . '</lastmod>';
|
||||
}
|
||||
if (isset($u['priority'])) {
|
||||
$xml .= '<priority>' . $u['priority'] . '</priority>';
|
||||
}
|
||||
$xml .= '</url>';
|
||||
}
|
||||
$xml .= '</urlset>';
|
||||
|
||||
return response($xml, 200, ['Content-Type' => 'application/xml; charset=utf-8']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间戳 → W3C 日期(空值返回 null)
|
||||
*/
|
||||
protected function dateOf(int $ts): ?string
|
||||
{
|
||||
if ($ts <= 0) {
|
||||
return null;
|
||||
}
|
||||
return date('Y-m-d', $ts);
|
||||
}
|
||||
}
|
||||
@@ -206,10 +206,17 @@ class Doc extends BackendBase
|
||||
$ids = $this->descendantIds((int) $doc->id);
|
||||
$ids[] = (int) $doc->id;
|
||||
|
||||
foreach ($ids as $docId) {
|
||||
DocSearchService::remove((int) $docId);
|
||||
// 事务化:文档表 + 搜索索引,任何一步失败回滚
|
||||
try {
|
||||
Db::transaction(function () use ($ids) {
|
||||
foreach ($ids as $docId) {
|
||||
DocSearchService::remove((int) $docId);
|
||||
}
|
||||
DocsDoc::whereIn('id', $ids)->delete();
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
return $this->result->error('删除失败:' . $e->getMessage());
|
||||
}
|
||||
DocsDoc::whereIn('id', $ids)->delete();
|
||||
|
||||
DocTreeService::clearCache();
|
||||
return $this->result->success(null, '删除成功,共移除 ' . count($ids) . ' 篇');
|
||||
@@ -217,6 +224,11 @@ class Doc extends BackendBase
|
||||
|
||||
/**
|
||||
* 拖拽排序:接收有序的 id 数组与父级关系
|
||||
*
|
||||
* 安全要点:
|
||||
* 1. 校验节点归属(project/version),防止前端把任意文档塞进别人的树
|
||||
* 2. 禁止自身成环(pid = self 或 pid 命中后代),避免树渲染栈溢出
|
||||
* 3. 操作整体事务:中间失败回滚
|
||||
*/
|
||||
public function sort()
|
||||
{
|
||||
@@ -227,15 +239,55 @@ class Doc extends BackendBase
|
||||
return $this->result->error('排序数据为空');
|
||||
}
|
||||
|
||||
foreach ($nodes as $index => $node) {
|
||||
$docId = (int) ($node['id'] ?? 0);
|
||||
if ($docId <= 0) {
|
||||
continue;
|
||||
// 取第一个有效节点的项目/版本作为作用域(前端应保证 nodes 同源)
|
||||
$first = null;
|
||||
foreach ($nodes as $n) {
|
||||
if ((int) ($n['id'] ?? 0) > 0) {
|
||||
$first = DocsDoc::find((int) $n['id']);
|
||||
if ($first) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
DocsDoc::where('id', $docId)->update([
|
||||
'pid' => (int) ($node['pid'] ?? 0),
|
||||
'sort' => (int) ($node['sort'] ?? $index),
|
||||
]);
|
||||
}
|
||||
if (!$first) {
|
||||
return $this->result->error('排序数据无效,找不到对应文档');
|
||||
}
|
||||
|
||||
$projectId = (int) $first->project_id;
|
||||
$versionId = (int) $first->version_id;
|
||||
|
||||
try {
|
||||
Db::transaction(function () use ($nodes, $projectId, $versionId) {
|
||||
foreach ($nodes as $index => $node) {
|
||||
$docId = (int) ($node['id'] ?? 0);
|
||||
if ($docId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$doc = DocsDoc::where('id', $docId)
|
||||
->where('project_id', $projectId)
|
||||
->where('version_id', $versionId)
|
||||
->find();
|
||||
if (!$doc) {
|
||||
throw new \RuntimeException('文档 #' . $docId . ' 不在指定项目/版本范围内');
|
||||
}
|
||||
|
||||
$newPid = (int) ($node['pid'] ?? 0);
|
||||
if ($newPid === $docId) {
|
||||
throw new \RuntimeException('文档 #' . $docId . ' 不能把自己设为父级');
|
||||
}
|
||||
if ($newPid > 0 && $this->isDescendant($docId, $newPid)) {
|
||||
throw new \RuntimeException('文档 #' . $docId . ' 的新父级属于其后代,会形成环');
|
||||
}
|
||||
|
||||
DocsDoc::where('id', $docId)->update([
|
||||
'pid' => $newPid,
|
||||
'sort' => (int) ($node['sort'] ?? $index),
|
||||
]);
|
||||
}
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
return $this->result->error($e->getMessage());
|
||||
}
|
||||
|
||||
DocTreeService::clearCache();
|
||||
@@ -273,6 +325,14 @@ class Doc extends BackendBase
|
||||
return $this->result->error('请选择目标项目与版本');
|
||||
}
|
||||
|
||||
// 校验:version_id 必须属于 project_id,防止前端篡改导入到无关项目
|
||||
$version = DocsVersion::where('id', $versionId)
|
||||
->where('project_id', $projectId)
|
||||
->find();
|
||||
if (!$version) {
|
||||
return $this->result->error('所选版本不属于该项目');
|
||||
}
|
||||
|
||||
$file = $this->request->file('file');
|
||||
if (!$file) {
|
||||
return $this->result->error('请选择要导入的文件');
|
||||
|
||||
@@ -15,6 +15,7 @@ use addon\docs\model\DocsProject;
|
||||
use addon\docs\model\DocsVersion;
|
||||
use addon\docs\service\DocSearchService;
|
||||
use addon\docs\service\DocTreeService;
|
||||
use think\facade\Db;
|
||||
use ywxapp\controller\BackendBase;
|
||||
|
||||
/**
|
||||
@@ -149,14 +150,21 @@ class Project extends BackendBase
|
||||
return $this->result->error('项目不存在');
|
||||
}
|
||||
|
||||
$docIds = DocsDoc::where('project_id', $id)->column('id');
|
||||
foreach ($docIds as $docId) {
|
||||
DocSearchService::remove((int) $docId);
|
||||
}
|
||||
// 事务化级联:项目 / 版本 / 文档 / 搜索索引,任何一步失败整体回滚
|
||||
try {
|
||||
Db::transaction(function () use ($id) {
|
||||
$docIds = DocsDoc::where('project_id', $id)->column('id');
|
||||
foreach ($docIds as $docId) {
|
||||
DocSearchService::remove((int) $docId);
|
||||
}
|
||||
|
||||
DocsDoc::where('project_id', $id)->delete();
|
||||
DocsVersion::where('project_id', $id)->delete();
|
||||
$project->delete();
|
||||
DocsDoc::where('project_id', $id)->delete();
|
||||
DocsVersion::where('project_id', $id)->delete();
|
||||
DocsProject::where('id', $id)->delete();
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
return $this->result->error('删除失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
DocTreeService::clearCache();
|
||||
return $this->result->success(null, '删除成功');
|
||||
|
||||
@@ -15,6 +15,7 @@ use addon\docs\model\DocsProject;
|
||||
use addon\docs\model\DocsVersion;
|
||||
use addon\docs\service\DocSearchService;
|
||||
use addon\docs\service\DocTreeService;
|
||||
use think\facade\Db;
|
||||
use ywxapp\controller\BackendBase;
|
||||
|
||||
/**
|
||||
@@ -156,13 +157,20 @@ class Version extends BackendBase
|
||||
return $this->result->error('该项目仅剩一个版本,不能删除');
|
||||
}
|
||||
|
||||
$docIds = DocsDoc::where('version_id', $id)->column('id');
|
||||
foreach ($docIds as $docId) {
|
||||
DocSearchService::remove((int) $docId);
|
||||
}
|
||||
// 事务化级联:版本 / 文档 / 搜索索引
|
||||
try {
|
||||
Db::transaction(function () use ($id, $version) {
|
||||
$docIds = DocsDoc::where('version_id', $id)->column('id');
|
||||
foreach ($docIds as $docId) {
|
||||
DocSearchService::remove((int) $docId);
|
||||
}
|
||||
|
||||
DocsDoc::where('version_id', $id)->delete();
|
||||
$version->delete();
|
||||
DocsDoc::where('version_id', $id)->delete();
|
||||
DocsVersion::where('id', $id)->delete();
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
return $this->result->error('删除失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
DocTreeService::clearCache();
|
||||
return $this->result->success(null, '删除成功');
|
||||
|
||||
Reference in new issue
Block a user