181 lines
5.7 KiB
JavaScript
181 lines
5.7 KiB
JavaScript
import { execFileSync } from 'node:child_process'
|
|
import { readdirSync, readFileSync } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { parseArgs } from 'node:util'
|
|
|
|
const FORBIDDEN_PATH_PATTERNS = [
|
|
/^\.env(?:\.|$)/u,
|
|
/\/\.env(?:\.|$)/u,
|
|
/^\.private(?:\/|$)/u,
|
|
/\/\.private(?:\/|$)/u,
|
|
/\.(?:pem|key|p12|pfx|jks)$/iu,
|
|
/(?:^|\/)id_(?:rsa|ed25519|ecdsa)$/u,
|
|
]
|
|
|
|
const ALLOWED_ENV_EXAMPLES = /(?:^|\/)\.env(?:\.[^/]+)?\.example$/u
|
|
|
|
const SECRET_PATTERNS = [
|
|
{
|
|
name: 'private-key-header',
|
|
regex: /-----BEGIN (?:RSA |EC |OPENSSH |DSA |PGP )?PRIVATE KEY-----/u,
|
|
},
|
|
{
|
|
name: 'github-token',
|
|
regex: /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{20,}\b/u,
|
|
},
|
|
{
|
|
name: 'aws-access-key',
|
|
regex: /\bAKIA[0-9A-Z]{16}\b/u,
|
|
},
|
|
{
|
|
name: 'hardcoded-sensitive-env',
|
|
regex: /\b(?:DINGTALK_CLIENT_SECRET|ADMIN_API_KEY|NEXTAUTH_SECRET|AWS_SECRET_ACCESS_KEY|POSTGRES_PASSWORD|XIAOYI_EVAL_VLM_API_KEY)\s*[:=]\s*['"]([^'"]{8,})['"]/u,
|
|
ignoreValues: /^(?:build-time-|example|placeholder|your-|test-|mock-|dummy|changeme|\$\{\{|\$)/iu,
|
|
},
|
|
{
|
|
name: 'database-url-with-password',
|
|
regex: /postgres(?:ql)?:\/\/[^:\s/]+:([^@\s/]{6,})@(?!localhost\b|127\.0\.0\.1\b|postgres\b|db\b)/iu,
|
|
ignoreValues: /^(?:build|password|example|placeholder|\$\{|\$|<)/iu,
|
|
},
|
|
]
|
|
|
|
const TECHNICAL_BRAND_REGEX = /(?<![A-Za-z0-9_])(?:Pi(?: Agent)?|PhotoCraft)(?![A-Za-z0-9_])|\bpcraft:/u
|
|
|
|
function normalizePath(filePath) {
|
|
return String(filePath || '').replaceAll('\\', '/').replace(/^\.\//, '')
|
|
}
|
|
|
|
export function scanChangedPaths(paths) {
|
|
const violations = []
|
|
for (const rawPath of paths) {
|
|
const filePath = normalizePath(rawPath)
|
|
if (!filePath || ALLOWED_ENV_EXAMPLES.test(filePath)) continue
|
|
if (FORBIDDEN_PATH_PATTERNS.some(pattern => pattern.test(filePath))) {
|
|
violations.push({
|
|
type: 'forbidden-path',
|
|
file: filePath,
|
|
message: `Forbidden credential or environment file in PR: ${filePath}`,
|
|
})
|
|
}
|
|
}
|
|
return violations
|
|
}
|
|
|
|
export function scanDiffContent(diffText) {
|
|
const violations = []
|
|
let currentFile = ''
|
|
let lineNumber = 0
|
|
|
|
for (const line of String(diffText || '').split(/\r?\n/u)) {
|
|
if (line.startsWith('+++ b/')) {
|
|
currentFile = normalizePath(line.slice(6))
|
|
continue
|
|
}
|
|
const hunkMatch = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)/u)
|
|
if (hunkMatch) {
|
|
lineNumber = Number.parseInt(hunkMatch[1], 10) - 1
|
|
continue
|
|
}
|
|
if (line.startsWith('+') && !line.startsWith('+++')) {
|
|
lineNumber += 1
|
|
const content = line.slice(1)
|
|
if (currentFile.endsWith('.test.mjs') || currentFile.endsWith('.test.ts') || currentFile.endsWith('.spec.ts')) {
|
|
continue
|
|
}
|
|
for (const rule of SECRET_PATTERNS) {
|
|
const match = content.match(rule.regex)
|
|
if (!match) continue
|
|
const captured = match[1] ?? ''
|
|
if (rule.ignoreValues && rule.ignoreValues.test(captured)) continue
|
|
violations.push({
|
|
type: rule.name,
|
|
file: currentFile || '<diff>',
|
|
line: lineNumber,
|
|
message: `Potential ${rule.name} leak in ${currentFile || '<diff>'}:${lineNumber}`,
|
|
})
|
|
}
|
|
} else if (!line.startsWith('-')) {
|
|
lineNumber += 1
|
|
}
|
|
}
|
|
return violations
|
|
}
|
|
|
|
const WHITELABEL_SCANNED_FILES = [
|
|
'agent.mjs', 'desktop-host.mjs', 'xiaoyi-release.mjs', 'native-tools.mjs',
|
|
'image-context-pruning.mjs', 'photocraft-extension.mjs', 'production-goal.mjs',
|
|
'production-operations.mjs', 'production-tools.mjs', 'production-tool-schema.mjs',
|
|
'production-image-worker.mjs', 'production-screen-tools.mjs', 'runtime-policy.mjs',
|
|
'screen-review.mjs',
|
|
]
|
|
|
|
export function scanWhitelabelSource(repoRoot) {
|
|
const srcDir = path.join(repoRoot, 'apps', 'detail-agent-pi', 'src')
|
|
const violations = []
|
|
let existing
|
|
try {
|
|
existing = new Set(readdirSync(srcDir))
|
|
} catch {
|
|
return violations
|
|
}
|
|
|
|
for (const file of WHITELABEL_SCANNED_FILES) {
|
|
if (!existing.has(file)) continue
|
|
const content = readFileSync(path.join(srcDir, file), 'utf8')
|
|
for (const [index, rawLine] of content.split(/\r?\n/u).entries()) {
|
|
const copy = rawLine.trim()
|
|
if (/^(?:\/\/|\/\*|\*)/u.test(copy) || copy === "aliases: ['PhotoCraft', 'native'],") continue
|
|
if (TECHNICAL_BRAND_REGEX.test(copy)) {
|
|
violations.push({
|
|
type: 'whitelabel-leak',
|
|
file: `apps/detail-agent-pi/src/${file}`,
|
|
line: index + 1,
|
|
message: `Technical brand token in apps/detail-agent-pi/src/${file}:${index + 1}`,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
return violations
|
|
}
|
|
|
|
function main() {
|
|
const { values } = parseArgs({
|
|
options: {
|
|
base: { type: 'string', default: '' },
|
|
head: { type: 'string', default: 'HEAD' },
|
|
cwd: { type: 'string', default: process.cwd() },
|
|
},
|
|
})
|
|
|
|
const violations = [...scanWhitelabelSource(values.cwd)]
|
|
|
|
if (values.base) {
|
|
const changed = execFileSync(
|
|
'git',
|
|
['diff', '--name-only', `${values.base}...${values.head}`],
|
|
{ cwd: values.cwd, encoding: 'utf8' },
|
|
).split(/\r?\n/u).filter(Boolean)
|
|
violations.push(...scanChangedPaths(changed))
|
|
|
|
const diff = execFileSync(
|
|
'git',
|
|
['diff', '--unified=0', `${values.base}...${values.head}`],
|
|
{ cwd: values.cwd, encoding: 'utf8', maxBuffer: 20 * 1024 * 1024 },
|
|
)
|
|
violations.push(...scanDiffContent(diff))
|
|
}
|
|
|
|
if (violations.length > 0) {
|
|
for (const v of violations) {
|
|
console.error(`::error file=${v.file}${v.line ? `,line=${v.line}` : ''}::${v.message}`)
|
|
}
|
|
process.exit(1)
|
|
}
|
|
console.log('PR security and whitelabel gate passed.')
|
|
}
|
|
|
|
if (import.meta.url === `file://${process.argv[1]?.replaceAll('\\', '/')}` ||
|
|
process.argv[1]?.endsWith('check-pr-security.mjs')) {
|
|
main()
|
|
}
|