|
|
@ -151,16 +151,13 @@ pub struct Cors(); |
|
|
|
|
|
|
|
|
impl Cors { |
|
|
impl Cors { |
|
|
fn get_header(headers: &HeaderMap<'_>, name: &str) -> String { |
|
|
fn get_header(headers: &HeaderMap<'_>, name: &str) -> String { |
|
|
match headers.get_one(name) { |
|
|
headers.get_one(name).map_or_default(ToOwned::to_owned) |
|
|
Some(h) => h.to_owned(), |
|
|
|
|
|
_ => String::new(), |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Check a request's `Origin` header against the list of allowed origins.
|
|
|
// Check a request's `Origin` header against the list of allowed origins.
|
|
|
// If a match exists, return it. Otherwise, return None.
|
|
|
// If a match exists, return it. Otherwise, return None.
|
|
|
fn get_allowed_origin(headers: &HeaderMap<'_>) -> Option<String> { |
|
|
fn get_allowed_origin(headers: &HeaderMap<'_>) -> Option<String> { |
|
|
let origin = Cors::get_header(headers, "Origin"); |
|
|
let origin = Self::get_header(headers, "Origin"); |
|
|
let safari_extension_origin = "file://"; |
|
|
let safari_extension_origin = "file://"; |
|
|
let desktop_custom_file_origin = "bw-desktop-file://bundle"; |
|
|
let desktop_custom_file_origin = "bw-desktop-file://bundle"; |
|
|
|
|
|
|
|
|
@ -188,14 +185,14 @@ impl Fairing for Cors { |
|
|
async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response<'r>) { |
|
|
async fn on_response<'r>(&self, request: &'r Request<'_>, response: &mut Response<'r>) { |
|
|
let req_headers = request.headers(); |
|
|
let req_headers = request.headers(); |
|
|
|
|
|
|
|
|
if let Some(origin) = Cors::get_allowed_origin(req_headers) { |
|
|
if let Some(origin) = Self::get_allowed_origin(req_headers) { |
|
|
response.set_header(Header::new("Access-Control-Allow-Origin", origin)); |
|
|
response.set_header(Header::new("Access-Control-Allow-Origin", origin)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Preflight request
|
|
|
// Preflight request
|
|
|
if request.method() == Method::Options { |
|
|
if request.method() == Method::Options { |
|
|
let req_allow_headers = Cors::get_header(req_headers, "Access-Control-Request-Headers"); |
|
|
let req_allow_headers = Self::get_header(req_headers, "Access-Control-Request-Headers"); |
|
|
let req_allow_method = Cors::get_header(req_headers, "Access-Control-Request-Method"); |
|
|
let req_allow_method = Self::get_header(req_headers, "Access-Control-Request-Method"); |
|
|
|
|
|
|
|
|
response.set_header(Header::new("Access-Control-Allow-Methods", req_allow_method)); |
|
|
response.set_header(Header::new("Access-Control-Allow-Methods", req_allow_method)); |
|
|
response.set_header(Header::new("Access-Control-Allow-Headers", req_allow_headers)); |
|
|
response.set_header(Header::new("Access-Control-Allow-Headers", req_allow_headers)); |
|
|
@ -214,7 +211,7 @@ pub struct Cached<R> { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
impl<R> Cached<R> { |
|
|
impl<R> Cached<R> { |
|
|
pub fn long(response: R, is_immutable: bool) -> Cached<R> { |
|
|
pub fn long(response: R, is_immutable: bool) -> Self { |
|
|
Self { |
|
|
Self { |
|
|
response, |
|
|
response, |
|
|
is_immutable, |
|
|
is_immutable, |
|
|
@ -222,7 +219,7 @@ impl<R> Cached<R> { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn short(response: R, is_immutable: bool) -> Cached<R> { |
|
|
pub fn short(response: R, is_immutable: bool) -> Self { |
|
|
Self { |
|
|
Self { |
|
|
response, |
|
|
response, |
|
|
is_immutable, |
|
|
is_immutable, |
|
|
@ -230,7 +227,7 @@ impl<R> Cached<R> { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn ttl(response: R, ttl: u32, is_immutable: bool) -> Cached<R> { |
|
|
pub fn ttl(response: R, ttl: u32, is_immutable: bool) -> Self { |
|
|
Self { |
|
|
Self { |
|
|
response, |
|
|
response, |
|
|
is_immutable, |
|
|
is_immutable, |
|
|
@ -447,19 +444,19 @@ pub fn get_uuid() -> String { |
|
|
#[inline] |
|
|
#[inline] |
|
|
pub fn upcase_first(s: &str) -> String { |
|
|
pub fn upcase_first(s: &str) -> String { |
|
|
let mut c = s.chars(); |
|
|
let mut c = s.chars(); |
|
|
match c.next() { |
|
|
c.next().map_or_default(|f| f.to_uppercase().collect::<String>() + c.as_str()) |
|
|
None => String::new(), |
|
|
|
|
|
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(), |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#[inline] |
|
|
#[inline] |
|
|
pub fn lcase_first(s: &str) -> String { |
|
|
pub fn lcase_first(mut s: String) -> String { |
|
|
let mut c = s.chars(); |
|
|
// get_mut(0..1) is None if empty or if the first char is multi-byte
|
|
|
match c.next() { |
|
|
if let Some(first) = s.get_mut(0..1) { |
|
|
None => String::new(), |
|
|
first.make_ascii_lowercase(); |
|
|
Some(f) => f.to_lowercase().collect::<String>() + c.as_str(), |
|
|
} else if let Some(c) = s.chars().next() { |
|
|
|
|
|
let lc: String = c.to_lowercase().collect(); |
|
|
|
|
|
s.replace_range(..c.len_utf8(), &lc); |
|
|
} |
|
|
} |
|
|
|
|
|
s |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub fn try_parse_string<S, T>(string: Option<S>) -> Option<T> |
|
|
pub fn try_parse_string<S, T>(string: Option<S>) -> Option<T> |
|
|
@ -467,11 +464,7 @@ where |
|
|
S: AsRef<str>, |
|
|
S: AsRef<str>, |
|
|
T: FromStr, |
|
|
T: FromStr, |
|
|
{ |
|
|
{ |
|
|
if let Some(Ok(value)) = string.map(|s| s.as_ref().parse::<T>()) { |
|
|
string.and_then(|s| s.as_ref().parse::<T>().ok()) |
|
|
Some(value) |
|
|
|
|
|
} else { |
|
|
|
|
|
None |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//
|
|
|
//
|
|
|
@ -513,15 +506,15 @@ pub fn get_env_bool(key: &'static str) -> Option<bool> { |
|
|
const TRUE_VALUES: &[&str] = &["true", "t", "yes", "y", "1"]; |
|
|
const TRUE_VALUES: &[&str] = &["true", "t", "yes", "y", "1"]; |
|
|
const FALSE_VALUES: &[&str] = &["false", "f", "no", "n", "0"]; |
|
|
const FALSE_VALUES: &[&str] = &["false", "f", "no", "n", "0"]; |
|
|
|
|
|
|
|
|
match get_env_str_value(key) { |
|
|
let val = get_env_str_value(key)?; |
|
|
Some(val) if TRUE_VALUES.contains(&val.to_lowercase().as_ref()) => Some(true), |
|
|
if TRUE_VALUES.iter().any(|v| val.eq_ignore_ascii_case(v)) { |
|
|
Some(val) if FALSE_VALUES.contains(&val.to_lowercase().as_ref()) => Some(false), |
|
|
Some(true) |
|
|
Some(_) => { |
|
|
} else if FALSE_VALUES.iter().any(|v| val.eq_ignore_ascii_case(v)) { |
|
|
println!("[ERROR] `{key}` contains an invalid boolean value and is ignored, the default is used instead."); |
|
|
Some(false) |
|
|
INVALID_ENV_VARS.lock().unwrap().push(key); |
|
|
} else { |
|
|
None |
|
|
println!("[ERROR] `{key}` contains an invalid boolean value and is ignored, the default is used instead."); |
|
|
} |
|
|
INVALID_ENV_VARS.lock().unwrap().push(key); |
|
|
_ => None, |
|
|
None |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -715,10 +708,12 @@ impl<'de> Visitor<'de> for LowerCaseVisitor { |
|
|
|
|
|
|
|
|
// Inner function to handle a special case for the 'ssn' key.
|
|
|
// Inner function to handle a special case for the 'ssn' key.
|
|
|
// This key is part of the Identity Cipher (Social Security Number)
|
|
|
// This key is part of the Identity Cipher (Social Security Number)
|
|
|
fn process_json_key(key: &str) -> String { |
|
|
fn process_json_key(mut key: String) -> String { |
|
|
match key.to_lowercase().as_ref() { |
|
|
if key.eq_ignore_ascii_case("ssn") { |
|
|
"ssn" => "ssn".into(), |
|
|
key.make_ascii_lowercase(); |
|
|
_ => lcase_first(key), |
|
|
key |
|
|
|
|
|
} else { |
|
|
|
|
|
lcase_first(key) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -747,8 +742,8 @@ pub enum NumberOrString { |
|
|
impl NumberOrString { |
|
|
impl NumberOrString { |
|
|
pub fn into_string(self) -> String { |
|
|
pub fn into_string(self) -> String { |
|
|
match self { |
|
|
match self { |
|
|
NumberOrString::Number(n) => n.to_string(), |
|
|
Self::Number(n) => n.to_string(), |
|
|
NumberOrString::String(s) => s, |
|
|
Self::String(s) => s, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -756,16 +751,14 @@ impl NumberOrString { |
|
|
pub fn into_i32(&self) -> Result<i32, crate::Error> { |
|
|
pub fn into_i32(&self) -> Result<i32, crate::Error> { |
|
|
use std::num::ParseIntError as PIE; |
|
|
use std::num::ParseIntError as PIE; |
|
|
match self { |
|
|
match self { |
|
|
NumberOrString::Number(n) => { |
|
|
Self::Number(n) => { |
|
|
if let Some(n) = n.to_i32() { |
|
|
if let Ok(n) = i32::try_from(*n) { |
|
|
Ok(n) |
|
|
Ok(n) |
|
|
} else { |
|
|
} else { |
|
|
err!("Number does not fit in i32") |
|
|
err!("Number does not fit in i32") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
NumberOrString::String(s) => { |
|
|
Self::String(s) => s.parse().map_err(|e: PIE| crate::Error::new("Can't convert to number", e.to_string())), |
|
|
s.parse().map_err(|e: PIE| crate::Error::new("Can't convert to number", e.to_string())) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -773,10 +766,8 @@ impl NumberOrString { |
|
|
pub fn into_i64(&self) -> Result<i64, crate::Error> { |
|
|
pub fn into_i64(&self) -> Result<i64, crate::Error> { |
|
|
use std::num::ParseIntError as PIE; |
|
|
use std::num::ParseIntError as PIE; |
|
|
match self { |
|
|
match self { |
|
|
NumberOrString::Number(n) => Ok(*n), |
|
|
Self::Number(n) => Ok(*n), |
|
|
NumberOrString::String(s) => { |
|
|
Self::String(s) => s.parse().map_err(|e: PIE| crate::Error::new("Can't convert to number", e.to_string())), |
|
|
s.parse().map_err(|e: PIE| crate::Error::new("Can't convert to number", e.to_string())) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -833,50 +824,25 @@ where |
|
|
|
|
|
|
|
|
pub fn convert_json_key_lcase_first(src_json: Value) -> Value { |
|
|
pub fn convert_json_key_lcase_first(src_json: Value) -> Value { |
|
|
match src_json { |
|
|
match src_json { |
|
|
Value::Array(elm) => { |
|
|
Value::Array(mut elm) => { |
|
|
let mut new_array: Vec<Value> = Vec::with_capacity(elm.len()); |
|
|
for obj in &mut elm { |
|
|
|
|
|
*obj = convert_json_key_lcase_first(obj.take()); |
|
|
for obj in elm { |
|
|
|
|
|
new_array.push(convert_json_key_lcase_first(obj)); |
|
|
|
|
|
} |
|
|
} |
|
|
Value::Array(new_array) |
|
|
Value::Array(elm) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Value::Object(obj) => { |
|
|
Value::Object(obj) => { |
|
|
let mut json_map = JsonMap::new(); |
|
|
let mut json_map = JsonMap::new(); |
|
|
for (key, value) in obj { |
|
|
for (key, value) in obj { |
|
|
match (key, value) { |
|
|
json_map.insert(process_json_key(key), convert_json_key_lcase_first(value)); |
|
|
(key, Value::Object(elm)) => { |
|
|
|
|
|
let inner_value = convert_json_key_lcase_first(Value::Object(elm)); |
|
|
|
|
|
json_map.insert(process_json_key(&key), inner_value); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
(key, Value::Array(elm)) => { |
|
|
|
|
|
let mut inner_array: Vec<Value> = Vec::with_capacity(elm.len()); |
|
|
|
|
|
|
|
|
|
|
|
for inner_obj in elm { |
|
|
|
|
|
inner_array.push(convert_json_key_lcase_first(inner_obj)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
json_map.insert(process_json_key(&key), Value::Array(inner_array)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
(key, value) => { |
|
|
|
|
|
json_map.insert(process_json_key(&key), value); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Value::Object(json_map) |
|
|
Value::Object(json_map) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
value => value, |
|
|
value => value, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pub enum FeatureFlagFilter { |
|
|
pub enum FeatureFlagFilter { |
|
|
#[allow(dead_code)] |
|
|
|
|
|
Unfiltered, |
|
|
|
|
|
ValidOnly, |
|
|
ValidOnly, |
|
|
InvalidOnly, |
|
|
InvalidOnly, |
|
|
} |
|
|
} |
|
|
@ -891,7 +857,6 @@ pub fn parse_experimental_client_feature_flags( |
|
|
.map(str::trim) |
|
|
.map(str::trim) |
|
|
.filter(|flag| !flag.is_empty()) |
|
|
.filter(|flag| !flag.is_empty()) |
|
|
.filter(|flag| match filter_mode { |
|
|
.filter(|flag| match filter_mode { |
|
|
FeatureFlagFilter::Unfiltered => true, |
|
|
|
|
|
FeatureFlagFilter::ValidOnly => SUPPORTED_FEATURE_FLAGS.contains(flag), |
|
|
FeatureFlagFilter::ValidOnly => SUPPORTED_FEATURE_FLAGS.contains(flag), |
|
|
FeatureFlagFilter::InvalidOnly => !SUPPORTED_FEATURE_FLAGS.contains(flag), |
|
|
FeatureFlagFilter::InvalidOnly => !SUPPORTED_FEATURE_FLAGS.contains(flag), |
|
|
}) |
|
|
}) |
|
|
@ -911,9 +876,9 @@ pub fn is_client_feature_flag_enabled(flag: &str) -> bool { |
|
|
// #[expect(clippy::nonminimal_bool, reason = "Mostly copy/paste from std, keep as-is")]
|
|
|
// #[expect(clippy::nonminimal_bool, reason = "Mostly copy/paste from std, keep as-is")]
|
|
|
#[expect(clippy::decimal_bitwise_operands, reason = "Mostly copy/paste from std, keep as-is")] |
|
|
#[expect(clippy::decimal_bitwise_operands, reason = "Mostly copy/paste from std, keep as-is")] |
|
|
#[cfg(any(not(feature = "unstable"), test))] |
|
|
#[cfg(any(not(feature = "unstable"), test))] |
|
|
pub fn is_global_hardcoded(ip: std::net::IpAddr) -> bool { |
|
|
pub fn is_global_hardcoded(ip: core::net::IpAddr) -> bool { |
|
|
match ip { |
|
|
match ip { |
|
|
std::net::IpAddr::V4(ip) => { |
|
|
core::net::IpAddr::V4(ip) => { |
|
|
!(ip.octets()[0] == 0 // "This network"
|
|
|
!(ip.octets()[0] == 0 // "This network"
|
|
|
|| ip.is_private() |
|
|
|| ip.is_private() |
|
|
|| (ip.octets()[0] == 100 && (ip.octets()[1] & 0b1100_0000 == 0b0100_0000)) // ip.is_shared()
|
|
|
|| (ip.octets()[0] == 100 && (ip.octets()[1] & 0b1100_0000 == 0b0100_0000)) // ip.is_shared()
|
|
|
@ -925,12 +890,14 @@ pub fn is_global_hardcoded(ip: std::net::IpAddr) -> bool { |
|
|
ip.octets()[0] == 192 && ip.octets()[1] == 0 && ip.octets()[2] == 0 |
|
|
ip.octets()[0] == 192 && ip.octets()[1] == 0 && ip.octets()[2] == 0 |
|
|
&& ip.octets()[3] != 9 && ip.octets()[3] != 10 |
|
|
&& ip.octets()[3] != 9 && ip.octets()[3] != 10 |
|
|
) |
|
|
) |
|
|
|
|
|
// 6a44-relay anycast address (`192.88.99.2/32`)
|
|
|
|
|
|
|| matches!(ip.octets(), [192, 88, 99, 2]) |
|
|
|| ip.is_documentation() |
|
|
|| ip.is_documentation() |
|
|
|| (ip.octets()[0] == 198 && (ip.octets()[1] & 0xfe) == 18) // ip.is_benchmarking()
|
|
|
|| (ip.octets()[0] == 198 && (ip.octets()[1] & 0xfe) == 18) // ip.is_benchmarking()
|
|
|
|| (ip.octets()[0] & 240 == 240 && !ip.is_broadcast()) // ip.is_reserved()
|
|
|
|| (ip.octets()[0] & 240 == 240 && !ip.is_broadcast()) // ip.is_reserved()
|
|
|
|| ip.is_broadcast()) |
|
|
|| ip.is_broadcast()) |
|
|
} |
|
|
} |
|
|
std::net::IpAddr::V6(ip) => { |
|
|
core::net::IpAddr::V6(ip) => { |
|
|
!(ip.is_unspecified() |
|
|
!(ip.is_unspecified() |
|
|
|| ip.is_loopback() |
|
|
|| ip.is_loopback() |
|
|
// IPv4-mapped Address (`::ffff:0:0/96`)
|
|
|
// IPv4-mapped Address (`::ffff:0:0/96`)
|
|
|
@ -939,6 +906,8 @@ pub fn is_global_hardcoded(ip: std::net::IpAddr) -> bool { |
|
|
|| matches!(ip.segments(), [0x64, 0xff9b, 1, _, _, _, _, _]) |
|
|
|| matches!(ip.segments(), [0x64, 0xff9b, 1, _, _, _, _, _]) |
|
|
// Discard-Only Address Block (`100::/64`)
|
|
|
// Discard-Only Address Block (`100::/64`)
|
|
|
|| matches!(ip.segments(), [0x100, 0, 0, 0, _, _, _, _]) |
|
|
|| matches!(ip.segments(), [0x100, 0, 0, 0, _, _, _, _]) |
|
|
|
|
|
// Dummy IPv6 Prefix (100:0:0:1::/64)
|
|
|
|
|
|
|| matches!(ip.segments(), [0x100, 0, 0, 1, _, _, _, _]) |
|
|
// IETF Protocol Assignments (`2001::/23`)
|
|
|
// IETF Protocol Assignments (`2001::/23`)
|
|
|
|| (matches!(ip.segments(), [0x2001, b, _, _, _, _, _, _] if b < 0x200) |
|
|
|| (matches!(ip.segments(), [0x2001, b, _, _, _, _, _, _] if b < 0x200) |
|
|
&& !( |
|
|
&& !( |
|
|
@ -946,6 +915,8 @@ pub fn is_global_hardcoded(ip: std::net::IpAddr) -> bool { |
|
|
u128::from_be_bytes(ip.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0001 |
|
|
u128::from_be_bytes(ip.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0001 |
|
|
// Traversal Using Relays around NAT Anycast (`2001:1::2`)
|
|
|
// Traversal Using Relays around NAT Anycast (`2001:1::2`)
|
|
|
|| u128::from_be_bytes(ip.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0002 |
|
|
|| u128::from_be_bytes(ip.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0002 |
|
|
|
|
|
// DNS-SD Service Registration Protocol Anycast (`2001:1::3`)
|
|
|
|
|
|
|| u128::from_be_bytes(ip.octets()) == 0x2001_0001_0000_0000_0000_0000_0000_0003 |
|
|
// AMT (`2001:3::/32`)
|
|
|
// AMT (`2001:3::/32`)
|
|
|
|| matches!(ip.segments(), [0x2001, 3, _, _, _, _, _, _]) |
|
|
|| matches!(ip.segments(), [0x2001, 3, _, _, _, _, _, _]) |
|
|
// AS112-v6 (`2001:4:112::/48`)
|
|
|
// AS112-v6 (`2001:4:112::/48`)
|
|
|
@ -1027,11 +998,11 @@ mod tests { |
|
|
#[test] |
|
|
#[test] |
|
|
#[ignore = "exhaustive IPv4 check is too slow for the regular test suite"] |
|
|
#[ignore = "exhaustive IPv4 check is too slow for the regular test suite"] |
|
|
fn test_ipv4_global() { |
|
|
fn test_ipv4_global() { |
|
|
for a in 0..u8::MAX { |
|
|
for a in 0..=u8::MAX { |
|
|
println!("Iter: {a}/255"); |
|
|
println!("Iter: {a}/255"); |
|
|
for b in 0..u8::MAX { |
|
|
for b in 0..=u8::MAX { |
|
|
for c in 0..u8::MAX { |
|
|
for c in 0..=u8::MAX { |
|
|
for d in 0..u8::MAX { |
|
|
for d in 0..=u8::MAX { |
|
|
let ip = IpAddr::V4(std::net::Ipv4Addr::new(a, b, c, d)); |
|
|
let ip = IpAddr::V4(std::net::Ipv4Addr::new(a, b, c, d)); |
|
|
assert_eq!(ip.is_global(), is_global_hardcoded(ip), "IP mismatch: {ip}"); |
|
|
assert_eq!(ip.is_global(), is_global_hardcoded(ip), "IP mismatch: {ip}"); |
|
|
} |
|
|
} |
|
|
|