Skip to content

feat(audit): add localized security audit logs - #5462

Merged
Calcium-Ion merged 1 commit into
mainfrom
feat/audit-logging
Jun 12, 2026
Merged

feat(audit): add localized security audit logs#5462
Calcium-Ion merged 1 commit into
mainfrom
feat/audit-logging

Conversation

@Calcium-Ion

@Calcium-Ion Calcium-Ion commented Jun 12, 2026

Copy link
Copy Markdown
Member

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

Summary by CodeRabbit

  • New Features

    • Comprehensive audit logging system tracking administrative actions, channel operations, user management, and login events.
    • Enhanced audit log display showing operation details, changed fields, success/failure status, and login metadata.
    • Multi-language support for audit messages and operation descriptions.
  • Documentation

    • Updated contribution guidelines for pull request standards.

@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 89d04fff-086b-4a3a-aaff-4c88bb656fa5

📥 Commits

Reviewing files that changed from the base of the PR and between 27b2b2c and b2fdcfa.

📒 Files selected for processing (25)
  • AGENTS.md
  • CLAUDE.md
  • constant/context_key.go
  • controller/audit.go
  • controller/channel.go
  • controller/channel_upstream_update.go
  • controller/option.go
  • controller/passkey.go
  • controller/redemption.go
  • controller/twofa.go
  • controller/user.go
  • middleware/audit.go
  • middleware/auth.go
  • model/log.go
  • web/default/src/features/usage-logs/components/columns/common-logs-columns.tsx
  • web/default/src/features/usage-logs/components/dialogs/details-dialog.tsx
  • web/default/src/features/usage-logs/constants.ts
  • web/default/src/features/usage-logs/lib/format.ts
  • web/default/src/features/usage-logs/types.ts
  • web/default/src/i18n/locales/en.json
  • web/default/src/i18n/locales/fr.json
  • web/default/src/i18n/locales/ja.json
  • web/default/src/i18n/locales/ru.json
  • web/default/src/i18n/locales/vi.json
  • web/default/src/i18n/locales/zh.json

Walkthrough

This PR introduces comprehensive audit logging across the application. It adds backend infrastructure for capturing and recording operation and login events, middleware to intercept write requests, handler-level auditing for channels, users, and system operations, frontend type extensions and rendering logic, and complete internationalization support across six languages.

Changes

Comprehensive Audit Logging System

Layer / File(s) Summary
Audit context key and core utilities
constant/context_key.go, controller/audit.go
Introduces ContextKeyAuditLogged context key, audit template dictionaries mapping actions to English fallback text, and core utilities: auditContentEN for template expansion, auditOperatorInfo for admin metadata extraction, and recordManageAudit/recordManageAuditFor/recordUserSecurityAudit for recording operation and security audit logs.
Log model recording functions
model/log.go
Adds LogTypeLogin = 7 constant, buildOpField helper for operation descriptors, RecordLoginLog for login audit persistence, and RecordOperationAuditLog for operation audit persistence; updates formatUserLogs to remove audit_info for non-admin users.
Audit middleware and request-level recording
middleware/audit.go
Implements auditResponseWriter to buffer response bodies (up to 64 KiB), beginAdminAudit/finishAdminAudit handlers for intercepting write operations, static route-action mapping for recognized endpoints, and asynchronous audit dispatch via goroutine pool.
Auth middleware audit integration
middleware/auth.go
Wraps authHelper to conditionally enable request-level auditing for admin and root routes via beginAdminAudit/finishAdminAudit.
Channel operation auditing
controller/channel.go
Records audit logs for channel operations: channel.key_view (GetChannelKey), channel.create (AddChannel), channel.update with changed_fields (UpdateChannel), channel.delete (DeleteChannel), channel.copy (CopyChannel), and tag/batch/multi-key operations; introduces equalStringPtr nil-safe string comparison helper.
Channel upstream update auditing
controller/channel_upstream_update.go
Records channel.upstream_apply and channel.upstream_apply_all audit events with channel ID or result count.
User login and management auditing
controller/user.go
Adds loginMethodFromContext and recordLoginAudit for login event tracking; replaces prior string-based logging with structured recordManageAuditFor calls for user updates, bindings, deletions, creations, quota changes, and general management actions.
Passkey and 2FA account auditing
controller/passkey.go, controller/twofa.go
Records user security audit events for passkey register/delete; records management audit for passkey resets and admin 2FA disables; fixes PasskeyLoginFinish to execute setupLogin instead of early return.
Redemption and option auditing
controller/redemption.go, controller/option.go
Records redemption.create and option.update audit logs with relevant metadata.
Frontend audit data types and constants
web/default/src/features/usage-logs/types.ts, web/default/src/features/usage-logs/constants.ts
Extends LogOtherData to support operation metadata, admin audit fallback details, and login fields; adds LOGIN log type with teal color.
Audit content rendering and formatting
web/default/src/features/usage-logs/lib/format.ts
Adds AUDIT_TEMPLATES dictionary with i18n template strings and renderAuditContent helper for template lookup and rendering.
Audit log display in columns and dialogs
web/default/src/features/usage-logs/components/columns/common-logs-columns.tsx, web/default/src/features/usage-logs/components/dialogs/details-dialog.tsx
Routes audit and login log types through renderAuditContent; adds "Operation Audit Info" section (operation text, changed fields, request details, result) and "Login Info" section (login method, IP, user agent).
Internationalization for audit messages
web/default/src/i18n/locales/{en,fr,ja,ru,vi,zh}.json
Adds ~72 new translation keys per locale covering audit action labels, operation descriptions, quota changes, login messages, reset actions, and management notifications with template placeholders.
Documentation updates
AGENTS.md, CLAUDE.md
Adds "Rule 8: Pull Requests" section describing PR contributor identification, AI-generation/assistance disclosure, and mandatory PR template usage.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • QuantumNous/new-api#1931: Passkey reset route and behavior changes that directly relate to the new AdminResetPasskey audit logging in this PR.

  • QuantumNous/new-api#1653: Adds 2FA-protected GetChannelKey endpoint, which overlaps with this PR's new channel.key_view management audit recording.

Suggested reviewers

  • seefs001
  • creamlike1024

Poem

🐰 Hops with glee through audit trails,
Recording every action, never fails!
From channels deep to logins bright,
The logs now tell what happened right. 🔐
Six languages sing of deeds well done,
Audit logging—the PR's won! 🎉

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/audit-logging

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@Calcium-Ion
Calcium-Ion merged commit d0c4305 into main Jun 12, 2026
1 of 2 checks passed
@Calcium-Ion
Calcium-Ion deleted the feat/audit-logging branch June 13, 2026 08:34
YeMao11 pushed a commit to YeMao11/new-api that referenced this pull request Jun 16, 2026
Upstream changes (50+ commits, v1.0.0-rc.11):
- data-table perf: row selection memo, column pinning, badge display
- Fixes: channel test dialog (QuantumNous#5517), CC Switch model selector (QuantumNous#5515),
  API key form options (QuantumNous#5512), cell overflow (QuantumNous#5510), kimi k2.6 temp (QuantumNous#5390),
  Anthropic-compatible GLM chunked encoding (QuantumNous#5307), streaming image relay (QuantumNous#4608)
- Feat: audit auth method tracking (QuantumNous#5462), channel affinity clear toggle (QuantumNous#5306),
  relay idle timeout config (QuantumNous#5309), 6-decimal pricing precision (QuantumNous#5332)
- Classic frontend: Rsbuild support, Semi React 19 adapter
- Shared dialog wrapper, JSON code editor, debounce channel search

Conflict resolved: web/bun.lock (accepted upstream, will regenerate)

Co-Authored-By: Claude <noreply@anthropic.com>
YeMao11 pushed a commit to YeMao11/new-api that referenced this pull request Jun 16, 2026
Merge upstream v1.0.0-rc.11 (50+ commits):
- data-table perf: row selection memo, column pinning, badge display
- Fixes: channel test dialog (QuantumNous#5517), CC Switch (QuantumNous#5515), API key (QuantumNous#5512),
  kimi k2.6 temp (QuantumNous#5390), GLM chunked encoding (QuantumNous#5307), streaming image (QuantumNous#4608)
- Feat: audit auth tracking (QuantumNous#5462), channel affinity toggle (QuantumNous#5306),
  relay idle timeout (QuantumNous#5309), 6-decimal pricing (QuantumNous#5332)
- Shared dialog wrapper, JSON code editor, classic Rsbuild support

SEO optimization:
- robots.txt: 10 AI crawler blocks + 22 path disallows + crawl-delay
- sitemap.xml: 7 public URLs with 6-language hreflang annotations
- index.html: hreflang tags, og:locale:alternate, og:image, canonical,
  5 structured data types (Organization, SoftwareApplication, FAQPage,
  WebSite, SearchAction), expanded keywords (gateway, agent router,
  aggregation, orchestration)
- i18n/config.ts: sync <html lang> with active language for SEO

Co-Authored-By: Claude <noreply@anthropic.com>
ruanhangjian pushed a commit to ruanhangjian/new-api that referenced this pull request Jul 11, 2026
noah-wung pushed a commit to noah-wung/new-api that referenced this pull request Jul 17, 2026
zhaodechao2008 pushed a commit to zhaodechao2008/new-api that referenced this pull request Jul 27, 2026
330079598 pushed a commit to 330079598/new-api that referenced this pull request Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant