问题描述
使用 HAR 文件诊断 Web 应用程序很常见,尤其是在向他人寻求技术支持时。但是,HAR 文件包含请求 cookie 等敏感信息。例如,如果遵循 this guide,如果未清理 HAR 文件,它可以将 cookie 值共享给技术支持。
所以我更喜欢在共享 HAR 文件之前删除请求 cookie。
有什么简单的命令可以做到吗?
解决方法
不加选择地删除所有 request.cookies:
jq 'del(.. | .request?.cookies?)' www.ibm.com.har
举个例子,给定这个 JSON 文件:
$ jq -M . /tmp/hartest.json
{
"one": {
"foo": {
"bar": true,"baz": 23
}
},"two": {
"foo": {
"bar": "Hello","baz": "World"
}
},"three": {
"fu": {
"bar": "gone","baz": "forgotten"
}
}
}
我可以用这个命令去掉所有的“bar”键:
jq -M 'del( .. | .bar?)' /tmp/hartest.json
通过 diff
运行之前和之后:
$ diff <(jq -M . /tmp/hartest.json) <(jq -M 'del( .. | .bar?)' /tmp/hartest.json )
4d3
< "bar": true,10d8
< "bar": "Hello",16d13
< "bar": "gone",
如果我只想摆脱'.foo.bar',我会用这个:
jq -M 'del( .. | .foo?.bar?)' /tmp/hartest.json
再次运行 diff
给我们:
$ diff <(jq -M . /tmp/hartest.json) <(jq -M 'del( .. | .foo?.bar?)' /tmp/hartest.json )
4d3
< "bar": true,