"Urlencoded payload size is bigger than allowed (default: 256kB)"
"Urlencoded payload size is bigger than allowed (default: 256kB)"
我有一个格式化 MySQL 查询的 Rust 小程序,但我发现它在更大的查询上失败,返回
Urlencoded payload size is bigger than allowed (default: 256kB)
我正在使用 actix web,它看起来像这样
use actix_web::{
web, App, HttpResponse, HttpServer, Result,
};
#[derive(Deserialize)]
pub struct MyParams {
q: String,
}
fn index(params: web::Form<MyParams>) -> Result<HttpResponse> {
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body("test"))
// .body(mysql_format2(¶ms.q[..])))
}
fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().service(
web::resource("/")
.route(web::post().to(index))
)
})
.bind("127.0.0.1:48627")?
.run()
}
PHP 调用它的脚本看起来像
$this->FormattedMySQL = str_repeat("make a big query ", 1024*256);
$query = http_build_query([
'q' => $this->FormattedMySQL,
]);
// if I change this to 16385 (+1)
// then it breaks and returns that error
$query = substr($query, 0, 16384);
if ($this->FormatMySQL && strlen($query) <= 262144) {
try {
$this->VerbosePrint('formatting');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MYSQL_FORMAT_ENDPOINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$this->FormattedMySQL = curl_exec($ch);
curl_close($ch);
} catch (Exception $_) { }
}
我错过了什么,或者为什么这似乎是针对 16kB 而不是 256kB 抛出此错误?
我看到也可以 set the Payload
config 但我不确定 how/where 是否可以在我现有的代码中应用它。
快速回答是还有第二个配置 (FormConfig
),这就是您遇到的问题。目前尚不清楚 returns 与 PayloadConfig
相同的错误类型(这是有原因的,我将在 "longer version" 中解释)
将您的 actix 服务器定义更改为此以将其更改为 256kb:
HttpServer::new(|| {
App::new().service(
web::resource("/")
.route(web::post()
.to(index)
)
.data(web::Form::<MyParams>::configure(|cfg| cfg.limit(256 * 1024)))
)
})
.bind("127.0.0.1:48627")?
.run()
您还需要导入 actix_web::FromRequest
,因为这是对象类型从请求更改为 urlencoded 形式的地方,也是 configure()
方法所在的地方。
现在,进行说明!
Actix 与其他框架非常相似,具有多层限制。你找到了一个,这是另一个。
您找到的那个可以防止因内存耗尽而导致的拒绝服务(即有人发送一个故意较大的有效负载来攻击您的服务器,因为它必须将正文存储在某个地方以进行处理)。它管理请求的 整个 负载。
你要击中的那个是对每个单独字段的小得多的限制,它的存在是为了防止一种不同类型的力竭攻击。假设您的攻击者知道您正在用某些东西解析输入;让我们假设它是 JSON。通过发送任意大且复杂的 JSON,他们可以在处理单个请求时有效地锁定您的服务器。小数据输入,非常大后果。
因此,这两个限制通常相互独立,您可以根据自己的要求微调限制。很多小田?没问题。一大块?也不是问题。
FormConfig
限制的作用位于here in case you want to see the code itself. As the type return is the same as that of the PayloadConfig
limit ( (the Overflow
variant of this struct)[https://docs.rs/actix-web/1.0.7/actix_web/error/enum.UrlencodedError.html]), 消息不明确,结果你摸不着头脑
错误的主要目的是"similar",我认为,是为了防止服务器向潜在的攻击者指示他们达到了哪个限制,it seems to be what they had in mind。面向用户的错误描述是问题所在,这可能会通过适当的 PR 进行调整。
我有一个格式化 MySQL 查询的 Rust 小程序,但我发现它在更大的查询上失败,返回
Urlencoded payload size is bigger than allowed (default: 256kB)
我正在使用 actix web,它看起来像这样
use actix_web::{
web, App, HttpResponse, HttpServer, Result,
};
#[derive(Deserialize)]
pub struct MyParams {
q: String,
}
fn index(params: web::Form<MyParams>) -> Result<HttpResponse> {
Ok(HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body("test"))
// .body(mysql_format2(¶ms.q[..])))
}
fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new().service(
web::resource("/")
.route(web::post().to(index))
)
})
.bind("127.0.0.1:48627")?
.run()
}
PHP 调用它的脚本看起来像
$this->FormattedMySQL = str_repeat("make a big query ", 1024*256);
$query = http_build_query([
'q' => $this->FormattedMySQL,
]);
// if I change this to 16385 (+1)
// then it breaks and returns that error
$query = substr($query, 0, 16384);
if ($this->FormatMySQL && strlen($query) <= 262144) {
try {
$this->VerbosePrint('formatting');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MYSQL_FORMAT_ENDPOINT);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$this->FormattedMySQL = curl_exec($ch);
curl_close($ch);
} catch (Exception $_) { }
}
我错过了什么,或者为什么这似乎是针对 16kB 而不是 256kB 抛出此错误?
我看到也可以 set the Payload
config 但我不确定 how/where 是否可以在我现有的代码中应用它。
快速回答是还有第二个配置 (FormConfig
),这就是您遇到的问题。目前尚不清楚 returns 与 PayloadConfig
相同的错误类型(这是有原因的,我将在 "longer version" 中解释)
将您的 actix 服务器定义更改为此以将其更改为 256kb:
HttpServer::new(|| {
App::new().service(
web::resource("/")
.route(web::post()
.to(index)
)
.data(web::Form::<MyParams>::configure(|cfg| cfg.limit(256 * 1024)))
)
})
.bind("127.0.0.1:48627")?
.run()
您还需要导入 actix_web::FromRequest
,因为这是对象类型从请求更改为 urlencoded 形式的地方,也是 configure()
方法所在的地方。
现在,进行说明!
Actix 与其他框架非常相似,具有多层限制。你找到了一个,这是另一个。
您找到的那个可以防止因内存耗尽而导致的拒绝服务(即有人发送一个故意较大的有效负载来攻击您的服务器,因为它必须将正文存储在某个地方以进行处理)。它管理请求的 整个 负载。
你要击中的那个是对每个单独字段的小得多的限制,它的存在是为了防止一种不同类型的力竭攻击。假设您的攻击者知道您正在用某些东西解析输入;让我们假设它是 JSON。通过发送任意大且复杂的 JSON,他们可以在处理单个请求时有效地锁定您的服务器。小数据输入,非常大后果。
因此,这两个限制通常相互独立,您可以根据自己的要求微调限制。很多小田?没问题。一大块?也不是问题。
FormConfig
限制的作用位于here in case you want to see the code itself. As the type return is the same as that of the PayloadConfig
limit ( (the Overflow
variant of this struct)[https://docs.rs/actix-web/1.0.7/actix_web/error/enum.UrlencodedError.html]), 消息不明确,结果你摸不着头脑
错误的主要目的是"similar",我认为,是为了防止服务器向潜在的攻击者指示他们达到了哪个限制,it seems to be what they had in mind。面向用户的错误描述是问题所在,这可能会通过适当的 PR 进行调整。