wreq 1.3.2.2 → 1.3.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +10 -10
  3. data/Cargo.toml +2 -2
  4. data/README.md +11 -4
  5. data/VERSION +1 -1
  6. data/crates/wreq-util/src/emulate/macros.rs +31 -197
  7. data/crates/wreq-util/src/emulate/profile/chromium/chrome.rs +731 -0
  8. data/crates/wreq-util/src/emulate/profile/chromium/edge.rs +418 -0
  9. data/crates/wreq-util/src/emulate/profile/chromium/header.rs +74 -0
  10. data/crates/wreq-util/src/emulate/profile/chromium/http2.rs +59 -0
  11. data/crates/wreq-util/src/emulate/profile/chromium/opera.rs +312 -0
  12. data/crates/wreq-util/src/emulate/profile/{chrome → chromium}/tls.rs +146 -137
  13. data/crates/wreq-util/src/emulate/profile/chromium.rs +9 -0
  14. data/crates/wreq-util/src/emulate/profile/firefox/header.rs +48 -54
  15. data/crates/wreq-util/src/emulate/profile/firefox/http2.rs +92 -102
  16. data/crates/wreq-util/src/emulate/profile/firefox/tls.rs +155 -146
  17. data/crates/wreq-util/src/emulate/profile/firefox.rs +312 -440
  18. data/crates/wreq-util/src/emulate/profile.rs +5 -7
  19. data/crates/wreq-util/src/emulate.rs +33 -1
  20. data/crates/wreq-util/tests/client.rs +187 -46
  21. data/crates/wreq-util/tests/emulate_chrome.rs +7 -0
  22. data/crates/wreq-util/tests/emulate_firefox.rs +15 -0
  23. data/crates/wreq-util/tests/emulate_opera.rs +24 -0
  24. data/lib/wreq_ruby/emulate.rb +27 -0
  25. data/src/emulate.rs +27 -0
  26. data/test/emulation_client_hello_test.rb +142 -0
  27. data/test/emulation_test.rb +21 -105
  28. metadata +9 -9
  29. data/crates/wreq-util/src/emulate/profile/chrome/header.rs +0 -116
  30. data/crates/wreq-util/src/emulate/profile/chrome/http2.rs +0 -65
  31. data/crates/wreq-util/src/emulate/profile/chrome.rs +0 -2304
  32. data/crates/wreq-util/src/emulate/profile/opera/header.rs +0 -29
  33. data/crates/wreq-util/src/emulate/profile/opera/http2.rs +0 -50
  34. data/crates/wreq-util/src/emulate/profile/opera/tls.rs +0 -103
  35. data/crates/wreq-util/src/emulate/profile/opera.rs +0 -299
@@ -1,21 +1,19 @@
1
1
  //! Emulation for different browsers.
2
2
 
3
- pub mod chrome;
3
+ pub(super) mod chromium;
4
4
  pub mod firefox;
5
5
  pub mod okhttp;
6
- pub mod opera;
7
6
  pub mod safari;
8
7
 
8
+ pub use chromium::{chrome, opera};
9
+
9
10
  use typed_builder::TypedBuilder;
10
11
  #[cfg(feature = "emulation-compression")]
11
12
  use wreq::header::ACCEPT_ENCODING;
12
13
  use wreq::{
13
14
  Group,
14
- header::{ACCEPT, ACCEPT_LANGUAGE, HeaderMap, HeaderName, HeaderValue, USER_AGENT},
15
- http2::{
16
- HeadersDependency, HeadersPriority, Http2Options, Priorities, Priority, PseudoId,
17
- PseudoOrder, SettingId, SettingsOrder, StreamDependency, StreamId,
18
- },
15
+ header::{ACCEPT, ACCEPT_LANGUAGE, HeaderMap, HeaderValue, USER_AGENT},
16
+ http2::{Http2Options, PseudoId, PseudoOrder, SettingId, SettingsOrder},
19
17
  tls::{
20
18
  AlpnProtocol, AlpsProtocol, EchGrease, EchGreaseAead, ExtensionType, KeyShare, Tls13Cipher,
21
19
  TlsOptions, TlsVersion, compress::CertificateCompressor,
@@ -3,7 +3,12 @@ mod macros;
3
3
  pub mod compress;
4
4
  pub mod profile;
5
5
 
6
- use profile::{chrome::*, firefox::*, okhttp::*, opera::*, safari::*};
6
+ use profile::{
7
+ chromium::{chrome::*, edge::*, opera::*},
8
+ firefox::*,
9
+ okhttp::*,
10
+ safari::*,
11
+ };
7
12
  #[cfg(feature = "emulation-serde")]
8
13
  use serde::{Deserialize, Serialize};
9
14
  use typed_builder::TypedBuilder;
@@ -90,6 +95,16 @@ define_enum!(
90
95
  Edge146 => ("edge_146", edge146::emulation),
91
96
  Edge147 => ("edge_147", edge147::emulation),
92
97
  Edge148 => ("edge_148", edge148::emulation),
98
+ Edge149 => ("edge_149", edge149::emulation),
99
+ Edge150 => ("edge_150", edge150::emulation),
100
+ Edge151 => ("edge_151", edge151::emulation),
101
+ Edge152 => ("edge_152", edge152::emulation),
102
+ Edge153 => ("edge_153", edge153::emulation),
103
+ Edge154 => ("edge_154", edge154::emulation),
104
+ Edge155 => ("edge_155", edge155::emulation),
105
+ Edge156 => ("edge_156", edge156::emulation),
106
+ Edge157 => ("edge_157", edge157::emulation),
107
+ Edge158 => ("edge_158", edge158::emulation),
93
108
 
94
109
  // Opera versions
95
110
  Opera116 => ("opera_116", opera116::emulation),
@@ -108,6 +123,15 @@ define_enum!(
108
123
  Opera129 => ("opera_129", opera129::emulation),
109
124
  Opera130 => ("opera_130", opera130::emulation),
110
125
  Opera131 => ("opera_131", opera131::emulation),
126
+ Opera132 => ("opera_132", opera132::emulation),
127
+ Opera133 => ("opera_133", opera133::emulation),
128
+ Opera134 => ("opera_134", opera134::emulation),
129
+ Opera135 => ("opera_135", opera135::emulation),
130
+ Opera136 => ("opera_136", opera136::emulation),
131
+ Opera137 => ("opera_137", opera137::emulation),
132
+ Opera138 => ("opera_138", opera138::emulation),
133
+ Opera139 => ("opera_139", opera139::emulation),
134
+ Opera140 => ("opera_140", opera140::emulation),
111
135
 
112
136
  // Firefox versions
113
137
  Firefox109 => ("firefox_109", ff109::emulation),
@@ -131,6 +155,14 @@ define_enum!(
131
155
  Firefox150 => ("firefox_150", ff150::emulation),
132
156
  Firefox151 => ("firefox_151", ff151::emulation),
133
157
  Firefox152 => ("firefox_152", ff152::emulation),
158
+ Firefox153 => ("firefox_153", ff153::emulation),
159
+ Firefox154 => ("firefox_154", ff154::emulation),
160
+ Firefox155 => ("firefox_155", ff155::emulation),
161
+ Firefox156 => ("firefox_156", ff156::emulation),
162
+ Firefox157 => ("firefox_157", ff157::emulation),
163
+ Firefox158 => ("firefox_158", ff158::emulation),
164
+ Firefox159 => ("firefox_159", ff159::emulation),
165
+ Firefox160 => ("firefox_160", ff160::emulation),
134
166
 
135
167
  // Safari versions
136
168
  SafariIos17_2 => ("safari_ios_17.2", safari_ios_17_2::emulation),
@@ -66,9 +66,6 @@ fn check_header_order<'a>(
66
66
  previous = Some(position);
67
67
  }
68
68
 
69
- assert!(request.contains("\r\nupgrade-insecure-requests: 1\r\n"));
70
- assert!(request.contains("\r\nsec-fetch-user: ?1\r\n"));
71
-
72
69
  stream
73
70
  .write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 0\r\nconnection: close\r\n\r\n")
74
71
  .await
@@ -76,64 +73,105 @@ fn check_header_order<'a>(
76
73
  })
77
74
  }
78
75
 
79
- async fn assert_emulation_headers(profile: Profile, expected: &'static [&'static str]) {
76
+ async fn assert_emulation_headers(
77
+ emulation: impl wreq::IntoEmulation,
78
+ expected: &'static [&'static str],
79
+ ) {
80
80
  let server = server::low_level_with_response(move |request, stream| {
81
81
  check_header_order(request, stream, expected)
82
82
  });
83
- let response = Client::builder()
84
- .emulation(profile)
83
+ Client::builder()
84
+ .emulation(emulation)
85
+ .no_proxy()
85
86
  .build()
86
87
  .expect("client")
87
88
  .get(format!("http://{}/headers", server.addr()))
88
89
  .send()
89
90
  .await
90
91
  .expect("request");
91
-
92
- assert_eq!(response.status(), wreq::StatusCode::OK);
93
92
  }
94
93
 
95
94
  #[tokio::test]
96
- async fn test_client_emulation_device() {
97
- let server = server::http(move |req| async move {
98
- let header = |name| req.headers().get(name).and_then(|value| value.to_str().ok());
95
+ async fn test_edge158_mobile_headers() {
96
+ for (platform, platform_hint, user_agent) in [
97
+ (
98
+ Platform::Android,
99
+ "\"Android\"",
100
+ "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/158.0.0.0 Mobile Safari/537.36 EdgA/158.0.0.0",
101
+ ),
102
+ (
103
+ Platform::IOS,
104
+ "\"iOS\"",
105
+ "Mozilla/5.0 (iPhone; CPU iPhone OS 18_7_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 EdgiOS/158.0.0.0 Mobile/15E148 Safari/605.1.15",
106
+ ),
107
+ ] {
108
+ let server = server::http(move |request| async move {
109
+ let headers = request.headers();
110
+ assert_eq!(headers["user-agent"], user_agent);
111
+ assert_eq!(headers["sec-ch-ua-platform"], platform_hint);
112
+ assert_eq!(headers["sec-ch-ua-mobile"], "?1");
113
+ if platform == Platform::Android {
114
+ assert!(!headers["accept"].to_str().unwrap().contains("image/jxl"));
115
+ }
116
+ http::Response::default()
117
+ });
99
118
 
100
- assert_eq!(
101
- header("user-agent"),
102
- Some(
103
- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36"
119
+ Client::builder()
120
+ .emulation(
121
+ Emulation::builder()
122
+ .profile(Emulation::Edge158)
123
+ .platform(platform)
124
+ .build(),
104
125
  )
105
- );
106
- assert_eq!(
107
- header("sec-ch-ua"),
108
- Some(r#""Not=A?Brand";v="99", "Google Chrome";v="151", "Chromium";v="151""#)
109
- );
110
- assert_eq!(header("sec-ch-ua-mobile"), Some("?0"));
111
- assert_eq!(header("sec-ch-ua-platform"), Some("\"Linux\""));
112
- http::Response::default()
113
- });
114
-
115
- let url = format!("http://{}/ua", server.addr());
116
- let res = Client::builder()
117
- .emulation(
118
- Emulation::builder()
119
- .profile(Emulation::Chrome151)
120
- .platform(Platform::Linux)
121
- .http2(true)
122
- .build(),
123
- )
124
- .build()
125
- .expect("Unable to build client")
126
- .get(&url)
127
- .send()
128
- .await
129
- .expect("request");
130
-
131
- assert_eq!(res.status(), wreq::StatusCode::OK);
126
+ .no_proxy()
127
+ .build()
128
+ .unwrap()
129
+ .get(format!("http://{}/", server.addr()))
130
+ .send()
131
+ .await
132
+ .unwrap();
133
+ }
132
134
  }
133
135
 
134
136
  #[tokio::test]
135
- async fn test_chrome_default_header_order() {
136
- assert_emulation_headers(Emulation::Chrome133, CHROMIUM_HEADER_ORDER).await;
137
+ async fn test_linux_navigation_emulation_headers() {
138
+ let linux = |profile| {
139
+ Emulation::builder()
140
+ .profile(profile)
141
+ .platform(Platform::Linux)
142
+ .build()
143
+ };
144
+ assert_emulation_headers(linux(Profile::Chrome152), CHROMIUM_HEADER_ORDER).await;
145
+
146
+ let server = server::http(|request| async move {
147
+ assert_eq!(request.version(), http::Version::HTTP_2);
148
+ let mut response = http::Response::default();
149
+ response
150
+ .headers_mut()
151
+ .insert("user-agent", request.headers()["user-agent"].clone());
152
+ response
153
+ });
154
+ let client = Client::builder()
155
+ .http2_only()
156
+ .no_proxy()
157
+ .build()
158
+ .expect("client");
159
+ for (profile, major) in [
160
+ (Profile::Firefox150, 150),
161
+ (Profile::Firefox151, 151),
162
+ (Profile::Firefox152, 152),
163
+ ] {
164
+ let response = client
165
+ .get(format!("http://{}/headers", server.addr()))
166
+ .emulation(linux(profile))
167
+ .send()
168
+ .await
169
+ .expect("request");
170
+ let expected = format!(
171
+ "Mozilla/5.0 (X11; Linux x86_64; rv:{major}.0) Gecko/20100101 Firefox/{major}.0"
172
+ );
173
+ assert_eq!(response.headers()["user-agent"], expected, "{profile:?}");
174
+ }
137
175
  }
138
176
 
139
177
  #[tokio::test]
@@ -142,6 +180,109 @@ async fn test_firefox_default_header_order() {
142
180
  }
143
181
 
144
182
  #[tokio::test]
145
- async fn test_opera_default_header_order() {
146
- assert_emulation_headers(Emulation::Opera116, CHROMIUM_HEADER_ORDER).await;
183
+ async fn test_firefox_mobile_headers() {
184
+ let server = server::http(|request| async move {
185
+ let mut response = http::Response::default();
186
+ for name in ["user-agent", "accept-language"] {
187
+ response
188
+ .headers_mut()
189
+ .insert(name, request.headers()[name].clone());
190
+ }
191
+ response
192
+ });
193
+ let client = Client::builder()
194
+ .http2_only()
195
+ .no_proxy()
196
+ .build()
197
+ .expect("client");
198
+
199
+ for (profile, platform, user_agent, language) in [
200
+ (
201
+ Profile::Firefox160,
202
+ Platform::Android,
203
+ "Mozilla/5.0 (Android 16; Mobile; rv:160.0) Gecko/160.0 Firefox/160.0",
204
+ "en-US",
205
+ ),
206
+ (
207
+ Profile::Firefox153,
208
+ Platform::IOS,
209
+ "Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/153.3 Mobile/15E148 Safari/604.1",
210
+ "en-US,en;q=0.9",
211
+ ),
212
+ ] {
213
+ let response = client
214
+ .get(format!("http://{}/headers", server.addr()))
215
+ .emulation(
216
+ Emulation::builder()
217
+ .profile(profile)
218
+ .platform(platform)
219
+ .build(),
220
+ )
221
+ .send()
222
+ .await
223
+ .expect("request");
224
+ assert_eq!(response.headers()["user-agent"], user_agent, "{profile:?}");
225
+ assert_eq!(
226
+ response.headers()["accept-language"],
227
+ language,
228
+ "{profile:?}"
229
+ );
230
+ }
231
+ }
232
+
233
+ #[tokio::test]
234
+ async fn test_opera_linux_headers() {
235
+ for (profile, expected_user_agent) in [
236
+ (
237
+ Emulation::Opera138,
238
+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/154.0.0.0 Safari/537.36 OPR/138.0.0.0",
239
+ ),
240
+ (
241
+ Emulation::Opera139,
242
+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36 OPR/139.0.0.0",
243
+ ),
244
+ (
245
+ Emulation::Opera140,
246
+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/157.0.0.0 Safari/537.36 OPR/140.0.0.0",
247
+ ),
248
+ ] {
249
+ let server = server::http(move |request| async move {
250
+ assert_eq!(request.version(), http::Version::HTTP_2);
251
+ assert_eq!(
252
+ request.headers()["user-agent"],
253
+ expected_user_agent,
254
+ "{profile:?}"
255
+ );
256
+ assert_eq!(
257
+ request.headers()["sec-ch-ua-platform"],
258
+ "\"Linux\"",
259
+ "{profile:?}"
260
+ );
261
+ if profile == Profile::Opera140 {
262
+ assert!(
263
+ request.headers()["accept"]
264
+ .to_str()
265
+ .unwrap()
266
+ .contains("image/jxl,image/avif")
267
+ );
268
+ }
269
+ http::Response::default()
270
+ });
271
+
272
+ Client::builder()
273
+ .emulation(
274
+ Emulation::builder()
275
+ .profile(profile)
276
+ .platform(Platform::Linux)
277
+ .build(),
278
+ )
279
+ .http2_only()
280
+ .no_proxy()
281
+ .build()
282
+ .expect("client")
283
+ .get(format!("http://{}/", server.addr()))
284
+ .send()
285
+ .await
286
+ .expect("request");
287
+ }
147
288
  }
@@ -345,3 +345,10 @@ test_emulation!(
345
345
  ],
346
346
  "52d84b11737d980aef856699f885ca86"
347
347
  );
348
+
349
+ test_emulation!(
350
+ test_edge158,
351
+ Emulation::Edge158,
352
+ ["t13d1516h2_8daaf6152771_806a8c22fdea"],
353
+ "52d84b11737d980aef856699f885ca86"
354
+ );
@@ -112,6 +112,13 @@ test_emulation!(
112
112
  "6ea73faa8fc5aac76bded7bd238f6433"
113
113
  );
114
114
 
115
+ test_emulation!(
116
+ test_firefox_160,
117
+ Emulation::Firefox160,
118
+ ["t13d1517h2_8daaf6152771_3cbfd9057e0d"],
119
+ "6ea73faa8fc5aac76bded7bd238f6433"
120
+ );
121
+
115
122
  /// Firefox 147 changed the generated `Accept-Language` q-value: the list used to divide 1.0
116
123
  /// across its entries, giving `q=0.5` for two, and now decrements by 0.1 per entry, giving
117
124
  /// `q=0.9`. See Bugzilla 2000765, landed in mozilla-firefox 0e050ae5116d.
@@ -147,6 +154,14 @@ fn firefox_accept_language_matches_the_profile_version() {
147
154
  ("Firefox150", Emulation::Firefox150),
148
155
  ("Firefox151", Emulation::Firefox151),
149
156
  ("Firefox152", Emulation::Firefox152),
157
+ ("Firefox153", Emulation::Firefox153),
158
+ ("Firefox154", Emulation::Firefox154),
159
+ ("Firefox155", Emulation::Firefox155),
160
+ ("Firefox156", Emulation::Firefox156),
161
+ ("Firefox157", Emulation::Firefox157),
162
+ ("Firefox158", Emulation::Firefox158),
163
+ ("Firefox159", Emulation::Firefox159),
164
+ ("Firefox160", Emulation::Firefox160),
150
165
  ];
151
166
 
152
167
  fn accept_language(profile: Profile) -> String {
@@ -111,3 +111,27 @@ test_emulation!(
111
111
  ["t13d1516h2_8daaf6152771_02713d6af862"],
112
112
  "52d84b11737d980aef856699f885ca86"
113
113
  );
114
+
115
+ test_emulation!(
116
+ test_opera132,
117
+ Emulation::Opera132,
118
+ ["t13d1516h2_8daaf6152771_d8a2da3f94cd"],
119
+ "52d84b11737d980aef856699f885ca86"
120
+ );
121
+
122
+ test_emulation!(
123
+ test_opera134,
124
+ Emulation::Opera134,
125
+ ["t13d1516h2_8daaf6152771_806a8c22fdea"],
126
+ "52d84b11737d980aef856699f885ca86"
127
+ );
128
+
129
+ test_emulation!(
130
+ test_opera140,
131
+ Emulation::Opera140,
132
+ [
133
+ "t13d1517h2_8daaf6152771_cb7bf5808d99",
134
+ "t13d1518h2_8daaf6152771_e2d80978ab2e"
135
+ ],
136
+ "52d84b11737d980aef856699f885ca86"
137
+ );
@@ -90,6 +90,16 @@ module Wreq
90
90
  Edge146 = nil
91
91
  Edge147 = nil
92
92
  Edge148 = nil
93
+ Edge149 = nil
94
+ Edge150 = nil
95
+ Edge151 = nil
96
+ Edge152 = nil
97
+ Edge153 = nil
98
+ Edge154 = nil
99
+ Edge155 = nil
100
+ Edge156 = nil
101
+ Edge157 = nil
102
+ Edge158 = nil
93
103
 
94
104
  Firefox109 = nil
95
105
  Firefox117 = nil
@@ -112,6 +122,14 @@ module Wreq
112
122
  Firefox150 = nil
113
123
  Firefox151 = nil
114
124
  Firefox152 = nil
125
+ Firefox153 = nil
126
+ Firefox154 = nil
127
+ Firefox155 = nil
128
+ Firefox156 = nil
129
+ Firefox157 = nil
130
+ Firefox158 = nil
131
+ Firefox159 = nil
132
+ Firefox160 = nil
115
133
 
116
134
  SafariIos17_2 = nil
117
135
  SafariIos17_4_1 = nil
@@ -168,6 +186,15 @@ module Wreq
168
186
  Opera129 = nil
169
187
  Opera130 = nil
170
188
  Opera131 = nil
189
+ Opera132 = nil
190
+ Opera133 = nil
191
+ Opera134 = nil
192
+ Opera135 = nil
193
+ Opera136 = nil
194
+ Opera137 = nil
195
+ Opera138 = nil
196
+ Opera139 = nil
197
+ Opera140 = nil
171
198
  end
172
199
 
173
200
  unless method_defined?(:to_s)
data/src/emulate.rs CHANGED
@@ -115,6 +115,16 @@ define_ruby_enum!(
115
115
  Edge146,
116
116
  Edge147,
117
117
  Edge148,
118
+ Edge149,
119
+ Edge150,
120
+ Edge151,
121
+ Edge152,
122
+ Edge153,
123
+ Edge154,
124
+ Edge155,
125
+ Edge156,
126
+ Edge157,
127
+ Edge158,
118
128
  Firefox109,
119
129
  Firefox117,
120
130
  Firefox128,
@@ -136,6 +146,14 @@ define_ruby_enum!(
136
146
  Firefox150,
137
147
  Firefox151,
138
148
  Firefox152,
149
+ Firefox153,
150
+ Firefox154,
151
+ Firefox155,
152
+ Firefox156,
153
+ Firefox157,
154
+ Firefox158,
155
+ Firefox159,
156
+ Firefox160,
139
157
  SafariIos17_2,
140
158
  SafariIos17_4_1,
141
159
  SafariIos16_5,
@@ -189,6 +207,15 @@ define_ruby_enum!(
189
207
  Opera129,
190
208
  Opera130,
191
209
  Opera131,
210
+ Opera132,
211
+ Opera133,
212
+ Opera134,
213
+ Opera135,
214
+ Opera136,
215
+ Opera137,
216
+ Opera138,
217
+ Opera139,
218
+ Opera140,
192
219
  );
193
220
 
194
221
  // Defines constant registration, `into_ffi`/`from_ffi`, and handlers for
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+ require "socket"
5
+ require "stringio"
6
+ require "timeout"
7
+
8
+ class EmulationClientHelloTest < Minitest::Test
9
+ SUPPORTED_GROUPS = 10
10
+ SIGNATURE_ALGORITHMS = 13
11
+ KEY_SHARE = 51
12
+ TRUST_ANCHOR_IDS = 51_764
13
+ ENCRYPTED_CLIENT_HELLO = 65_037
14
+
15
+ def test_reusing_chrome_emulation_preserves_trust_anchors
16
+ [Wreq::Profile::Chrome152, Wreq::Profile::Chrome153, Wreq::Profile::Chrome154, Wreq::Profile::Chrome155, Wreq::Profile::Chrome156, Wreq::Profile::Chrome157].each do |profile|
17
+ emulation = Wreq::Emulation.new(profile: profile)
18
+ client = Wreq::Client.new(emulation: emulation, no_proxy: true)
19
+ _, extensions = capture_client_hello { |url| client.get(url, timeout: 5) }
20
+ expected = extensions.fetch(TRUST_ANCHOR_IDS)
21
+ refute_empty expected, profile.to_s
22
+
23
+ request_client = Wreq::Client.new(no_proxy: true)
24
+ 2.times do
25
+ _, actual_extensions = capture_client_hello do |url|
26
+ request_client.get(url, emulation: emulation, timeout: 5)
27
+ end
28
+ assert_equal expected, actual_extensions.fetch(TRUST_ANCHOR_IDS), "#{profile}: reusing an object must retain its configuration"
29
+ end
30
+ end
31
+ end
32
+
33
+ def test_edge158_sends_signature_grease_without_trust_anchors
34
+ client = Wreq::Client.new(emulation: Wreq::Emulation.new(profile: Wreq::Profile::Edge158), no_proxy: true)
35
+ _, extensions = capture_client_hello { |url| client.get(url, timeout: 5) }
36
+
37
+ _, grease, *signatures = extensions.fetch(SIGNATURE_ALGORITHMS).unpack("n*")
38
+ assert_equal 0x0a0a, grease & 0x0f0f
39
+ assert_equal grease >> 8, grease & 0xff
40
+ assert_equal [0x0904, 0x0905, 0x0906], signatures.first(3) # ML-DSA
41
+ refute extensions.key?(TRUST_ANCHOR_IDS)
42
+ end
43
+
44
+ def test_firefox_client_hello_matches_version
45
+ suites_153 = [
46
+ 0x1301, 0x1303, 0x1302,
47
+ 0xc02b, 0xc02f, 0xcca9, 0xcca8, 0xc02c, 0xc030, 0xc00a, 0xc013, 0xc014, 0x009c, 0x009d, 0x002f, 0x0035
48
+ ]
49
+ # Firefox 154 disabled TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA.
50
+ suites_154 = suites_153.reject { |suite| suite == 0xc00a }
51
+
52
+ [
53
+ [Wreq::Profile::Firefox153, suites_153, 15, [4588, 29, 23, 24, 25, 256, 257]],
54
+ [Wreq::Profile::Firefox154, suites_154, 15, [4588, 29, 23, 24, 25, 256, 257]],
55
+ [Wreq::Profile::Firefox155, suites_154, 16, [4588, 29, 23, 24, 25, 256, 257]],
56
+ [Wreq::Profile::Firefox156, suites_154, 16, [4588, 29, 23, 24, 25]]
57
+ ].each do |profile, expected_suites, padding_remainder, groups|
58
+ client = Wreq::Client.new(emulation: Wreq::Emulation.new(profile: profile), no_proxy: true)
59
+ suites, extensions = capture_client_hello { |url| client.get(url, timeout: 5) }
60
+ assert_equal [4588, 29, 23], key_share_groups(extensions.fetch(KEY_SHARE)), "#{profile}: key share groups"
61
+ assert_equal expected_suites, suites, "#{profile}: cipher suites"
62
+
63
+ assert_equal padding_remainder, ech_payload_size(extensions.fetch(ENCRYPTED_CLIENT_HELLO)) % 32, "#{profile}: ECH GREASE ciphertext"
64
+ assert_equal groups, extensions.fetch(SUPPORTED_GROUPS).unpack("n*").drop(1), "#{profile}: supported groups"
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ # Each listener forces a new connection. Stop after ClientHello so this test
71
+ # inspects the wire bytes without depending on certificates or an external service.
72
+ def capture_client_hello
73
+ server = TCPServer.new("127.0.0.1", 0)
74
+ thread = Thread.new do
75
+ socket = nil
76
+ Timeout.timeout(5) do
77
+ socket = server.accept
78
+ handshake = +"".b
79
+ loop do
80
+ header = read_tls_bytes(socket, 5)
81
+ raise "expected a TLS handshake record" unless header.getbyte(0) == 22
82
+ handshake << read_tls_bytes(socket, header.byteslice(3, 2).unpack1("n"))
83
+ next if handshake.bytesize < 4
84
+ length = handshake.byteslice(1, 3).bytes.reduce(0) { |n, byte| (n << 8) | byte }
85
+ break if handshake.bytesize >= length + 4
86
+ end
87
+ parse_client_hello(handshake)
88
+ end
89
+ ensure
90
+ socket&.close
91
+ end
92
+ thread.report_on_exception = false
93
+
94
+ assert_raises(Wreq::ConnectionError) { yield "https://127.0.0.1:#{server.addr[1]}/" }
95
+ thread.value
96
+ ensure
97
+ server&.close
98
+ thread&.kill if thread&.alive?
99
+ thread&.join
100
+ end
101
+
102
+ def read_tls_bytes(io, length)
103
+ bytes = io.read(length)
104
+ raise EOFError, "truncated ClientHello" unless bytes&.bytesize == length
105
+ bytes
106
+ end
107
+
108
+ def parse_client_hello(handshake)
109
+ io = StringIO.new(handshake)
110
+ raise "expected ClientHello" unless read_tls_bytes(io, 1).unpack1("C") == 1
111
+ read_tls_bytes(io, 3 + 2 + 32) # Handshake length, legacy version and random.
112
+ read_tls_bytes(io, read_tls_bytes(io, 1).unpack1("C")) # Session ID.
113
+ suites = read_tls_bytes(io, read_tls_bytes(io, 2).unpack1("n")).unpack("n*")
114
+ read_tls_bytes(io, read_tls_bytes(io, 1).unpack1("C")) # Compression methods.
115
+ extensions = StringIO.new(read_tls_bytes(io, read_tls_bytes(io, 2).unpack1("n")))
116
+ parsed = {}
117
+ until extensions.eof?
118
+ type, length = read_tls_bytes(extensions, 4).unpack("nn")
119
+ parsed[type] = read_tls_bytes(extensions, length)
120
+ end
121
+ [suites, parsed]
122
+ end
123
+
124
+ def ech_payload_size(extension)
125
+ io = StringIO.new(extension)
126
+ read_tls_bytes(io, 6) # Outer tag, HPKE suite and config ID.
127
+ read_tls_bytes(io, read_tls_bytes(io, 2).unpack1("n")) # Encapsulated key.
128
+ read_tls_bytes(io, 2).unpack1("n")
129
+ end
130
+
131
+ def key_share_groups(extension)
132
+ io = StringIO.new(extension)
133
+ entries = StringIO.new(read_tls_bytes(io, read_tls_bytes(io, 2).unpack1("n")))
134
+ groups = []
135
+ until entries.eof?
136
+ group, length = read_tls_bytes(entries, 4).unpack("nn")
137
+ read_tls_bytes(entries, length) # Key exchange bytes.
138
+ groups << group
139
+ end
140
+ groups
141
+ end
142
+ end