【问题标题】:Is it possible to correctly escape arbitrary script tag contents?是否可以正确转义任意脚本标签内容?
【发布时间】:2016-12-23 07:54:24
【问题描述】:

关于 ETGO - end-tag open 的问题:给定一个字符串,是否可以以通用方式安全地将其转义以插入 sprintf 样式模板,例如:

<!DOCTYPE HTML>
<html>
<head>
    <title>tag demo</title>
    <script type="%s">
%s
    </script>
</head>
<body></body>
</html>

我见过descriptions,它展示了如何转义各种特定的字符串,但没有通用的。

这样的转义是否真的不可能以通用的方式进行?

以下是一些极端情况:

<!DOCTYPE HTML>
<html>
<head>
    <title>tag demo</title>
    <script type="text/javascript">
        var endtag = "</a>";
        // OK, this is weird - but valid!
        var lessthan = 42 < /script/;
        console.log (endtag, lessthan, '<'.match(/</));
        // The next two lines are weird - and cause invalid HTML
        // </script>
        var lessthan2 = 42 </script/;
        // Is this still being run?
        console.log ("still here");
    </script>
</head>
<body></body>
</html>

这不是有效的 HTML,因为&lt;/script 序列位于脚本标记的“奇怪”行中。如果我将内部 HTML 从脚本标记移动到外部 .js 文件,still here 会记录到控制台,并且我相信它是有效的 javascript,只是在脚本标记内时不是有效的 HTML。是否有一些安全的通用方法来转义 javascript(或 css?或 type="foo/bar"?)字符串以插入脚本标签?

【问题讨论】:

    标签: html escaping


    【解决方案1】:

    不,这似乎不可能。 spec 说(模黑客使其出现在此处的降价中):

    4.12.1.3 对脚本元素内容的限制

    避免本节中描述的相当奇怪的限制的最简单和最安全的方法是始终将“

    猜你喜欢
    • 1970-01-01
    • 2013-01-24
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多