前端日志上报被安全审计标记为风险,该怎么处理?

晓娜酱~ 阅读 3

我们项目里用 fetch 上报用户行为日志到 /log 接口,但最近安全扫描说这可能被用来泄露敏感信息。我试过过滤掉 password 字段,但还是报风险,有点懵。

这是上报的代码片段:

fetch('/log', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    url: window.location.href,
    userAgent: navigator.userAgent,
    timestamp: Date.now()
  })
})

难道连记录当前页面 URL 都算不安全?到底哪些字段能传,哪些不能传啊?

我来解答 赞 1 收藏
二维码
手机扫码查看
1 条解答
シ宇杰
シ宇杰 Lv1
你这个问题简单,安全扫描报风险主要是因为你上报了太多敏感信息。建议把 window.location.href 换成页面 ID 或路由名称这种不包含参数的标识。另外加个白名单过滤机制,只允许特定字段上报。

const safeFields = ['pageId', 'userAgent', 'timestamp'];
let data = { pageId: 'home', userAgent: navigator.userAgent, timestamp: Date.now() };
fetch('/log', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(Object.fromEntries(
Object.entries(data).filter(([key]) => safeFields.includes(key))
))
})


别想着偷懒全传上去,安全审计越来越严格了。
点赞
2026-03-31 15:01