35 lines
1.7 KiB
JavaScript
35 lines
1.7 KiB
JavaScript
// 插件文件能力的安全策略(v3.26.36):文件名白名单 + 配额。
|
|
// 这两条是安全边界,破坏即等于允许路径穿越或塞满磁盘,必须由 CI 守住。
|
|
const assert = require('assert')
|
|
const path = require('path')
|
|
|
|
const sandbox = {}
|
|
new Function('window', require('fs').readFileSync(path.join(__dirname, '..', 'src', 'plugin-files-policy.js'), 'utf8'))(sandbox)
|
|
const P = sandbox.IWBPluginFilesPolicy
|
|
assert.ok(P && typeof P.fileOk === 'function', '应导出 fileOk')
|
|
|
|
// ---- 文件名白名单 ----
|
|
for (const ok of ['a.jpg', 'a1b2c3.jpg', 'cover_2026-11-22.png', 'x.webp', 'A.JPEG']) {
|
|
assert.strictEqual(P.fileOk(ok), true, '应允许:' + ok)
|
|
}
|
|
for (const bad of [
|
|
'', '.jpg', 'a.gif', 'a.txt', 'a.jpg/png', '../x.jpg', 'a/b.jpg', '../../etc/passwd',
|
|
'a' + String.fromCharCode(92) + 'b.jpg', 'a.jpg ', ' a.jpg', 'a.jpg' + String.fromCharCode(0),
|
|
'a'.repeat(80) + '.jpg', 'x.jpg?1', 'x.jpg#a', '图片.jpg',
|
|
]) {
|
|
assert.strictEqual(P.fileOk(bad), false, '应拒绝:' + JSON.stringify(bad))
|
|
}
|
|
|
|
// ---- 配额 ----
|
|
const L = P.LIMITS
|
|
assert.ok(L.FILE_MAX > 0 && L.TOTAL_MAX >= L.FILE_MAX)
|
|
assert.deepStrictEqual(P.quotaOk(0, 1024), { ok: true })
|
|
assert.strictEqual(P.quotaOk(0, 0).ok, false, '空文件应拒绝')
|
|
assert.strictEqual(P.quotaOk(0, L.FILE_MAX + 1).reason, 'file-too-large')
|
|
assert.strictEqual(P.quotaOk(L.TOTAL_MAX - 10, 1024).reason, 'total-too-large')
|
|
// 覆盖同名文件:旧大小不重复计
|
|
assert.strictEqual(P.quotaOk(L.TOTAL_MAX - 100, 1024, 4096).ok, true)
|
|
assert.strictEqual(P.quotaOk(L.TOTAL_MAX - 100, 1024, 0).ok, false)
|
|
|
|
console.log('plugin files policy checks passed(白名单/路径穿越/配额/覆盖计费)')
|