doujiaci7976 2018-10-24 08:38
浏览 187
已采纳

屏蔽敏感的URL查询参数

Say I have this url

https://example.com:8080?private-token=foo&authenticity_token=bar

And I have a function to determine whether to mask a param.

How can I mask the url, but maintaining the order of params.

Currently I have

u, err := url.Parse(originalURL)
if err != nil {
    panic(err)
}
m, _ := url.ParseQuery(u.RawQuery)
for key := range m {
    if toMask(key) {
        m.Set(key, "FILTERED")
    }
}
u.RawQuery = m.Encode()
return u.String()

But this would return url with the params being switched around.

https://example.com:8080?authenticity_token=FILTERED&private-token=FILTERED
  • 写回答

1条回答 默认 最新

  • douba8758 2018-10-24 09:29
    关注

    First, the order of the params should not be of any importance.

    But I can see some situation where this rule does not apply (eg when you hash an URL). In this case, you should normalize the URL before using it.

    Finally to respond to your question, you cannot keep the order if using Query, as Values is a map, and map don't bother with ordering. You should thus work on the query using u.RawQuery.

    u, err := url.Parse(originalURL)
    if err != nil {
        panic(err)
    }
    newQuery := ""
    for i, queryPart := range strings.Split(u.RawQuery, ";") {
        // you now have a slice of string ["private-token=foo", "authenticity_token=bar"]
        splitParam :=  strings.Split(queryPart, "=")
        if toMask(splitParam[0]) {
            splitParam[1] = "FILTERED"
        }
        if i != 0 {
            newQuery = newQuery + ";"
        }
        newQuery = splitParam[0] + "=" + splitParam[1]
    }
    u.RawQuery = newQuery
    return u.String()
    

    This code is just example. You have to better check for special cases or errors. You can also use regexp if you want to.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端