已关闭
[Bug]: index advisor 中存在潜在 SQL 注入风险 #1
SetnameWang创建于  7月27日关闭于  8月12日
SetnameWang
SetnameWang成员
7月27日 创建

测试类型

工具功能

测试版本

7.0.0-RC3

问题描述

advisor/index_advisor/postgres_env.py 在构造部分 PostgreSQL/openGauss SQL 时使用 Python % 拼接动态值。表名、列名及 HypoPG index ID 可能直接进入最终 SQL;当这些值包含引号或特殊 SQL 语法时,可能改变语句语义,形成潜在 SQL 注入风险。

涉及范围:

  • information_schema.columns 的表名查询;
  • step()cal_cur_cost() 中的 hypopg_create_index DDL;
  • step() 中的 hypopg_relation_size index ID 查询。

操作系统和硬件信息

Host: Windows with Docker Desktop

Container image: swr.cn-north-4.myhuaweicloud.com/kunpeng-ai/opengauss-distributed:0328

Database: openGauss-lite 7.0.0-RC3

测试环境

轻量版容器化

被测功能

advisor/index_advisor/postgres_env.py

预置条件

  • index advisor 可以连接 PostgreSQL/openGauss;
  • HypoPG 扩展可用;
  • schema、表名或列名包含引号等特殊字符。

操作步骤

  1. 创建包含引号等特殊字符的 schema、表名或列名。
  2. 初始化 postgresql_env,触发 catalog 查询。
  3. 调用 step()cal_cur_cost() 创建 HypoPG 假想索引。
  4. 检查传给 psycopg2 cursor 的 SQL 文本与参数。

预期输出

  • schema、table 和 HypoPG index ID 通过 psycopg2 参数绑定传递;
  • DDL 中的 schema、table、column 使用 psycopg2.sql.SQLpsycopg2.sql.Identifier 组成;
  • HypoPG DDL 作为参数传给 hypopg_create_index
  • workload payload 的 EXPLAIN {sql} 保持不变,benchmark SQL 语义不发生变化。

实际输出

原实现通过 % 将表名、列名和 index ID 直接拼入 SQL 文本,未区分 SQL 结构、标识符和数据参数。安全扫描因此报告多处潜在 SQL 注入风险。

日志信息

相关旧代码模式:

  • WHERE table_name='%s' % table
  • hypopg_create_index('CREATE INDEX ON %s (%s)') % (table_name, column_name)
  • hypopg_relation_size(%s) % str(index_id)

提单组织

开发自提

likedislike
SetnameWangSetnameWang成员
7月27日 添加了label:bug
opengauss_bot
opengauss_bot成员
7月27日 评论:

This issue requires an assignee. Since you haven't specified one, we've assigned TestManager as the default assignee for this issue.

likedislike
opengauss_botopengauss_bot成员
7月27日 将 TestManager 设为负责人
opengauss_botopengauss_bot成员
7月27日 添加了label:sig/Community
opengauss_bot
opengauss_bot成员
7月27日 评论:

Welcome To openGauss Community

Hey @Setname , thanks for your contribution to the community.

Bot Usage Manual

I'm the Bot here serving you. You can find the instructions on how to interact with me at Here . That means you can comment below every pull request or issue to trigger Bot Commands.

Contact Guide

If you have any questions, please contact the SIG: Community ,
and any of the maintainers: @CarrotGo, @chendong76, @chenxiaobin19, @congzhou2603, @dodders, @hwworkholic, @jemappellehc, @muyulinzhong, @quemingjian, @shenzheng4, @shirley_zhengx, @superlchf, @totaj, @wlff234, @wofanzheng, @ywzq1161327784 ,
and any of the committers: @Igali, @libiao2024, @wangtianyuan1994, @zhangxubo .

likedislike
SetnameWangSetnameWang成员
7月27日 修改了issue 的描述
SetnameWangSetnameWang成员
7月27日 修改了issue 的描述
SetnameWangSetnameWang成员
7月27日 修改标题为 “[Bug]: index advisor 中存在潜在 SQL 注入风险”,原标题为“[Bug]: Index advisor cannot estimate hypothetical index size on openGauss 7.0”
SetnameWangSetnameWang成员
7月27日 关联了pull request:fix: secure index advisor SQL composition
SetnameWangSetnameWang成员
7月27日 issue状态由 待办的 改变为 待回归
SetnameWangSetnameWang成员
7月28日 将 Setname 设为负责人
sungang14sungang14成员
8月3日 关联了看板:openGauss 7.0.0-LTS
sungang14sungang14成员
8月3日 issue优先级由 无优先级 改变为 次要
SetnameWang
SetnameWang成员
8月12日 评论:

自验结果:python -B -m unittest advisor.index_advisor.test.test_postgres_env,5/5 通过
image.png

测试代码可以在以下目录查看:
advisor/index_advisor/test/test_postgres_env.py

本次修复为安全补充修复,通过ut覆盖。
可以通过以下命令复测:
python -B -m unittest advisor.index_advisor.test.test_postgres_env

likedislike
SetnameWangSetnameWang成员
8月12日 修改了issue 的描述
l1azzzy
l1azzzy成员
8月12日 评论:

验收日期:2026-8-12
验收版本:7.0.0.B017
验收结论:验收通过
image.png
image.png
image.png

likedislike
l1azzzyl1azzzy成员
8月12日 issue状态由 待回归 改变为 已验收
l1azzzyl1azzzy成员
8月12日 关闭了 issue