sqlite-web 0.7.2
gitea.db
issue
Create
Query
access
access_token
action
action_artifact
action_run
action_run_index
action_run_job
action_runner
action_runner_token
action_schedule
action_schedule_spec
action_task
action_task_output
action_task_step
action_tasks_version
action_variable
app_state
attachment
auth_token
badge
branch
collaboration
comment
commit_status
commit_status_index
commit_status_summary
commit_sync_log
commit_sync_status
dbfs_data
dbfs_meta
deploy_key
email_address
email_hash
external_login_user
follow
gpg_key
gpg_key_import
hook_task
issue
issue_assignees
issue_content_history
issue_dependency
issue_index
issue_label
issue_pin
issue_user
issue_watch
label
language_stat
lfs_lock
lfs_meta_object
login_source
milestone
mirror
notice
notification
oauth2_application
oauth2_authorization_code
oauth2_grant
org_user
package
package_blob
package_blob_upload
package_cleanup_rule
package_file
package_property
package_version
project
project_board
project_issue
protected_branch
protected_tag
public_key
pull_auto_merge
pull_request
push_mirror
reaction
release
renamed_branch
repo_archiver
repo_hidden_file
repo_indexer_status
repo_license
repo_redirect
repo_topic
repo_transfer
repo_unit
repository
review
review_state
secret
session
sqlite_sequence
star
stopwatch
system_setting
task
team
team_invite
team_repo
team_unit
team_user
topic
tracked_time
two_factor
upload
user
user_badge
user_blocking
user_open_id
user_redirect
user_setting
version
watch
webauthn_credential
webhook
Toggle helper tables
Structure
Content
Query
Insert
Drop
Import
Export
Delete row 238 from issue
id
238
repo_id
22
index
17
poster_id
1
original_author
original_author_id
0
name
🔍 代码审查报告:app-260519 - 1
content
## 自动代码审查报告 **分支**: app-260519 **提交**: `6c086caae
## 自动代码审查报告 **分支**: app-260519 **提交**: `6c086caae3f89423a968fde5f171071ce76cd7d6` **提交人**: LITTLEMAIDI (11833999+littlemaidi@user.noreply.gitee.com) **时间**: 2026-05-20 14:10:42 --- ## 1. 审查摘要 - **代码质量评分**:5.5 / 10 分 - **总体评价**:该 Model 承载了核心支付流水、退款及营收统计逻辑,业务覆盖较全。但代码存在明显的 **SQL 注入风险**、**事务回滚隐患**、**N+1 查询性能瓶颈** 以及 **硬编码敏感密钥** 等问题。代码风格偏向早期 CI3 开发习惯,缺乏现代 PHP 类型约束与防御性编程设计。 - **风险等级**:🔴 高 - **框架说明**:注:代码结构、加载方式(`get_instance()`、`$this->load->model()`、`system/` 目录)及查询构造器特征高度符合 **CodeIgniter 3** 框架,而非 `phpci`。以下审查基于 CI3 架构规范与 PHP 8+ 最佳实践进行。若实际为 `phpci`,请确认底层是否兼容 CI3 语法。 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | `refund()` / `update_refund_amount()` | **SQL 注入风险**:多处使用字符串拼接构造 `WHERE` 和 `UPDATE` 语句(如 `'_id = "' . $pay_id . '"'`),未使用查询构造器或参数绑定。若 `$pay_id` 或 `$type` 未严格过滤,将导致注入。 | 全面改用 CI3 Query Builder 或预处理机制,利用框架自动转义功能。 | `$this->db->where('_id', $pay_id)->where_in('_status', [1,4])->get()->row();`<br>`$this->db->set('_refund_amount', '_refund_amount + ' . floatval($refund_amount), FALSE)->update($this->table_name, $where);` | | 🔴 严重 | `mobile_refund()` | **硬编码敏感密钥**:退款验签使用硬编码字符串 `'1441600902'` 等作为 Salt。一旦泄露或需多环境隔离,将引发严重安全与运维问题。 | 将密钥移至 `application/config/config.php` 或环境变量,通过 `$this->config->item()` 读取。 | `$salt = $this->config->item('refund_salt_wechat');`<br>`$data['refund_key'] = md5($order_info['_id'] . $order_info['_trade_no'] . $salt);` | | 🔴 严重 | `refund()` | **事务安全性隐患**:依赖全局 `throwError()` 中断流程。若该函数直接 `exit/die` 或抛出未捕获异常,会导致 `$this->db->trans_begin()` 开启的事务未执行 `trans_rollback()`,引发数据不一致。 | 使用 `try...catch` 包裹事务逻辑,确保任何异常都能触发回滚。 | `try { $this->db->trans_begin(); ... } catch (\Throwable $e) { $this->db->trans_rollback(); log_message('error', $e->getMessage()); throwError($e->getMessage()); }` | | 🟠 警告 | `get_bill_pay_log()` / `get_refundable_pay_log()` | **N+1 查询性能瓶颈**:在 `foreach ($log_data as &$v)` 循环内频繁调用 `get_one()` 查询关联模型(订单扩展、门店配置、挂账日志等)。数据量 >50 时响应时间呈指数级增长。 | 提取循环内查询条件,使用 `where_in` 批量查询,在内存中建立映射关系后填充。 | 见下方优化示例 | | 🟠 警告 | `add_order_pay_log()` | **数组状态污染**:循环内 `unset($params['_payment_amount'])` 会修改原数组引用。若 `$params` 在循环外被复用,将导致后续逻辑丢失字段。 | 在循环内创建副本操作,避免污染入参。 | `$current_params = $params; unset($current_params['_payment_amount']); $insertRes = $this->_deal_add_pay_log(..., $current_params);` | | 🟠 警告 | 多处方法 | **频繁加载模型**:在业务方法内部重复调用 `$this->load->model()`,增加框架解析与实例化开销。 | 将高频依赖模型移至构造函数加载,或配置 `autoload.php`。 | `public function __construct() { parent::__construct(); $this->load->model(['ahead_vip_model','ahead_bill_model','ManageMap']); }` | | 🟡 建议 | 全局常量定义 | **魔法数字泛滥**:状态码 `14, 15, 11, 6, 17~28` 等散落在代码中,可读性与可维护性差。 | 提取为类常量,统一使用常量进行条件判断。 | `const PLATFORM_CUSTOM_MIN = 17; const PLATFORM_CUSTOM_MAX = 28;`<br>`if ($platform >= self::PLATFORM_CUSTOM_MIN && $platform <= self::PLATFORM_CUSTOM_MAX) { ... }` | | 🟡 建议 | 全局注释/规范 | **过期注释与类型缺失**:存在 `//之前不知道为啥被注释掉...2020-09-30` 等无效注释;方法缺乏 PHPDoc 类型声明,不符合现代 PHP 规范。 | 清理无用注释,补充 `@param`、`@return` 类型提示,遵循 PSR-12 缩进与命名规范。 | `/** @param int $pay_id @param float $refund_amount @return bool */ public function refund(int $pay_id, float $refund_amount): bool` | ### 💡 N+1 查询优化示例 (`get_bill_pay_log` 循环段) ```php // 1. 批量提取需要查询的 ID $order_ids = array_unique(array_column($log_data, 'relation_id')); $shop_ids = array_unique(array_column($log_data, 'shop_id')); // 2. 批量查询并建立映射 $order_ext_map = []; if ($order_ids) { $this->load->model('ahead_yc_order_extension_model'); $extensions = $this->ahead_yc_order_extension_model->select(['where_in' => ['_order_id', $order_ids]]); foreach ($extensions as $ext) { $order_ext_map[$ext['_order_id']] = $ext; } } // 3. 循环内直接读取映射,消除 DB 调用 foreach ($log_data as &$v) { $v['manager_reason'] = ''; if ($v['type'] == 1 && isset($order_ext_map[$v['relation_id']]['_alter_timing_reason'])) { $v['manager_reason'] = $order_ext_map[$v['relation_id']]['_alter_timing_reason']; } // ... 其他逻辑保持不变 } ``` ## 3. 总结与行动建议 ### 🚨 优先修复的关键问题(P0/P1) 1. **修复 SQL 注入漏洞**:立即替换 `refund()`、`update_refund_amount()` 中所有字符串拼接的 SQL 条件,改用 `$this->db->where()`、`$this->db->set()` 或预处理语句。 2. **加固事务边界**:将 `refund()` 中的事务逻辑包裹在 `try...catch` 中,确保任何异常或 `throwError()` 触发前执行 `$this->db->trans_rollback()`。 3. **移除硬编码密钥**:将 `mobile_refund()` 中的验签 Salt 抽离至配置文件,支持多环境隔离与密钥轮换。 ### 🛠 后续重构与优化方向 1. **性能架构升级**: - 彻底消除 Model 方法内的循环 DB 查询,采用 `where_in` + 内存映射模式。 - 考虑将高频只读配置(如支付平台映射、门店自定义支付)接入 Redis 缓存,降低 DB 压力。 2. **代码规范化**: - 清理 `2020-09-30` 等历史调试注释,统一使用 PHPDoc 规范。 - 逐步引入 PHP 8 类型声明(`int`, `float`, `array`, `bool`),提升静态分析能力。 - 将魔法数字收敛至类常量或独立配置类。 3. **框架适配建议**: - 若项目确为 CI3,建议启用 `$config['db_debug'] = FALSE;` 生产环境配置,并统一使用 CI3 的 `trans_strict` 模式。 - 若实际使用 `phpci`,请确认其底层是否完全兼容 CI3 的 Query Builder 与事务 API,必要时需适配对应框架的 ORM 语法。 > ⚠️ **局限性说明**:由于提供的代码在 `add_by_vip_recharge_order` 方法末尾被截断,该方法的完整逻辑、返回值处理及潜在缺陷未能纳入本次审查。建议补充完整代码后再次进行深度评估。 --- *此 Issue 由代码审查服务自动创建*
...
milestone_id
0
priority
0
is_closed
0
is_pull
0
num_comments
0
ref
deadline_unix
0
created_unix
1779257443
updated_unix
1779257443
closed_unix
0
is_locked
0
content_version
0
time_estimate
0
Delete
Cancel