Skip to content
Merged
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://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" }
Expand Down
60 changes: 53 additions & 7 deletions impit/src/fingerprint/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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),
Expand All @@ -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:#?}");
}
}
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` 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,
}
}

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` 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,
}
}

Expand Down
99 changes: 99 additions & 0 deletions impit/src/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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`. 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<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_map(|(left_out, id)| left_out.then_some(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 +526,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