Unverified Commit de3e9d4f authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Update reqwest, remove experimental system TLS feature (#88290)

I added this as an option in https://github.com/vercel/next.js/pull/81818 as a solution for users with corporate firewalls that MITM TLS traffic.

Reqwest 0.13.x now uses `rustls-platform-verifier` by default, which doesn't have the tradeoffs that `rustls-native-certs` had. We should now pick up and work with system certs by default, and we no longer depend on shipping our own blob of trusted PKI roots! (though wasmer still pulls this in...)  

This PR was generated with Opus + OpenCode, but there was a ton of manual iteration to get it working on CI.

### TLS Provider

`reqwest` now defaults to `aws-lc-rs` instead of `ring`. This causes a few problems:
- On Windows, this requires cmake and NASM. There's a prebuilt NASM blob we can use, but it still appears to need cmake. We could install this in CI, but I don't want to make Windows development any more complicated than it already is.
- On Linux, there are issues with it picking up the wrong glibc version in our CI: https://github.com/aws/aws-lc-rs/issues/673

We just use this for fetching Google Fonts, so it's not worth it: Fall back to using `ring` on these platforms.

### CI Testing

Manually triggered a build-and-release job so that it tries to build for the whole platform matrix: https://github.com/vercel/next.js/actions/runs/20936098900

### Manual Testing
  
Followed the test plan in https://github.com/vercel/next.js/pull/81818

![Screenshot 2026-01-08 at 12.27.11 PM.png](https://app.graphite.com/user-attachments/assets/79ff941b-a828-4804-8695-b2400cd3680b.png)
parent 2b314e74
This diff is collapsed.
......@@ -176,9 +176,6 @@ opt-level = 3
[profile.release.package.turbopack-wasm]
opt-level = 3
[profile.release.package.rustls]
opt-level = "s"
[profile.release.package.turbopack-ecmascript-plugins]
opt-level = 3
......@@ -441,7 +438,7 @@ rand = "0.9.0"
rayon = "1.10.0"
regex = "1.10.6"
regress = "0.10.4"
reqwest = { version = "0.12.24", default-features = false }
reqwest = { version = "0.13.1", default-features = false }
ringmap = "0.1.3"
roaring = "0.10.10"
rstest = "0.16.0"
......
......@@ -10,7 +10,7 @@ use turbo_tasks::{
FxIndexMap, NonLocalValue, OperationValue, ResolvedVc, TaskInput, Vc, debug::ValueDebugFormat,
trace::TraceRawVcs,
};
use turbo_tasks_env::{EnvMap, ProcessEnv};
use turbo_tasks_env::EnvMap;
use turbo_tasks_fetch::FetchClientConfig;
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::{
......@@ -989,7 +989,6 @@ pub struct ExperimentalConfig {
turbopack_client_side_nested_async_chunking: Option<bool>,
turbopack_server_side_nested_async_chunking: Option<bool>,
turbopack_import_type_bytes: Option<bool>,
turbopack_use_system_tls_certs: Option<bool>,
/// Disable automatic configuration of the sass loader.
#[serde(default)]
turbopack_use_builtin_sass: Option<bool>,
......@@ -2008,28 +2007,8 @@ impl NextConfig {
}
#[turbo_tasks::function]
pub async fn fetch_client(
&self,
env: Vc<Box<dyn ProcessEnv>>,
) -> Result<Vc<FetchClientConfig>> {
// Support both an env var and the experimental flag to provide more flexibility to
// developers on locked down systems, depending on if they want to configure this on a
// per-system or per-project basis.
let use_system_tls_certs = env
.read(rcstr!("NEXT_TURBOPACK_EXPERIMENTAL_USE_SYSTEM_TLS_CERTS"))
.await?
.as_ref()
.and_then(|env_value| {
// treat empty value same as an unset value
(!env_value.is_empty()).then(|| env_value == "1" || env_value == "true")
})
.or(self.experimental.turbopack_use_system_tls_certs)
.unwrap_or(false);
Ok(FetchClientConfig {
tls_built_in_webpki_certs: !use_system_tls_certs,
tls_built_in_native_certs: use_system_tls_certs,
}
.cell())
pub fn fetch_client(&self) -> Vc<FetchClientConfig> {
FetchClientConfig::default().cell()
}
}
......
......@@ -1109,7 +1109,7 @@ async fn insert_next_shared_aliases(
next_font_google_replacer_mapping,
);
let fetch_client = next_config.fetch_client(execution_context.env());
let fetch_client = next_config.fetch_client();
import_map.insert_alias(
AliasPattern::exact(rcstr!(
"@vercel/turbopack-next/internal/font/google/cssmodule.module.css"
......
......@@ -308,7 +308,6 @@ export const experimentalSchema = {
turbopackClientSideNestedAsyncChunking: z.boolean().optional(),
turbopackServerSideNestedAsyncChunking: z.boolean().optional(),
turbopackImportTypeBytes: z.boolean().optional(),
turbopackUseSystemTlsCerts: z.boolean().optional(),
turbopackUseBuiltinBabel: z.boolean().optional(),
turbopackUseBuiltinSass: z.boolean().optional(),
turbopackModuleIds: z.enum(['named', 'deterministic']).optional(),
......
......@@ -485,31 +485,6 @@ export interface ExperimentalConfig {
*/
turbopackInferModuleSideEffects?: boolean
/**
* Use the system-provided CA roots instead of bundled CA roots for external HTTPS requests
* made by Turbopack. Currently this is only used for fetching data from Google Fonts.
*
* This may be useful in cases where you or an employer are MITMing traffic.
*
* This option is experimental because:
* - This may cause small performance problems, as it uses [`rustls-native-certs`](
* https://github.com/rustls/rustls-native-certs).
* - In the future, this may become the default, and this option may be eliminated, once
* <https://github.com/seanmonstar/reqwest/issues/2159> is resolved.
*
* Users who need to configure this behavior system-wide can override the project
* configuration using the `NEXT_TURBOPACK_EXPERIMENTAL_USE_SYSTEM_TLS_CERTS=1` environment
* variable.
*
* This option is ignored on Windows on ARM, where the native TLS implementation is always
* used.
*
* If you need to set a proxy, Turbopack [respects the common `HTTP_PROXY` and `HTTPS_PROXY`
* environment variable convention](https://docs.rs/reqwest/latest/reqwest/#proxies). HTTP
* proxies are supported, SOCKS proxies are not currently supported.
*/
turbopackUseSystemTlsCerts?: boolean
/**
* Set this to `false` to disable the automatic configuration of the babel loader when a Babel
* configuration file is present. This option is enabled by default.
......
......@@ -11,26 +11,9 @@ bench = false
[lints]
workspace = true
# Enable specific tls features per-target.
#
# Be careful when selecting tls backend, including change default tls backend.
# If you changed, must verify with ALL build targets with next-swc to ensure
# it works. next-swc have various platforms, some doesn't support native (using openssl-sys)
# and some aren't buildable with rustls.
[target.'cfg(all(target_os = "windows", target_arch = "aarch64"))'.dependencies]
reqwest = { workspace = true, features = ["native-tls"] }
# If you modify this cfg, make sure you update `FetchClient::try_build_uncached_reqwest_client`,
# `has_rustls_cause`, and `errors_on_tls_connection`
[target.'cfg(not(any(all(target_os = "windows", target_arch = "aarch64"), target_arch="wasm32")))'.dependencies]
reqwest = { workspace = true, features = ["rustls-tls-webpki-roots", "rustls-tls-native-roots"] }
# we just need the `rustls::Error` type (no default features)
rustls = { version = "0.23.0", default-features = false }
[dependencies]
anyhow = { workspace = true }
quick_cache = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
tokio = { workspace = true }
turbo-rcstr = { workspace = true }
......@@ -38,9 +21,37 @@ turbo-tasks = { workspace = true }
turbo-tasks-fs = { workspace = true }
turbopack-core = { workspace = true }
# Enable specific tls features per-target.
#
# Be careful when selecting tls backend. If you must verify ALL build targets of
# next-swc to ensure it works. next-swc have various platforms, some don't
# support native-tls (using openssl-sys) and some aren't buildable with rustls.
#
# The TLS backend used must match with the construction of `request::Client` in
# `src/client.rs`.
# On macOS, use the default rustls configuration (uses aws-lc-rs).
[target.'cfg(target_os = "macos")'.dependencies]
reqwest = { workspace = true, features = ["rustls"] }
# On Linux (all architectures) and Windows x86-64, use ring for rustls.
# - Linux: aws-lc-rs has cross-compilation issues in our CI:
# https://github.com/aws/aws-lc-rs/issues/673
# - On Linux, linking with the native SSL library can be distro-dependent, so
# it's easier for us to statically link ring or aws-lc-rs.
# - Windows: aws-lc-sys requires NASM and CMake which we don't have installed in
# CI, and would complicate the development environment setup. We could
# possibly switch to always using `native-tls` on Windows in the future.
[target.'cfg(any(target_os = "linux", all(windows, not(target_arch = "aarch64"))))'.dependencies]
reqwest = { workspace = true, features = ["rustls-no-provider"] }
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
# On Windows ARM64, use native-tls because rustls with ring has build issues.
[target.'cfg(all(windows, target_arch = "aarch64"))'.dependencies]
reqwest = { workspace = true, features = ["native-tls"] }
[dev-dependencies]
mockito = { version = "1.7.0", default-features = false }
tokio = { workspace = true, features = ["full"] }
turbo-tasks-testing = { workspace = true }
turbo-tasks-backend = { workspace = true }
......@@ -19,31 +19,8 @@ static CLIENT_CACHE: LazyLock<Cache<ReadRef<FetchClientConfig>, reqwest::Client>
/// This is needed because [`reqwest::ClientBuilder`] does not implement the required traits. This
/// factory cannot be a closure because closures do not implement `Eq` or `Hash`.
#[turbo_tasks::value(shared)]
#[derive(Hash)]
pub struct FetchClientConfig {
/// Whether to load embedded webpki root certs with rustls. Default is true.
///
/// Ignored for:
/// - Windows on ARM, which uses `native-tls` instead of `rustls-tls`.
/// - Ignored for WASM targets, which use the runtime's TLS implementation.
pub tls_built_in_webpki_certs: bool,
/// Whether to load native root certs using the `rustls-native-certs` crate. This may make
/// reqwest client initialization slower, so it's not used by default.
///
/// Ignored for:
/// - Windows on ARM, which uses `native-tls` instead of `rustls-tls`.
/// - Ignored for WASM targets, which use the runtime's TLS implementation.
pub tls_built_in_native_certs: bool,
}
impl Default for FetchClientConfig {
fn default() -> Self {
Self {
tls_built_in_webpki_certs: true,
tls_built_in_native_certs: false,
}
}
}
#[derive(Hash, Default)]
pub struct FetchClientConfig {}
impl FetchClientConfig {
/// Returns a cached instance of `reqwest::Client` it exists, otherwise constructs a new one.
......@@ -68,20 +45,24 @@ impl FetchClientConfig {
}
fn try_build_uncached_reqwest_client(&self) -> reqwest::Result<reqwest::Client> {
let client_builder = reqwest::Client::builder();
// make sure this cfg matches the one in `Cargo.toml`!
#[cfg(not(any(
all(target_os = "windows", target_arch = "aarch64"),
target_arch = "wasm32"
)))]
let client_builder = client_builder
.use_rustls_tls()
.tls_built_in_root_certs(false)
.tls_built_in_webpki_certs(self.tls_built_in_webpki_certs)
.tls_built_in_native_certs(self.tls_built_in_native_certs);
client_builder.build()
#[allow(unused_mut)]
let mut builder = reqwest::Client::builder();
#[cfg(any(target_os = "linux", all(windows, not(target_arch = "aarch64"))))]
{
use std::sync::Once;
static ONCE: Once = Once::new();
ONCE.call_once(|| {
rustls::crypto::ring::default_provider()
.install_default()
.unwrap()
});
builder = builder.tls_backend_rustls();
}
#[cfg(all(windows, target_arch = "aarch64"))]
{
builder = builder.tls_backend_native();
}
builder.build()
}
}
......@@ -95,7 +76,6 @@ impl FetchClientConfig {
) -> Result<Vc<FetchResult>> {
let url_ref = &*url;
let this = self.await?;
let tls_built_in_native_certs = this.tls_built_in_native_certs;
let response_result: reqwest::Result<HttpResponse> = async move {
let reqwest_client = this.try_get_cached_reqwest_client()?;
......@@ -130,12 +110,9 @@ impl FetchClientConfig {
Err(err) => {
// the client failed to construct or the HTTP request failed
mark_session_dependent();
Ok(Vc::cell(Err(FetchError::from_reqwest_error(
&err,
&url,
tls_built_in_native_certs,
)
.resolved_cell())))
Ok(Vc::cell(Err(
FetchError::from_reqwest_error(&err, &url).resolved_cell()
)))
}
}
}
......
......@@ -7,10 +7,7 @@ use turbopack_core::issue::{Issue, IssueSeverity, IssueStage, OptionStyledString
#[derive(Debug)]
#[turbo_tasks::value(shared)]
pub enum FetchErrorKind {
Connect {
has_system_certs: bool,
has_rustls_cause: bool,
},
Connect,
Timeout,
Status(u16),
Other,
......@@ -23,51 +20,10 @@ pub struct FetchError {
pub detail: ResolvedVc<StyledString>,
}
/// Attempt to determine if there's a `rustls::Error` in the error's source chain.
///
/// This logic is fragile (e.g. depends that our copy of rustls and the version that reqwest uses
/// match exactly), but it's covered by unit tests. This seems slightly better than using `Display`
/// or `Debug` and inspecting the string.
fn has_rustls_cause(err: &reqwest::Error) -> bool {
// make sure this cfg matches the one in `Cargo.toml`!
#[cfg(not(any(
all(target_os = "windows", target_arch = "aarch64"),
target_arch = "wasm32"
)))]
{
let mut source = std::error::Error::source(err);
while let Some(err) = source {
if err.downcast_ref::<rustls::Error>().is_some() {
return true;
}
if let Some(err) = err.downcast_ref::<std::io::Error>() {
// `std::io::Error`'s `source` implementation returns the source of the wrapped
// error instead of the wrapped error itself, so we need to special-case this,
// otherwise we risk skipping over the rustls error.
source = err.get_ref().map(|e| e as &dyn std::error::Error);
} else {
source = std::error::Error::source(err);
}
}
return false;
};
// uses native-tls
#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
return false;
}
impl FetchError {
pub(crate) fn from_reqwest_error(
error: &reqwest::Error,
url: &str,
webpki_certs_only: bool,
) -> FetchError {
pub(crate) fn from_reqwest_error(error: &reqwest::Error, url: &str) -> FetchError {
let kind = if error.is_connect() {
FetchErrorKind::Connect {
has_system_certs: webpki_certs_only,
has_rustls_cause: has_rustls_cause(error),
}
FetchErrorKind::Connect
} else if error.is_timeout() {
FetchErrorKind::Timeout
} else if let Some(status) = error.status() {
......@@ -140,41 +96,12 @@ impl Issue for FetchIssue {
Ok(Vc::cell(Some(
match kind {
FetchErrorKind::Connect {
has_system_certs,
has_rustls_cause,
} => {
let base_message = StyledString::Line(vec![
StyledString::Text(rcstr!(
"There was an issue establishing a connection while requesting "
)),
StyledString::Code(url.clone()),
]);
if !*has_system_certs && *has_rustls_cause {
StyledString::Stack(vec![
base_message,
StyledString::Line(vec![
StyledString::Strong(rcstr!("Hint: ")),
StyledString::Text(rcstr!(
"It looks like this error was TLS-related. Try enabling \
system TLS certificates with "
)),
StyledString::Code(rcstr!(
"NEXT_TURBOPACK_EXPERIMENTAL_USE_SYSTEM_TLS_CERTS=1"
)),
StyledString::Text(rcstr!(" as an environment variable, or set ")),
StyledString::Code(rcstr!(
"experimental.turbopackUseSystemTlsCerts"
)),
StyledString::Text(rcstr!(" in your ")),
StyledString::Code(rcstr!("next.config.js")),
StyledString::Text(rcstr!(" file.")),
]),
])
} else {
base_message
}
}
FetchErrorKind::Connect => StyledString::Line(vec![
StyledString::Text(rcstr!(
"There was an issue establishing a connection while requesting "
)),
StyledString::Code(url.clone()),
]),
FetchErrorKind::Status(status) => StyledString::Line(vec![
StyledString::Text(rcstr!("Received response with status ")),
StyledString::Code(RcStr::from(status.to_string())),
......
......@@ -141,13 +141,7 @@ async fn errors_on_failed_connection() {
let err_vc = &*response_vc.await?.unwrap_err();
let err = err_vc.await?;
assert!(matches!(
*err.kind.await?,
FetchErrorKind::Connect {
has_rustls_cause: false,
..
}
));
assert_eq!(*err.kind.await?, FetchErrorKind::Connect);
assert_eq!(*err.url.await?, url);
let issue = err_vc.to_issue(IssueSeverity::Error, get_issue_context().owned().await?);
......@@ -205,63 +199,10 @@ async fn errors_on_404() {
.unwrap()
}
#[cfg(not(any(
all(target_os = "windows", target_arch = "aarch64"),
target_arch = "wasm32"
)))]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn errors_on_tls_connection() {
let _guard = GLOBAL_TEST_LOCK.lock().await;
run_once(&REGISTRATION, || async {
let mut server = mockito::Server::new_async().await;
let _resource_mock = server
.mock("GET", "/")
.with_body("responsebody")
.create_async()
.await;
// construct an HTTPS url, but mockito runs an HTTP server, so this should fail to connect
let url = RcStr::from(format!("https://{}", server.socket_address()));
let client_vc = FetchClientConfig {
tls_built_in_webpki_certs: true,
tls_built_in_native_certs: false,
}
.cell();
let response_vc = client_vc.fetch(url.clone(), None);
let err_vc = &*response_vc.await?.unwrap_err();
let err = err_vc.await?;
assert_eq!(
*err.kind.await?,
FetchErrorKind::Connect {
has_rustls_cause: true,
has_system_certs: false,
}
);
let issue = err_vc.to_issue(IssueSeverity::Error, get_issue_context().owned().await?);
let description = issue
.description()
.await?
.unwrap()
.await?
.to_unstyled_string();
assert!(description.contains("NEXT_TURBOPACK_EXPERIMENTAL_USE_SYSTEM_TLS_CERTS=1"));
assert!(description.contains("experimental.turbopackUseSystemTlsCerts"));
anyhow::Ok(())
})
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn client_cache() {
// a simple fetch that should always succeed
async fn simple_fetch(path: &str, client: FetchClientConfig) -> anyhow::Result<()> {
async fn simple_fetch(path: &str) -> anyhow::Result<()> {
let mut server = mockito::Server::new_async().await;
let _resource_mock = server
.mock("GET", &*format!("/{path}"))
......@@ -270,7 +211,7 @@ async fn client_cache() {
.await;
let url = RcStr::from(format!("{}/{}", server.url(), path));
let response = match &*client
let response = match &*FetchClientConfig::default()
.cell()
.fetch(url.clone(), /* user_agent */ None)
.await?
......@@ -293,41 +234,13 @@ async fn client_cache() {
__test_only_reqwest_client_cache_clear();
assert_eq!(__test_only_reqwest_client_cache_len(), 0);
simple_fetch(
"/foo",
FetchClientConfig {
tls_built_in_native_certs: false,
..Default::default()
},
)
.await
.unwrap();
simple_fetch("/foo").await.unwrap();
assert_eq!(__test_only_reqwest_client_cache_len(), 1);
// the client is reused if the config is the same (by equality)
simple_fetch(
"/bar",
FetchClientConfig {
tls_built_in_native_certs: false,
..Default::default()
},
)
.await
.unwrap();
simple_fetch("/bar").await.unwrap();
assert_eq!(__test_only_reqwest_client_cache_len(), 1);
// the client is recreated if the config is different
simple_fetch(
"/bar",
FetchClientConfig {
tls_built_in_native_certs: true,
..Default::default()
},
)
.await
.unwrap();
assert_eq!(__test_only_reqwest_client_cache_len(), 2);
Ok(())
})
.await
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment