diff --git a/Cargo.lock b/Cargo.lock index a80a3a9a..1dc04056 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -941,8 +941,8 @@ dependencies = [ [[package]] name = "h2" -version = "0.4.7" -source = "git+https://github.com/apify/h2?rev=7f393a728a8db07cabb1b78d2094772b33943b9a#7f393a728a8db07cabb1b78d2094772b33943b9a" +version = "0.4.13" +source = "git+https://github.com/apify/h2?rev=7d1c54a5e3f9c5c6ae31ce534d75a6d7cd82e22f#7d1c54a5e3f9c5c6ae31ce534d75a6d7cd82e22f" dependencies = [ "atomic-waker", "bytes", diff --git a/Cargo.toml b/Cargo.toml index 4326c7b1..d3b03b2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ members = [ ] [patch.crates-io] -h2 = { git = "https://github.com/apify/h2", rev = "7f393a728a8db07cabb1b78d2094772b33943b9a" } +h2 = { git = "https://github.com/apify/h2", rev = "7d1c54a5e3f9c5c6ae31ce534d75a6d7cd82e22f" } rustls = { git = "https://github.com/apify/rustls", rev="800ae48131094ddcaf9469d0ba773130b65ece63" } tower-http = { git = "https://github.com/apify/tower-http", rev="4d96dbd81f86559c38cf76b8ce671089544a26e3" } hyper-util = { git = "https://github.com/apify/hyper-util", rev="401e6fb99bb6bd4956a0fafca39f4b7b37be8a0a" } diff --git a/impit/src/fingerprint/database.rs b/impit/src/fingerprint/database.rs index 1f644b5a..b19e3fb0 100644 --- a/impit/src/fingerprint/database.rs +++ b/impit/src/fingerprint/database.rs @@ -67,11 +67,9 @@ mod tests { extensions.into_iter().map(|(typ, _)| typ).collect() } - // TLS 1.3 forbids repeating an extension type, and BoringSSL-based servers (Google, Cloudflare) - // reject such a ClientHello with `decode_error` (#575). - #[test] - fn no_profile_sends_an_extension_type_twice() { - let profiles = [ + /// Every built-in profile, by name. + fn profiles() -> Vec<(&'static str, BrowserFingerprint)> { + vec![ ("chrome_100", chrome_100::fingerprint()), ("chrome_101", chrome_101::fingerprint()), ("chrome_104", chrome_104::fingerprint()), @@ -97,10 +95,15 @@ mod tests { ("okhttp4", okhttp4::fingerprint()), ("okhttp5", okhttp5::fingerprint()), ("ios_18", ios_18::fingerprint()), - ]; + ] + } + // TLS 1.3 forbids repeating an extension type, and BoringSSL-based servers (Google, Cloudflare) + // reject such a ClientHello with `decode_error` (#575). + #[test] + fn no_profile_sends_an_extension_type_twice() { let mut duplicates = Vec::new(); - for (name, fingerprint) in profiles { + for (name, fingerprint) in profiles() { let types = client_hello_extension_types(fingerprint); assert!( types.contains(&0x0000), @@ -117,4 +120,47 @@ mod tests { "repeated ClientHello extensions: {duplicates:?}" ); } + + // Chrome leaves out MAX_FRAME_SIZE (5), Firefox MAX_HEADER_LIST_SIZE (6), and Firefox 128 also + // ENABLE_PUSH (2) while adding setting 9 = 1. The values are measured on the real browsers + // (chrome_116 on Chrome for Testing 116), except chrome_100-110's: only their leaving out 5 + // comes from Chromium's source, and their `2:0` contradicts that source for 100, 101 and 104. + // ios_18 and okhttp aren't measured and send hyper's defaults, `5:16384` and `6:16384`. + #[test] + fn every_profile_has_the_expected_h2_settings() { + // (max_frame_size, max_header_list_size, + // IMPIT_H2_OMIT_SETTINGS, IMPIT_H2_NO_RFC7540_PRIORITIES) + let chrome = (None, Some(262_144), "5", ""); + let firefox = (Some(16_384), None, "6", ""); + let firefox128 = (Some(16_384), None, "2,6", "1"); + let hyper_defaults = (Some(16_384), Some(16_384), "", ""); + + let mut wrong = Vec::new(); + for (name, fingerprint) in profiles() { + let (max_frame_size, max_header_list_size, omit, no_rfc7540) = match name { + "firefox_128" => firefox128, + _ if name.starts_with("chrome_") => chrome, + _ if name.starts_with("firefox_") => firefox, + _ => hyper_defaults, + }; + let http2 = &fingerprint.http2; + let actual = ( + http2.max_frame_size, + http2.max_header_list_size, + http2.h2_settings_env(), + ); + let expected = ( + max_frame_size, + max_header_list_size, + [ + ("IMPIT_H2_OMIT_SETTINGS", omit.to_string()), + ("IMPIT_H2_NO_RFC7540_PRIORITIES", no_rfc7540.to_string()), + ], + ); + if actual != expected { + wrong.push(format!("{name}: got {actual:?}, expected {expected:?}")); + } + } + assert!(wrong.is_empty(), "wrong HTTP/2 SETTINGS: {wrong:#?}"); + } } diff --git a/impit/src/fingerprint/database/chrome.rs b/impit/src/fingerprint/database/chrome.rs index daf909e3..8442af11 100644 --- a/impit/src/fingerprint/database/chrome.rs +++ b/impit/src/fingerprint/database/chrome.rs @@ -320,6 +320,10 @@ pub mod chrome_151 { max_header_list_size: Some(262_144), header_table_size: Some(65_536), max_concurrent_streams: None, + // Chrome doesn't send MAX_FRAME_SIZE. + max_frame_size: None, + send_enable_push: true, + no_rfc7540_priorities: None, } } @@ -463,6 +467,10 @@ pub mod chrome_142 { max_header_list_size: Some(262_144), header_table_size: Some(65_536), max_concurrent_streams: None, + // Chrome doesn't send MAX_FRAME_SIZE. + max_frame_size: None, + send_enable_push: true, + no_rfc7540_priorities: None, } } @@ -606,6 +614,10 @@ pub mod chrome_136 { max_header_list_size: Some(262_144), header_table_size: Some(65_536), max_concurrent_streams: None, + // Chrome doesn't send MAX_FRAME_SIZE. + max_frame_size: None, + send_enable_push: true, + no_rfc7540_priorities: None, } } @@ -743,6 +755,10 @@ pub mod chrome_133 { max_header_list_size: Some(262_144), header_table_size: Some(65_536), max_concurrent_streams: None, + // Chrome doesn't send MAX_FRAME_SIZE. + max_frame_size: None, + send_enable_push: true, + no_rfc7540_priorities: None, } } @@ -880,6 +896,10 @@ pub mod chrome_124 { max_header_list_size: Some(262_144), header_table_size: Some(65_536), max_concurrent_streams: None, + // Chrome doesn't send MAX_FRAME_SIZE. + max_frame_size: None, + send_enable_push: true, + no_rfc7540_priorities: None, } } @@ -1020,6 +1040,10 @@ pub mod chrome_131 { max_header_list_size: Some(262_144), header_table_size: Some(65_536), max_concurrent_streams: None, + // Chrome doesn't send MAX_FRAME_SIZE. + max_frame_size: None, + send_enable_push: true, + no_rfc7540_priorities: None, } } @@ -1155,6 +1179,10 @@ pub mod chrome_100 { max_header_list_size: Some(262_144), header_table_size: Some(65_536), max_concurrent_streams: Some(1_000), + // Chrome 100-116 don't send MAX_FRAME_SIZE (Chromium's source; a capture of 116). + max_frame_size: None, + send_enable_push: true, + no_rfc7540_priorities: None, } } @@ -1458,6 +1486,10 @@ pub mod chrome_125 { max_header_list_size: Some(262_144), header_table_size: Some(65_536), max_concurrent_streams: None, + // Chrome doesn't send MAX_FRAME_SIZE. + max_frame_size: None, + send_enable_push: true, + no_rfc7540_priorities: None, } } diff --git a/impit/src/fingerprint/database/firefox.rs b/impit/src/fingerprint/database/firefox.rs index 2b150457..782aff15 100644 --- a/impit/src/fingerprint/database/firefox.rs +++ b/impit/src/fingerprint/database/firefox.rs @@ -123,9 +123,14 @@ pub mod firefox_128 { ], initial_stream_window_size: Some(131_072), initial_connection_window_size: Some(12_582_912), + // Firefox doesn't send MAX_HEADER_LIST_SIZE. max_header_list_size: None, header_table_size: Some(65_536), max_concurrent_streams: None, + max_frame_size: Some(16_384), + // Firefox 128 sends no ENABLE_PUSH, but SETTINGS_NO_RFC7540_PRIORITIES = 1. + send_enable_push: false, + no_rfc7540_priorities: Some(true), } } @@ -271,9 +276,13 @@ pub mod firefox_133 { ], initial_stream_window_size: Some(131_072), initial_connection_window_size: Some(12_582_912), + // Firefox doesn't send MAX_HEADER_LIST_SIZE. max_header_list_size: None, header_table_size: Some(65_536), max_concurrent_streams: None, + max_frame_size: Some(16_384), + send_enable_push: true, + no_rfc7540_priorities: None, } } @@ -424,9 +433,13 @@ pub mod firefox_135 { ], initial_stream_window_size: Some(131_072), initial_connection_window_size: Some(12_582_912), + // Firefox doesn't send MAX_HEADER_LIST_SIZE. max_header_list_size: None, header_table_size: Some(65_536), max_concurrent_streams: None, + max_frame_size: Some(16_384), + send_enable_push: true, + no_rfc7540_priorities: None, } } diff --git a/impit/src/fingerprint/database/okhttp.rs b/impit/src/fingerprint/database/okhttp.rs index 869aeff7..9c895bd4 100644 --- a/impit/src/fingerprint/database/okhttp.rs +++ b/impit/src/fingerprint/database/okhttp.rs @@ -36,9 +36,13 @@ fn shared_http2_fingerprint() -> Http2Fingerprint { initial_stream_window_size: Some(16_777_216), // Connection window is also increased to 16 MiB via WINDOW_UPDATE initial_connection_window_size: Some(16_777_216), - max_header_list_size: None, + // Not measured: this and `max_frame_size` are hyper's defaults. + max_header_list_size: Some(16_384), header_table_size: None, max_concurrent_streams: None, + max_frame_size: Some(16_384), + send_enable_push: true, + no_rfc7540_priorities: None, } } diff --git a/impit/src/fingerprint/database/safari.rs b/impit/src/fingerprint/database/safari.rs index f9b5e37b..ca06cfe7 100644 --- a/impit/src/fingerprint/database/safari.rs +++ b/impit/src/fingerprint/database/safari.rs @@ -132,9 +132,13 @@ pub mod ios_18 { initial_stream_window_size: Some(2_097_152), // 65_535 (h2 default) + 10_420_225 WINDOW_UPDATE = 10_485_760. initial_connection_window_size: Some(10_485_760), - max_header_list_size: None, + // Not measured: this and `max_frame_size` are hyper's defaults. + max_header_list_size: Some(16_384), header_table_size: None, max_concurrent_streams: Some(100), + max_frame_size: Some(16_384), + send_enable_push: true, + no_rfc7540_priorities: None, } } diff --git a/impit/src/fingerprint/mod.rs b/impit/src/fingerprint/mod.rs index e48f287b..bee21a77 100644 --- a/impit/src/fingerprint/mod.rs +++ b/impit/src/fingerprint/mod.rs @@ -99,14 +99,65 @@ impl TlsFingerprint { } } +/// HTTP/2 configuration of a browser fingerprint, mostly the client's first SETTINGS frame. +/// +/// `Some` values are passed to hyper's HTTP/2 builder; `None` keeps hyper's default unless the +/// field's doc says otherwise. hyper's own SETTINGS correspond to `max_frame_size: Some(16_384)`, +/// `max_header_list_size: Some(16_384)`, `send_enable_push: true` and +/// `no_rfc7540_priorities: None`. +/// +/// Leaving settings out and adding setting 9 go through the `IMPIT_H2_OMIT_SETTINGS` and +/// `IMPIT_H2_NO_RFC7540_PRIORITIES` env vars, which impit's h2 fork (apify/h2) reads when an HTTP/2 +/// connection starts. Like `pseudo_header_order` (`IMPIT_H2_PSEUDOHEADERS_ORDER`), they are +/// process-wide: the last client built with a fingerprint decides them for every new HTTP/2 +/// connection in the process. (Clients without a fingerprint don't offer `h2` in ALPN and never +/// use HTTP/2.) Only the SETTINGS frame changes; h2 still enforces the configured limits. #[derive(Clone, Debug)] pub struct Http2Fingerprint { pub pseudo_header_order: Vec, pub initial_stream_window_size: Option, pub initial_connection_window_size: Option, + /// SETTINGS_MAX_HEADER_LIST_SIZE (id 6); `None`: not sent. pub max_header_list_size: Option, pub header_table_size: Option, pub max_concurrent_streams: Option, + /// SETTINGS_MAX_FRAME_SIZE (id 5); `None`: not sent. `Some(v)` must be in + /// `16_384..=16_777_215` (RFC 9113 ยง6.5.2): h2 panics on other values when a connection + /// starts, not when the client is built. + pub max_frame_size: Option, + /// Whether SETTINGS_ENABLE_PUSH (id 2) is sent, always as `0`. Without it the server may push, + /// and every pushed stream is refused. + pub send_enable_push: bool, + /// SETTINGS_NO_RFC7540_PRIORITIES (id 9, RFC 9218); `None`: not sent. + pub no_rfc7540_priorities: Option, +} + +impl Http2Fingerprint { + /// The values of the env vars that make apify/h2 send these SETTINGS: the ids to leave out + /// (ascending, comma-separated) and setting 9's value. Empty strings change nothing. + pub(crate) fn h2_settings_env(&self) -> [(&'static str, String); 2] { + let omit = [ + (!self.send_enable_push, "2"), + (self.max_frame_size.is_none(), "5"), + (self.max_header_list_size.is_none(), "6"), + ] + .into_iter() + .filter_map(|(left_out, id)| left_out.then_some(id)) + .collect::>() + .join(","); + let no_rfc7540_priorities = match self.no_rfc7540_priorities { + Some(true) => "1", + Some(false) => "0", + None => "", + }; + [ + ("IMPIT_H2_OMIT_SETTINGS", omit), + ( + "IMPIT_H2_NO_RFC7540_PRIORITIES", + no_rfc7540_priorities.to_string(), + ), + ] + } } /// TLS extensions configuration. @@ -475,3 +526,51 @@ impl TlsFingerprint { ) } } + +#[cfg(test)] +mod tests { + use super::*; + + fn http2( + max_frame_size: Option, + max_header_list_size: Option, + send_enable_push: bool, + no_rfc7540_priorities: Option, + ) -> Http2Fingerprint { + Http2Fingerprint { + pseudo_header_order: vec![], + initial_stream_window_size: None, + initial_connection_window_size: None, + max_header_list_size, + header_table_size: None, + max_concurrent_streams: None, + max_frame_size, + send_enable_push, + no_rfc7540_priorities, + } + } + + #[test] + fn h2_settings_env_lists_left_out_ids_and_setting_9() { + let sent = Some(16_384); + let cases = [ + (http2(sent, sent, true, None), "", ""), + (http2(None, sent, true, None), "5", ""), + (http2(sent, None, true, None), "6", ""), + (http2(sent, sent, false, None), "2", ""), + (http2(None, None, false, None), "2,5,6", ""), + (http2(sent, None, false, Some(true)), "2,6", "1"), + (http2(sent, sent, true, Some(false)), "", "0"), + ]; + for (fingerprint, omit, no_rfc7540) in cases { + assert_eq!( + fingerprint.h2_settings_env(), + [ + ("IMPIT_H2_OMIT_SETTINGS", omit.to_string()), + ("IMPIT_H2_NO_RFC7540_PRIORITIES", no_rfc7540.to_string()), + ], + "{fingerprint:?}" + ); + } + } +} diff --git a/impit/src/impit.rs b/impit/src/impit.rs index 47c71154..2a37d479 100644 --- a/impit/src/impit.rs +++ b/impit/src/impit.rs @@ -296,6 +296,10 @@ impl Impit { if let Some(max) = fingerprint.http2.max_concurrent_streams { client = client.http2_max_concurrent_streams(max); } + + if let Some(size) = fingerprint.http2.max_frame_size { + client = client.http2_max_frame_size(size); + } } if config.max_http_version == Version::HTTP_3 { @@ -384,6 +388,13 @@ impl Impit { ); } + // Always set, even to "", so a profile doesn't inherit the previous one's values. + if let Some(ref fingerprint) = config.fingerprint { + for (name, value) in fingerprint.http2.h2_settings_env() { + std::env::set_var(name, value); + } + } + Ok(Impit { base_client, h3_client, diff --git a/impit/tests/h2_settings_env.rs b/impit/tests/h2_settings_env.rs new file mode 100644 index 00000000..58b48df9 --- /dev/null +++ b/impit/tests/h2_settings_env.rs @@ -0,0 +1,32 @@ +//! The HTTP/2 SETTINGS env vars are process-wide, so this test has its own test binary: the lib +//! tests build fingerprinted clients in parallel and would race with it. + +use impit::{cookie::Jar, fingerprint::database as fingerprints, impit::Impit}; + +fn h2_settings_env() -> (String, String) { + ( + std::env::var("IMPIT_H2_OMIT_SETTINGS").unwrap(), + std::env::var("IMPIT_H2_NO_RFC7540_PRIORITIES").unwrap(), + ) +} + +// A profile with nothing to leave out, built after one that leaves settings out, must not inherit +// them. +#[test] +fn later_profile_overrides_h2_settings_env() { + Impit::::builder() + .with_fingerprint(fingerprints::firefox_128::fingerprint()) + .build() + .unwrap(); + assert_eq!(h2_settings_env(), ("2,6".to_string(), "1".to_string())); + + // Without a fingerprint, impit leaves the vars alone, like IMPIT_H2_PSEUDOHEADERS_ORDER. + Impit::::builder().build().unwrap(); + assert_eq!(h2_settings_env(), ("2,6".to_string(), "1".to_string())); + + Impit::::builder() + .with_fingerprint(fingerprints::ios_18::fingerprint()) + .build() + .unwrap(); + assert_eq!(h2_settings_env(), (String::new(), String::new())); +}