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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度