Files
weiguang-jewelry-ledger/新账系统/server/middleware/request-guard.js
T
黄健铭andClaude Opus 5.5 9ec37196d4 目录重组:后端按core/domains分领域存放(仅移动与更新引用)
- 50个后端模块用git mv移入core/与domains/<领域>/,入口server.js、launch.js、restore.js及server/原位不动
- 所有require路径由脚本按新旧位置统一换算(后端、server/、测试)
- 新增test/app-paths.test.js,锁定默认数据目录、public目录与飞书配置文件的绝对位置
- 维护与部署.md补充目录说明并更新模块路径

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 13:01:19 -08:00

25 lines
1.2 KiB
JavaScript

'use strict';
const { fail } = require('../../core/ledger');
// 访问地址、请求方式与 POST 来源校验;顺序:Host → 解析地址 → 405 → Origin/X-Requested-With/Content-Type。
function checkRequest(req, production) {
const expected = production ? new URL(process.env.PUBLIC_URL).host : req.headers.host;
if (production && req.headers.host !== expected) fail('访问地址不匹配', 403);
if (!production && !/^(localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/.test(req.headers.host || ''))
fail('仅允许本机地址访问', 403);
const url = new URL(req.url, 'http://' + req.headers.host);
const route = url.pathname;
const isPost = req.method === 'POST';
if (!['GET', 'POST'].includes(req.method)) fail('不支持的请求方式', 405);
if (isPost) {
const expectedOrigin = production ? new URL(process.env.PUBLIC_URL).origin : 'http://' + req.headers.host;
if (
req.headers.origin !== expectedOrigin ||
req.headers['x-requested-with'] !== 'maimai-ledger' ||
!req.headers['content-type']?.startsWith('application/json')
)
fail('请从系统页面提交操作', 403);
}
return { url, route, isPost };
}
module.exports = { checkRequest };