fix: avoid sending HEAD bodies over TLS HTTP/2 (#2648)

Signed-off-by: 唐小鸭 <tangtang1251@qq.com>
Signed-off-by: houseme <housemecn@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: houseme <housemecn@gmail.com>
Co-authored-by: loverustfs <hello@rustfs.com>
This commit is contained in:
唐小鸭
2026-04-25 09:33:08 +08:00
committed by GitHub
parent 7215761784
commit d949d4e794
10 changed files with 701 additions and 2 deletions
+1
View File
@@ -73,3 +73,4 @@ rcgen.workspace = true
anyhow.workspace = true
rustls.workspace = true
zip.workspace = true
clap.workspace = true
+22
View File
@@ -0,0 +1,22 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use clap::Parser;
use e2e_test::tls_gen::{Args, run};
fn main() -> anyhow::Result<()> {
let out_dir = run(Args::parse())?;
println!("Generated RustFS TLS bundle in {}", out_dir.display());
Ok(())
}
+2
View File
@@ -125,3 +125,5 @@ mod replication_extension_test;
#[cfg(test)]
mod snowball_auto_extract_test;
pub mod tls_gen;
@@ -0,0 +1,201 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Regression test for TLS/HTTP2 `HEAD` responses on missing objects.
//!
//! Before the fix, RustFS returned `404` for a missing object but still wrote
//! the XML error payload on a `HEAD` request. Under HTTP/2 this emitted DATA
//! frames after the response headers, which clients surfaced as a protocol
//! error. This test keeps the request at the raw HTTPS layer so it can validate
//! the final wire-facing behavior rather than SDK-level error mapping.
#![cfg(test)]
use crate::common::{RustFSTestEnvironment, init_logging, rustfs_binary_path};
use http::Version;
use http::header::HOST;
use rcgen::generate_simple_self_signed;
use reqwest::{Certificate, Client, Response, StatusCode};
use rustfs_signer::constants::UNSIGNED_PAYLOAD;
use rustfs_signer::sign_v4;
use s3s::Body;
use serial_test::serial;
use std::error::Error;
use std::path::Path;
use std::process::Command;
use tokio::fs;
use tokio::time::{Duration, sleep};
use tracing::info;
const ACCESS_KEY: &str = "rustfsadmin";
const SECRET_KEY: &str = "rustfsadmin";
const BUCKET: &str = "test-head-tls-bodyless-bucket";
async fn generate_tls_bundle(tls_dir: &Path) -> Result<Vec<u8>, Box<dyn Error + Send + Sync>> {
fs::create_dir_all(tls_dir).await?;
let cert = generate_simple_self_signed(vec!["localhost".to_string(), "127.0.0.1".to_string()])?;
let cert_pem = cert.cert.pem();
let key_pem = cert.signing_key.serialize_pem();
fs::write(tls_dir.join("rustfs_cert.pem"), cert_pem.as_bytes()).await?;
fs::write(tls_dir.join("rustfs_key.pem"), key_pem.as_bytes()).await?;
Ok(cert_pem.into_bytes())
}
fn local_https_h2_client(ca_pem: &[u8]) -> Result<Client, Box<dyn Error + Send + Sync>> {
let _ca_cert = Certificate::from_pem(ca_pem)?;
Ok(Client::builder()
.no_proxy()
.no_gzip()
.no_brotli()
.no_zstd()
.no_deflate()
.danger_accept_invalid_certs(true)
.build()?)
}
async fn signed_empty_request(
client: &Client,
method: http::Method,
url: &str,
) -> Result<Response, Box<dyn Error + Send + Sync>> {
let uri = url.parse::<http::Uri>()?;
let authority = uri.authority().ok_or("request URL missing authority")?.to_string();
let request = http::Request::builder()
.method(method.as_str())
.uri(uri)
.header(HOST, authority)
.header("x-amz-content-sha256", UNSIGNED_PAYLOAD)
.body(Body::empty())?;
let signed = sign_v4(request, 0, ACCESS_KEY, SECRET_KEY, "", "us-east-1");
let reqwest_method = reqwest::Method::from_bytes(method.as_str().as_bytes())?;
let mut builder = client.request(reqwest_method, url);
for (name, value) in signed.headers() {
builder = builder.header(name, value);
}
Ok(builder.send().await?)
}
async fn ensure_bucket_exists(client: &Client, endpoint: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
let bucket_url = format!("{endpoint}/{BUCKET}/");
let response = signed_empty_request(client, http::Method::HEAD, &bucket_url).await?;
if response.status() == StatusCode::OK {
return Ok(());
}
let response = signed_empty_request(client, http::Method::PUT, &bucket_url).await?;
match response.status() {
StatusCode::OK => Ok(()),
StatusCode::CONFLICT => Ok(()),
status => Err(format!("unexpected bucket setup status: {status}").into()),
}
}
async fn wait_for_tls_server_ready(client: &Client, endpoint: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
let ready_url = format!("{endpoint}/");
for _attempt in 0..60 {
match signed_empty_request(client, http::Method::GET, &ready_url).await {
Ok(response) if response.status().is_success() => return Ok(()),
Ok(_) | Err(_) => sleep(Duration::from_millis(500)).await,
}
}
Err("RustFS TLS server failed to become ready within 30 seconds".into())
}
async fn start_tls_rustfs_server(env: &mut RustFSTestEnvironment, tls_dir: &Path) -> Result<(), Box<dyn Error + Send + Sync>> {
let binary_path = rustfs_binary_path();
let mut command = Command::new(&binary_path);
command
.env("RUST_LOG", "rustfs=info,rustfs_notify=debug")
.env("RUSTFS_TLS_PATH", tls_dir)
.current_dir(&env.temp_dir);
for key in [
"RUSTFS_ADDRESS",
"RUSTFS_VOLUMES",
"RUSTFS_ACCESS_KEY",
"RUSTFS_SECRET_KEY",
"RUSTFS_TLS_PATH",
"RUSTFS_OBS_LOG_DIRECTORY",
] {
command.env_remove(key);
}
let process = command
.env("RUSTFS_TLS_PATH", tls_dir)
.env("RUSTFS_CONSOLE_ENABLE", "false")
.args([
"--address",
&env.address,
"--access-key",
&env.access_key,
"--secret-key",
&env.secret_key,
&env.temp_dir,
])
.spawn()?;
env.process = Some(process);
Ok(())
}
#[tokio::test]
#[serial]
async fn test_head_missing_object_over_tls_http2_is_bodyless() -> Result<(), Box<dyn Error + Send + Sync>> {
init_logging();
let mut env = RustFSTestEnvironment::new().await?;
let tls_dir = std::path::PathBuf::from(&env.temp_dir).join("tls");
let ca_pem = generate_tls_bundle(&tls_dir).await?;
start_tls_rustfs_server(&mut env, &tls_dir).await?;
let endpoint = format!("https://{}", env.address);
let client = local_https_h2_client(&ca_pem)?;
wait_for_tls_server_ready(&client, &endpoint).await?;
ensure_bucket_exists(&client, &endpoint).await?;
let missing_key = "head-does-not-exist.txt";
let object_url = format!("{endpoint}/{BUCKET}/{missing_key}");
let get_response = signed_empty_request(&client, http::Method::GET, &object_url).await?;
assert_eq!(get_response.status(), StatusCode::NOT_FOUND);
let get_version = get_response.version();
let get_body = get_response.bytes().await?;
let get_body_text = String::from_utf8_lossy(&get_body);
assert!(
get_body_text.contains("<Code>NoSuchKey</Code>") || get_body_text.contains("<Code>NoSuchObject</Code>"),
"GET missing-object error body should expose NoSuchKey/NoSuchObject, got: {}",
get_body_text
);
info!("GET missing object over TLS used {:?} and returned {} bytes", get_version, get_body.len());
let head_response = signed_empty_request(&client, http::Method::HEAD, &object_url).await?;
assert_eq!(head_response.status(), StatusCode::NOT_FOUND);
assert_eq!(head_response.version(), Version::HTTP_2, "HEAD regression test must exercise HTTP/2");
let head_body = head_response.bytes().await?;
assert!(
head_body.is_empty(),
"HEAD missing-object response must not send body bytes over TLS/HTTP2, got {} bytes: {:?}",
head_body.len(),
head_body
);
Ok(())
}
+1
View File
@@ -17,6 +17,7 @@ mod get_deleted_object_test;
mod grpc_lock_client;
mod grpc_lock_server;
mod head_deleted_object_versioning_test;
mod head_tls_bodyless_test;
mod lifecycle;
mod lock;
mod node_interact_test;
+259
View File
@@ -0,0 +1,259 @@
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use anyhow::{Context, Result, bail};
use clap::Parser;
use rcgen::{
BasicConstraints, CertificateParams, CertifiedIssuer, DnType, ExtendedKeyUsagePurpose, IsCa, KeyPair, KeyUsagePurpose,
SanType,
};
use std::fs;
use std::net::IpAddr;
use std::path::{Path, PathBuf};
use time::{Duration, OffsetDateTime};
pub const DEFAULT_OUT_DIR: &str = "target/tls";
pub const OUTPUT_FILES: [&str; 7] = [
"rustfs_cert.pem",
"rustfs_key.pem",
"ca.crt",
"public.crt",
"client_ca.crt",
"client_cert.pem",
"client_key.pem",
];
#[derive(Debug, Parser)]
#[command(name = "tls_gen", about = "Generate a full RustFS TLS bundle for local TLS and mTLS tests.")]
pub struct Args {
#[arg(long, default_value = DEFAULT_OUT_DIR)]
pub out_dir: PathBuf,
#[arg(long, default_value_t = 365)]
pub days: i64,
#[arg(long)]
pub force: bool,
}
pub fn run(args: Args) -> Result<PathBuf> {
if args.days <= 0 {
bail!("--days must be a positive integer");
}
write_bundle(&args.out_dir, args.force, args.days)?;
Ok(args.out_dir)
}
pub fn ensure_writable(out_dir: &Path, force: bool) -> Result<()> {
if force {
return Ok(());
}
let existing: Vec<_> = OUTPUT_FILES
.iter()
.map(|name| out_dir.join(name))
.filter(|path| path.exists())
.collect();
if existing.is_empty() {
return Ok(());
}
let existing_list = existing
.iter()
.map(|path| path.file_name().and_then(|name| name.to_str()).unwrap_or("<unknown>"))
.collect::<Vec<_>>()
.join(", ");
bail!(
"Refusing to overwrite existing files in {}: {}. Re-run with --force to replace them.",
out_dir.display(),
existing_list
)
}
fn write_bundle(out_dir: &Path, force: bool, days: i64) -> Result<()> {
fs::create_dir_all(out_dir).with_context(|| format!("failed to create output directory {}", out_dir.display()))?;
ensure_writable(out_dir, force)?;
let ca_key = generate_private_key()?;
let ca = build_ca_certificate(ca_key, days)?;
let server_key = generate_private_key()?;
let server_cert = build_leaf_certificate(
&server_key,
"localhost",
&[SanType::DnsName("localhost".try_into()?)],
&[
SanType::IpAddress(IpAddr::V4("127.0.0.1".parse()?)),
SanType::IpAddress(IpAddr::V6("::1".parse()?)),
],
ExtendedKeyUsagePurpose::ServerAuth,
&ca,
days,
)?;
let client_key = generate_private_key()?;
let client_cert = build_leaf_certificate(
&client_key,
"rustfs-test-client",
&[SanType::DnsName("rustfs-test-client".try_into()?)],
&[],
ExtendedKeyUsagePurpose::ClientAuth,
&ca,
days,
)?;
let ca_pem = ca.pem();
let bundle = [
("rustfs_cert.pem", server_cert.pem()),
("rustfs_key.pem", server_key.serialize_pem()),
("ca.crt", ca_pem.clone()),
("public.crt", ca_pem.clone()),
("client_ca.crt", ca_pem),
("client_cert.pem", client_cert.pem()),
("client_key.pem", client_key.serialize_pem()),
];
for (name, content) in bundle {
fs::write(out_dir.join(name), content).with_context(|| format!("failed to write {}", out_dir.join(name).display()))?;
}
Ok(())
}
fn build_ca_certificate(signing_key: KeyPair, days: i64) -> Result<CertifiedIssuer<'static, KeyPair>> {
let mut params = base_params("RustFS Test CA", days)?;
params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
params.key_usages = vec![KeyUsagePurpose::KeyCertSign, KeyUsagePurpose::CrlSign];
CertifiedIssuer::self_signed(params, signing_key).context("failed to create CA certificate")
}
fn build_leaf_certificate(
signing_key: &KeyPair,
common_name: &str,
dns_names: &[SanType],
ip_addresses: &[SanType],
usage: ExtendedKeyUsagePurpose,
issuer: &CertifiedIssuer<'_, KeyPair>,
days: i64,
) -> Result<rcgen::Certificate> {
let mut params = base_params(common_name, days)?;
params.is_ca = IsCa::ExplicitNoCa;
params.key_usages = vec![KeyUsagePurpose::DigitalSignature, KeyUsagePurpose::KeyEncipherment];
params.extended_key_usages = vec![usage];
params.use_authority_key_identifier_extension = true;
params.subject_alt_names.extend_from_slice(dns_names);
params.subject_alt_names.extend_from_slice(ip_addresses);
params
.signed_by(signing_key, issuer)
.with_context(|| format!("failed to create leaf certificate for {common_name}"))
}
fn base_params(common_name: &str, days: i64) -> Result<CertificateParams> {
let mut params = CertificateParams::default();
let issued_at = OffsetDateTime::now_utc() - Duration::minutes(5);
params.not_before = issued_at;
params.not_after = issued_at + Duration::days(days);
params.distinguished_name.push(DnType::CountryName, "US");
params.distinguished_name.push(DnType::OrganizationName, "RustFS");
params.distinguished_name.push(DnType::CommonName, common_name);
Ok(params)
}
fn generate_private_key() -> Result<KeyPair> {
KeyPair::generate().context("failed to generate private key")
}
#[cfg(test)]
mod tests {
use super::{Args, OUTPUT_FILES, ensure_writable, run};
use std::fs;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
fn unique_temp_dir() -> PathBuf {
let suffix = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system time must be after unix epoch")
.as_nanos();
std::env::temp_dir().join(format!("rustfs-tls-gen-{suffix}"))
}
struct TempDir {
path: PathBuf,
}
impl TempDir {
fn new() -> Self {
let path = unique_temp_dir();
fs::create_dir_all(&path).expect("temporary directory should be created");
Self { path }
}
fn path(&self) -> &Path {
&self.path
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.path);
}
}
#[test]
fn run_writes_full_bundle() {
let temp_dir = TempDir::new();
let out_dir = run(Args {
out_dir: temp_dir.path().join("tls"),
days: 365,
force: false,
})
.expect("bundle generation should succeed");
for name in OUTPUT_FILES {
let content = fs::read(out_dir.join(name)).unwrap_or_else(|error| panic!("{name} should exist: {error}"));
assert!(!content.is_empty(), "{name} should not be empty");
}
}
#[test]
fn ensure_writable_rejects_existing_files_without_force() {
let temp_dir = TempDir::new();
let existing = temp_dir.path().join(OUTPUT_FILES[0]);
fs::write(&existing, "existing").expect("existing file should be created");
let error = ensure_writable(temp_dir.path(), false).expect_err("existing files must be rejected");
let message = format!("{error:#}");
assert!(message.contains("Refusing to overwrite existing files"));
assert!(message.contains(OUTPUT_FILES[0]));
}
#[test]
fn run_rejects_non_positive_days() {
let temp_dir = TempDir::new();
let error = run(Args {
out_dir: temp_dir.path().join("tls"),
days: 0,
force: false,
})
.expect_err("non-positive days must fail");
assert_eq!(format!("{error:#}"), "--days must be a positive integer");
}
}