Files
SupplierSystem/server/tests/uploadExtWhitelist.test.ts
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

60 lines
3.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import { sanitizeUploadExt } from '../src/services/storageBucketConfig';
/**
* H11:base64 照片通道的**扩展名直传** → 存储型 XSS(2026-09-24 修)。
*
* 事实核对(本轮):
* - 通用上传接口 `routes/storage.ts` **早已**过 `assertBucketUploadAllowed`(扩展名+mime+大小)+ 服务端随机名;
* - 但 `services/storageUploadService.saveBase64Upload`(独立自采入库 / 发车核货 / 直送 / 质量报告四条链路
* 都用它)只做字符集过滤(`resolveExt` 去掉非 `[a-z0-9]`),**没有白名单** →
* 请求体里 `ext=html` 就能把 HTML 落进桶里(公共可读桶即同源可执行页面);
* - `sku-images` 那条(`saveSkuImageToStorage`)mime 由服务端决定,且兜底 SVG 的文案已过
* `escapeSvgText` → 不构成本项问题(本测试不做断言)。
*
* 修法:把客户端扩展名收敛到**该桶白名单**内,且刻意保功能:
* 图片桶降级为 jpg(不抛错,现场拍照不受影响),文档桶直接拒。
*/
const SRC = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8');
describe('sanitizeUploadExt(H11 扩展名收口)', () => {
it('图片桶:白名单内原样通过', () => {
for (const ext of ['jpg', 'jpeg', 'png', 'webp', 'JPG', '.png']) {
expect(sanitizeUploadExt('inspection-photos', ext)).toBe(ext.replace(/^\./, '').toLowerCase());
}
expect(sanitizeUploadExt('delivery-photos', 'png')).toBe('png');
});
it('图片桶:非法扩展名**降级为 jpg**(不抛错,保证现场还能拍照)', () => {
for (const bad of ['html', 'htm', 'svg', 'js', 'heic', 'gif', '']) {
expect(sanitizeUploadExt('inspection-photos', bad)).toBe('jpg');
}
// 字符集过滤后仍非法的一律降级(不会带路径/点)
expect(sanitizeUploadExt('inspection-photos', '../../etc/passwd')).toBe('jpg');
expect(sanitizeUploadExt('inspection-photos', 'x.php')).toBe('jpg');
});
it('文档/安装包桶:非法类型直接拒绝(不得当文档存)', () => {
expect(sanitizeUploadExt('quality-reports', 'pdf')).toBe('pdf');
expect(sanitizeUploadExt('quality-reports', 'jpg')).toBe('jpg');
expect(() => sanitizeUploadExt('quality-reports', 'html')).toThrow(/不允许的文件类型/);
expect(() => sanitizeUploadExt('invoices', 'svg')).toThrow(/不允许的文件类型/);
expect(sanitizeUploadExt('weigh-apk', 'apk')).toBe('apk');
expect(() => sanitizeUploadExt('weigh-apk', 'html')).toThrow(/不允许的文件类型/);
});
it('未配置白名单的桶:保持原行为(仅字符集过滤)', () => {
expect(sanitizeUploadExt('some-unknown-bucket', 'PNG')).toBe('png');
});
it('base64 落盘通道必须走这道收口(防回退)', () => {
const svc = SRC('src/services/storageUploadService.ts');
expect(svc).toContain('sanitizeUploadExt(bucket, ext)');
// 旧写法(直接把客户端 ext 拼进文件名)不得回退
expect(svc).not.toContain('const basename = buildTimedPhotoBasename(ext);');
});
});