Files
eSIM-Tools/scripts/inject-sentry-config.js
Abner 65068df5dd fix(sentry): 修复 CSP 内联脚本问题并增强敏感数据脱敏
- 新增 sentry-config.js 外链脚本替代 HTML 内联配置
- 修改 HTML 文件引用外链脚本以符合 CSP 策略
- 重写 inject-sentry-config.js 仅处理配置文件
- 添加 sentry-sdk-loaded 事件触发,修复 SDK 超时覆盖问题
- 增强 beforeSend 脱敏逻辑,覆盖 query_string 和更多敏感参数
2025-12-13 23:36:44 +08:00

53 lines
1.7 KiB
JavaScript

#!/usr/bin/env node
/**
* Sentry 配置注入脚本
*
* 在构建时将 Sentry 环境变量注入到 sentry-config.js 文件中
* 用法:在 npm run build 中调用此脚本
*/
const fs = require('fs');
const path = require('path');
const SENTRY_DSN = process.env.SENTRY_DSN || '';
const SENTRY_ENVIRONMENT = process.env.SENTRY_ENVIRONMENT || 'production';
const SENTRY_RELEASE = process.env.SENTRY_RELEASE ||
(process.env.COMMIT_REF ? `esim-tools@${process.env.COMMIT_REF.slice(0, 7)}` : 'esim-tools@unknown');
// 要处理的配置文件(改为处理独立的 JS 配置文件,而非 HTML 内联脚本)
const configFile = 'dist/src/js/sentry-config.js';
console.log('[Sentry Config] 开始注入配置...');
console.log(` DSN: ${SENTRY_DSN ? '已配置' : '未配置'}`);
console.log(` Environment: ${SENTRY_ENVIRONMENT}`);
console.log(` Release: ${SENTRY_RELEASE}`);
const fullPath = path.join(process.cwd(), configFile);
if (!fs.existsSync(fullPath)) {
console.error(` ✗ 错误: ${configFile} 不存在`);
console.log('[Sentry Config] 失败,请确保构建流程正确复制了 sentry-config.js');
process.exit(1);
}
let content = fs.readFileSync(fullPath, 'utf8');
// 替换配置占位符
content = content.replace(
/window\.SENTRY_DSN\s*=\s*['"][^'"]*['"]/,
`window.SENTRY_DSN = '${SENTRY_DSN}'`
);
content = content.replace(
/window\.SENTRY_ENVIRONMENT\s*=\s*['"][^'"]*['"]/,
`window.SENTRY_ENVIRONMENT = '${SENTRY_ENVIRONMENT}'`
);
content = content.replace(
/window\.SENTRY_RELEASE\s*=\s*['"][^'"]*['"]/,
`window.SENTRY_RELEASE = '${SENTRY_RELEASE}'`
);
fs.writeFileSync(fullPath, content, 'utf8');
console.log(` ✓ 已处理: ${configFile}`);
console.log('[Sentry Config] 完成');