在 ASP.NET MVC 中构建 ActionLink 时是否应该调用 Html.Encode

发布于 2024-08-21 22:15:54 字数 677 浏览 6 评论 0原文

如果从数据库中提取链接的名称,您是否应该调用 Html.Encode 方法来清理名称?

例如:

Html.ActionLink(Model.PersonFromDB.FirstName,
                "Action",
                "Controller",
                new RouteValueDictionary { { "id", Model.PersonFromDB.Id } },
                null)

或:

Html.ActionLink(Html.Encode(Model.PersonFromDB.FirstName),
                "Action",
                "Controller",
                new RouteValueDictionary { { "id", Model.PersonFromDB.Id } },
                null)

您希望执行此操作以确保 < 之间没有危险字符串注入到页面中,这是有意义的/code> 标签,但是脚本和此类在锚标签之间可执行吗?

If the name of a link is pulled from the database, should you be calling the Html.Encode method to clean the name?

For example:

Html.ActionLink(Model.PersonFromDB.FirstName,
                "Action",
                "Controller",
                new RouteValueDictionary { { "id", Model.PersonFromDB.Id } },
                null)

or:

Html.ActionLink(Html.Encode(Model.PersonFromDB.FirstName),
                "Action",
                "Controller",
                new RouteValueDictionary { { "id", Model.PersonFromDB.Id } },
                null)

It would make sense that you would want to do this to ensure that there are no dangerous strings injected into the page between <a> and </a> tags, but are scripts and such executable between anchor tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

む无字情书 2024-08-28 22:15:54

不,因为根据 SO 上的这个线程 HtmlAction.Link() 已经对值进行了 HTML 编码,因此您最终会执行两次。

No, since according to this thread on SO HtmlAction.Link() already HTML encodes values, so you'd end up doing it twice.

七分※倦醒 2024-08-28 22:15:54

这当然是个好主意,但您可能应该阻止用户输入潜在的恶意字符串作为他们的名字。

It's certainly a good idea, but you should probably be preventing users from entering in potentially malicious strings as their first name.

不语却知心 2024-08-28 22:15:54

是的,绝对是。作为一般规则,对于您要输出的任何最初从不受信任的来源获得的 HTML,假设该格式还不是 HTML(并且经过充分审查),您应该始终对字符串进行 HTML 编码,以防止注入攻击。

Yes, absolutely. As a general rule, for any HTML that you are going to output that was originally obtained from an untrusted source, assuming the format wasn't HTML already (and sufficiently vetted), you should always HTML encode the string to protect against injection attacks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文