gpt4 book ai didi

go - 在 HTML 中取消转义 css 输入

转载 作者:IT王子 更新时间:2023-10-29 01:39:42 25 4
gpt4 key购买 nike

如何对 html 进行转义?

我正在像这样将一个 css 文件传递​​到 html 中

<style>{{.file}}</style>

我明白了

<style>ZgotmplZ</style>

我试过用 template.HTML(data) 包装字段,但没有用。

最佳答案

Go HTML 模板包正确地避开了 CSS。引用自 documentation of the template package :

The escaping is contextual, so actions can appear within JavaScript, CSS, and URI contexts.

“ZgotmplZ” 是一个特殊值,如果您尝试包含的值在上下文中无效或不安全,它会用作替代值。

所以问题在于您尝试包含的 CSS 值,它不安全。先尝试一些简单的东西,看看它是否有效,例如:

body {background-color: #000}

在文档中找到关于 "ZgotmplZ" 的讨论(类型为 ErrorCode ),引用它:

“ZgotmplZ”解释:

示例模板:

<img src="{{.X}}">
where {{.X}} evaluates to `javascript:...`

讨论:

"ZgotmplZ" is a special value that indicates that unsafe content reached a
CSS or URL context at runtime. The output of the example will be
<img src="#ZgotmplZ">
If the data comes from a trusted source, use content types to exempt it
from filtering: URL(`javascript:...`).

解决方案

由于您尝试插入的代码是在 CSS 代码而不是 HTML 的上下文中,您不能/不应该使用 template.HTML(data)

有一个预定义类型 CSS用于安全包含来自可信来源的 CSS 代码,例如您指定的 CSS 代码不是来自用户填写的 HTML 表单。示例:

var safeCss = template.CSS(`body {background-image: url("paper.gif");}`)

并将 safeCss 值传递给您的模板参数。

关于go - 在 HTML 中取消转义 css 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27906812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com