-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmod.rs
More file actions
860 lines (810 loc) · 32.4 KB
/
mod.rs
File metadata and controls
860 lines (810 loc) · 32.4 KB
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
use std::borrow::Cow;
use std::collections::{HashMap, VecDeque};
use std::fs::File;
use std::io::Read;
use std::num::NonZero;
use std::sync::Arc;
use std::time::Duration;
use bytes::Bytes;
use cookie::Cookie;
use itertools::Itertools;
use libdd_trace_protobuf::pb::Span;
use libddwaf::object::{WafMap, WafOwned};
use libddwaf::{Builder, Config as WafConfig, Handle};
use tokio::sync::Mutex;
use tracing::{debug, info};
use crate::appsec::processor::context::Context;
use crate::appsec::processor::response::ExpectedResponseFormat;
use crate::appsec::{is_enabled, is_standalone};
use crate::config::Config;
use crate::lifecycle::invocation::triggers::IdentifiedTrigger;
mod apisec;
pub mod context;
pub mod response;
mod ruleset;
pub struct Processor {
handle: Handle,
ruleset_version: String,
waf_timeout: Duration,
api_sec_sampler: Option<Arc<Mutex<apisec::Sampler>>>, // Must be [`Arc`] so [`Processor`] can be [`Send`].
context_buffer: VecDeque<Context>,
}
impl Processor {
const CONTEXT_BUFFER_DEFAULT_CAPACITY: NonZero<usize> = NonZero::new(5).unwrap();
/// Creates a new [`Processor`] instance using the provided [`Config`].
///
/// # Errors
/// - If [`Config::serverless_appsec_enabled`] is `false`;
/// - If the [`Config::appsec_rules`] points to a non-existent file;
/// - If the [`Config::appsec_rules`] points to a file that is not a valid JSON-encoded ruleset;
/// - If the in-app WAF fails to initialize, integrate the ruleset, or build the WAF instance.
pub fn new(cfg: &Config) -> Result<Self, Error> {
Self::with_capacity(cfg, Self::CONTEXT_BUFFER_DEFAULT_CAPACITY)
}
/// Creates a new [`Processor`] instance using the provided [`Config`].
///
/// # Errors
/// - If [`Config::serverless_appsec_enabled`] is `false`;
/// - If the [`Config::appsec_rules`] points to a non-existent file;
/// - If the [`Config::appsec_rules`] points to a file that is not a valid JSON-encoded ruleset;
/// - If the in-app WAF fails to initialize, integrate the ruleset, or build the WAF instance.
pub fn with_capacity(cfg: &Config, capacity: NonZero<usize>) -> Result<Self, Error> {
if !is_enabled(cfg) {
return Err(Error::FeatureDisabled);
}
debug!("aap: starting App & API Protection processor");
if is_standalone() {
info!(
"aap: starting App & API Protection in standalone mode. APM tracing will be disabled for this service."
);
}
let Some(mut builder) = Builder::new(&WafConfig::default()) else {
return Err(Error::WafBuilderCreationFailed);
};
let rules = Self::get_rules(cfg)?;
let mut diagnostics = WafOwned::<WafMap>::default();
if !builder.add_or_update_config("rules", &rules, Some(&mut diagnostics)) {
return Err(Error::WafRulesetLoadingError(diagnostics));
}
let ruleset_version =
if let Some(version) = diagnostics.get(b"ruleset_version").and_then(|o| o.to_str()) {
debug!("aap: loaded ruleset version {version}");
version.to_string()
} else {
String::new()
};
let Some(handle) = builder.build() else {
return Err(Error::WafInitializationFailed);
};
Ok(Self {
handle,
ruleset_version,
waf_timeout: cfg.appsec_waf_timeout,
api_sec_sampler: if cfg.api_security_enabled {
Some(Arc::new(Mutex::new(apisec::Sampler::with_interval(
cfg.api_security_sample_delay,
))))
} else {
None
},
context_buffer: VecDeque::with_capacity(capacity.get()),
})
}
/// Process the intercepted payload for the next invocation.
pub async fn process_invocation_next(&mut self, rid: &str, payload: &IdentifiedTrigger) {
if payload.is_unknown() {
return;
}
self.new_context(rid).await.run(payload);
}
/// Process the intercepted payload for the result of an invocation.
pub async fn process_invocation_result(&mut self, rid: &str, payload: &Bytes) {
// Taking the sampler first, as it implies a temporary immutable borrow...
let api_sec_sampler = self.api_sec_sampler.as_ref().map(Arc::clone);
let api_sec_sampler = if let Some(api_sec_sampler) = &api_sec_sampler {
Some(api_sec_sampler.lock().await)
} else {
None
};
let Some(ctx) = self.get_context_mut(rid) else {
// Nothing to do...
return;
};
match ctx.expected_response_format.parse(payload.as_ref()) {
Ok(Some(payload)) => {
debug!("aap: successfully parsed response payload, evaluating ruleset...");
ctx.run(payload.as_ref());
}
Ok(None) => debug!("aap: no response payload available"),
Err(e) => debug!("aap: failed to parse invocation result payload: {e}"),
}
let (method, route, status_code) = ctx.endpoint_info();
if api_sec_sampler
.is_some_and(|mut sampler| sampler.decision_for(&method, &route, &status_code))
{
debug!(
"aap: extracing API Security schema for request <{method}, {route}, {status_code}>"
);
ctx.extract_schemas();
}
// Finally, mark the response as having been seen.
ctx.set_response_seen().await;
}
/// Returns the first `aws.lambda` span from the provided trace, if one
/// exists.
///
/// Placeholder spans (resource == `INVOCATION_SPAN_RESOURCE`) emitted by
/// Go and Java tracers are excluded: they are always dropped by the chunk
/// processor before reaching the backend, so tagging them would waste the
/// `AppSec` context and trigger a premature context deletion that would leave
/// the real, extension-built `aws.lambda` span untagged.
///
/// # Returns
/// The span on which security information will be attached.
pub fn service_entry_span_mut(trace: &mut [Span]) -> Option<&mut Span> {
trace.iter_mut().find(|span| {
span.name == "aws.lambda" && span.resource != crate::traces::INVOCATION_SPAN_RESOURCE
})
}
/// Processes an intercepted [`Span`].
///
/// # Returns
/// Returns [`true`] the span can already be finalized and sent to the
/// backend, meaning that if there was a security context, we have already
/// had a chance to see the response for the corresponding span already.
/// Otherwise, returns false, and an optional [`Context`] that can be used
/// to defer some processing to when the response becomes available.
pub fn process_span(&mut self, span: &mut Span) -> (bool, Option<&mut Context>) {
let Some(rid) = span.meta.get("request_id").cloned() else {
// Can't match this to a request ID, nothing to do...
debug!(
"aap | {} @ {} | no request_id found in span meta, nothing to do...",
span.name, span.span_id
);
return (true, None);
};
let Some(ctx) = self.get_context(&rid) else {
// Nothing to do...
debug!(
"aap | {} @ {} | no security context is associated with request {rid}, nothing to do...",
span.name, span.span_id
);
return (true, None);
};
debug!(
"aap | {} @ {} | processing span with request {rid}",
span.name, span.span_id
);
ctx.process_span(span);
// Span is finalized from a security standpoint if we've seen the
// response for it already.
if !ctx.is_pending_response() {
debug!(
"aap | {} @ {} | span is finalized, deleting context",
span.name, span.span_id
);
self.delete_context(&rid);
return (true, None);
}
// Fetch the context again here, as otherwise the borrow checker yells.
(false, self.get_context_mut(&rid))
}
/// Parses the App & API Protection ruleset from the provided [`Config`], or
/// the default built-in ruleset if the [`Config::appsec_rules`] field is
/// [`None`].
fn get_rules(cfg: &Config) -> Result<WafMap, Error> {
if let Some(ref rules) = cfg.appsec_rules {
let file = File::open(rules).map_err(|e| Error::AppsecRulesError(rules.clone(), e))?;
serde_json::from_reader(file)
} else {
serde_json::from_reader(ruleset::default_recommended_ruleset())
}
.map_err(Error::WafRulesetParseError)
}
/// Creates a new [`Context`] for the given request ID, and tracks it in the
/// context buffer.
async fn new_context(&mut self, rid: &str) -> &mut Context {
let dropped = if let Some(idx) = self.context_buffer.iter().position(|c| c.rid == rid) {
// This request ID was already seen, remove it from the buffer...
self.context_buffer.remove(idx)
} else if self.context_buffer.len() == self.context_buffer.capacity() {
// We're at capacity, remove the oldest context before proceeding...
self.context_buffer.pop_front()
} else {
None
};
if let Some(mut ctx) = dropped {
// Ensure any pending trace is flushed out before continuing...
ctx.set_response_seen().await;
}
// Insert the new context at the back of the buffer.
self.context_buffer.push_back(Context::new(
rid.to_string(),
&mut self.handle,
&self.ruleset_version,
self.waf_timeout,
));
// Retrieve a mutable reference to it from the buffer.
self.context_buffer
.back_mut()
.expect("should have at least one element")
}
/// Retrieves the [`Context`] associated with the given request ID, if there
/// is one.
fn get_context(&self, rid: &str) -> Option<&Context> {
self.context_buffer.iter().find(|c| c.rid == rid)
}
/// Retrieves the [`Context`] associated with the given request ID, if there
/// is one.
fn get_context_mut(&mut self, rid: &str) -> Option<&mut Context> {
self.context_buffer.iter_mut().find(|c| c.rid == rid)
}
/// Removes the [`Context`] associated with the given request ID from the
/// buffer, if there is one.
fn delete_context(&mut self, rid: &str) {
if let Some(idx) = self.context_buffer.iter().position(|c| c.rid == rid) {
self.context_buffer.remove(idx);
}
}
}
impl std::fmt::Debug for Processor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Processor")
.field("waf_timeout", &self.waf_timeout)
.finish_non_exhaustive()
}
}
/// Error conditions that can arise from calling [`Processor::new`].
#[derive(thiserror::Error, Debug)]
pub enum Error {
/// The App & API Protection feature is not enabled
#[error("aap: feature is not enabled")]
FeatureDisabled,
/// The WAF builder could not be created (unlikely)
#[error("aap: WAF builder creation failed")]
WafBuilderCreationFailed,
/// The user-configured App & API Protection ruleset file could not be read
#[error("aap: failed to open WAF rules file {0:#?}: {1}")]
AppsecRulesError(String, std::io::Error),
/// The App & API Protection ruleset could not be parsed from JSON
#[error("aap: failed to parse WAF rulesset: {0}")]
WafRulesetParseError(serde_json::Error),
/// The App & API Protection ruleset could not be loaded into the WAF
#[error("aap: failed to load configured WAF ruleset: {0:?}")]
WafRulesetLoadingError(WafOwned<WafMap>),
/// The WAF initialization produced a [`None`] handle (this happens when the
/// configured ruleset contains no active rule nor processor)
#[error(
"aap: WAF initialization failed (is there any active rule or processor in the ruleset?)"
)]
WafInitializationFailed,
}
/// A trait representing the general payload of an invocation event. Most
/// fields are optional and default to [`None`] or empty [`HashMap`]s.
pub trait InvocationPayload {
fn corresponding_response_format(&self) -> ExpectedResponseFormat;
/// The raw URI of the request (query string excluded).
fn raw_uri(&self) -> Option<String> {
None
}
/// The HTTP method for the request (e.g. `GET`, `POST`, etc.).
fn method(&self) -> Option<String> {
None
}
/// The route for the request (e.g. `/api/v1/users/{id}`).
fn route(&self) -> Option<String> {
None
}
/// The Client IP for the request (e.g. `127.0.0.1`).
fn client_ip(&self) -> Option<String> {
None
}
/// The multi-value headers for the request, without the cookies.
fn request_headers_no_cookies(&self) -> HashMap<String, Vec<String>> {
HashMap::default()
}
/// The cookies for the request.
fn request_cookies(&self) -> HashMap<String, Vec<String>> {
HashMap::default()
}
/// The query string parameters for the request.
fn query_params(&self) -> HashMap<String, Vec<String>> {
HashMap::default()
}
/// The path parameters for the request.
fn path_params(&self) -> HashMap<String, String> {
HashMap::default()
}
/// The body payload of the request encoded as a [`WafObject`].
fn request_body<'a>(&'a self) -> Option<Box<dyn Read + 'a>> {
None
}
/// The status code of the response (e.g, 200, 404, etc...).
fn response_status_code(&self) -> Option<i64> {
None
}
/// The headers for the response, without the cookies.
fn response_headers_no_cookies(&self) -> HashMap<String, Vec<String>> {
HashMap::default()
}
/// The body payload of the response encoded as a [`WafObject`].
fn response_body<'a>(&'a self) -> Option<Box<dyn Read + 'a>> {
None
}
}
impl InvocationPayload for IdentifiedTrigger {
fn corresponding_response_format(&self) -> ExpectedResponseFormat {
match self {
Self::APIGatewayHttpEvent(_)
| Self::APIGatewayRestEvent(_)
| Self::ALBEvent(_)
| Self::LambdaFunctionUrlEvent(_) => ExpectedResponseFormat::ApiGatewayResponse,
Self::APIGatewayWebSocketEvent(_) => ExpectedResponseFormat::Raw,
// Events that are not relevant to App & API Protection
Self::MSKEvent(_)
| Self::SqsRecord(_)
| Self::SnsRecord(_)
| Self::DynamoDbRecord(_)
| Self::S3Record(_)
| Self::EventBridgeEvent(_)
| Self::KinesisRecord(_)
| Self::StepFunctionEvent(_)
| Self::Unknown => ExpectedResponseFormat::Unknown,
}
}
fn raw_uri(&self) -> Option<String> {
match self {
Self::APIGatewayHttpEvent(t) => Some(format!(
"{domain}{path}",
domain = t.request_context.domain_name,
path = t.request_context.http.path
)),
Self::APIGatewayRestEvent(t) => Some(format!(
"{domain}{path}",
domain = t.request_context.domain_name,
path = t.request_context.path
)),
Self::APIGatewayWebSocketEvent(t) => Some(
if t.request_context.stage.is_empty() || t.request_context.stage == "$default" {
format!(
"{domain}{path}",
domain = t.request_context.domain_name,
path = t.path.as_ref().map_or("", |s| s.as_str())
)
} else {
format!(
"{domain}/${stage}{path}",
domain = t.request_context.domain_name,
stage = t.request_context.stage,
path = t.path.as_ref().map_or("", |s| s.as_str())
)
},
),
#[allow(clippy::match_same_arms)]
Self::ALBEvent(_) => None,
Self::LambdaFunctionUrlEvent(t) => Some(format!(
"{domain}{path}",
domain = t.request_context.domain_name,
path = t.request_context.http.path
)),
// Events that are not relevant to App & API Protection
Self::MSKEvent(_)
| Self::SqsRecord(_)
| Self::SnsRecord(_)
| Self::DynamoDbRecord(_)
| Self::S3Record(_)
| Self::EventBridgeEvent(_)
| Self::KinesisRecord(_)
| Self::StepFunctionEvent(_)
| Self::Unknown => None,
}
}
fn method(&self) -> Option<String> {
match self {
Self::APIGatewayHttpEvent(t) => Some(t.request_context.http.method.clone()),
Self::APIGatewayRestEvent(t) => Some(t.request_context.method.clone()),
Self::APIGatewayWebSocketEvent(t) => t.request_context.http_method.clone(),
Self::ALBEvent(t) => Some(t.http_method.clone()),
Self::LambdaFunctionUrlEvent(t) => Some(t.request_context.http.method.clone()),
// Events that are not relevant to App & API Protection
Self::MSKEvent(_)
| Self::SqsRecord(_)
| Self::SnsRecord(_)
| Self::DynamoDbRecord(_)
| Self::S3Record(_)
| Self::EventBridgeEvent(_)
| Self::KinesisRecord(_)
| Self::StepFunctionEvent(_)
| Self::Unknown => None,
}
}
fn route(&self) -> Option<String> {
match self {
Self::APIGatewayHttpEvent(t) => Some(t.route_key.clone()),
Self::APIGatewayRestEvent(t) => Some(format!(
"{method} {resource}",
method = t.request_context.method,
resource = t.request_context.resource_path
)),
Self::APIGatewayWebSocketEvent(t) => Some(t.request_context.route_key.clone()),
Self::ALBEvent(t) => Some(format!(
"{method} {path}",
method = t.http_method,
path = t.path.as_ref().map_or("", |s| s.as_str()),
)),
Self::LambdaFunctionUrlEvent(t) => Some(format!(
"{method} {path}",
method = t.request_context.http.method,
path = t.request_context.http.path
)),
// Events that are not relevant to App & API Protection
Self::MSKEvent(_)
| Self::SqsRecord(_)
| Self::SnsRecord(_)
| Self::DynamoDbRecord(_)
| Self::S3Record(_)
| Self::EventBridgeEvent(_)
| Self::KinesisRecord(_)
| Self::StepFunctionEvent(_)
| Self::Unknown => None,
}
}
fn client_ip(&self) -> Option<String> {
match self {
Self::APIGatewayHttpEvent(t) => Some(t.request_context.http.source_ip.clone()),
Self::APIGatewayRestEvent(t) => Some(t.request_context.identity.source_ip.clone()),
Self::APIGatewayWebSocketEvent(t) => t.request_context.identity.source_ip.clone(),
#[allow(clippy::match_same_arms)]
Self::ALBEvent(_) => None, // TODO: Can we extract from the headers instead?
Self::LambdaFunctionUrlEvent(t) => Some(t.request_context.http.source_ip.clone()),
// Events that are not relevant to App & API Protection
Self::MSKEvent(_)
| Self::SqsRecord(_)
| Self::SnsRecord(_)
| Self::DynamoDbRecord(_)
| Self::S3Record(_)
| Self::EventBridgeEvent(_)
| Self::KinesisRecord(_)
| Self::StepFunctionEvent(_)
| Self::Unknown => None,
}
}
fn request_headers_no_cookies(&self) -> HashMap<String, Vec<String>> {
fn cloned<'a, K: Clone + 'a, V: Clone + 'a>((k, v): (&'a K, &'a V)) -> (K, V) {
(k.clone(), v.clone())
}
fn as_multi<K, V>((k, v): (K, V)) -> (K, Vec<V>) {
(k, vec![v])
}
fn without_cookie<K: PartialEq<&'static str>, V>((k, _): &(K, V)) -> bool {
*k != "cookie"
}
match self {
Self::APIGatewayHttpEvent(t) => t.headers.iter().map(cloned).map(as_multi).collect(),
Self::APIGatewayRestEvent(t) => t
.multi_value_headers
.iter()
.filter(without_cookie)
.map(cloned)
.collect(),
Self::APIGatewayWebSocketEvent(t) => t
.multi_value_headers
.iter()
.filter(without_cookie)
.map(cloned)
.collect(),
Self::ALBEvent(t) => {
if t.multi_value_headers.is_empty() {
t.headers
.iter()
.filter(without_cookie)
.map(cloned)
.map(as_multi)
.collect()
} else {
t.multi_value_headers
.iter()
.filter(without_cookie)
.map(cloned)
.collect()
}
}
Self::LambdaFunctionUrlEvent(t) => t.headers.iter().map(cloned).map(as_multi).collect(),
// Events that are not relevant to App & API Protection
Self::MSKEvent(_)
| Self::SqsRecord(_)
| Self::SnsRecord(_)
| Self::DynamoDbRecord(_)
| Self::S3Record(_)
| Self::EventBridgeEvent(_)
| Self::KinesisRecord(_)
| Self::StepFunctionEvent(_)
| Self::Unknown => HashMap::default(),
}
}
fn request_cookies(&self) -> HashMap<String, Vec<String>> {
fn parse_cookie<'a>(
cookie: impl Into<Cow<'a, str>>,
) -> impl Iterator<Item = (String, String)> {
Cookie::split_parse(cookie)
.filter_map(Result::ok)
.map(|c| (c.name().to_string(), c.value().to_string()))
}
fn list_to_map(list: impl AsRef<[String]>) -> HashMap<String, Vec<String>> {
list.as_ref()
.iter()
.filter_map(|c| c.split_once('='))
.chunk_by(|(k, _)| (*k).to_string())
.into_iter()
.map(|(k, v)| (k, v.into_iter().map(|(_, v)| v.to_string()).collect()))
.collect()
}
match self {
Self::APIGatewayHttpEvent(t) => t.cookies.as_ref().map(list_to_map).unwrap_or_default(),
Self::APIGatewayRestEvent(t) => t
.multi_value_headers
.get("cookie")
.cloned()
.or_else(|| t.headers.get("cookie").map(|v| vec![v.clone()]))
.unwrap_or_default()
.iter()
.flat_map(parse_cookie)
.chunk_by(|(k, _)| (*k).clone())
.into_iter()
.map(|(k, v)| (k, v.into_iter().map(|(_, v)| v.clone()).collect()))
.collect(),
Self::APIGatewayWebSocketEvent(t) => t
.multi_value_headers
.get("cookie")
.cloned()
.unwrap_or_default()
.iter()
.flat_map(parse_cookie)
.chunk_by(|(k, _)| k.clone())
.into_iter()
.map(|(k, v)| (k, v.into_iter().map(|(_, v)| v.clone()).collect()))
.collect(),
Self::ALBEvent(t) => t
.multi_value_headers
.get("cookie")
.cloned()
.or_else(|| t.headers.get("cookie").map(|v| vec![v.clone()]))
.unwrap_or_default()
.iter()
.flat_map(parse_cookie)
.chunk_by(|(k, _)| (*k).clone())
.into_iter()
.map(|(k, v)| (k, v.into_iter().map(|(_, v)| v.clone()).collect()))
.collect(),
Self::LambdaFunctionUrlEvent(t) => {
t.cookies.as_ref().map(list_to_map).unwrap_or_default()
}
// Events that are not relevant to App & API Protection
Self::MSKEvent(_)
| Self::SqsRecord(_)
| Self::SnsRecord(_)
| Self::DynamoDbRecord(_)
| Self::S3Record(_)
| Self::EventBridgeEvent(_)
| Self::KinesisRecord(_)
| Self::StepFunctionEvent(_)
| Self::Unknown => HashMap::default(),
}
}
fn query_params(&self) -> HashMap<String, Vec<String>> {
match self {
Self::APIGatewayHttpEvent(t) => t
.query_string_parameters
.iter()
.map(|(k, v)| (k.clone(), v.split(',').map(str::to_string).collect()))
.collect(),
Self::APIGatewayRestEvent(t) => t.query_parameters.clone(),
Self::APIGatewayWebSocketEvent(t) => t.query_parameters.clone(),
Self::ALBEvent(t) => {
if t.multi_value_query_parameters.is_empty() {
t.query_parameters
.iter()
.map(|(k, v)| (k.clone(), vec![v.clone()]))
.collect()
} else {
t.multi_value_query_parameters.clone()
}
}
Self::LambdaFunctionUrlEvent(t) => t
.query_string_parameters
.iter()
.map(|(k, v)| (k.clone(), vec![v.clone()]))
.collect(),
// Events that are not relevant to App & API Protection
Self::MSKEvent(_)
| Self::SqsRecord(_)
| Self::SnsRecord(_)
| Self::DynamoDbRecord(_)
| Self::S3Record(_)
| Self::EventBridgeEvent(_)
| Self::KinesisRecord(_)
| Self::StepFunctionEvent(_)
| Self::Unknown => HashMap::default(),
}
}
fn path_params(&self) -> HashMap<String, String> {
match self {
Self::APIGatewayHttpEvent(t) => t.path_parameters.clone(),
Self::APIGatewayRestEvent(t) => t.path_parameters.clone(),
Self::APIGatewayWebSocketEvent(t) => t.path_parameters.clone(),
#[allow(clippy::match_same_arms)]
Self::ALBEvent(_) => HashMap::default(),
#[allow(clippy::match_same_arms)]
Self::LambdaFunctionUrlEvent(_) => HashMap::default(),
// Events that are not relevant to App & API Protection
Self::MSKEvent(_)
| Self::SqsRecord(_)
| Self::SnsRecord(_)
| Self::DynamoDbRecord(_)
| Self::S3Record(_)
| Self::EventBridgeEvent(_)
| Self::KinesisRecord(_)
| Self::StepFunctionEvent(_)
| Self::Unknown => HashMap::default(),
}
}
fn request_body<'a>(&'a self) -> Option<Box<dyn Read + 'a>> {
match self {
Self::APIGatewayHttpEvent(t) => t.body.reader().ok().flatten(),
Self::APIGatewayRestEvent(t) => t.body.reader().ok().flatten(),
Self::APIGatewayWebSocketEvent(t) => t.body.reader().ok().flatten(),
Self::ALBEvent(t) => t.body.reader().ok().flatten(),
Self::LambdaFunctionUrlEvent(t) => t.body.reader().ok().flatten(),
// Events that are not relevant to App & API Protection
Self::MSKEvent(_)
| Self::SqsRecord(_)
| Self::SnsRecord(_)
| Self::DynamoDbRecord(_)
| Self::S3Record(_)
| Self::EventBridgeEvent(_)
| Self::KinesisRecord(_)
| Self::StepFunctionEvent(_)
| Self::Unknown => None,
}
}
}
#[cfg_attr(coverage_nightly, coverage(off))] // Test modules skew coverage metrics
#[cfg(test)]
mod tests {
use std::io::Write;
use super::*;
#[test]
fn test_new_with_default_config() {
let config = Config {
serverless_appsec_enabled: true,
..Config::default()
};
let _ = Processor::new(&config).expect("Should not fail");
}
#[test]
fn test_new_disabled() {
let config = Config {
serverless_appsec_enabled: false, // Explicitly testing this condition
..Config::default()
};
assert!(matches!(
Processor::new(&config),
Err(Error::FeatureDisabled)
));
}
#[test]
fn test_new_with_invalid_config() {
let tmp = tempfile::NamedTempFile::new().expect("Failed to create tempfile");
let config = Config {
serverless_appsec_enabled: true,
appsec_rules: Some(
tmp.path()
.to_str()
.expect("Failed to get tempfile path")
.to_string(),
),
..Config::default()
};
assert!(matches!(
Processor::new(&config),
Err(Error::WafRulesetParseError(_))
));
}
#[test]
fn test_new_with_no_rules_or_processors() {
let mut tmp = tempfile::NamedTempFile::new().expect("Failed to create tempfile");
tmp.write_all(
br#"{
"version": "2.2",
"metadata":{
"ruleset_version": "0.0.0-blank"
},
"scanners":[{
"id": "406f8606-52c4-4663-8db9-df70f9e8766c",
"name": "ZIP Code",
"key": {
"operator": "match_regex",
"parameters": {
"regex": "\\b(?:zip|postal)\\b",
"options": {
"case_sensitive": false,
"min_length": 3
}
}
},
"value": {
"operator": "match_regex",
"parameters": {
"regex": "^[0-9]{5}(?:-[0-9]{4})?$",
"options": {
"case_sensitive": true,
"min_length": 5
}
}
},
"tags": {
"type": "zipcode",
"category": "address"
}
}]
}"#,
)
.expect("Failed to write to temp file");
tmp.flush().expect("Failed to flush temp file");
let config = Config {
serverless_appsec_enabled: true,
appsec_rules: Some(
tmp.path()
.to_str()
.expect("Failed to get tempfile path")
.to_string(),
),
..Config::default()
};
let result = Processor::new(&config);
assert!(
matches!(
result,
Err(Error::WafInitializationFailed), // There is no rule nor processor in the ruleset
),
concat!(
"should have failed with ",
stringify!(Error::WafCreationFailed),
" but was {:?}"
),
result
);
}
#[test]
fn service_entry_span_mut_skips_placeholder_lambda_span() {
let mut trace = vec![
Span {
name: "aws.lambda".into(),
resource: crate::traces::INVOCATION_SPAN_RESOURCE.into(),
span_id: 1,
..Default::default()
},
Span {
name: "aws.lambda".into(),
resource: "real.lambda.invocation".into(),
span_id: 2,
..Default::default()
},
];
let selected = Processor::service_entry_span_mut(&mut trace)
.expect("expected non-placeholder aws.lambda span");
assert_eq!(selected.name, "aws.lambda");
assert_ne!(selected.resource, crate::traces::INVOCATION_SPAN_RESOURCE);
assert_eq!(selected.span_id, 2);
}
#[test]
fn service_entry_span_mut_returns_none_for_only_placeholder() {
let mut trace = vec![Span {
name: "aws.lambda".into(),
resource: crate::traces::INVOCATION_SPAN_RESOURCE.into(),
span_id: 1,
..Default::default()
}];
assert!(Processor::service_entry_span_mut(&mut trace).is_none());
}
}