文档
Welcome! 安装(Installation) 示例列表(List of examples) 依赖注入与服务定位器(Dependency Injection/Service Location) MVC 架构(The MVC Architecture) 使用控制器(Using Controllers) 使用模型(Working with Models) 模型元数据(Models Meta-Data) 事务管理(Model Transactions) Phalcon 查询语言(Phalcon Query Language (PHQL)) 缓存对象关系映射(Caching in the ORM) 对象文档映射 ODM (Object-Document Mapper) 使用视图(Using Views) 视图助手(View Helpers) 资源文件管理(Assets Management) Volt 模版引擎(Volt: Template Engine) MVC 应用(MVC Applications) 路由(Routing) 调度控制器(Dispatching Controllers) 微应用(Micro Applications) 使用命名空间(Working with Namespaces) 事件管理器(Events Manager) 请求环境 (Request Environment) 返回响应(Returning Responses) Cookie 管理(Cookies Management) 生成 URL 和 路径(Generating URLs and Paths) 闪存消息(Flashing Messages) 使用 Session 存储数据(Storing data in Session) 过滤与清理(Filtering and Sanitizing) 上下文编码(Contextual Escaping) 验证(Validation) 表单(Forms) 读取配置(Reading Configurations) 分页(Pagination) 使用缓存提高性能(Improving Performance with Cache) 安全(Security) 加密/解密( Encryption/Decryption ) 访问控制列表 ACL(Access Control Lists ACL) 多语言支持(Multi-lingual Support) 通用类加载器 ( Universal Class Loader ) 日志记录(Logging) 注释解析器(Annotations Parser) 命令行应用(Command Line Applications) 队列(Queueing) 数据库抽象层(Database Abstraction Layer) 国际化(Internationalization) 数据库迁移(Database Migrations) 调试应用程序(Debugging Applications) Phalcon 开发工具(Phalcon Developer Tools) 提高性能:下一步该做什么?(Increasing Performance: What's next?) 单元测试(Unit testing) 授权(License)
教程

发布于 2015-08-21 15:19:15 | 265 次阅读 | 评论: 0 | 来源: 网络整理

上下文编码(Contextual Escaping)

网站及其它B/S应用极易受到 XSS 攻击,尽管PHP提供了转义功能,在某些情况下依然不够安全。在Phalcon中 PhalconEscaper 提供了上下文转义功能,这个模块是由C语言实现的, 这在进行转义时可以有更好的性能。

Phalcon的上下文转义组件基于 OWASP 提供的`XSS (Cross Site Scripting) 预防作弊表`_

另外,这个组件依赖于 mbstring 扩展,以支持几乎所有的字符集。

下面的例子中展示了这个组件是如何工作的:

<?php

    // 带有额外的html标签的恶意的文档标题
    $maliciousTitle = '</title><script>alert(1)</script>';

    // 恶意的css类名
    $className      = ';`(';

    // 恶意的css字体名
    $fontName       = 'Verdana"</style>';

    // 恶意的Javascript文本
    $javascriptText = "';</script>Hello";

    // 创建转义实例对象
    $e              = new PhalconEscaper();

?>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title><?php echo $e->escapeHtml($maliciousTitle) ?></title>

    <style type="text/css">
    .<?php echo $e->escapeCss($className) ?> {
        font-family  : "<?php echo $e->escapeCss($fontName) ?>";
        color: red;
    }
    </style>

</head>

<body>

    <div class='<?php echo $e->escapeHtmlAttr($className) ?>'>hello</div>

    <script>var some = '<?php echo $e->escapeJs($javascriptText) ?>'</script>

</body>
</html>

结果如下:

../_images/escape.jpeg

Phalcon会根据文本所处的上下文进行转义。 恰当的上下文环境对防范XSS攻击来说是非常重要的。

HTML 编码(Escaping HTML)

最不安全的情形即是在html标签中插入非安全的数据。

<div class="comments"><!-- Escape untrusted data here! --></div>

我们可以使用escapeHtml方法对这些文本进行转义:

<div class="comments"><?php echo $e->escapeHtml('></div><h1>myattack</h1>'); ?></div>

结果如下:

<div class="comments">&gt;&lt;/div&gt;&lt;h1&gt;myattack&lt;/h1&gt;</div>

HTML 属性编码(Escaping HTML Attributes)

对html属性进行转义和对html内容进行转义略有不同。对html的属性进行转义是通过对所有的非字母和数字转义来实现的。类例的转义都会如此进行的,除了一些复杂的属性外如:href和url:

<table width="Escape untrusted data here!"><tr><td>Hello</td></tr></table>

我们这里使用escapeHtmlAttr方法对html属性进行转义:

<table width="<?php echo $e->escapeHtmlAttr('"><h1>Hello</table'); ?>"><tr><td>Hello</td></tr></table>

结果如下:

<table width="&#x22;&#x3e;&#x3c;h1&#x3e;Hello&#x3c;&#x2f;table"><tr><td>Hello</td></tr></table>

URL 编码(Escaping URLs)

一些html的属性如href或url需要使用特定的方法进行转义:

<a href="Escape untrusted data here!">Some link</a>

我们这里使用escapeUrl方法进行url的转义:

<a href="<?php echo $e->escapeUrl('"><script>alert(1)</script><a href="#'); ?>">Some link</a>

结果如下:

<a href="%22%3E%3Cscript%3Ealert%281%29%3C%2Fscript%3E%3Ca%20href%3D%22%23">Some link</a>

CSS 编码(Escaping CSS)

CSS标识/值也可以进行转义:

<a style="color: Escape untrusted data here">Some link</a>

这里我们使用escapeCss方法进行转义:

<a style="color: <?php echo $e->escapeCss('"><script>alert(1)</script><a href="#'); ?>">Some link</a>

结果:

<a style="color: 22 3e 3c script3e alert28 129 3c 2f script3e 3c a20 href3d 22 23 ">Some link</a>

Javascript 编码(Escaping Javascript)

插入Javascript代码的字符串也需要进行适当的转义:

<script>document.title = 'Escape untrusted data here'</script>

这里我们使用escapeJs进行转义:

<script>document.title = '<?php echo $e->escapejs("'; alert(100); var x='"); ?>'</script>
<script>document.title = 'x27; alert(100); var xx3dx27'</script>
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务