diff --git a/Cargo.lock b/Cargo.lock index 683053a9..36731c68 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 867f2ad3..e5049de5 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="2b5779e20250d357aa04780fdfcfdf338b24263a" } 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..1f27946c 100644 --- a/impit/src/fingerprint/database.rs +++ b/impit/src/fingerprint/database.rs @@ -117,4 +117,70 @@ 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. + // The unmeasured ios_18 and okhttp keep what they sent before: `5:16384` and `6:16384`, + // hyper's defaults. + #[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 unchanged = (Some(16_384), Some(16_384), "", ""); + let profiles = [ + ("chrome_100", chrome_100::fingerprint(), chrome), + ("chrome_101", chrome_101::fingerprint(), chrome), + ("chrome_104", chrome_104::fingerprint(), chrome), + ("chrome_107", chrome_107::fingerprint(), chrome), + ("chrome_110", chrome_110::fingerprint(), chrome), + ("chrome_116", chrome_116::fingerprint(), chrome), + ("chrome_124", chrome_124::fingerprint(), chrome), + ("chrome_125", chrome_125::fingerprint(), chrome), + ("chrome_131", chrome_131::fingerprint(), chrome), + ("chrome_133", chrome_133::fingerprint(), chrome), + ("chrome_136", chrome_136::fingerprint(), chrome), + ("chrome_142", chrome_142::fingerprint(), chrome), + ("chrome_151", chrome_151::fingerprint(), chrome), + ("chrome_155", chrome_155::fingerprint(), chrome), + ("chrome_156", chrome_156::fingerprint(), chrome), + ("chrome_157", chrome_157::fingerprint(), chrome), + ("firefox_128", firefox_128::fingerprint(), firefox128), + ("firefox_133", firefox_133::fingerprint(), firefox), + ("firefox_135", firefox_135::fingerprint(), firefox), + ("firefox_144", firefox_144::fingerprint(), firefox), + ("firefox_158", firefox_158::fingerprint(), firefox), + ("okhttp3", okhttp3::fingerprint(), unchanged), + ("okhttp4", okhttp4::fingerprint(), unchanged), + ("okhttp5", okhttp5::fingerprint(), unchanged), + ("ios_18", ios_18::fingerprint(), unchanged), + ]; + + let mut wrong = Vec::new(); + for (name, fingerprint, settings) in profiles { + let (max_frame_size, max_header_list_size, omit, no_rfc7540) = settings; + 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..713417a0 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` keep the hyper defaults sent before. + 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..50294b6e 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` keep the hyper defaults sent before. + 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..47acf138 100644 --- a/impit/src/fingerprint/mod.rs +++ b/impit/src/fingerprint/mod.rs @@ -99,14 +99,76 @@ 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. For `max_frame_size` (SETTINGS id 5) and +/// `max_header_list_size` (id 6), `None` means the setting is not sent; for the other `Option` +/// fields, `None` keeps hyper's default. `send_enable_push: true` sends ENABLE_PUSH (id 2) as `0` +/// (hyper always disables push); `false` leaves it out, and the server may then push, but every +/// pushed stream is refused. `no_rfc7540_priorities: Some(b)` adds SETTINGS_NO_RFC7540_PRIORITIES +/// (id 9, RFC 9218) with the value `b as u32`. +/// +/// Leaving settings out and adding setting 9 need impit's h2 (apify/h2) with support for the +/// `IMPIT_H2_OMIT_SETTINGS` and `IMPIT_H2_NO_RFC7540_PRIORITIES` environment variables. impit sets +/// both whenever it builds a client with a fingerprint, and h2 reads them 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. So clients with different profiles in one process share the most +/// recently built one's left-out settings and setting 9. (Clients without a fingerprint don't +/// offer `h2` in ALPN and never use HTTP/2.) Only the SETTINGS frame changes; the limits h2 +/// enforces stay as configured. +/// +/// For custom fingerprints: a struct literal must set the newer fields `max_frame_size`, +/// `send_enable_push` and `no_rfc7540_priorities`, and `max_header_list_size: None` now leaves +/// setting 6 out instead of sending hyper's 16 KiB default. To send what hyper sends by default, +/// use `max_frame_size: Some(16_384)`, `max_header_list_size: Some(16_384)`, +/// `send_enable_push: true` and `no_rfc7540_priorities: None`. #[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`. + pub send_enable_push: bool, + /// SETTINGS_NO_RFC7540_PRIORITIES (id 9); `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(|&(left_out, _)| left_out) + .map(|(_, id)| 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 +537,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..b3709813 --- /dev/null +++ b/impit/tests/h2_settings_env.rs @@ -0,0 +1,41 @@ +//! 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}; +use std::sync::Mutex; + +const OMIT_SETTINGS: &str = "IMPIT_H2_OMIT_SETTINGS"; +const NO_RFC7540_PRIORITIES: &str = "IMPIT_H2_NO_RFC7540_PRIORITIES"; + +/// Serializes the tests in this file: they all set the same process-wide env vars. +static ENV_LOCK: Mutex<()> = Mutex::new(()); + +fn h2_settings_env() -> (String, String) { + ( + std::env::var(OMIT_SETTINGS).unwrap(), + std::env::var(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() { + let _guard = ENV_LOCK.lock().unwrap(); + + 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())); +}