Files
weiguang-jewelry-ledger/新账系统/server/middleware/static.js
T
黄健铭andClaude Opus 5.5 f9d217022c 修复静态资源查找命中原型属性
白名单查找改为只认自有属性(Object.hasOwn),constructor、__proto__、toString 等名称按未知地址 404「地址不存在」,
不再落到 Object.prototype 上抛出类型错误。
经 HTTP 进入时路径恒以 / 开头,原代码对 /constructor 等本已返回 404;原型命中只在不带 / 的名称直接调用时出现,
因此本修复对现有 HTTP 响应无可见变化,测试同时覆盖 HTTP 与直接调用两种情形。

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

56 lines
2.2 KiB
JavaScript

'use strict';
const fs = require('node:fs');
const path = require('node:path');
const { fail } = require('../../ledger');
const { send } = require('../http');
const PUBLIC_DIR = path.join(__dirname, '..', '..', 'public');
const ASSETS = {
'/': 'index.html',
'/private-operations-ui.js': 'private-operations-ui.js',
'/app.js': 'app.js',
'/order-copy.js': 'order-copy.js',
'/order-templates.js': 'order-templates.js',
'/order-templates-ui.js': 'order-templates-ui.js',
'/order-edit-ui.js': 'order-edit-ui.js',
'/order-cancellations-ui.js': 'order-cancellations-ui.js',
'/order-changes-ui.js': 'order-changes-ui.js',
'/order-after-sales-ui.js': 'order-after-sales-ui.js',
'/order-ownership-ui.js': 'order-ownership-ui.js',
'/daily-funds-ui.js': 'daily-funds-ui.js',
'/team-funds-ui.js': 'team-funds-ui.js',
'/cost-funding-ui.js': 'cost-funding-ui.js',
'/price-locks-ui.js': 'price-locks-ui.js',
'/price-lock-changes-ui.js': 'price-lock-changes-ui.js',
'/material-pools-ui.js': 'material-pools-ui.js',
'/recoveries-ui.js': 'recoveries-ui.js',
'/recovery-changes-ui.js': 'recovery-changes-ui.js',
'/inventory-changes-ui.js': 'inventory-changes-ui.js',
'/pockets-ui.js': 'pockets-ui.js',
'/shop-settings-ui.js': 'shop-settings-ui.js',
'/employees-ui.js': 'employees-ui.js',
'/credit-card-fees.js': 'credit-card-fees.js',
'/calculations.js': 'calculations.js',
'/order-formula.js': 'order-formula.js',
'/fee-estimates.js': 'fee-estimates.js',
'/order-cost-presets-ui.js': 'order-cost-presets-ui.js',
'/app.css': 'app.css',
'/print.js': 'print.js',
};
// 白名单静态文件;其余(含任何 POST)一律 404。
function serveStatic(res, route, isPost) {
// 只查自有属性,/constructor、/__proto__ 等不能命中原型。
const file = Object.hasOwn(ASSETS, route) ? ASSETS[route] : undefined;
if (!file || isPost) fail('地址不存在', 404);
send(
res,
200,
fs.readFileSync(path.join(PUBLIC_DIR, file)),
file.endsWith('.js')
? 'application/javascript; charset=utf-8'
: file.endsWith('.css')
? 'text/css; charset=utf-8'
: 'text/html; charset=utf-8',
);
}
module.exports = { serveStatic, ASSETS };