xss: NGINX 中的原生跨站脚本支持
安装
您可以在任何基于 RHEL 的发行版中安装此模块,包括但不限于:
- RedHat Enterprise Linux 7、8、9 和 10
- CentOS 7、8、9
- AlmaLinux 8、9
- Rocky Linux 8、9
- Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-xss
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-xss
通过在 /etc/nginx/nginx.conf 的顶部添加以下内容来启用该模块:
load_module modules/ngx_http_xss_filter_module.so;
本文档描述了 nginx-module-xss v0.6,于 2022 年 12 月 26 日发布。
## accessing /foo?callback=process gives the response
## body "process(...);" (without quotes) where "..."
## is the original response body of the /foo location.
server {
location /foo {
# your content handler goes here...
xss_get on;
xss_callback_arg 'callback';
xss_input_types 'application/json'; # default
xss_output_type 'application/x-javascript'; # default
}
...
}
描述
此模块为 nginx 添加了跨站 AJAX 支持。目前仅支持跨站 GET,但未来将添加跨站 POST。
跨站 GET 目前实现为 JSONP(或“带填充的 JSON”)。有关更多详细信息,请参见 http://en.wikipedia.org/wiki/JSON#JSONP。
指令
xss_get
语法: xss_get on | off
默认值: xss_get off
上下文: http, server, location, if location
启用 GET 请求的 JSONP 支持。
xss_callback_arg
语法: xss_callback_arg <name>
默认值: none
上下文: http, http, location, if location
指定响应中使用的 JavaScript 回调函数名称。
例如,
location /foo {
xss_get on;
xss_callback_arg c;
...
}
那么
GET /foo?c=blah
返回
blah(...);
xss_override_status
语法: xss_override_status on | off
默认值: xss_check_status on
上下文: http, server, location, if location
指定在实际处理响应时是否将 30x、40x 和 50x 状态覆盖为 200。
xss_check_status
语法: xss_check_status on | off
默认值: xss_check_status on
上下文: http, server, location, if location
默认情况下,ngx_xss 仅处理状态码为 200 或 201 的响应。
xss_input_types
语法: xss_input_types [mime-type]...
默认值: xss_input_types application/json
上下文: http, server, location, if location
仅处理指定 MIME 类型的响应。
示例:
xss_input_types application/json text/plain;
限制
- ngx_xss 不会与 ngx_echo 的子请求接口一起工作,因为子请求的“延迟链”机制在 nginx 核心中施加了底层限制。标准的 ngx_addition 模块也属于此类别。然而,建议您使用 ngx_lua 作为内容处理程序来发出子请求 和 ngx_xss 来执行 JSONP,因为 ngx_lua 的 ngx.location.capture() 接口并未利用“延迟链”机制,从而避免了这一限制。我们在生产中采用这种方法,效果很好。
故障排除
使用“info”错误日志级别(或更低)以获取更多诊断信息,当出现问题时。
另见
GitHub
您可以在 nginx-module-xss 的 GitHub 仓库 中找到此模块的其他配置提示和文档。