Archery icon indicating copy to clipboard operation
Archery copied to clipboard

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

Open hhyo opened this issue 2 years ago • 25 comments

测试基于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 https://github.com/hhyo/Archery/issues/1307#issuecomment-1019402627

hhyo avatar Jan 25 '22 14:01 hhyo

我想想,

unknowissue avatar Jan 26 '22 07:01 unknowissue

测试基于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 avatar Jan 26 '22 10:01 unknowissue

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

unknowissue avatar Jan 26 '22 10:01 unknowissue

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

unknowissue avatar Feb 21 '22 06:02 unknowissue

子查询之前就不支持?

LeoQuote avatar Feb 22 '22 02:02 LeoQuote

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

hhyo avatar Feb 22 '22 04:02 hhyo

[{'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去重

forkTemi avatar Feb 22 '22 07:02 forkTemi

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

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

unknowissue avatar Feb 23 '22 14:02 unknowissue

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

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 avatar Feb 23 '22 14:02 unknowissue

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

unknowissue avatar Feb 23 '22 15:02 unknowissue

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

https://github.com/hanchuanchuan/goInception/issues/429

unknowissue avatar Mar 03 '22 04:03 unknowissue

第一点待解决

hhyo avatar Mar 05 '22 02:03 hhyo

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

unknowissue avatar Mar 05 '22 03:03 unknowissue

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

hhyo avatar Mar 06 '22 09:03 hhyo

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)'}]

参考: https://github.com/hanchuanchuan/goInception/pull/355 字段说明:

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

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

已提交issue https://github.com/hanchuanchuan/goInception/issues/436

unknowissue avatar Mar 07 '22 01:03 unknowissue

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

unknowissue avatar Mar 26 '22 02:03 unknowissue

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

hhyo avatar Mar 27 '22 06:03 hhyo

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

hanchuanchuan avatar Mar 27 '22 15:03 hanchuanchuan

使用 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 avatar Mar 29 '22 08:03 unknowissue

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

非常感谢!!!🤞🤞

unknowissue avatar Mar 29 '22 08:03 unknowissue

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

unknowissue avatar Mar 29 '22 10:03 unknowissue

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

hhyo avatar Mar 29 '22 14:03 hhyo

Inception 版本是通过queryprint方法,它的返回中type来判断是否支持的 goInception版本是通过masking方法,它的返回中是没有这个type的 https://github.com/hhyo/Archery/blob/a8967be4a31b68f42ee3fde34cf776c4bb525bf5/sql/utils/data_masking.py#L95-L104 type说明 所以不能像Inception 那样进行识别并拦截

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

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

unknowissue avatar Mar 30 '22 07:03 unknowissue

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

https://github.com/hhyo/Archery/blob/a8967be4a31b68f42ee3fde34cf776c4bb525bf5/sql/utils/data_masking.py#L95-L104

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;

hhyo avatar Apr 30 '22 04:04 hhyo

谢谢大佬!😊😊

unknowissue avatar May 04 '22 14:05 unknowissue