minco_aws_adapters/
lib.rs1#![forbid(unsafe_code)]
3
4#[cfg(feature = "appsync-events")]
5pub mod appsync_events;
6#[cfg(feature = "cognito")]
7pub mod cognito;
8pub mod iam;
9#[cfg(all(feature = "jobs", feature = "sqs"))]
10pub mod jobs_sqs;
11#[cfg(any(feature = "ses", feature = "webhook"))]
12pub mod notification;
13pub mod plugin;
14#[cfg(feature = "s3")]
15pub mod s3;
16#[cfg(feature = "s3")]
17pub mod s3_storage;
18#[cfg(feature = "ses")]
19pub mod ses;
20#[cfg(feature = "sqs")]
21pub mod sqs;
22#[cfg(feature = "static-site")]
23pub mod static_site;
24#[cfg(feature = "webhook")]
25pub mod webhook;
26
27#[derive(Debug, thiserror::Error)]
28pub enum AwsAdapterError {
29 #[error("adapter configuration is invalid: {0}")]
30 InvalidConfiguration(String),
31 #[error("AWS provider request failed: {0}")]
32 Provider(String),
33 #[error("provider response was incomplete: {0}")]
34 IncompleteResponse(String),
35 #[error("adapter serialization failed: {0}")]
36 Serialization(#[from] serde_json::Error),
37}
38
39#[cfg(any(feature = "s3", feature = "sqs"))]
40pub(crate) fn validated_service_uri(value: &str) -> Option<http::Uri> {
41 let uri = value.parse::<http::Uri>().ok()?;
42 let authority = uri.authority()?;
43 let host = uri.host()?;
44 if authority.as_str().contains('@')
45 || uri.query().is_some()
46 || !valid_uri_host(host)
47 || !matches!(uri.scheme_str(), Some("https" | "http"))
48 || (uri.scheme_str() == Some("http") && !is_loopback_host(host))
49 {
50 return None;
51 }
52 Some(uri)
53}
54
55#[cfg(any(feature = "s3", feature = "sqs"))]
56fn is_loopback_host(host: &str) -> bool {
57 let host = host
58 .strip_prefix('[')
59 .and_then(|value| value.strip_suffix(']'))
60 .unwrap_or(host);
61 host.eq_ignore_ascii_case("localhost")
62 || host
63 .parse::<std::net::IpAddr>()
64 .is_ok_and(|address| address.is_loopback())
65}
66
67#[cfg(any(feature = "s3", feature = "sqs"))]
68fn valid_uri_host(host: &str) -> bool {
69 let unbracketed = host
70 .strip_prefix('[')
71 .and_then(|value| value.strip_suffix(']'))
72 .unwrap_or(host);
73 if unbracketed.parse::<std::net::IpAddr>().is_ok() {
74 return true;
75 }
76 !unbracketed.is_empty()
77 && unbracketed.len() <= 253
78 && unbracketed.split('.').all(|label| {
79 !label.is_empty()
80 && label.len() <= 63
81 && !label.starts_with('-')
82 && !label.ends_with('-')
83 && label
84 .bytes()
85 .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
86 })
87}
88
89#[cfg(feature = "s3")]
90pub(crate) fn provider_error(context: &str, error: &(dyn std::error::Error + 'static)) -> String {
91 let mut parts = vec![context.to_owned()];
92 let mut current = Some(error);
93 while let Some(source) = current {
94 let message = source
95 .to_string()
96 .chars()
97 .map(|character| {
98 if character.is_control() {
99 ' '
100 } else {
101 character
102 }
103 })
104 .collect::<String>();
105 if !message.is_empty() && parts.last() != Some(&message) {
106 parts.push(message);
107 }
108 current = source.source();
109 if parts.len() == 6 {
110 break;
111 }
112 }
113 let mut message = parts.join(": ");
114 if message.len() > 2048 {
115 let mut boundary = 2045;
116 while !message.is_char_boundary(boundary) {
117 boundary -= 1;
118 }
119 message.truncate(boundary);
120 message.push_str("...");
121 }
122 message
123}
124
125#[cfg(all(test, feature = "s3"))]
126mod diagnostic_tests {
127 use super::*;
128
129 #[cfg(feature = "s3")]
130 #[test]
131 fn provider_errors_preserve_bounded_source_context_without_controls() {
132 let source = std::io::Error::other("connector\nfailed");
133 let message = provider_error("S3 PutObject", &source);
134 assert_eq!(message, "S3 PutObject: connector failed");
135 assert!(message.len() <= 2048);
136 }
137
138 #[cfg(feature = "s3")]
139 #[test]
140 fn provider_error_truncates_unicode_only_at_character_boundaries() {
141 let source = std::io::Error::other("é".repeat(2000));
142 let message = provider_error("provider", &source);
143 assert!(message.len() <= 2048);
144 assert!(message.ends_with("..."));
145 }
146
147 #[cfg(feature = "s3")]
148 #[test]
149 fn service_uri_validation_rejects_userinfo_queries_and_remote_plaintext() {
150 assert!(validated_service_uri("https://sqs.ap-southeast-2.amazonaws.com/queue").is_some());
151 assert!(validated_service_uri("http://127.0.0.1:4566").is_some());
152 assert!(validated_service_uri("https://user@example.com").is_none());
153 assert!(validated_service_uri("https://example.com/path?token=value").is_none());
154 assert!(validated_service_uri("http://example.com").is_none());
155 }
156}