css - 自定义 CSS 安全性

标签 css security

我正在网站上工作,用户可以创建自定义 CSS 样式表。我知道这总是存在危险,但是有什么方法可以使我的验证更加安全吗?我正在使用这个:

$customCSS = $_POST["submittedCustomCSS"]; //put user's submitted stylesheet into variable
$customCSS = htmlspecialchars($customCSS); //hopefully validate it?
file_put_contents("../custom.css", $customCSS); //save user's stylesheet

显示自定义CSS的页面支持PHP,通过<link rel="stylesheet" href="<?php echo $postID; ?>/custom.css">显示CSS
有什么办法可以让这个更安全吗?提前致谢! :)

最佳答案

htmlspecialchars($customCSS); //hopefully validate it?

不,这还不够。这可能会阻止 CSS 转义 </style>它嵌入其中的元素,但没有任何作用来阻止 CSS 设置页面上任意元素的样式,或加载自定义字体或滥用 CSS 的其他有问题的功能,这些功能的安全含义仍知之甚少。


如果自定义样式表可以应用于其作者无法访问的任何页面,那么您需要比这更加严格。有多种方法可以利用自定义样式表来窃取信用卡号码或 XSRF token 等不需要运行 JS 的数据。

例如,如果一个用户可以选择使用另一用户的自定义样式表,那么这可能会导致安全漏洞,并且您不应该要求用户能够读取和审查 CSS 文件才能安全地使用您网站的功能.

"Scriptless Attacks – Stealing the Pie Without Touching the Sill"解释了注入(inject) CSS 可能出现问题的一些方式:

We show that CSS markup, which is traditionally considered to be only used for decoration/display purposes, actually enables an attacker to perform malicious activities.

...

We introduce several novel attacks that we call scriptless attacks, as an attacker can obtain the credit card number by injecting markup to this page without relying on any kind of (JavaScript) code execution.

...

Neither of the discussed attacks depends on user interaction on the victim’s part, but uses a mix of benign HTML, CSS and Web Open Font Format (WOFF [23]) features combined with a HTTP-request-based side channel to measure and ex- filtrate almost arbitrary data displayed on the website.

关于css - 自定义 CSS 安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20307965/

相关文章:

Javascript实现防XSS转义功能

c - 格式字符串利用长度

android - iOS/Android 指纹认证服务器端

html - 是否可以使用 CDN 仅对 div 应用 Bootstrap ?

internet-explorer - CSS simple float left 在IE兼容模式下的问题

internet-explorer - IE7 中的字体系列 CSS3

mysql - 表内的 SQL 查询

css - 以不同方式定位六边形网格的每一行?

HTML 和 CSS 表格

javascript - 如何确保请求是从原始软件发送的?