Home Backend Development PHP Tutorial YII Framework框架教程之安全方案详解_php实例

YII Framework框架教程之安全方案详解_php实例

Jun 07, 2016 pm 05:08 PM
framework yii security plan

本文讲述了YII Framework框架的安全方案。分享给大家供大家参考,具体如下:

web应用的安全问题是很重要的,在“黑客”盛行的年代,你的网站可能明天都遭受着攻击,为了从某种程度上防止被攻击,YII提供了防止攻击的几种解决方案。当然这里讲的安全是片面的,但是值得一看。

官方提供的解决方案有:如下

1. 跨站脚本攻击的防范

跨站脚本攻击(简称 XSS),即web应用从用户收集用户数据。 攻击者常常向易受攻击的web应用注入JavaScript,VBScript,ActiveX,HTML或 Flash来迷惑访问者以收集访问者的信息。 举个例子,一个未经良好设计的论坛系统可能不经检查就显示用户所输入的内容。 攻击者可以在帖子内容中注入一段恶意的JavaScript代码。 这样,当其他访客在阅读这个帖子的时候,这些JavaScript代码就可以在访客的电脑上运行了。

一个防范XSS攻击的最重要的措施之一就是:在显示用户输入的内容之前进行内容检查。 比如,你可以对内容中的HTML进行转义处理。但是在某些情况下这种方法就不可取了,因为这种方法禁用了所有的HTML标签。

Yii集成了HTMLPurifier并且为开发者提供了一个很有用的组件CHtmlPurifier, 这个组件封装了HTMLPurifier类。它可以将通过有效的审查、安全和白名单功能来把所审核的内容中的所有的恶意代码清除掉,并且确保过滤之后的内容过滤符合标准。

CHtmlPurifier组件可以作为一个widget或者filter来使用。 当作为一个widget来使用的时候,CHtmlPurifier可以对在视图中显示的内容进行安全过滤。 以下是代码示例:

<&#63;php $this->beginWidget('CHtmlPurifier'); &#63;>
//...这里显示用户输入的内容...
<&#63;php $this->endWidget(); &#63;>

Copy after login

2. 跨站请求伪造攻击的防范

跨站请求伪造(简称CSRF)攻击,即攻击者在用户浏览器在访问恶意网站的时候,让用户的浏览器向一个受信任的网站发起攻击者指定的请求。 举个例子,一个恶意网站有一个图片,这个图片的src地址指向一个银行网站:http://bank.example/withdraw?transfer=10000&to=someone。 如果用户在登陆银行的网站之后访问了这个恶意网页,那么用户的浏览器会向银行网站发送一个指令,这个指令的内容可能是“向攻击者的帐号转账10000元”。 跨站攻击方式利用用户信任的某个特定网站,而CSRF攻击正相反,它利用用户在某个网站中的特定用户身份。

要防范CSRF攻击,必须谨记一条:GET请求只允许检索数据而不能修改服务器上的任何数据。 而POST请求应当含有一些可以被服务器识别的随机数值,用来保证表单数据的来源和运行结果发送的去向是相同的。

Yii实现了一个CSRF防范机制,用来帮助防范基于POST的攻击。 这个机制的核心就是在cookie中设定一个随机数据,然后把它同表单提交的POST数据中的相应值进行比较。

默认情况下,CSRF防范是禁用的。如果你要启用它,可以编辑应用配置 中的组件中的CHttpRequest部分。

代码示例:

return array(
  'components'=>array(
    'request'=>array(
      'enableCsrfValidation'=>true,
    ),
  ),
);

Copy after login

要显示一个表单,请使用CHtml::form而不要自己写HTML代码。因为CHtml::form可以自动地在表单中嵌入一个隐藏项,这个隐藏项储存着验证所需的随机数据,这些数据可在表单提交的时候发送到服务器进行验证。

3. Cookie攻击的防范

保护cookie免受攻击是非常重要的。因为session ID通常存储在Cookie中。 如果攻击者窃取到了一个有效的session ID,他就可以使用这个session ID对应的session信息。

这里有几条防范对策:

您可以使用SSL来产生一个安全通道,并且只通过HTTPS连接来传送验证cookie。这样攻击者是无法解密所传送的cookie的。

设置cookie的过期时间,对所有的cookie和seesion令牌也这样做。这样可以减少被攻击的机会。

防范跨站代码攻击,因为它可以在用户的浏览器触发任意代码,这些代码可能会泄露用户的cookie。

在cookie有变动的时候验证cookie的内容。

Yii实现了一个cookie验证机制,可以防止cookie被修改。启用之后可以对cookie的值进行HMAC检查。

Cookie验证在默认情况下是禁用的。如果你要启用它,可以编辑应用配置 中的组件中的CHttpRequest部分。

代码示例:

return array(
  'components'=>array(
    'request'=>array(
      'enableCookieValidation'=>true,
    ),
  ),
);

Copy after login

一定要使用经过Yii验证过的cookie数据。使用Yii内置的cookies组件来进行cookie操作,不要使用$_COOKIES。

// 检索一个名为$name的cookie值
$cookie=Yii::app()->request->cookies[$name];
$value=$cookie->value;
......
// 设置一个cookie
$cookie=new CHttpCookie($name,$value);
Yii::app()->request->cookies[$name]=$cookie;

Copy after login

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix May 05, 2023 pm 04:01 PM

.NET Framework 4 is required by developers and end users to run the latest versions of applications on Windows. However, while downloading and installing .NET Framework 4, many users complained that the installer stopped midway, displaying the following error message - " .NET Framework 4 has not been installed because Download failed with error code 0x800c0006 ". If you are also experiencing it while installing .NETFramework4 on your device then you are at the right place

How to identify Windows upgrade issues using SetupDiag on Windows 11/10 How to identify Windows upgrade issues using SetupDiag on Windows 11/10 Apr 17, 2023 am 10:07 AM

Whenever your Windows 11 or Windows 10 PC has an upgrade or update issue, you will usually see an error code indicating the actual reason behind the failure. However, sometimes confusion can arise when an upgrade or update fails without an error code being displayed. With handy error codes, you know exactly where the problem is so you can try to fix it. But since no error code appears, it becomes challenging to identify the issue and resolve it. This will take up a lot of your time to simply find out the reason behind the error. In this case, you can try using a dedicated tool called SetupDiag provided by Microsoft that helps you easily identify the real reason behind the error.

SCNotification has stopped working [5 steps to fix it] SCNotification has stopped working [5 steps to fix it] May 17, 2023 pm 09:35 PM

As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Apr 17, 2023 pm 02:25 PM

Microsoft Windows users who have installed Microsoft.NET version 4.5.2, 4.6, or 4.6.1 must install a newer version of the Microsoft Framework if they want Microsoft to support the framework through future product updates. According to Microsoft, all three frameworks will cease support on April 26, 2022. After the support date ends, the product will not receive "security fixes or technical support." Most home devices are kept up to date through Windows updates. These devices already have newer versions of frameworks installed, such as .NET Framework 4.8. Devices that are not updating automatically may

KB5012643 for Windows 11 breaks .NET Framework 3.5 apps KB5012643 for Windows 11 breaks .NET Framework 3.5 apps May 09, 2023 pm 01:07 PM

It's been a week since we talked about the new safe mode bug affecting users who installed KB5012643 for Windows 11. This pesky issue didn't appear on the list of known issues Microsoft posted on launch day, thus catching everyone by surprise. Well, just when you thought things couldn't get any worse, Microsoft drops another bomb for users who have installed this cumulative update. Windows 11 Build 22000.652 causes more problems So the tech company is warning Windows 11 users that they may experience problems launching and using some .NET Framework 3.5 applications. Sound familiar? But please don't be surprised

How to use PHP framework Yii to develop a highly available cloud backup system How to use PHP framework Yii to develop a highly available cloud backup system Jun 27, 2023 am 09:04 AM

With the continuous development of cloud computing technology, data backup has become something that every enterprise must do. In this context, it is particularly important to develop a highly available cloud backup system. The PHP framework Yii is a powerful framework that can help developers quickly build high-performance web applications. The following will introduce how to use the Yii framework to develop a highly available cloud backup system. Designing the database model In the Yii framework, the database model is a very important part. Because the data backup system requires a lot of tables and relationships

Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Yii2 vs Phalcon: Which framework is better for developing graphics rendering applications? Jun 19, 2023 am 08:09 AM

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

Data query in Yii framework: access data efficiently Data query in Yii framework: access data efficiently Jun 21, 2023 am 11:22 AM

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

See all articles