在网络请求日志中读取秘密的正则表达式

问题描述 投票:0回答:1

我们正在记录请求,当然这些请求也包含授权标头等敏感数据。现在我想最好使用正则表达式来重新记录它。因为我总是发现设计正则表达式非常复杂,所以我想我在这里问。

部分回复如下所示:

Http: createRide (422): http(serviceResponse: POST https://staging.test.com/sometpath {
   ["Authorization": "Bearer xxxxxxxxxx", "X-REQUEST-ID": "5FE8D9C0-B3F0-4DAE-9188-A0CB1906EB5D", "X-Client-Version": "3.0.0", "X-Client-Identifier": "something", "Content-Type": "application/json", "X-Api-Version": "1", "X-Client-Secret": "xxxxxxxxx"]
 }
  {
  "data" : {
  }

正则表达式应按如下方式工作:

找到包含名称

secret
authorization
的标头字段,最好不区分大小写,并将其值替换为例如
***

结果应如下所示:

Http: createRide (422): http(serviceResponse: POST https://staging.test.com/sometpath {
   ["Authorization": "***", "X-REQUEST-ID": "5FE8D9C0-B3F0-4DAE-9188-A0CB1906EB5D", "X-Client-Version": "3.0.0", "X-Client-Identifier": "something", "Content-Type": "application/json", "X-Api-Version": "1", "X-Client-Secret": "***"]
 }
  {
  "data" : {
  }
swift regex bash shell
1个回答
0
投票

这应该有效

masked_log=$(echo "$log" | sed -E 's/(Authorization|Secret)":\s*"[^"]*"/\1": "***"/Ig')
© www.soinside.com 2019 - 2024. All rights reserved.