perimeterx-fastly-enforcer 2.2.2

PerimeterX Fastly Compute@Edge Rust Enforcer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
use crate::handlers::pxcrypto::sha256_hex;
use crate::modules::pxconstants::{
    ACTIVITY_API, ADDITIONAL_S2S_ACTIVITY_HEADER_NAME, ADDITIONAL_S2S_URL_HEADER_NAME,
    APPLICATION_JSON, CI_BODY_MAX_LENGTH, COMPROMISED_CREDENTIALS_HEADER_VALUE,
    DEFAULT_COMPROMISED_CREDENTIALS_HEADER,
};
use crate::modules::pxutils::{get_value_at_path, set_json_int, set_json_str};
use crate::px_debug;
use crate::pxconfig::{
    PXConfig, PXCredentialEndpointConfig, PXPreparedCredentialEndpoint, PXRawCredentials,
};
use crate::pxcontext::PXContext;
use fastly::{Body, Request, Response};
use regex::Regex;
use serde_json::json;

pub(crate) const CI_VERSION_V2: &str = "v2";
pub(crate) const CI_VERSION_MULTISTEP: &str = "multistep_sso";
pub(crate) const CI_VERSION_BOTH: &str = "both";
pub(crate) const CI_VERSION_V1: &str = "v1";
pub(crate) const SSO_STEP_USER: &str = "user";
const SSO_STEP_PASS: &str = "pass";

/// Hashed credential payload attached to Risk API and async activities.
#[derive(Debug, Clone, Default)]
pub struct PXCredentialIntelligenceData {
    pub endpoint_index: usize,
    pub ci_version: String,
    pub user: Option<String>,
    pub pass: Option<String>,
    pub sso_step: Option<String>,
    pub raw_username: Option<String>,
    pub is_login_successful: Option<bool>,
    pub response_status_code: Option<u16>,
}

/// Extract and hash credentials; mark route sensitive when extraction succeeds.
pub(crate) fn enrich_context_from_request(req: &mut Request, conf: &PXConfig, ctx: &mut PXContext) {
    if !conf.login_credentials_extraction_enabled {
        return;
    }

    let endpoint_index = match find_matching_endpoint(req, conf) {
        Some(i) => i,
        None => return,
    };

    let raw = match extract_credentials(req, conf, endpoint_index) {
        Some(c) if c.user.is_some() || c.pass.is_some() => c,
        _ => return,
    };

    let protocol = resolve_protocol(conf, endpoint_index);
    if protocol == CI_VERSION_V1 {
        px_debug!("credentials intelligence v1 is not supported");
        return;
    }

    let mut hashed = match hash_credentials(protocol, &raw) {
        Some(h) => h,
        None => return,
    };
    hashed.endpoint_index = endpoint_index;

    ctx.credential_intelligence = Some(hashed);
    ctx.is_sensitive_route = true;
}

/// Set compromised flag from Risk API data enrichment (already on context).
pub(crate) fn is_credentials_compromised(ctx: &PXContext) -> bool {
    ctx.get_data_enrichment()
        .map(|de| de.get_breached_account() == 1)
        .unwrap_or(false)
}

/// Add compromised-credentials and optional manual additional_s2s headers to the origin request.
pub(crate) fn modify_incoming_request(req: &mut Request, conf: &PXConfig, ctx: &PXContext) {
    if ctx.credential_intelligence.is_none() {
        return;
    }

    if is_credentials_compromised(ctx) {
        let header_name = if conf.compromised_credentials_header.is_empty() {
            DEFAULT_COMPROMISED_CREDENTIALS_HEADER
        } else {
            conf.compromised_credentials_header.as_str()
        };
        req.set_header(header_name, COMPROMISED_CREDENTIALS_HEADER_VALUE);
    }

    if conf.additional_s2s_activity_enabled || !conf.additional_s2s_activity_header_enabled {
        return;
    }

    let activity = build_additional_s2s_activity_base(conf, ctx);
    let activity_json = match serde_json::to_string(&activity) {
        Ok(s) => s,
        Err(e) => {
            px_debug!("failed to serialize additional_s2s activity: {}", e);
            return;
        }
    };
    let url = format!("https://{}{}", conf.human_collector_host, ACTIVITY_API);
    req.set_header(ADDITIONAL_S2S_ACTIVITY_HEADER_NAME, activity_json);
    req.set_header(ADDITIONAL_S2S_URL_HEADER_NAME, url);
}

/// Inspect origin response and determine login success for additional_s2s.
pub(crate) fn enrich_context_from_response(
    resp: &mut Response,
    conf: &PXConfig,
    ctx: &mut PXContext,
) {
    let Some(ci) = ctx.credential_intelligence.as_mut() else {
        return;
    };

    let status = resp.get_status().as_u16();
    ci.response_status_code = Some(status);

    let endpoint_index = ci.endpoint_index;
    ci.is_login_successful = evaluate_login_successful(resp, conf, endpoint_index, status);
}

/// Send automatic `additional_s2s` when configured and credentials were extracted.
pub(crate) fn send_additional_s2s(ctx: &PXContext, conf: &PXConfig) {
    if !conf.additional_s2s_activity_enabled {
        return;
    }
    let Some(ci) = ctx.credential_intelligence.as_ref() else {
        return;
    };
    if ctx
        .block_reason
        .as_ref()
        .is_some_and(|r| *r != crate::pxcontext::BlockReason::None)
    {
        return;
    }

    let mut activity = build_additional_s2s_activity_base(conf, ctx);
    if let Some(details) = activity.get_mut("details").and_then(|v| v.as_object_mut()) {
        if let Some(login_ok) = ci.is_login_successful {
            details.insert("login_successful".to_owned(), json!(login_ok));
        }
        if let Some(status) = ci.response_status_code {
            details.insert("http_status_code".to_owned(), json!(status));
        }
        if should_send_raw_username(conf, ctx, ci) {
            if let Some(raw) = &ci.raw_username {
                details.insert("raw_username".to_owned(), json!(raw));
            }
        }
    }

    post_activity_payload(activity, ctx, conf);
}

/// Merge CI fields into Risk API `additional` or activity `details`.
pub(crate) fn apply_ci_fields_to_details(
    details: &mut serde_json::Value,
    ctx: &PXContext,
    include_credentials: bool,
    include_compromised: bool,
) {
    let Some(ci) = ctx.credential_intelligence.as_ref() else {
        return;
    };

    if !ci.ci_version.is_empty() {
        set_json_str!(details, "ci_version"; ci.ci_version);
    }
    if include_credentials {
        if let Some(u) = &ci.user {
            set_json_str!(details, "user"; u);
        }
        if let Some(p) = &ci.pass {
            set_json_str!(details, "pass"; p);
        }
    }
    if let Some(step) = &ci.sso_step {
        set_json_str!(details, "sso_step"; step);
    }
    if include_compromised && ctx.credential_intelligence.is_some() {
        details["credentials_compromised"] = json!(is_credentials_compromised(ctx));
    }
}

fn should_send_raw_username(
    conf: &PXConfig,
    ctx: &PXContext,
    ci: &PXCredentialIntelligenceData,
) -> bool {
    if !conf.send_raw_username_on_additional_s2s_activity {
        return false;
    }
    if !is_credentials_compromised(ctx) {
        return false;
    }
    match ci.is_login_successful {
        Some(true) => true,
        None => true,
        Some(false) => false,
    }
}

fn find_matching_endpoint(req: &Request, conf: &PXConfig) -> Option<usize> {
    let path = req.get_path();
    let method = req.get_method_str();
    for (index, endpoint) in conf.prepared_ci_endpoints.iter().enumerate() {
        if endpoint_matches(endpoint, path, method) {
            return Some(index);
        }
    }
    None
}

pub(crate) fn endpoint_matches(
    endpoint: &PXPreparedCredentialEndpoint,
    path: &str,
    method: &str,
) -> bool {
    if !endpoint.config.method.eq_ignore_ascii_case(method) {
        return false;
    }
    if endpoint.config.path_type.eq_ignore_ascii_case("regex") {
        endpoint
            .path_regex
            .as_ref()
            .is_some_and(|re| re.is_match(path))
    } else {
        endpoint.config.path == path
    }
}

fn extract_credentials(
    req: &mut Request,
    conf: &PXConfig,
    endpoint_index: usize,
) -> Option<PXRawCredentials> {
    let endpoint = conf.prepared_ci_endpoints.get(endpoint_index)?;
    let sent_through = endpoint.config.sent_through.as_str();

    let raw = if sent_through.eq_ignore_ascii_case("custom") {
        conf.ci_extract_credentials_fn
            .and_then(|f| f(req, endpoint_index))
    } else if sent_through.eq_ignore_ascii_case("header") {
        extract_from_headers(req, &endpoint.config)
    } else if sent_through.eq_ignore_ascii_case("query-param") {
        extract_from_query(req, &endpoint.config)
    } else if sent_through.eq_ignore_ascii_case("body") {
        extract_from_body(req, &endpoint.config)
    } else {
        None
    }?;

    let raw = raw.without_empty_fields();
    (raw.user.is_some() || raw.pass.is_some()).then_some(raw)
}

fn extract_from_headers(
    req: &Request,
    config: &PXCredentialEndpointConfig,
) -> Option<PXRawCredentials> {
    let user = read_header_field(req, &config.user_field);
    let pass = read_header_field(req, &config.pass_field);
    if user.is_some() || pass.is_some() {
        Some(PXRawCredentials { user, pass })
    } else {
        None
    }
}

fn read_header_field(req: &Request, name: &str) -> Option<String> {
    if name.is_empty() {
        return None;
    }
    req.get_header_str_lossy(name)
        .map(|v| v.into_owned())
        .filter(|v| !v.is_empty())
}

fn extract_from_query(
    req: &Request,
    config: &PXCredentialEndpointConfig,
) -> Option<PXRawCredentials> {
    let query = req.get_url().query().unwrap_or_default();
    let params = parse_urlencoded(query);
    let user = params.get(&config.user_field).cloned();
    let pass = params.get(&config.pass_field).cloned();
    if user.is_some() || pass.is_some() {
        Some(PXRawCredentials { user, pass })
    } else {
        None
    }
}

fn extract_from_body(
    req: &mut Request,
    config: &PXCredentialEndpointConfig,
) -> Option<PXRawCredentials> {
    let content_type = req
        .get_header_str_lossy("content-type")
        .map(|v| v.into_owned())
        .unwrap_or_default();
    if content_type.is_empty() {
        return None;
    }

    if req.get_content_length().unwrap_or(0) > CI_BODY_MAX_LENGTH {
        return None;
    }

    let body = req.get_body_prefix_mut(CI_BODY_MAX_LENGTH);
    if body.is_empty() {
        return None;
    }

    let body_str = std::str::from_utf8(body.as_slice()).ok()?;

    if content_type.contains("json") {
        let json: serde_json::Value = serde_json::from_str(body_str).ok()?;
        let user = read_json_field(&json, &config.user_field);
        let pass = read_json_field(&json, &config.pass_field);
        if user.is_some() || pass.is_some() {
            return Some(PXRawCredentials { user, pass });
        }
        return None;
    }

    if content_type.contains("application/x-www-form-urlencoded") {
        let params = parse_urlencoded(body_str);
        let user = params.get(&config.user_field).cloned();
        let pass = params.get(&config.pass_field).cloned();
        if user.is_some() || pass.is_some() {
            return Some(PXRawCredentials { user, pass });
        }
        return None;
    }

    if content_type.contains("multipart/form-data") {
        let boundary = parse_multipart_boundary(&content_type)?;
        return extract_from_multipart(body_str, boundary, config);
    }

    None
}

fn read_json_field(json: &serde_json::Value, field: &str) -> Option<String> {
    if field.is_empty() {
        return None;
    }
    get_value_at_path(json, field).and_then(|v| v.as_str().map(str::to_owned))
}

pub(crate) fn parse_multipart_boundary(content_type: &str) -> Option<String> {
    content_type.split(';').map(str::trim).find_map(|part| {
        part.strip_prefix("boundary=")
            .map(str::trim)
            .map(|boundary| boundary.trim_matches(['"', '\'']))
            .map(str::to_owned)
            .filter(|boundary| !boundary.is_empty())
    })
}

pub(crate) fn extract_from_multipart(
    body: &str,
    boundary: String,
    config: &PXCredentialEndpointConfig,
) -> Option<PXRawCredentials> {
    let delimiter = format!("--{boundary}");
    let mut user = None;
    let mut pass = None;

    for part in body.split(&delimiter) {
        let part = part.trim();
        if part.is_empty() || part == "--" {
            continue;
        }
        let Some(name) = parse_multipart_field_name(part) else {
            continue;
        };
        let value = parse_multipart_field_value(part);
        if name == config.user_field {
            user = value;
        } else if name == config.pass_field {
            pass = value;
        }
    }

    if user.is_some() || pass.is_some() {
        Some(PXRawCredentials { user, pass })
    } else {
        None
    }
}

fn parse_multipart_field_name(part: &str) -> Option<String> {
    for line in part.lines() {
        let lower = line.to_ascii_lowercase();
        if lower.starts_with("content-disposition:") && lower.contains("name=") {
            let start = line.find("name=")? + 5;
            let rest = line.get(start..)?;
            let name = rest.trim_matches(['"', '\'', ' ']);
            let name = name.split(';').next()?.trim_matches(['"', '\'']);
            if !name.is_empty() {
                return Some(name.to_owned());
            }
        }
    }
    None
}

fn parse_multipart_field_value(part: &str) -> Option<String> {
    let mut lines = part.lines();
    while let Some(line) = lines.next() {
        if line.is_empty() {
            let value: String = lines.collect::<Vec<_>>().join("\n");
            let value = value.trim_end_matches('\r').trim().to_owned();
            if value.is_empty() {
                return None;
            }
            return Some(value);
        }
    }
    None
}

pub(crate) fn parse_urlencoded(input: &str) -> std::collections::HashMap<String, String> {
    let mut map = std::collections::HashMap::new();
    for pair in input.split('&') {
        if pair.is_empty() {
            continue;
        }
        let (key, value) = match pair.split_once('=') {
            Some((k, v)) => (k, v),
            None => (pair, ""),
        };
        let key = percent_decode(key.replace('+', " ").as_str());
        let value = percent_decode(value.replace('+', " ").as_str());
        if !key.is_empty() {
            map.insert(key, value);
        }
    }
    map
}

fn percent_decode(input: &str) -> String {
    let bytes = input.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        let Some(&byte) = bytes.get(i) else {
            break;
        };
        if byte == b'%' {
            if let (Some(&b1), Some(&b2)) = (bytes.get(i + 1), bytes.get(i + 2)) {
                let hex = [b1, b2];
                if let Ok(hex_str) = std::str::from_utf8(&hex) {
                    if let Ok(decoded) = u8::from_str_radix(hex_str, 16) {
                        out.push(decoded);
                        i += 3;
                        continue;
                    }
                }
            }
        }
        out.push(byte);
        i += 1;
    }
    String::from_utf8(out).unwrap_or_default()
}

fn resolve_protocol(conf: &PXConfig, endpoint_index: usize) -> &str {
    conf.prepared_ci_endpoints
        .get(endpoint_index)
        .and_then(|e| {
            if e.config.protocol.is_empty() {
                None
            } else {
                Some(e.config.protocol.as_str())
            }
        })
        .unwrap_or(conf.credentials_intelligence_version.as_str())
}

pub(crate) fn hash_credentials(
    protocol: &str,
    raw: &PXRawCredentials,
) -> Option<PXCredentialIntelligenceData> {
    match protocol {
        CI_VERSION_V2 => hash_v2(raw),
        CI_VERSION_MULTISTEP => hash_multistep(raw),
        CI_VERSION_BOTH => {
            if raw.user.is_some() && raw.pass.is_some() {
                hash_v2(raw)
            } else {
                hash_multistep(raw)
            }
        }
        CI_VERSION_V1 => None,
        _ => hash_v2(raw),
    }
}

pub(crate) fn hash_v2(raw: &PXRawCredentials) -> Option<PXCredentialIntelligenceData> {
    let user = raw.user.as_deref()?;
    let pass = raw.pass.as_deref()?;
    let normalized = normalize_username(user);
    let hashed_user = sha256_hex(&normalized)?;
    let hashed_pass = sha256_hex(&format!("{}{}", hashed_user, sha256_hex(pass)?))?;
    Some(PXCredentialIntelligenceData {
        ci_version: CI_VERSION_V2.to_owned(),
        user: Some(hashed_user),
        pass: Some(hashed_pass),
        raw_username: Some(user.to_owned()),
        ..Default::default()
    })
}

fn hash_multistep(raw: &PXRawCredentials) -> Option<PXCredentialIntelligenceData> {
    if raw.user.is_some() {
        let user = raw.user.clone()?;
        return Some(PXCredentialIntelligenceData {
            ci_version: CI_VERSION_MULTISTEP.to_owned(),
            user: Some(user.clone()), // not hashed
            sso_step: Some(SSO_STEP_USER.to_owned()),
            ..Default::default()
        });
    }
    if raw.pass.is_some() {
        let pass = raw.pass.as_deref()?;
        let hashed_pass = sha256_hex(pass)?;
        return Some(PXCredentialIntelligenceData {
            ci_version: CI_VERSION_MULTISTEP.to_owned(),
            pass: Some(hashed_pass),
            sso_step: Some(SSO_STEP_PASS.to_owned()),
            ..Default::default()
        });
    }
    None
}

pub(crate) fn normalize_username(username: &str) -> String {
    if !is_email_address(username) {
        return username.to_owned();
    }
    let lowercase = username.trim().to_ascii_lowercase();
    let (local, domain) = match lowercase.split_once('@') {
        Some(parts) => parts,
        None => return lowercase,
    };
    let mut local = local
        .split_once('+')
        .map_or(local, |(before_plus, _)| before_plus)
        .to_owned();
    if domain == "gmail.com" {
        local = local.replace('.', "");
    }
    format!("{local}@{domain}")
}

fn is_email_address(value: &str) -> bool {
    value.trim().split_once('@').is_some_and(|(local, domain)| {
        !local.is_empty() && !domain.is_empty() && !domain.contains('@')
    })
}

fn evaluate_login_successful(
    resp: &mut Response,
    conf: &PXConfig,
    endpoint_index: usize,
    status: u16,
) -> Option<bool> {
    let endpoint = conf.prepared_ci_endpoints.get(endpoint_index)?;
    let method = resolve_login_success_method(conf, &endpoint.config);

    match method.as_str() {
        "status" => {
            let statuses = resolve_login_success_statuses(conf, &endpoint.config);
            Some(statuses.contains(&status))
        }
        "header" => evaluate_login_header(resp, conf, &endpoint.config),
        "body" => evaluate_login_body(resp, conf, &endpoint.config),
        "custom" => conf
            .ci_login_successful_fn
            .and_then(|f| f(resp, endpoint_index)),
        _ => None,
    }
}

fn resolve_login_success_method(conf: &PXConfig, endpoint: &PXCredentialEndpointConfig) -> String {
    if !endpoint.login_successful_reporting_method.is_empty() {
        endpoint.login_successful_reporting_method.clone()
    } else if !conf.login_successful_reporting_method.is_empty() {
        conf.login_successful_reporting_method.clone()
    } else {
        "status".to_owned()
    }
}

fn resolve_login_success_statuses(
    conf: &PXConfig,
    endpoint: &PXCredentialEndpointConfig,
) -> Vec<u16> {
    if !endpoint.login_successful_statuses.is_empty() {
        endpoint.login_successful_statuses.clone()
    } else if !conf.login_successful_status.is_empty() {
        conf.login_successful_status.clone()
    } else {
        vec![200]
    }
}

fn resolve_login_body_regex(
    conf: &PXConfig,
    endpoint: &PXCredentialEndpointConfig,
) -> Option<Regex> {
    let pattern = if !endpoint.login_successful_body_regex.is_empty() {
        endpoint.login_successful_body_regex.as_str()
    } else if !conf.login_successful_body_regex.is_empty() {
        conf.login_successful_body_regex.as_str()
    } else {
        return None;
    };
    let raw = pattern
        .strip_prefix(crate::pxconfig::REGEX_PREFIX)
        .unwrap_or(pattern);
    Regex::new(raw).ok()
}

fn evaluate_login_header(
    resp: &Response,
    conf: &PXConfig,
    endpoint: &PXCredentialEndpointConfig,
) -> Option<bool> {
    let name = if !endpoint.login_successful_header_name.is_empty() {
        endpoint.login_successful_header_name.as_str()
    } else if !conf.login_successful_header_name.is_empty() {
        conf.login_successful_header_name.as_str()
    } else {
        return None;
    };

    let value = resp.get_header_str_lossy(name).map(|v| v.into_owned());
    let expected = if !endpoint.login_successful_header_value.is_empty() {
        Some(endpoint.login_successful_header_value.as_str())
    } else if !conf.login_successful_header_value.is_empty() {
        Some(conf.login_successful_header_value.as_str())
    } else {
        None
    };

    match (value, expected) {
        (Some(actual), Some(expected)) => Some(actual == expected),
        (Some(_), None) => Some(true),
        _ => Some(false),
    }
}

fn evaluate_login_body(
    resp: &mut Response,
    conf: &PXConfig,
    endpoint: &PXCredentialEndpointConfig,
) -> Option<bool> {
    let re = resolve_login_body_regex(conf, endpoint)?;
    let bytes = resp.take_body_bytes();
    let matched = std::str::from_utf8(&bytes)
        .ok()
        .is_some_and(|body| re.is_match(body));
    resp.set_body(Body::from(bytes));
    Some(matched)
}

pub(crate) fn build_additional_s2s_activity_base(
    conf: &PXConfig,
    ctx: &PXContext,
) -> serde_json::Value {
    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0);

    let mut details = json!({
        "client_uuid": ctx.uuid.as_deref().unwrap_or_default(),
        "module_version": crate::modules::pxconstants::PX_MODULE_VERSION,
        "request_id": ctx.request_id.to_string(),
    });
    apply_ci_fields_to_details(&mut details, ctx, false, true);

    let mut activity = json!({
        "timestamp": timestamp,
        "type": "additional_s2s",
        "socket_ip": ctx.ip,
        "px_app_id": conf.app_id,
        "url": ctx.full_url,
        "details": details,
    });

    set_json_str!(&mut activity, "vid"; ctx.vid.as_deref().unwrap_or_default());
    set_json_str!(&mut activity, "pxhd"; ctx.get_pxhd().unwrap_or_default());
    activity
}

fn post_activity_payload(mut activity: serde_json::Value, ctx: &PXContext, conf: &PXConfig) {
    if let Some(details) = activity.get_mut("details") {
        set_json_int!(details, "risk_rtt"; ctx.risk_rtt.unwrap_or(0));
    }

    let body = activity.to_string();
    px_debug!("additional_s2s activity body: {}", body);
    let url = format!("https://{}{}", conf.human_collector_host, ACTIVITY_API);
    let req = Request::post(url)
        .with_header("Authorization", format!("Bearer {}", conf.auth_token))
        .with_header("Content-Type", APPLICATION_JSON)
        .with_body(body.as_bytes())
        .send_async(&conf.human_collector_backend);

    match req {
        Ok(r) => {
            r.poll();
        }
        Err(e) => px_debug!("Error sending additional_s2s activity: {}", e),
    }
}