来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
91 lines
2.7 KiB
JavaScript
91 lines
2.7 KiB
JavaScript
// 验证 canteen_app_rls 下分公司隔离(含 product_catalog 分类)
|
|
import 'dotenv/config';
|
|
import pg from 'pg';
|
|
|
|
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
|
|
|
|
const roleOk = await pool.query(`SELECT 1 FROM pg_roles WHERE rolname = 'canteen_app_rls'`);
|
|
if (!roleOk.rowCount) {
|
|
console.error('canteen_app_rls role missing');
|
|
process.exit(1);
|
|
}
|
|
|
|
const branch = (
|
|
await pool.query(
|
|
`SELECT branch_code, COUNT(*)::int AS n
|
|
FROM product_catalog
|
|
WHERE item_type = 'category' AND branch_code IS NOT NULL
|
|
GROUP BY branch_code
|
|
ORDER BY n DESC
|
|
LIMIT 1`
|
|
)
|
|
).rows[0];
|
|
|
|
if (!branch) {
|
|
console.log('no branch categories to test');
|
|
process.exit(0);
|
|
}
|
|
|
|
const otherBranch = (
|
|
await pool.query(
|
|
`SELECT branch_code
|
|
FROM product_catalog
|
|
WHERE item_type = 'category'
|
|
AND branch_code IS NOT NULL
|
|
AND branch_code <> $1
|
|
GROUP BY branch_code
|
|
LIMIT 1`,
|
|
[branch.branch_code]
|
|
)
|
|
).rows[0]?.branch_code;
|
|
|
|
const client = await pool.connect();
|
|
try {
|
|
await client.query('BEGIN');
|
|
await client.query('SET LOCAL ROLE canteen_app_rls');
|
|
|
|
await client.query(`SELECT set_config('app.branch_code', $1, true)`, [branch.branch_code]);
|
|
const own = await client.query(
|
|
`SELECT COUNT(*)::int AS n FROM product_catalog WHERE item_type = 'category'`
|
|
);
|
|
console.log(
|
|
`rls branch=${branch.branch_code}: categories=${own.rows[0].n} (owner branch count=${branch.n})`
|
|
);
|
|
if (Number(own.rows[0].n) !== Number(branch.n)) {
|
|
throw new Error('RLS category count mismatch vs owner branch count');
|
|
}
|
|
|
|
await client.query(`SELECT set_config('app.branch_code', 'BR_NOT_EXIST', true)`, []);
|
|
const wrong = await client.query(
|
|
`SELECT COUNT(*)::int AS n FROM product_catalog WHERE item_type = 'category'`
|
|
);
|
|
console.log(`rls wrong branch: categories=${wrong.rows[0].n} (expect 0)`);
|
|
if (Number(wrong.rows[0].n) !== 0) {
|
|
throw new Error('expected 0 categories for wrong branch');
|
|
}
|
|
|
|
if (otherBranch) {
|
|
await client.query(`SELECT set_config('app.branch_code', $1, true)`, [otherBranch]);
|
|
const other = await client.query(
|
|
`SELECT COUNT(*)::int AS n FROM product_catalog WHERE item_type = 'category'`
|
|
);
|
|
const leak = await client.query(
|
|
`SELECT COUNT(*)::int AS n FROM product_catalog
|
|
WHERE item_type = 'category' AND branch_code = $1`,
|
|
[branch.branch_code]
|
|
);
|
|
console.log(
|
|
`rls branch=${otherBranch}: categories=${other.rows[0].n}; leak of ${branch.branch_code}=${leak.rows[0].n} (expect 0)`
|
|
);
|
|
if (Number(leak.rows[0].n) !== 0) {
|
|
throw new Error('cross-branch category leak under RLS');
|
|
}
|
|
}
|
|
|
|
await client.query('ROLLBACK');
|
|
console.log('verify-rls-role: OK');
|
|
} finally {
|
|
client.release();
|
|
}
|
|
await pool.end();
|