Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] 测试基于goInception的脱敏发现两个问题 #1360

Open
hhyo opened this issue Jan 25, 2022 · 25 comments · Fixed by #1371
Open

[bug] 测试基于goInception的脱敏发现两个问题 #1360

hhyo opened this issue Jan 25, 2022 · 25 comments · Fixed by #1371

Comments

@hhyo
Copy link
Owner

hhyo commented Jan 25, 2022

测试基于goInception的脱敏发现两个问题,覆盖场景比Inception的少,需要解决一下

  1. 无法识别哪些语句不能正常脱敏,导致 QUERY_CHECK 参数失效,不能脱敏的语句也能正常查询,有较大风险
    嵌套子查询: select * from (select * from users ) t;
    多列函数:select concat(id,phone) from users;

  2. 不支持诸如 [*,column_a]| [a.*, column_a, b.*] 的脱敏
    select *,phone from users;
    select a.*,a.phone,b.* from users a join sql_users_groups b on a.id =b.users_id;

上述例子都可以在演示环境进行测试

Originally posted by @hhyo in #1307 (comment)

@unknowissue
Copy link
Contributor

我想想,

@unknowissue
Copy link
Contributor

测试基于goInception的脱敏发现两个问题,覆盖场景比Inception的少,需要解决一下

  1. 无法识别哪些语句不能正常脱敏,导致 QUERY_CHECK 参数失效,不能脱敏的语句也能正常查询,有较大风险
    嵌套子查询: select * from (select * from users ) t;
    多列函数:select concat(id,phone) from users;
  2. 不支持诸如 [,column_a]| [a., column_a, b.*] 的脱敏
    select *,phone from users;
    select a.*,a.phone,b.* from users a join sql_users_groups b on a.id =b.users_id;

上述例子都可以在演示环境进行测试

Originally posted by @hhyo in #1307 (comment)

[不支持诸如 [,column_a]| [a., column_a, b.*] 的脱敏]
这个应该可以支持,目前有问题是因为goInception 返回 的json有重复条目

[
    {
        "index": 0, 
        "field": "id", 
        "type": "int", 
        "table": "users", 
        "schema": "db1", 
        "alias": "id"
    }, 
    {
        "index": 1, 
        "field": "phone", 
        "type": "varchar(80)", 
        "table": "users", 
        "schema": "db1", 
        "alias": "phone"
    }, 
    {
        "index": 2, 
        "field": "email", 
        "type": "varchar(30)", 
        "table": "users", 
        "schema": "db1", 
        "alias": "email"
    }, 
    {
        "index": 3, 
        "field": "create_time", 
        "type": "datetime", 
        "table": "users", 
        "schema": "db1", 
        "alias": "create_time"
    }, 
    {
        "index": 1, 
        "field": "phone", 
        "type": "varchar(80)", 
        "table": "users", 
        "schema": "db1", 
        "alias": "phone"
    }
]

去掉 query_tree=DelRepeat(query_tree,'index') 函数就可以了,
image
image

但是去掉后,好像又有其他的问题,我考虑一下。

其他的还得在想想

@unknowissue
Copy link
Contributor

嗷嗷,去掉去重函数后,['UNION', 'UNION ALL'] 会有下标越界的问题,
我写个判断,如果 ['UNION', 'UNION ALL'] 关键字就去重吧。

@unknowissue
Copy link
Contributor

就剩下子查询的问题了,我还不知道咋解决,其他同学有啥想法没

@LeoQuote
Copy link
Collaborator

子查询之前就不支持?

@hhyo
Copy link
Owner Author

hhyo commented Feb 22, 2022

第一点问题不在是否支持,在于不支持的能否识别

@forkTemi
Copy link

[{'index': 0, 'field': 'id', 'type': 'int(11)', 'table': 'data_masking_rules', 'schema': 'archery', 'alias': 'id'},
{'index': 0, 'field': 'id', 'type': 'int(11)', 'table': 'auth_permission', 'schema': 'archery', 'alias': 'name'}]
DelRepeat函数处理后只剩第一条数据,是否应该全匹配去重而不是根据index去重

@unknowissue
Copy link
Contributor

DelRepeat函数处理后只剩第一条数据,是否应该全匹配去重而不是根据index去重

昂,最新的已经改了,用最新的提交看看?

@unknowissue
Copy link
Contributor

unknowissue commented Feb 23, 2022

嘶,缝合了一下,有点新问题

union 

select id,phone,email,create_time,phone  from users a where id in (select b.id from users b )
union all
select id,phone,email,create_time,phone  from users a where id in (select b.id from users b )
和
select  phone , a.*   , a.*  ,phone  from users a 
union all
select  phone , b.*   , b.*  ,phone  from users b 

我明天look look

@unknowissue
Copy link
Contributor

明天先问问goInception的同学,能不能做进去吧。
感觉 select * from (select * from users ) t; 用正则也不好匹配

@unknowissue
Copy link
Contributor

明天先问问goInception的同学,能不能做进去吧。

hanchuanchuan/goInception#429

@hhyo hhyo closed this as completed in #1371 Mar 5, 2022
@hhyo hhyo reopened this Mar 5, 2022
@hhyo
Copy link
Owner Author

hhyo commented Mar 5, 2022

第一点待解决

@unknowissue
Copy link
Contributor

那边的小哥哥还不理我。。。

@hhyo
Copy link
Owner Author

hhyo commented Mar 6, 2022

select concat(id,password) from sql_users limit 100; 抛错,demo环境可复现

@unknowissue
Copy link
Contributor

unknowissue commented Mar 7, 2022

select concat(id,password) from sql_users limit 100; 抛错,demo环境可复现

已复现

原因是goInception的返回结果发生了变化
最新的1.2.5-23 里 index是不同的
[{'index': 0, 'field': 'id', 'type': 'int', 'table': 'users', 'schema': 'db1', 'alias': 'concat(id,phone)'}, {'index': 1, 'field': 'phone', 'type': 'varchar(80)', 'table': 'users', 'schema': 'db1', 'alias': 'concat(id,phone)'}]

最新的1.2.5-10 里 index是重复的(符合预期的)
[{'index': 0, 'field': 'id', 'type': 'int', 'table': 'users', 'schema': 'db1', 'alias': 'concat(id,phone)'}, {'index': 0, 'field': 'phone', 'type': 'varchar(80)', 'table': 'users', 'schema': 'db1', 'alias': 'concat(id,phone)'}]

参考:
hanchuanchuan/goInception#355
字段说明:

index: 列索引,标识为第几列,可能重复(如函数引用了多列时)
field: 原始列名
type: 列类型,扩展属性,以供自定义使用
table: 原始表名
schema: 数据库
alias: 列别名,该值可能不准确(如未定义列别名或列名重复时)

咱们有同学懂go么?那边的小哥哥好像比较忙。。。。

已提交issue
hanchuanchuan/goInception#436

@unknowissue
Copy link
Contributor

有无大佬懂Go,支持一下goInception吧。。。😂😂😂😂

@hhyo
Copy link
Owner Author

hhyo commented Mar 27, 2022

能够在返回信息中增加expression-type,就可以做识别处理 https://hanchuanchuan.github.io/goInception/tree.html#expression-type

hhyo added a commit that referenced this issue Mar 27, 2022
@hanchuanchuan
Copy link

fixed. 如有问题欢迎随时提交issue.

@unknowissue
Copy link
Contributor

使用 goInception-linux-v1.2.5-29-ge46a69b.tar.gz
测试了一下:

  1. 嵌套子查询: select * from (select * from users ) t;
    这个可以了
    image

select concat(id,password) from sql_users limit 100; 抛错,demo环境可复现

也可以了
image

@unknowissue
Copy link
Contributor

fixed. 如有问题欢迎随时提交issue.

非常感谢!!!🤞🤞

@unknowissue
Copy link
Contributor

我看1.8.3版本使用了Inception版本,我们还是要做成开关模式么。。。

@hhyo
Copy link
Owner Author

hhyo commented Mar 29, 2022

能完全替换就不做开关,语法不支持不要紧,要求是按照当前逻辑能识别并且拦截

@unknowissue
Copy link
Contributor

Inception 版本是通过queryprint方法,它的返回中type来判断是否支持的
goInception版本是通过masking方法,它的返回中是没有这个type的

for select_item in old_select_list:
if select_item['type'] not in ('FIELD_ITEM', 'aggregate', 'FUNC_ITEM'):
raise Exception('不支持该查询语句脱敏!请联系管理员')
elif select_item['type'] == 'aggregate':
if select_item['aggregate'].get('type') not in ('FIELD_ITEM', 'INT_ITEM'):
raise Exception('不支持该查询语句脱敏!请联系管理员')
# 增加单列函数的脱敏
elif select_item['type'] == 'FUNC_ITEM':
if len(select_item['args']) != 1:
raise Exception('不支持该查询语句脱敏!请联系管理员')

type说明
所以不能像Inception 那样进行识别并拦截

需要看看有没有其他的方法?
或者 也用 goInception的 queryprint 方法🤣🤣🤣🤣

要不然,我们先用用看,收集一下哪些语法不支持?

@hhyo
Copy link
Owner Author

hhyo commented Apr 30, 2022

Inception 版本是通过queryprint方法,它的返回中type来判断是否支持的 goInception版本是通过masking方法,它的返回中是没有这个type的

for select_item in old_select_list:
if select_item['type'] not in ('FIELD_ITEM', 'aggregate', 'FUNC_ITEM'):
raise Exception('不支持该查询语句脱敏!请联系管理员')
elif select_item['type'] == 'aggregate':
if select_item['aggregate'].get('type') not in ('FIELD_ITEM', 'INT_ITEM'):
raise Exception('不支持该查询语句脱敏!请联系管理员')
# 增加单列函数的脱敏
elif select_item['type'] == 'FUNC_ITEM':
if len(select_item['args']) != 1:
raise Exception('不支持该查询语句脱敏!请联系管理员')

type说明
所以不能像Inception 那样进行识别并拦截
需要看看有没有其他的方法? 或者 也用 goInception的 queryprint 方法🤣🤣🤣🤣

要不然,我们先用用看,收集一下哪些语法不支持?

已提交PR #1483,将会在下个版本完全替换Inception

不支持的语法,不过这种会直接解析错误,不影响query_check的判断

select
  *
from
  (
    select
      concat(a.phone, a.id),
      a.phone
    from
      users a
      join sql_users_groups b on a.id = b.users_id
    union
    select
      concat(a.phone, a.id),
      a.phone
    from
      users a
      join sql_users_groups b on a.id = b.users_id
  ) as t limit 100;

@unknowissue
Copy link
Contributor

谢谢大佬!😊😊

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 a pull request may close this issue.

5 participants