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]>
90 lines
3.1 KiB
TypeScript
90 lines
3.1 KiB
TypeScript
/**
|
|
* crypto.ts — 严格 1:1 复刻 web cipher.ts (字节级一致)
|
|
*
|
|
* 流程: 明文 → md5(hex) → AES-ECB-Pkcs7(key=md5(VITE_CIPHER_KEY)) → Base64 → Hex
|
|
*
|
|
* 关键: cipher.ts:34-37 `Hex.stringify(Base64.parse(result))` 是对 Base64 字符串做 hex,
|
|
* 不是对密文字节做 hex。这是 ddcc 实验里踩过的最大坑。
|
|
*
|
|
* 输入 '123456' 应输出 96 字符 hex (与 web 一致)。
|
|
*/
|
|
import CryptoJS from 'crypto-js'
|
|
import { existsSync, readFileSync } from 'node:fs'
|
|
import { dirname, resolve } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
/** 密钥来源: 环境变量 VITE_CIPHER_KEY, 或 web/.env — 不再硬编码回退弱密钥 (P4 安全) */
|
|
let _cipherKey: string | null = null
|
|
|
|
function loadCipherKey(): string {
|
|
if (_cipherKey) return _cipherKey
|
|
if (process.env.VITE_CIPHER_KEY) {
|
|
_cipherKey = process.env.VITE_CIPHER_KEY
|
|
return _cipherKey
|
|
}
|
|
try {
|
|
const here = dirname(fileURLToPath(import.meta.url))
|
|
const candidates = [
|
|
resolve(here, '../../../../web/apps/jnpf-web-apps-main/.env'),
|
|
resolve(here, '../../../web/apps/jnpf-web-apps-main/.env'),
|
|
resolve(here, '../../web/apps/jnpf-web-apps-main/.env'),
|
|
]
|
|
for (const p of candidates) {
|
|
if (existsSync(p)) {
|
|
const m = readFileSync(p, 'utf-8').match(/VITE_CIPHER_KEY\s*=\s*(\S+)/)
|
|
if (m) {
|
|
_cipherKey = m[1]
|
|
return _cipherKey
|
|
}
|
|
}
|
|
}
|
|
} catch {
|
|
/* ignore — 下面抛错 */
|
|
}
|
|
// 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 {
|
|
return CryptoJS.MD5(plain).toString()
|
|
}
|
|
|
|
/** AES-ECB-Pkcs7(密钥=md5(CIPHER_KEY)),再 Base64→Hex */
|
|
function aesEcbHex(inputHex: string): string {
|
|
const aesKey = encryptByMd5(loadCipherKey())
|
|
const encrypted = CryptoJS.AES.encrypt(
|
|
inputHex,
|
|
CryptoJS.enc.Utf8.parse(aesKey),
|
|
{ mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 },
|
|
)
|
|
const base64 = encrypted.toString()
|
|
// 注意: 是对 base64 字符串做 hex (web cipher.ts:36)
|
|
return CryptoJS.enc.Hex.stringify(CryptoJS.enc.Base64.parse(base64))
|
|
}
|
|
|
|
/**
|
|
* 完整登录加密 (与 LoginForm.handleLogin 一致):
|
|
* md5(plain) → aesEncryption.encryptByAES(md5Hex)
|
|
*/
|
|
export function encryptPassword(plain: string): string {
|
|
return aesEcbHex(encryptByMd5(plain))
|
|
}
|
|
|
|
/**
|
|
* 纯 AES-ECB-Pkcs7 加密 (供数据源密码等场景, web Form.vue: handleSubmit 中:
|
|
* password: aesEncryption.encryptByAES(values.password)
|
|
* 流程: AES-ECB-Pkcs7(key=md5(CIPHER_KEY), data=plain) → Base64 → Hex
|
|
*/
|
|
export function encryptAesEcb(plain: string): string {
|
|
return aesEcbHex(plain)
|
|
}
|
|
|
|
/** 96 字符的 hex 即认为已加密 */
|
|
export function isEncrypted(p: string | undefined | null): boolean {
|
|
return !!p && p.length === 96 && /^[0-9a-f]+$/i.test(p)
|
|
}
|