我想将html类型字符串存储到数据库中,但要安全

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

我有我的反应鹅毛笔组件,它返回这样的东西
示例:


fsdafsd

fsdfsda

fsdafsad

这就是内容,我想将其保存到数据库中,但我不确定,我不认为我必须将其保存为字符串,因为有人可以将脚本嵌入到 itt 中,但它不安全,你能帮助我吗我该怎么做,

当我将其保存到数据库中时,我想以某种方式从数据库中获取相同的 html 字符串,以将其显示到网站中

我只是尝试使用库来删除这个html字符,但它只保存平面文本,我想从数据库中撤回它,因为它像html一样保存

html reactjs database security react-fullstack
1个回答
0
投票

您应该通过编码将 HTML 内容保存在数据库中,以防止安全漏洞或数据损坏。如果 HTML 保存为行,它将使您的应用程序容易受到 XSS 攻击。

在纯 JavaScript 中,您可以使用内置的

escape
函数进行编码,使用
unescape
进行解码

const encodedHtmlContent = escape(<your html content here>);
// save encodedHtmlContent in DB

// To display encoded html
const decodedHtmlContent = unescape(<your encoded html content here>);
//render this decodedHtmlContent. through your component or available Mrkdown libraries```

you can also create own encode / decode logic and use based on your requirement.


© www.soinside.com 2019 - 2024. All rights reserved.