来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
114 lines
3.4 KiB
JavaScript
114 lines
3.4 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Scan for PostgreSQL-invalid pattern:
|
|
* UPDATE target alias ... FROM ... JOIN ... ON ... alias.col
|
|
* (target alias cannot appear in FROM JOIN ON)
|
|
*/
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
function walk(d, acc = []) {
|
|
for (const e of fs.readdirSync(d, { withFileTypes: true })) {
|
|
const p = path.join(d, e.name);
|
|
if (e.isDirectory()) {
|
|
if (['node_modules', 'dist', '.git'].includes(e.name)) continue;
|
|
walk(p, acc);
|
|
} else if (/\.(ts|js|sql|cjs|mjs)$/.test(e.name)) {
|
|
acc.push(p);
|
|
}
|
|
}
|
|
return acc;
|
|
}
|
|
|
|
const roots = ['server/src', 'server/scripts', 'scripts', 'migrations'];
|
|
const files = roots.flatMap((r) => (fs.existsSync(r) ? walk(r) : []));
|
|
const findings = [];
|
|
|
|
function extractChunks(file, text) {
|
|
if (file.endsWith('.sql')) return [{ text, start: 0 }];
|
|
const chunks = [];
|
|
const tre = /`([\s\S]*?)`/g;
|
|
let tm;
|
|
while ((tm = tre.exec(text))) {
|
|
chunks.push({ text: tm[1], start: tm.index });
|
|
}
|
|
return chunks;
|
|
}
|
|
|
|
for (const file of files) {
|
|
const text = fs.readFileSync(file, 'utf8');
|
|
for (const chunk of extractChunks(file, text)) {
|
|
const sql = chunk.text;
|
|
const re = /UPDATE\s+(?:ONLY\s+)?([a-zA-Z_][\w.]*)\s+([a-zA-Z_]\w*)\b([\s\S]*?)(?=;|$)/gi;
|
|
let m;
|
|
while ((m = re.exec(sql))) {
|
|
const target = m[1];
|
|
const alias = m[2];
|
|
const body = m[3];
|
|
if (!/\bFROM\b/i.test(body) || !/\bJOIN\b/i.test(body)) continue;
|
|
|
|
const fromMatch = body.match(/\bFROM\b([\s\S]*?)(?=\bWHERE\b|\bRETURNING\b|$)/i);
|
|
if (!fromMatch) continue;
|
|
const fromPart = fromMatch[1];
|
|
|
|
const onParts = [
|
|
...fromPart.matchAll(
|
|
/\bON\b([\s\S]*?)(?=\b(?:INNER|LEFT|RIGHT|FULL|CROSS)?\s*JOIN\b|\bWHERE\b|$)/gi
|
|
),
|
|
];
|
|
const aliasDot = new RegExp(`\\b${alias}\\.`, 'i');
|
|
const badOns = onParts.map((x) => x[1]).filter((on) => aliasDot.test(on));
|
|
if (!badOns.length) continue;
|
|
|
|
const abs = chunk.start + m.index;
|
|
const line = text.slice(0, abs).split(/\n/).length;
|
|
findings.push({
|
|
file: file.replace(/\\/g, '/'),
|
|
line,
|
|
target,
|
|
alias,
|
|
sample: badOns[0].trim().replace(/\s+/g, ' ').slice(0, 160),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Also list UPDATE+FROM without JOIN but with suspicious patterns for review
|
|
const updateFrom = [];
|
|
for (const file of files) {
|
|
const text = fs.readFileSync(file, 'utf8');
|
|
for (const chunk of extractChunks(file, text)) {
|
|
const sql = chunk.text;
|
|
const re = /UPDATE\s+(?:ONLY\s+)?([a-zA-Z_][\w.]*)\s+([a-zA-Z_]\w*)\b([\s\S]*?)(?=;|$)/gi;
|
|
let m;
|
|
while ((m = re.exec(sql))) {
|
|
const target = m[1];
|
|
const alias = m[2];
|
|
const body = m[3];
|
|
if (!/\bFROM\b/i.test(body)) continue;
|
|
const abs = chunk.start + m.index;
|
|
const line = text.slice(0, abs).split(/\n/).length;
|
|
const hasJoin = /\bJOIN\b/i.test(body);
|
|
updateFrom.push({
|
|
file: file.replace(/\\/g, '/'),
|
|
line,
|
|
target,
|
|
alias,
|
|
hasJoin,
|
|
snippet: body.replace(/\s+/g, ' ').slice(0, 100),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log('=== BAD: target alias in JOIN ON ===');
|
|
console.log(JSON.stringify(findings, null, 2));
|
|
console.log('bad_count', findings.length);
|
|
console.log('\n=== UPDATE ... alias ... FROM (all, for review) ===');
|
|
for (const row of updateFrom) {
|
|
console.log(
|
|
`${row.file}:${row.line} ${row.target} AS ${row.alias} join=${row.hasJoin}`
|
|
);
|
|
}
|
|
console.log('update_from_count', updateFrom.length);
|