GR00T N1.5和GR00T N1.6
2026/6/18 0:19:40
Access-Control-Allow-Origin指定哪些源可以访问资源。// 允许任意域名跨域访问(生产环境不推荐) header("Access-Control-Allow-Origin: *"); // 或仅允许特定域名 // header("Access-Control-Allow-Origin: https://example.com"); // 允许的请求方法 header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); // 允许携带的请求头 header("Access-Control-Allow-Headers: Content-Type, Authorization"); // 处理预检请求 if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { http_response_code(200); exit; }上述代码应置于实际业务逻辑之前,确保响应头正确发送。对于复杂请求(如携带自定义头部或使用PUT方法),浏览器会先发送OPTIONS预检请求,服务器需正确响应方可继续。| 响应头 | 作用 |
|---|---|
| Access-Control-Allow-Origin | 指定允许访问的源 |
| Access-Control-Allow-Methods | 定义允许的HTTP方法 |
| Access-Control-Allow-Headers | 声明允许的请求头字段 |
Access-Control-Allow-Origin: https://example.com Access-Control-Allow-Methods: GET, POST Access-Control-Allow-Headers: Content-Type该配置表示仅允许 `https://example.com` 发起GET/POST请求,并支持自定义 `Content-Type` 头部。GET /data HTTP/1.1 Host: api.example.com Origin: https://site-a.com服务器响应包含 `Access-Control-Allow-Origin` 即可完成跨域访问。| 请求字段 | 说明 |
|---|---|
| Access-Control-Request-Method | 实际请求的HTTP方法 |
| Access-Control-Request-Headers | 实际请求中的自定义头部 |
// 允许的域名,* 表示任意域 header("Access-Control-Allow-Origin: *"); // 允许的HTTP方法 header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); // 允许携带的请求头 header("Access-Control-Allow-Headers: Content-Type, Authorization");上述代码通过header()函数发送原始HTTP响应头。其中Access-Control-Allow-Origin是必需字段,若需提高安全性,应指定具体域名而非使用通配符。Access-Control-Allow-Credentials: true:允许浏览器发送凭据Access-Control-Allow-Origin不能为通配符*,必须明确指定源credentials: 'include'app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', 'https://example.com'); res.header('Access-Control-Allow-Credentials', 'true'); res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization'); next(); });上述代码确保来自指定源的请求可携带认证信息。注意Origin必须精确匹配,避免安全风险。同时,客户端发起请求时也需显式包含凭据。No 'Access-Control-Allow-Origin' header present、Preflight request failed等。这些通常由后端未正确配置 CORS 策略引起,或前端发起携带凭据的请求时未设置credentials。Origin,响应是否返回正确的 CORS 头。检查预检请求(OPTIONS)的响应状态和允许的方法。fetch('https://api.example.com/data', { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', // 若需携带 cookie body: JSON.stringify({ id: 1 }) })该代码发起一个跨域 POST 请求。若目标域名未在服务器的Access-Control-Allow-Origin中列出,或未允许Content-Type头,将触发 CORS 错误。Access-Control-Allow-Origin明确匹配来源*Access-Control-Allow-Methods和Access-Control-Allow-Headers<script>标签跨域加载数据的古老技术。由于浏览器同源策略不阻止脚本资源的跨域加载,开发者通过动态创建<script>标签,将回调函数名作为参数传递给服务器,实现跨域数据获取。function handleResponse(data) { console.log('Received data:', data); } const script = document.createElement('script'); script.src = 'https://api.example.com/data?callback=handleResponse'; document.head.appendChild(script);上述代码动态插入一个<script>标签,请求目标URL包含回调函数名。服务器返回内容为:handleResponse({"name": "John", "age": 30});,浏览器执行该JS语句,从而调用本地定义的函数并传入数据。server { listen 80; server_name localhost; # 前端静态资源 location / { root /usr/share/nginx/html; try_files $uri $uri/ /index.html; } # 代理 API 请求到后端服务 location /api/ { proxy_pass http://backend:3000/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }上述配置中,所有以/api/开头的请求将被代理至后端服务(如运行在3000端口的服务),而前端仍由 Nginx 直接提供。由于请求目标与页面同源,浏览器不会触发跨域限制。<?php $target = 'https://api.example.com/data'; $context = stream_context_create([ 'http' => [ 'method' => $_SERVER['REQUEST_METHOD'], 'header' => "Content-Type: application/json\r\n", 'content' => file_get_contents('php://input') ] ]); $response = file_get_contents($target, false, $context); echo $response; ?>该代码通过stream_context_create构造HTTP请求上下文,将客户端请求转发至目标API,并返回响应。适用于跨域请求隔离和接口聚合场景。<img src="https://bank.com/transfer?to=attacker&amount=1000" width="0" height="0">该代码通过隐藏的图片标签发起GET请求,浏览器自动携带用户Cookie,完成未经授权的资金转移。关键参数to和amount由攻击者预设。GET /api/user HTTP/1.1 Host: api.example.com Referer: https://trusted-site.com/page上述请求中,服务器可校验Referer是否属于白名单域名,从而决定是否响应敏感数据。// 检查Referer是否在允许列表中 func isValidReferer(referer string) bool { allowed := []string{"https://example.com", "https://app.example.com"} for _, domain := range allowed { if strings.HasPrefix(referer, domain) { return true } } return false }该函数通过前缀匹配判断Referer是否来自合法域,防止未授权页面发起API调用。但需注意,Referer可被客户端篡改或缺失,因此应结合其他机制如CSRF Token增强安全性。// 生成32字节随机Token token, err := securecookie.GenerateRandomKey(32) if err != nil { return "", err } return base64.URLEncoding.EncodeToString(token), nil该代码生成Base64编码的随机字符串,确保不可预测性,防止被恶意猜测。// 生成CSRF Token session_start(); if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } $token = $_SESSION['csrf_token']; echo '<input type="hidden" name="csrf_token" value="' . $token . '">';表单提交时,服务器校验该Token是否匹配会话中存储的值,确保请求来自合法页面。package authz default allow = false allow { input.method == "GET" input.path == "/api/v1/data" input.subject.groups[_] == "finance-readers" input.device.compliant == true }| 阶段 | 动作 | 工具示例 |
|---|---|---|
| 检测 | 识别可疑登录行为 | ELK + 自定义规则 |
| 分析 | 关联IP信誉与用户历史 | Threat Intelligence API |
| 响应 | 自动封禁IP并通知SOC | SOAR 平台 |
图表:威胁建模演进路径
传统STRIDE → 基于场景的建模 → 实时AI驱动风险预测
趋势:从静态文档转向集成CI/CD管道的自动化检测