DeepChat 安全与隐私保护机制

DeepChat 安全与隐私保护机制

引言

在当今数字化时代,用户对个人数据的安全和隐私保护越来越关注。作为一个 AI 聊天平台,DeepChat 处理着大量敏感的用户对话数据,因此必须建立完善的安全和隐私保护机制。本文将深入分析 DeepChat 在数据安全、隐私保护和访问控制等方面的实现。

安全架构设计

整体安全架构

DeepChat 采用了分层的安全架构设计:

┌─────────────────────────────────────────────────────────────────────┐
│                        DeepChat 安全架构                            │
├─────────────────────────────────────────────────────────────────────┤
│  ┌─────────────────────────────────────────────────────────────────┐│
│  │                    应用层安全                                   ││
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────────┐ ││
│  │  │ 用户认证     │  │ 权限控制     │  │ 数据加密与隐私保护       │ ││
│  │  └─────────────┘  └─────────────┘  └─────────────────────────┘ ││
│  └─────────────────────────────────────────────────────────────────┘│
│                              │                                      │
│  ┌─────────────────────────────────────────────────────────────────┐│
│  │                    传输层安全                                   ││
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────────┐ ││
│  │  │ HTTPS/TLS   │  │ 网络代理     │  │ API 密钥安全管理         │ ││
│  │  └─────────────┘  └─────────────┘  └─────────────────────────┘ ││
│  └─────────────────────────────────────────────────────────────────┘│
│                              │                                      │
│  ┌─────────────────────────────────────────────────────────────────┐│
│  │                    存储层安全                                   ││
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────────┐ ││
│  │  │ 本地存储加密 │  │ 配置文件保护 │  │ 会话数据访问控制         │ ││
│  │  └─────────────┘  └─────────────┘  └─────────────────────────┘ ││
│  └─────────────────────────────────────────────────────────────────┘│
└─────────────────────────────────────────────────────────────────────┘

数据加密机制

本地数据存储加密

DeepChat 对本地存储的敏感数据进行加密处理:

class DataEncryption {
  private encryptionKey: string;
  
  constructor() {
    this.encryptionKey = this.getOrGenerateEncryptionKey();
  }
  
  // 加密数据
  encrypt(data: string): string {
    const iv = crypto.randomBytes(16);
    const cipher = crypto.createCipher('aes-256-gcm', this.encryptionKey);
    cipher.setAAD(iv);
    
    let encrypted = cipher.update(data, 'utf8', 'hex');
    encrypted += cipher.final('hex');
    
    const authTag = cipher.getAuthTag();
    
    return JSON.stringify({
      encrypted,
      iv: iv.toString('hex'),
      authTag: authTag.toString('hex')
    });
  }
  
  // 解密数据
  decrypt(encryptedData: string): string {
    const { encrypted, iv, authTag } = JSON.parse(encryptedData);
    
    const decipher = crypto.createDecipher('aes-256-gcm', this.encryptionKey);
    decipher.setAAD(Buffer.from(iv, 'hex'));
    decipher.setAuthTag(Buffer.from(authTag, 'hex'));
    
    let decrypted = decipher.update(encrypted, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    
    return decrypted;
  }
  
  // 获取或生成加密密钥
  private getOrGenerateEncryptionKey(): string {
    // 尝试从安全存储中获取密钥
    let key = secureStorage.get('encryption_key');
    
    if (!key) {
      // 生成新的密钥
      key = crypto.randomBytes(32).toString('hex');
      secureStorage.set('encryption_key', key);
    }
    
    return key;
  }
}

敏感配置信息保护

API 密钥等敏感配置信息采用加密存储:

class SecureConfigManager {
  private encryption: DataEncryption;
  
  constructor() {
    this.encryption = new DataEncryption();
  }
  
  // 保存敏感配置
  saveSensitiveConfig(key: string, value: string): void {
    const encryptedValue = this.encryption.encrypt(value);
    localStorage.setItem(`secure_config_${key}`, encryptedValue);
  }
  
  // 获取敏感配置
  getSensitiveConfig(key: string): string | null {
    const encryptedValue = localStorage.getItem(`secure_config_${key}`);
    if (!encryptedValue) return null;
    
    try {
      return this.encryption.decrypt(encryptedValue);
    } catch (error) {
      console.error('Failed to decrypt config:', error);
      return null;
    }
  }
  
  // 删除敏感配置
  removeSensitiveConfig(key: string): void {
    localStorage.removeItem(`secure_config_${key}`);
  }
}

网络安全与隐私保护

网络代理支持

DeepChat 支持通过代理服务器进行网络请求,增强隐私保护:

class ProxyManager {
  private proxyConfig: ProxyConfig | null = null;
  
  // 设置代理配置
  setProxy(config: ProxyConfig): void {
    this.proxyConfig = config;
    
    // 配置 HTTP 客户端使用代理
    httpClient.setProxy(config);
    
    // 配置 WebSocket 连接使用代理
    websocketClient.setProxy(config);
  }
  
  // 获取当前代理配置
  getProxy(): ProxyConfig | null {
    return this.proxyConfig;
  }
  
  // 禁用代理
  disableProxy(): void {
    this.proxyConfig = null;
    httpClient.disableProxy();
    websocketClient.disableProxy();
  }
}

interface ProxyConfig {
  host: string;
  port: number;
  username?: string;
  password?: string;
  protocol: 'http' | 'https' | 'socks5';
}

HTTPS/TLS 配置

确保所有外部通信都通过 HTTPS 进行:

class SecureHttpClient {
  private axiosInstance: AxiosInstance;
  
  constructor() {
    this.axiosInstance = axios.create({
      // 强制使用 HTTPS
      baseURL: 'https://',
      // 验证 SSL 证书
      httpsAgent: new https.Agent({
        rejectUnauthorized: true,
        // 可以指定自定义 CA 证书
        ca: this.loadCustomCACertificates()
      })
    });
    
    // 添加请求拦截器
    this.axiosInstance.interceptors.request.use(
      this.requestInterceptor.bind(this)
    );
  }
  
  private requestInterceptor(config: AxiosRequestConfig): AxiosRequestConfig {
    // 确保使用 HTTPS
    if (config.url && !config.url.startsWith('https://')) {
      throw new Error('Only HTTPS connections are allowed');
    }
    
    // 添加安全头部
    config.headers = {
      ...config.headers,
      'User-Agent': this.getSecureUserAgent(),
      'X-Content-Type-Options': 'nosniff',
      'X-Frame-Options': 'DENY',
      'X-XSS-Protection': '1; mode=block'
    };
    
    return config;
  }
  
  private loadCustomCACertificates(): Buffer[] {
    // 加载自定义 CA 证书(如果有的话)
    const caPaths = this.getCustomCACertificatePaths();
    return caPaths.map(path => fs.readFileSync(path));
  }
}

访问控制与权限管理

用户认证机制

DeepChat 实现了基本的用户认证机制:

class AuthenticationManager {
  private currentUser: User | null = null;
  private token: string | null = null;
  
  // 用户登录
  async login(username: string, password: string): Promise<boolean> {
    try {
      const response = await httpClient.post('/auth/login', {
        username,
        password: this.hashPassword(password)
      });
      
      this.token = response.data.token;
      this.currentUser = response.data.user;
      
      // 保存认证信息(加密存储)
      secureStorage.set('auth_token', this.token);
      
      return true;
    } catch (error) {
      console.error('Login failed:', error);
      return false;
    }
  }
  
  // 检查认证状态
  isAuthenticated(): boolean {
    if (this.token && this.currentUser) {
      return true;
    }
    
    // 尝试从存储中恢复认证信息
    const storedToken = secureStorage.get('auth_token');
    if (storedToken) {
      this.token = storedToken;
      // 验证令牌有效性
      return this.validateToken(storedToken);
    }
    
    return false;
  }
  
  // 用户登出
  logout(): void {
    this.currentUser = null;
    this.token = null;
    secureStorage.remove('auth_token');
  }
  
  private hashPassword(password: string): string {
    return crypto.createHash('sha256').update(password).digest('hex');
  }
  
  private validateToken(token: string): boolean {
    // 验证 JWT 令牌或其他类型的令牌
    try {
      const decoded = jwt.verify(token, this.getJWTSecret());
      return decoded.exp > Date.now() / 1000;
    } catch (error) {
      return false;
    }
  }
}

功能权限控制

对不同功能模块实现细粒度的权限控制:

class PermissionManager {
  private userPermissions: Set<string> = new Set();
  
  // 检查用户是否有特定权限
  hasPermission(permission: string): boolean {
    // 未启用认证时,默认允许所有权限
    if (!AuthenticationManager.getInstance().isAuthenticated()) {
      return true;
    }
    
    return this.userPermissions.has(permission);
  }
  
  // 检查多个权限
  hasPermissions(permissions: string[], operator: 'AND' | 'OR' = 'AND'): boolean {
    if (operator === 'AND') {
      return permissions.every(p => this.hasPermission(p));
    } else {
      return permissions.some(p => this.hasPermission(p));
    }
  }
  
  // 请求权限
  async requestPermission(permission: string): Promise<boolean> {
    // 检查是否已拥有权限
    if (this.hasPermission(permission)) {
      return true;
    }
    
    // 显示权限请求对话框
    const granted = await this.showPermissionDialog(permission);
    if (granted) {
      this.userPermissions.add(permission);
      this.savePermissions();
    }
    
    return granted;
  }
  
  private async showPermissionDialog(permission: string): Promise<boolean> {
    // 在 UI 中显示权限请求对话框
    return ipcRenderer.invoke('show-permission-dialog', {
      permission,
      description: this.getPermissionDescription(permission)
    });
  }
  
  private getPermissionDescription(permission: string): string {
    const descriptions: Record<string, string> = {
      'file-access': '访问本地文件系统',
      'network-access': '访问网络资源',
      'camera-access': '访问摄像头',
      'microphone-access': '访问麦克风',
      'mcp-tools': '执行 MCP 工具'
    };
    
    return descriptions[permission] || '访问特定功能';
  }
}

屏幕投影隐藏功能

隐私模式实现

DeepChat 提供了隐私模式,可以在屏幕共享或投影时隐藏敏感内容:

class PrivacyModeManager {
  private isEnabled: boolean = false;
  private originalContent: WeakMap<Element, string> = new WeakMap();
  
  // 启用隐私模式
  enablePrivacyMode(): void {
    if (this.isEnabled) return;
    
    this.isEnabled = true;
    
    // 隐藏敏感元素
    this.hideSensitiveElements();
    
    // 监听新创建的元素
    this.observeDOMChanges();
    
    // 发送事件通知
    this.dispatchPrivacyModeEvent(true);
  }
  
  // 禁用隐私模式
  disablePrivacyMode(): void {
    if (!this.isEnabled) return;
    
    this.isEnabled = false;
    
    // 恢复原始内容
    this.restoreSensitiveElements();
    
    // 停止监听 DOM 变化
    this.stopObservingDOMChanges();
    
    // 发送事件通知
    this.dispatchPrivacyModeEvent(false);
  }
  
  private hideSensitiveElements(): void {
    // 查找所有敏感元素
    const sensitiveElements = document.querySelectorAll(
      '.api-key, .password, .sensitive-data, [data-sensitive="true"]'
    );
    
    sensitiveElements.forEach(element => {
      // 保存原始内容
      this.originalContent.set(element, element.textContent || '');
      
      // 替换为隐藏内容
      element.textContent = '••••••••';
      element.classList.add('privacy-mode-hidden');
    });
  }
  
  private restoreSensitiveElements(): void {
    // 恢复所有被隐藏的元素
    document.querySelectorAll('.privacy-mode-hidden').forEach(element => {
      const originalContent = this.originalContent.get(element);
      if (originalContent) {
        element.textContent = originalContent;
        element.classList.remove('privacy-mode-hidden');
      }
    });
    
    this.originalContent = new WeakMap();
  }
  
  private dispatchPrivacyModeEvent(enabled: boolean): void {
    const event = new CustomEvent('privacy-mode-change', {
      detail: { enabled }
    });
    document.dispatchEvent(event);
  }
}

快捷键支持

提供快捷键快速切换隐私模式:

class KeyboardShortcutManager {
  private shortcuts: Map<string, () => void> = new Map();
  
  constructor() {
    this.registerDefaultShortcuts();
    this.setupGlobalShortcutListener();
  }
  
  private registerDefaultShortcuts(): void {
    // 注册隐私模式切换快捷键 (Ctrl+Shift+H)
    this.registerShortcut('Ctrl+Shift+H', () => {
      const privacyManager = PrivacyModeManager.getInstance();
      if (privacyManager.isEnabled) {
        privacyManager.disablePrivacyMode();
      } else {
        privacyManager.enablePrivacyMode();
      }
    });
    
    // 注册其他快捷键...
  }
  
  private registerShortcut(combination: string, callback: () => void): void {
    this.shortcuts.set(combination.toLowerCase(), callback);
  }
  
  private setupGlobalShortcutListener(): void {
    document.addEventListener('keydown', (event: KeyboardEvent) => {
      const combination = this.getPressedKeys(event);
      const callback = this.shortcuts.get(combination);
      
      if (callback) {
        event.preventDefault();
        callback();
      }
    });
  }
  
  private getPressedKeys(event: KeyboardEvent): string {
    const keys: string[] = [];
    
    if (event.ctrlKey) keys.push('ctrl');
    if (event.shiftKey) keys.push('shift');
    if (event.altKey) keys.push('alt');
    if (event.metaKey) keys.push('meta');
    
    // 添加主键
    if (event.key && !['Control', 'Shift', 'Alt', 'Meta'].includes(event.key)) {
      keys.push(event.key.toLowerCase());
    }
    
    return keys.join('+');
  }
}

安全审计与监控

操作日志记录

记录关键安全相关操作:

class SecurityAuditLogger {
  private logFile: string;
  
  constructor() {
    this.logFile = path.join(app.getPath('userData'), 'security.log');
  }
  
  // 记录安全事件
  logSecurityEvent(event: SecurityEvent): void {
    const logEntry: LogEntry = {
      timestamp: new Date().toISOString(),
      eventType: event.type,
      description: event.description,
      userId: event.userId,
      ip: event.ip,
      userAgent: event.userAgent,
      severity: event.severity
    };
    
    // 写入日志文件
    fs.appendFileSync(
      this.logFile,
      JSON.stringify(logEntry) + '\n'
    );
    
    // 如果是高严重性事件,发送通知
    if (event.severity === 'high') {
      this.sendSecurityAlert(logEntry);
    }
  }
  
  // 发送安全警报
  private sendSecurityAlert(logEntry: LogEntry): void {
    // 可以通过邮件、系统通知等方式发送警报
    ipcRenderer.send('security-alert', logEntry);
  }
}

interface SecurityEvent {
  type: 'login' | 'logout' | 'permission-denied' | 'suspicious-activity' | 'data-access';
  description: string;
  userId?: string;
  ip?: string;
  userAgent?: string;
  severity: 'low' | 'medium' | 'high';
}

小结

DeepChat 的安全与隐私保护机制涵盖了数据加密、网络安全、访问控制和隐私保护等多个方面:

  1. 数据加密 - 对本地存储的敏感数据进行加密保护
  2. 网络安全 - 通过 HTTPS/TLS 和代理支持确保通信安全
  3. 访问控制 - 实现用户认证和细粒度权限管理
  4. 隐私保护 - 提供隐私模式和屏幕投影隐藏功能
  5. 安全审计 - 记录安全事件并发送警报

这些机制共同构成了 DeepChat 的安全防护体系,确保用户数据的安全和隐私。通过这些措施,用户可以在享受 AI 助手强大功能的同时,不必担心敏感信息的泄露。

在下一篇文章中,我们将探讨 DeepChat 的性能优化与企业级应用实践。