让你的web应用更安全
- X-Frame-Options
- Cookie of secure and httpOnly
设置X-Frame-Options
https://developer.mozilla.org/en-US/docs/Web/HTTP/X-Frame-Options
X-Frame-Options 主要是为了防止点击劫持(clickjacking)点击劫持(clickjacking)是一种在网页中将恶意代码等隐藏在看似无害的内容(如按钮)之下,并诱使用户点击的手段。X-Frame-Options HTTP 头部字段用来指示传输的资源是否可以被包含在 或
DENY表明网页内容不可以被嵌入到任何frame中
SAMEORIGIN同源策略,声明网页内容可以被同一域下面的frame嵌入,但不能被不在同一域下面的frame嵌入.同源: 协议,域名和端口全部相同,即使是ip与域名对应,也认为是不同域下,下面说明了具体的情况,与: http://www.example.com/dir/page.html对比:
Compared URL | Outcome | Reason |
---|---|---|
http://www.example.com/dir/page2.html | 同源 | 协议主机端口相同 |
http://www.example.com/dir2/other.html | 同源 | 协议主机端口相同 |
http://username:password@www.example.com/dir2/other.html | 同源 | 协议主机端口相同 |
http://www.example.com:81/dir/other.html | 不同源 | 端口不同 |
https://www.example.com/dir/other.html | 不同源 | 协议不同 |
http://en.example.com/dir/other.html | 不同源 | 主机名不同 |
http://example.com/dir/other.html | 不同源 | 主机不同,必须完全匹配 |
http://v2.www.example.com/dir/other.html | 不同源 | 主机不同,必须完全匹配 |
http://www.example.com:80/dir/other.html | Depends | Port explicit. Depends on implementation in browser |
- ALLOW-FROM指定具体的来源
服务器配置
Apache
添加站点配置:
Header always append X-Frame-Options SAMEORIGIN
Nginx
添加 http , server 或者 location 配置
add_header X-Frame-Options SAMEORIGIN;
IIS
添加到站点的 Web.config
<system.webServer> ... <httpProtocol> <customHeaders> <add name="X-Frame-Options" value="SAMEORIGIN" /> </customHeaders> </httpProtocol> ...</system.webServer>
HAProxy
添加到 frontend, listen, 或者 backend 配置中:
rspadd X-Frame-Options:\ SAMEORIGIN
更安全的Cookie
Cookie 是浏览器储存在客户端的小的文本文件,用于客户端与服务器之间互传.Web服务器通过指定 http header 的 Set-Cookie, 其结构如下:
Set-Cookie: name=value[; expires=date][; domain=domain][; path=path][; secure][; httpOnly]
可以看到,Cookie主要包含一下几个字段:
- name
- value
- expire
- domain
- path
- secure
- httpOnly
这里主要讲secure 和 httpOnly
httpOnly标识
用来告诉浏览器不能通过JavaScript的 document.cookie 来访问 cookie, 目的是避免 跨站脚本攻击 (XSS)
secure标识
强制应用通过Https来传输 Cookie
PHP Yii2中设置Cookie的 httpOnly 和 secure
设置 _csrf
Yii2中的 Cookie yii\web\Cookie默认是 httpOnly的,
class Cookie extends \yii\base\Object{ public $name; public $value = ''; public $domain = ''; public $expire = 0; public $path = '/'; public $secure = false; public $httpOnly = true;}
使用 = Html::csrfMetaTags() ?> 就会生成一个 name=_csrf 的 Cookie, 默认是 httpOnly, 通过注入request来改变:
在 config/main.php 中
... 'components' => [ 'request' => [ 'csrfCookie' => [ 'httpOnly' => true, 'secure' => SECURE_COOKIE, ], ], ... ] ...
注入 csrfCookie 的 httpOnly 和 secure 属性值
生成secure的 Cookie
$cookies = Yii::$app->response->cookies;$cookies->add(new Cookie([ 'name' => 'accesstoken', 'value' => $accessToken, 'expire' => time() + Token::EXPIRE_TIME, 'secure' => SECURE_COOKIE ]));
注意更新 Cookie 的时候也需要更新 secure 属性
$cookies = Yii::$app->request->cookies;if (($cookie = $cookies->get('accesstoken')) !== null) { $cookie->secure = SECURE_COOKIE; // 这里很重要, 不然就会丢失 $cookie->expire = time() + Token::EXPIRE_TIME; Yii::$app->response->cookies->add($cookie);}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
