fix(security): P4 — spawn over execSync, token 0600, env-only crypto key
Per audit §8 / H2 / H3 / M4:
- form/create.ts AI-relogin: execSync(`...--password=123456...`) → spawnSync
array form (no shell → no command injection via cliEntry/appCode); creds from
config DEFAULT_ACCOUNT/DEFAULT_PASSWORD (env-overridable), no inline literal
- config.ts saveToken: token.json 0600 + dir 0700 + chmod fallback for existing
0644 files (token holds bearer, was world-readable)
- crypto.ts: drop hardcoded FALLBACK_KEY ('EY8...'); missing key → throw (refuse
to encrypt with weak key under AES-ECB) instead of silent weak fallback
- @types/pg already installed in P1 (kb SQL client no longer any-typed)
Verified: tsc 0; crypto still loads key from web/.env (96-char hex, no throw →
login intact in container); vitest unit 336 pass (blindspot_guards quarantined).
Co-Authored-By: Claude <[email protected]>
This commit is contained in:
1 parent
21eb5520c7
commit
2c6e92d3e9
3 files changed
+26
-14
No files matched your search
@@ -25,7 +25,7 @@ import { runGuards, handleGuardResult } from '../_blindspot_guards.ts'
|
||||
import { validateFormCreate } from './_validate.ts'
|
||||
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
|
||||
import { loadAppContext } from '../app/context.ts'
|
||||
import { loadToken } from '../../core/config.ts'
|
||||
import { loadToken, DEFAULT_ACCOUNT, DEFAULT_PASSWORD } from '../../core/config.ts'
|
||||
import { applyNamingConvention, type NamingConvention } from './_naming_convention.ts'
|
||||
import { autoStripEnCode, autoStripFullName } from '../_encode_validator.ts'
|
||||
import { runAutoFix, type FixContext } from '../_auto_fix.ts'
|
||||
@@ -408,11 +408,18 @@ export function buildFormCreateCommand(): Command {
|
||||
const healthR = await c.get<{ code: number }>('/visualdev/Base', { currentPage: 1, pageSize: 1 })
|
||||
if (healthR.code === 600) {
|
||||
log.warn('⚠ AI token 过期, 自动重登...')
|
||||
const { execSync } = await import('node:child_process')
|
||||
// P4 安全: spawnSync 数组式 (无 shell → 无命令注入); 凭据走 config (env 可覆盖), 不内联明文
|
||||
const { spawnSync } = await import('node:child_process')
|
||||
const cliEntry = process.argv[1] || 'src/cli.ts'
|
||||
execSync(`npx tsx ${cliEntry} app login --account=admin --password=123456 --appCode=${c.appCode}`, {
|
||||
stdio: 'pipe', timeout: 15000,
|
||||
})
|
||||
const relogin = spawnSync('npx', [
|
||||
'tsx', cliEntry, 'app', 'login',
|
||||
'--account', DEFAULT_ACCOUNT,
|
||||
'--password', DEFAULT_PASSWORD,
|
||||
'--appCode', c.appCode,
|
||||
], { stdio: 'pipe', timeout: 15000 })
|
||||
if (relogin.status !== 0) {
|
||||
log.warn(` 自动重登失败 (exit=${relogin.status}), 继续 (AI 调用可能仍失败)`)
|
||||
}
|
||||
log.info('✓ 重登完成, 继续 AI 调用')
|
||||
// v2.4+ L3: AI token 过期发生 (NCPF TTL 一般 30 分钟), 让 AI 跑 log last 能查
|
||||
ctx.addSuggestion(remindAiTokenRelogin())
|
||||
|
||||
+6
-3
@@ -11,7 +11,7 @@
|
||||
*
|
||||
* 覆盖: DACLI_TOKEN_DIR env 变量可自定义路径 (多账号 / CI / 测试场景)
|
||||
*/
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync, rmSync } from 'node:fs'
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync, rmSync, chmodSync } from 'node:fs'
|
||||
import { dirname, join, resolve } from 'node:path'
|
||||
import { homedir } from 'node:os'
|
||||
import type { TokenFile } from './types.ts'
|
||||
@@ -44,8 +44,11 @@ export function loadToken(): TokenFile | null {
|
||||
|
||||
export function saveToken(t: TokenFile): void {
|
||||
const dir = tokenDir()
|
||||
if (!existsSync(dir)) mkdirSync(dir, { recursive: true })
|
||||
writeFileSync(tokenPath(), JSON.stringify(t, null, 2), 'utf-8')
|
||||
if (!existsSync(dir)) mkdirSync(dir, { recursive: true, mode: 0o700 })
|
||||
const p = tokenPath()
|
||||
// P4 安全: token 含 bearer, 必须 0600 (仅本人可读); mode 选项只在新建时生效, chmod 兜底已存在的 0644 文件
|
||||
writeFileSync(p, JSON.stringify(t, null, 2), { encoding: 'utf-8', mode: 0o600 })
|
||||
try { chmodSync(p, 0o600) } catch { /* 某些 FS 不支持 chmod, 忽略 */ }
|
||||
}
|
||||
|
||||
export function clearToken(): void {
|
||||
|
||||
+8
-6
@@ -13,9 +13,7 @@ import { existsSync, readFileSync } from 'node:fs'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
/** 默认密钥来自 web .env VITE_CIPHER_KEY */
|
||||
const FALLBACK_KEY = 'EY8WePvjM5GGwQzn'
|
||||
|
||||
/** 密钥来源: 环境变量 VITE_CIPHER_KEY, 或 web/.env — 不再硬编码回退弱密钥 (P4 安全) */
|
||||
let _cipherKey: string | null = null
|
||||
|
||||
function loadCipherKey(): string {
|
||||
@@ -41,10 +39,14 @@ function loadCipherKey(): string {
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
/* ignore — fallback */
|
||||
/* ignore — 下面抛错 */
|
||||
}
|
||||
_cipherKey = FALLBACK_KEY
|
||||
return _cipherKey
|
||||
// P4 安全: 密钥缺失时拒绝运行, 不回退硬编码弱密钥 (AES-ECB 下弱密钥可被还原)
|
||||
throw new Error(
|
||||
'VITE_CIPHER_KEY 未配置, 且 web/apps/jnpf-web-apps-main/.env 未找到 — ' +
|
||||
'拒绝用硬编码弱密钥加密口令。请设置环境变量 VITE_CIPHER_KEY ' +
|
||||
'(值见 web .env 的 VITE_CIPHER_KEY), 或确保 web/.env 可被读取。',
|
||||
)
|
||||
}
|
||||
|
||||
export function encryptByMd5(plain: string): string {
|
||||
|
||||
Reference in new issue
Block a user