use crate::constants::DaoKeyPrefix;
use crate::error::{GarrisonError, GarrisonResult};
use async_trait::async_trait;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SamlAssertion {
#[serde(default)]
pub id: String,
pub issuer: String,
pub subject: String,
pub audience: String,
pub not_on_or_after: String,
#[serde(default)]
pub not_before: String,
pub attributes: Vec<(String, String)>,
#[serde(skip_serializing, skip_deserializing, default)]
pub raw_xml: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SamlResponse {
pub destination: String,
pub issuer: String,
pub assertion: Option<SamlAssertion>,
pub status_code: String,
#[serde(default)]
pub in_response_to: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SamlRequest {
pub id: String,
pub issue_instant: String,
pub destination: String,
pub issuer: String,
pub assertion_consumer_service_url: String,
}
#[async_trait]
pub trait SamlProvider: Send + Sync {
async fn build_authn_request(
&self,
sp_entity_id: &str,
acs_url: &str,
idp_sso_endpoint: &str,
) -> GarrisonResult<SamlRequest>;
async fn parse_response(&self, response_xml: &str) -> GarrisonResult<SamlResponse>;
async fn validate_assertion(&self, assertion: &SamlAssertion) -> GarrisonResult<bool>;
}
pub struct DefaultSamlProvider {
expected_destination: Option<String>,
expected_audience: Option<String>,
request_dao: Option<std::sync::Arc<dyn crate::dao::GarrisonDao>>,
warned_no_dao: std::sync::atomic::AtomicBool,
}
impl DefaultSamlProvider {
pub fn new() -> GarrisonResult<Self> {
Ok(Self {
expected_destination: None,
expected_audience: None,
request_dao: None,
warned_no_dao: std::sync::atomic::AtomicBool::new(false),
})
}
pub fn with_request_dao(mut self, dao: std::sync::Arc<dyn crate::dao::GarrisonDao>) -> Self {
self.request_dao = Some(dao);
self
}
pub fn with_expected_destination(mut self, destination: String) -> Self {
self.expected_destination = Some(destination);
self
}
pub fn with_expected_audience(mut self, audience: String) -> Self {
self.expected_audience = Some(audience);
self
}
}
impl Default for DefaultSamlProvider {
fn default() -> Self {
Self::new().expect("DefaultSamlProvider::new should not fail")
}
}
#[async_trait]
impl SamlProvider for DefaultSamlProvider {
async fn build_authn_request(
&self,
sp_entity_id: &str,
acs_url: &str,
idp_sso_endpoint: &str,
) -> GarrisonResult<SamlRequest> {
let request = SamlRequest {
id: Uuid::new_v4().to_string(),
issue_instant: Utc::now().to_rfc3339(),
destination: idp_sso_endpoint.to_string(),
issuer: sp_entity_id.to_string(),
assertion_consumer_service_url: acs_url.to_string(),
};
if let Some(d) = &self.request_dao {
let key = format!("{}req:{}", DaoKeyPrefix::Saml, request.id);
d.set(&key, "1", 600).await?;
}
Ok(request)
}
async fn parse_response(&self, response_xml: &str) -> GarrisonResult<SamlResponse> {
const SAML_RESPONSE_MAX_SIZE: usize = 64 * 1024;
if response_xml.len() > SAML_RESPONSE_MAX_SIZE {
return Err(GarrisonError::InvalidParam(format!(
"saml-response-too-large::{}::{}",
response_xml.len(),
SAML_RESPONSE_MAX_SIZE
)));
}
let mut response = parse_saml_response_xml(response_xml)?;
validate_status_code(&response.status_code)?;
enforce_in_response_to(
self.request_dao.as_ref(),
&self.warned_no_dao,
&response.in_response_to,
)
.await?;
validate_destination(
&response.destination,
self.expected_destination.as_deref(),
true,
)?;
if let Some(ref assertion) = response.assertion {
validate_audience(&assertion.audience, self.expected_audience.as_deref(), true)?;
}
if let Some(ref assertion) = response.assertion {
match self.validate_assertion(assertion).await {
Ok(true) => {
if let Some(d) = &self.request_dao {
if !check_assertion_replay(
&assertion.id,
&assertion.not_on_or_after,
d.as_ref(),
)
.await?
{
return Err(GarrisonError::InvalidParam(format!(
"sso-saml-assertion-replay::{}",
assertion.id
)));
}
}
},
Ok(false) => {
tracing::warn!("SAML Assertion signature verification failed, stripped");
response.assertion = None;
},
Err(GarrisonError::NotImplemented(_)) => {
tracing::warn!("SAML signature verification not implemented, Assertion stripped (fail-closed)");
response.assertion = None;
},
Err(e) => return Err(e),
}
}
Ok(response)
}
async fn validate_assertion(&self, _assertion: &SamlAssertion) -> GarrisonResult<bool> {
Err(GarrisonError::NotImplemented(
"sso-saml-signature-not-implemented::".to_string(),
))
}
}
fn validate_status_code(status_code: &str) -> GarrisonResult<()> {
const STATUS_SUCCESS: &str = "urn:oasis:names:tc:SAML:2.0:status:Success";
if status_code != STATUS_SUCCESS {
return Err(GarrisonError::InvalidParam(format!(
"sso-saml-status-not-success::{}",
status_code
)));
}
Ok(())
}
fn validate_destination(
actual: &str,
expected: Option<&str>,
allow_unvalidated: bool,
) -> GarrisonResult<()> {
match expected {
Some(exp) if !exp.is_empty() => {
if actual != exp {
return Err(GarrisonError::InvalidParam(format!(
"sso-saml-destination-mismatch::expected={}::actual={}",
exp, actual
)));
}
Ok(())
},
_ if allow_unvalidated => {
if !actual.is_empty() {
tracing::warn!(
actual = %actual,
"SAML Destination validation opted out (allow_unvalidated_destination), open redirect attack risk"
);
}
Ok(())
},
_ => Err(GarrisonError::InvalidParam(format!(
"sso-saml-destination-not-configured::actual={}",
actual
))),
}
}
fn validate_audience(
actual: &str,
expected: Option<&str>,
allow_unvalidated: bool,
) -> GarrisonResult<()> {
match expected {
Some(exp) if !exp.is_empty() => {
if actual != exp {
return Err(GarrisonError::InvalidParam(format!(
"sso-saml-audience-mismatch::expected={}::actual={}",
exp, actual
)));
}
Ok(())
},
_ if allow_unvalidated => {
if !actual.is_empty() {
tracing::warn!(
actual = %actual,
"SAML Audience validation opted out (allow_unvalidated_audience), cross-SP replay risk"
);
}
Ok(())
},
_ => Err(GarrisonError::InvalidParam(format!(
"sso-saml-audience-not-configured::actual={}",
actual
))),
}
}
struct SamlParseContext {
destination: String,
issuer: String,
status_code: String,
in_response_to: String,
assertion: Option<SamlAssertion>,
in_assertion: bool,
in_issuer: bool,
in_subject: bool,
in_audience: bool,
in_attribute: bool,
current_attr_name: String,
current_text: String,
assertion_issuer: String,
assertion_subject: String,
assertion_audience: String,
assertion_not_on_or_after: String,
assertion_not_before: String,
assertion_id: String,
assertion_attributes: Vec<(String, String)>,
assertion_start_pos: Option<u64>,
assertion_raw_xml: Option<String>,
}
impl SamlParseContext {
fn new() -> Self {
Self {
destination: String::new(),
issuer: String::new(),
status_code: String::new(),
in_response_to: String::new(),
assertion: None,
in_assertion: false,
in_issuer: false,
in_subject: false,
in_audience: false,
in_attribute: false,
current_attr_name: String::new(),
current_text: String::new(),
assertion_issuer: String::new(),
assertion_subject: String::new(),
assertion_audience: String::new(),
assertion_not_on_or_after: String::new(),
assertion_not_before: String::new(),
assertion_id: String::new(),
assertion_attributes: Vec::new(),
assertion_start_pos: None,
assertion_raw_xml: None,
}
}
fn handle_start_element(&mut self, event: &quick_xml::events::BytesStart, pos_before: u64) {
if !check_saml_namespace(event.name().as_ref()) {
return;
}
let local_name = extract_local_name(event.name().as_ref());
match local_name.as_str() {
"Response" => {
for attr in event.attributes().flatten() {
match attr.key.as_ref() {
"Destination" => {
self.destination = attr.value.to_string();
},
"InResponseTo" => {
self.in_response_to = attr.value.to_string();
},
_ => {},
}
}
},
"Assertion" => {
self.in_assertion = true;
self.assertion_start_pos = Some(pos_before);
self.assertion_issuer.clear();
self.assertion_subject.clear();
self.assertion_audience.clear();
self.assertion_not_on_or_after.clear();
self.assertion_not_before.clear();
self.assertion_id.clear();
self.assertion_attributes.clear();
for attr in event.attributes().flatten() {
if attr.key.as_ref() == "ID" {
self.assertion_id = attr.value.to_string();
}
}
},
"Issuer" => {
self.in_issuer = true;
self.current_text.clear();
},
"Subject" => self.in_subject = true,
"Audience" => {
self.in_audience = true;
self.current_text.clear();
},
"Attribute" => {
self.in_attribute = true;
self.current_attr_name.clear();
self.current_text.clear();
for attr in event.attributes().flatten() {
if attr.key.as_ref() == "Name" {
self.current_attr_name = attr.value.to_string();
}
}
},
"SubjectConfirmationData" => {
for attr in event.attributes().flatten() {
if attr.key.as_ref() == "NotOnOrAfter" {
self.assertion_not_on_or_after = attr.value.to_string();
}
}
},
"Conditions" => {
for attr in event.attributes().flatten() {
if attr.key.as_ref() == "NotBefore" {
self.assertion_not_before = attr.value.to_string();
}
}
},
_ => {
tracing::trace!(element = %local_name, "saml-unknown-start-element");
},
}
}
fn handle_empty_element(&mut self, event: &quick_xml::events::BytesStart) {
if !check_saml_namespace(event.name().as_ref()) {
return;
}
let local_name = extract_local_name(event.name().as_ref());
match local_name.as_str() {
"StatusCode" => {
for attr in event.attributes().flatten() {
if attr.key.as_ref() == "Value" {
self.status_code = attr.value.to_string();
}
}
},
"Conditions" => {
for attr in event.attributes().flatten() {
if attr.key.as_ref() == "NotBefore" {
self.assertion_not_before = attr.value.to_string();
}
}
},
"SubjectConfirmationData" => {
for attr in event.attributes().flatten() {
if attr.key.as_ref() == "NotOnOrAfter" {
self.assertion_not_on_or_after = attr.value.to_string();
}
}
},
"AttributeValue" if self.in_attribute => {
if self
.assertion_attributes
.iter()
.any(|(n, _)| n == &self.current_attr_name)
{
tracing::warn!(
attr_name = %self.current_attr_name,
"SAML Assertion contains duplicate attribute name, possible attribute pollution attack"
);
}
self.assertion_attributes
.push((self.current_attr_name.clone(), String::new()));
},
_ => {
tracing::trace!(element = %local_name, "saml-unknown-empty-element");
},
}
}
fn handle_end_element(
&mut self,
event: &quick_xml::events::BytesEnd,
pos_after: u64,
xml: &str,
) {
let local_name = extract_local_name(event.name().as_ref());
match local_name.as_str() {
"Assertion" => {
if self.in_assertion {
if let Some(start) = self.assertion_start_pos {
let start_usize = start as usize;
let pos_after_usize = pos_after as usize;
if pos_after_usize >= start_usize && pos_after_usize <= xml.len() {
self.assertion_raw_xml =
Some(xml[start_usize..pos_after_usize].to_string());
}
}
self.assertion = Some(SamlAssertion {
id: self.assertion_id.clone(),
issuer: self.assertion_issuer.clone(),
subject: self.assertion_subject.clone(),
audience: self.assertion_audience.clone(),
not_on_or_after: self.assertion_not_on_or_after.clone(),
not_before: self.assertion_not_before.clone(),
attributes: self.assertion_attributes.clone(),
raw_xml: self.assertion_raw_xml.clone(),
});
self.in_assertion = false;
}
},
"Issuer" => {
if self.in_issuer {
if self.in_assertion {
self.assertion_issuer = self.current_text.clone();
} else {
self.issuer = self.current_text.clone();
}
self.in_issuer = false;
self.current_text.clear();
}
},
"Subject" => self.in_subject = false,
"Audience" => {
if self.in_audience {
self.assertion_audience = self.current_text.clone();
self.in_audience = false;
self.current_text.clear();
}
},
"Attribute" => {
if self.in_attribute {
if self
.assertion_attributes
.iter()
.any(|(n, _)| n == &self.current_attr_name)
{
tracing::warn!(
attr_name = %self.current_attr_name,
"SAML Assertion contains duplicate attribute name, possible attribute pollution attack"
);
}
self.assertion_attributes
.push((self.current_attr_name.clone(), self.current_text.clone()));
self.in_attribute = false;
self.current_text.clear();
}
},
"NameID" => {
if self.in_subject {
self.assertion_subject = self.current_text.clone();
self.current_text.clear();
}
},
"AttributeValue" => {
},
_ => {
tracing::trace!(element = %local_name, "saml-unknown-end-element");
},
}
}
fn handle_text(&mut self, text: &str) {
self.current_text.push_str(text);
}
fn into_response(self) -> GarrisonResult<SamlResponse> {
if let Some(ref assertion) = self.assertion {
if assertion.not_on_or_after.is_empty() {
return Err(GarrisonError::InvalidToken(
"sso-saml-missing-not-on-or-after".to_string(),
));
}
let expiry =
chrono::DateTime::parse_from_rfc3339(&assertion.not_on_or_after).map_err(|e| {
GarrisonError::InvalidToken(format!("sso-saml-not-on-or-after-parse::{}", e))
})?;
if Utc::now().timestamp() >= expiry.timestamp() {
return Err(GarrisonError::InvalidToken(format!(
"sso-saml-assertion-expired::{}",
assertion.not_on_or_after
)));
}
if !assertion.not_before.is_empty() {
let not_before = chrono::DateTime::parse_from_rfc3339(&assertion.not_before)
.map_err(|e| {
GarrisonError::InvalidToken(format!("sso-saml-not-before-parse::{}", e))
})?;
if Utc::now().timestamp() < not_before.timestamp() {
return Err(GarrisonError::InvalidToken(format!(
"sso-saml-assertion-not-yet-valid::{}",
assertion.not_before
)));
}
}
}
Ok(SamlResponse {
destination: self.destination,
issuer: self.issuer,
assertion: self.assertion,
status_code: self.status_code,
in_response_to: self.in_response_to,
})
}
}
fn parse_saml_response_xml(xml: &str) -> GarrisonResult<SamlResponse> {
use quick_xml::events::Event;
use quick_xml::reader::Reader;
let mut reader = Reader::from_str(xml);
reader.config_mut().trim_text(true);
let mut ctx = SamlParseContext::new();
let mut buf = Vec::new();
loop {
let pos_before = reader.buffer_position();
match reader.read_event_into(&mut buf) {
Err(e) => {
return Err(GarrisonError::Internal(format!(
"sso-saml-xml-parse::{}",
e
)))
},
Ok(Event::Eof) => break,
Ok(Event::Start(e)) => ctx.handle_start_element(&e, pos_before),
Ok(Event::Empty(e)) => ctx.handle_empty_element(&e),
Ok(Event::End(e)) => {
let pos_after = reader.buffer_position();
ctx.handle_end_element(&e, pos_after, xml);
},
Ok(Event::Text(e)) => {
let text = e.as_ref().to_string();
ctx.handle_text(&text);
},
_ => {},
}
buf.clear();
}
ctx.into_response()
}
pub async fn check_assertion_replay(
assertion_id: &str,
not_on_or_after: &str,
dao: &dyn crate::dao::GarrisonDao,
) -> GarrisonResult<bool> {
if assertion_id.is_empty() {
return Ok(true);
}
let key = format!("{}consumed:{}", DaoKeyPrefix::Saml, assertion_id);
let ttl = if not_on_or_after.is_empty() {
300
} else {
let expiry = chrono::DateTime::parse_from_rfc3339(not_on_or_after).map_err(|e| {
GarrisonError::InvalidToken(format!("sso-saml-not-on-or-after-parse::{}", e))
})?;
let remaining = expiry.timestamp().saturating_sub(Utc::now().timestamp());
if remaining > 0 {
remaining as u64
} else {
300
}
};
let first = dao.set_if_absent(&key, "1", ttl).await?;
Ok(first)
}
pub(crate) async fn enforce_in_response_to(
dao: Option<&std::sync::Arc<dyn crate::dao::GarrisonDao>>,
warned_no_dao: &std::sync::atomic::AtomicBool,
in_response_to: &str,
) -> GarrisonResult<()> {
use std::sync::atomic::Ordering;
match dao {
Some(d) => {
if in_response_to.is_empty() {
return Ok(()); }
let key = format!("{}req:{}", DaoKeyPrefix::Saml, in_response_to);
if d.get_and_delete(&key).await?.is_none() {
return Err(GarrisonError::InvalidParam(format!(
"sso-saml-in-response-to-unknown::{}",
in_response_to
)));
}
Ok(())
},
None => {
if !in_response_to.is_empty() {
return Err(GarrisonError::InvalidParam(
"sso-saml-in-response-to-unverifiable".to_string(),
));
}
if !warned_no_dao.swap(true, Ordering::Relaxed) {
tracing::warn!(
"SAML request_dao not configured: InResponseTo binding and Assertion replay protection disabled"
);
}
Ok(())
},
}
}
fn extract_local_name(qualified: &str) -> String {
match qualified.rsplit_once(':') {
Some((_, local)) => local.to_string(),
None => qualified.to_string(),
}
}
fn check_saml_namespace(qualified: &str) -> bool {
match qualified.rsplit_once(':') {
Some((prefix, _)) => {
let valid = matches!(prefix, "saml" | "samlp" | "ds");
if !valid {
tracing::warn!(
qualified = %qualified,
"SAML XML element uses non-standard namespace prefix, possible namespace confusion attack"
);
}
valid
},
None => true,
}
}
#[cfg(feature = "protocol-saml")]
const SIG_ALG_RSA_SHA256: &str = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
#[cfg(feature = "protocol-saml")]
const SIG_ALG_ECDSA_SHA256: &str = "http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256";
#[cfg(feature = "protocol-saml")]
const SIG_ALG_RSA_1_5: &str = "http://www.w3.org/2000/09/xmldsig#rsa-1_5";
#[cfg(all(feature = "protocol-saml", test))]
fn is_signature_algorithm_allowed(algorithm: &str) -> bool {
matches!(algorithm, SIG_ALG_RSA_SHA256 | SIG_ALG_ECDSA_SHA256)
}
#[cfg(feature = "protocol-saml")]
fn extract_signature_method_algorithm(signature_xml: &str) -> Option<String> {
let method_start = signature_xml.find("<")?;
let rest = &signature_xml[method_start..];
let method_idx = rest.find("SignatureMethod")?;
let after_method = &rest[method_idx..];
let (alg_value_start, quote) = match (
after_method.find("Algorithm=\""),
after_method.find("Algorithm='"),
) {
(Some(d), Some(s)) if s < d => (s + "Algorithm='".len(), '\''),
(Some(d), _) => (d + "Algorithm=\"".len(), '"'),
(None, Some(s)) => (s + "Algorithm='".len(), '\''),
(None, None) => return None,
};
let after_alg = &after_method[alg_value_start..];
let alg_end = after_alg.find(quote)?;
let algorithm = &after_alg[..alg_end];
if algorithm.contains('<') || algorithm.contains('>') || algorithm.contains('&') {
tracing::warn!("saml-algorithm-contains-xml-special-chars");
return None;
}
if algorithm.len() > 128 {
tracing::warn!(len = algorithm.len(), "saml-algorithm-exceeds-max-length");
return None;
}
Some(algorithm.to_string())
}
#[cfg(feature = "protocol-saml")]
fn extract_signature_value(signature_xml: &str) -> Option<String> {
find_element_text_span(signature_xml, "SignatureValue")
}
#[cfg(feature = "protocol-saml")]
fn extract_signed_info_xml(assertion_xml: &str) -> Option<String> {
find_element_span(assertion_xml, "SignedInfo")
.map(|(start, end)| assertion_xml[start..end].to_string())
}
#[cfg(feature = "protocol-saml")]
fn extract_signature_xml(assertion_xml: &str) -> Option<String> {
find_element_span(assertion_xml, "Signature")
.map(|(start, end)| assertion_xml[start..end].to_string())
}
#[cfg(feature = "protocol-saml")]
const DIGEST_ALG_SHA256: &str = "http://www.w3.org/2001/04/xmlenc#sha256";
#[cfg(feature = "protocol-saml")]
struct ReferenceBinding {
uri: String,
digest_method: Option<String>,
digest_value_b64: String,
}
#[cfg(feature = "protocol-saml")]
fn extract_reference_bindings(signed_info_xml: &str) -> Vec<ReferenceBinding> {
let mut bindings = Vec::new();
for start_tag in ["<ds:Reference", "<Reference"] {
let mut search_from = 0usize;
while let Some(rel) = signed_info_xml[search_from..].find(start_tag) {
let abs_start = search_from + rel;
let after_tag = signed_info_xml[abs_start + start_tag.len()..]
.chars()
.next();
if !matches!(after_tag, Some(' ') | Some('>') | Some('/')) {
search_from = abs_start + start_tag.len();
continue;
}
let content_start = abs_start + start_tag.len();
let rel_end = match signed_info_xml[content_start..]
.find("</ds:Reference>")
.or_else(|| signed_info_xml[content_start..].find("</Reference>"))
{
Some(e) => e,
None => break,
};
let block = &signed_info_xml[abs_start..content_start + rel_end];
let uri = block
.find("URI=\"")
.and_then(|i| {
let vstart = i + "URI=\"".len();
block[vstart..]
.find('"')
.map(|e| block[vstart..vstart + e].to_string())
})
.unwrap_or_default();
let digest_method = block
.find("<ds:DigestMethod")
.or_else(|| block.find("<DigestMethod"))
.and_then(|i| {
let seg = &block[i..];
let key = "Algorithm=\"";
seg.find(key).and_then(|j| {
let vs = j + key.len();
seg[vs..].find('"').map(|e| seg[vs..vs + e].to_string())
})
});
let digest_value_b64 = extract_xml_text(block, "DigestValue").unwrap_or_default();
bindings.push(ReferenceBinding {
uri,
digest_method,
digest_value_b64,
});
search_from = content_start + rel_end;
}
if !bindings.is_empty() {
break;
}
}
bindings
}
#[cfg(feature = "protocol-saml")]
fn find_element_span(xml: &str, local_name: &str) -> Option<(usize, usize)> {
let prefixes = ["ds:", ""];
for open_prefix in prefixes {
let open = format!("<{}{}", open_prefix, local_name);
let other = if open_prefix.is_empty() { "ds:" } else { "" };
let closes = [
format!("</{}{}>", open_prefix, local_name),
format!("</{}{}>", other, local_name),
];
let mut search_from = 0usize;
while let Some(rel) = xml[search_from..].find(&open) {
let abs = search_from + rel;
match xml[abs + open.len()..].chars().next() {
Some(' ') | Some('>') | Some('/') | Some('\t') | Some('\n') | Some('\r') => {},
_ => {
search_from = abs + 1;
continue;
},
}
for close in &closes {
if let Some(close_rel) = xml[abs..].find(close.as_str()) {
return Some((abs, abs + close_rel + close.len()));
}
}
search_from = abs + 1;
}
}
None
}
#[cfg(feature = "protocol-saml")]
fn find_element_text_span(xml: &str, local_name: &str) -> Option<String> {
let (start, end) = find_element_span(xml, local_name)?;
let seg = &xml[start..end];
let gt = seg.find('>')?;
let close_start = seg[gt..].find("</")? + gt;
if close_start > gt {
Some(seg[gt + 1..close_start].trim().to_string())
} else {
Some(String::new())
}
}
#[cfg(feature = "protocol-saml")]
fn extract_xml_text(xml: &str, local_name: &str) -> Option<String> {
find_element_text_span(xml, local_name)
}
#[cfg(feature = "protocol-saml")]
fn root_element_identity(xml: &str) -> Option<(String, String)> {
let bytes = xml.as_bytes();
if bytes.first() != Some(&b'<') {
return None;
}
let mut quote: Option<u8> = None;
let mut i = 1usize;
let gt = loop {
if i >= bytes.len() {
return None;
}
match quote {
Some(q) => {
if bytes[i] == q {
quote = None;
}
},
None => {
if bytes[i] == b'"' || bytes[i] == b'\'' {
quote = Some(bytes[i]);
} else if bytes[i] == b'>' {
break i;
}
},
}
i += 1;
};
let (qname, attrs) = parse_tag_inner(&xml[1..gt]);
if qname.is_empty() || qname.starts_with('!') || qname.starts_with('?') {
return None;
}
let id = attrs
.iter()
.find(|(k, _)| k == "ID")
.map(|(_, v)| v.clone())?;
Some((qname, id))
}
#[cfg(feature = "protocol-saml")]
fn root_element_span(xml: &str, qname: &str) -> Option<(usize, usize)> {
let open_pat = format!("<{}", qname);
let close_pat = format!("</{}>", qname);
let is_valid_open = |abs: usize| -> bool {
matches!(
xml[abs + open_pat.len()..].chars().next(),
Some(' ') | Some('>') | Some('/')
)
};
let is_self_closing = |abs: usize| -> bool {
match xml[abs..].find('>') {
Some(tag_end) => xml[abs + tag_end - 1..abs + tag_end].ends_with('/'),
None => false,
}
};
let mut depth: usize = 0;
let mut pos = 0usize;
loop {
let o_rel = xml[pos..].find(&open_pat).map(|i| i + pos);
let c_rel = xml[pos..].find(&close_pat).map(|i| i + pos);
if let Some(o) = o_rel {
if o <= c_rel.unwrap_or(usize::MAX) && is_valid_open(o) && !is_self_closing(o) {
depth += 1;
pos = o + open_pat.len();
continue;
}
}
let close_abs = c_rel?;
depth = depth.checked_sub(1)?;
pos = close_abs + close_pat.len();
if depth == 0 {
return Some((0, pos));
}
}
}
#[cfg(feature = "protocol-saml")]
fn strip_enveloped_signature(element_xml: &str) -> String {
match extract_signature_xml(element_xml) {
Some(sig) => element_xml.replacen(&sig, "", 1),
None => element_xml.to_string(),
}
}
#[cfg(feature = "protocol-saml")]
fn canonicalize_signed_info_c14n_subset(input: &str) -> String {
let mut out = String::with_capacity(input.len());
let bytes = input.as_bytes();
let mut i = 0usize;
while i < input.len() {
if input[i..].starts_with("<!--") {
match input[i + 4..].find("-->") {
Some(end) => i += 4 + end + 3,
None => i = input.len(), }
continue;
}
if bytes[i] == b'<' {
match input[i..].find('>') {
Some(gt_rel) => {
let inner_raw = &input[i + 1..i + gt_rel];
if let Some(rest) = inner_raw.strip_prefix('/') {
out.push_str("</");
out.push_str(rest.trim());
out.push('>');
} else {
let self_closing = inner_raw.ends_with('/');
let inner = inner_raw.trim_end_matches('/').trim();
let (qname, attrs) = parse_tag_inner(inner);
out.push('<');
out.push_str(&qname);
let mut attrs = attrs;
attrs.sort_by(|a, b| a.0.cmp(&b.0));
for (k, v) in attrs {
out.push(' ');
out.push_str(&k);
out.push_str("=\"");
out.push_str(&v);
out.push('"');
}
if self_closing {
out.push_str("></");
out.push_str(&qname);
out.push('>');
} else {
out.push('>');
}
}
i += gt_rel + 1;
continue;
},
None => {
out.push_str(&input[i..]);
i = input.len();
},
}
} else {
let lt = input[i..].find('<').map(|p| i + p).unwrap_or(input.len());
out.push_str(&input[i..lt]);
i = lt;
}
}
out
}
#[cfg(feature = "protocol-saml")]
fn parse_tag_inner(inner: &str) -> (String, Vec<(String, String)>) {
let inner = inner.trim();
let (qname, rest) = match inner.find(|c: char| c.is_whitespace()) {
Some(ws) => (inner[..ws].to_string(), &inner[ws..]),
None => (inner.to_string(), ""),
};
let mut attrs: Vec<(String, String)> = Vec::new();
let bytes = rest.as_bytes();
let mut i = 0usize;
while i < bytes.len() {
while i < bytes.len() && bytes[i].is_ascii_whitespace() {
i += 1;
}
if i >= bytes.len() {
break;
}
let name_start = i;
while i < bytes.len() && bytes[i] != b'=' && !bytes[i].is_ascii_whitespace() {
i += 1;
}
let name = rest[name_start..i].to_string();
while i < bytes.len() && bytes[i].is_ascii_whitespace() {
i += 1;
}
if i < bytes.len() && bytes[i] == b'=' {
i += 1;
while i < bytes.len() && bytes[i].is_ascii_whitespace() {
i += 1;
}
if i < bytes.len() && (bytes[i] == b'"' || bytes[i] == b'\'') {
let quote = bytes[i];
i += 1;
let vstart = i;
while i < bytes.len() && bytes[i] != quote {
i += 1;
}
let value = rest[vstart..i].to_string();
if i < bytes.len() {
i += 1;
}
attrs.push((name, value));
} else {
let vstart = i;
while i < bytes.len() && !bytes[i].is_ascii_whitespace() {
i += 1;
}
attrs.push((name, rest[vstart..i].to_string()));
}
} else {
attrs.push((name, String::new()));
}
}
(qname, attrs)
}
#[cfg(feature = "protocol-saml")]
fn validate_reference_binding(
document_xml: &str,
binding: &ReferenceBinding,
) -> Result<(), &'static str> {
let Some(id) = binding.uri.strip_prefix('#') else {
return Err("reference-uri-not-fragment");
};
if id.is_empty() {
return Err("reference-uri-empty-id");
}
if binding.digest_method.as_deref() != Some(DIGEST_ALG_SHA256) {
return Err("digest-method-not-sha256");
}
let xml = document_xml.trim_start();
let Some((root_qname, root_id)) = root_element_identity(xml) else {
return Err("verified-root-element-not-found");
};
if root_id != id {
return Err("reference-uri-not-anchored-to-verified-assertion");
}
let Some((start, end)) = root_element_span(xml, &root_qname) else {
return Err("referenced-element-not-found");
};
let element_raw = &xml[start..end];
use base64::Engine as _;
use rsa::sha2::{Digest, Sha256};
use subtle::ConstantTimeEq;
let stripped = strip_enveloped_signature(element_raw);
let computed = Sha256::digest(stripped.as_bytes());
let expected = base64::engine::general_purpose::STANDARD
.decode(binding.digest_value_b64.trim())
.map_err(|_| "digest-value-base64-decode")?;
if expected.len() != computed.len() || expected.as_slice().ct_ne(&computed).into() {
return Err("digest-value-mismatch");
}
Ok(())
}
#[cfg(feature = "protocol-saml")]
fn verify_saml_signature(assertion_xml: &str, idp_public_key_pem: &str) -> GarrisonResult<bool> {
use rsa::pkcs1::DecodeRsaPublicKey;
use rsa::pkcs8::DecodePublicKey;
let signature_xml = match extract_signature_xml(assertion_xml) {
Some(xml) => xml,
None => {
tracing::warn!(
"SAML Assertion missing <ds:Signature> element, signature verification failed"
);
return Ok(false);
},
};
let algorithm = extract_signature_method_algorithm(&signature_xml);
match algorithm.as_deref() {
Some(SIG_ALG_RSA_SHA256) => {}, Some(SIG_ALG_ECDSA_SHA256) => {
tracing::warn!(
algorithm = %SIG_ALG_ECDSA_SHA256,
"SAML signature algorithm ecdsa-sha256 is whitelisted but verification is not implemented yet (awaiting an ECDSA library)"
);
return Ok(false);
},
Some(alg) if alg == SIG_ALG_RSA_1_5 => {
tracing::warn!(
algorithm = %alg,
"SAML signature algorithm rsa-1_5 rejected (weak algorithm, Bleichenbacher attack risk)"
);
return Ok(false);
},
Some(alg) => {
tracing::warn!(
algorithm = %alg,
"SAML signature algorithm not whitelisted (only rsa-sha256 / ecdsa-sha256 allowed)"
);
return Ok(false);
},
None => {
tracing::warn!("SAML <ds:Signature> missing <ds:SignatureMethod Algorithm=...>");
return Ok(false);
},
}
let signature_value_b64 = match extract_signature_value(&signature_xml) {
Some(v) => v,
None => {
tracing::warn!("SAML <ds:Signature> missing <ds:SignatureValue>");
return Ok(false);
},
};
use base64::Engine as _;
let signature_value = base64::engine::general_purpose::STANDARD
.decode(&signature_value_b64)
.map_err(|e| {
GarrisonError::InvalidParam(format!("sso-saml-signature-value-decode::{}", e))
})?;
let signed_info_xml = match extract_signed_info_xml(assertion_xml) {
Some(xml) => xml,
None => {
tracing::warn!("SAML Assertion missing <ds:SignedInfo>");
return Ok(false);
},
};
if signed_info_xml.contains("<!--") {
tracing::warn!(
"SAML <ds:SignedInfo> contains XML comments, rejected (C14N no-comments mode)"
);
return Ok(false);
}
let signed_info_canonical = canonicalize_signed_info_c14n_subset(&signed_info_xml);
tracing::warn!(
"SAML signature verification uses a common-subset Exclusive C14N for <ds:SignedInfo> \
(namespace rewriting not implemented). If your IdP relies on full inclusive C14N, \
verification may fail (fail-closed). See canonicalize_signed_info_c14n_subset docs."
);
use rsa::pkcs1v15::{Signature, VerifyingKey};
use rsa::sha2::Sha256;
use rsa::signature::Verifier;
let public_key = match rsa::RsaPublicKey::from_public_key_pem(idp_public_key_pem) {
Ok(k) => k,
Err(e1) => match rsa::RsaPublicKey::from_pkcs1_pem(idp_public_key_pem) {
Ok(k) => k,
Err(e2) => {
return Err(GarrisonError::InvalidParam(format!(
"sso-saml-idp-public-key-parse-failed::pkcs8_err={}::pkcs1_err={}",
e1, e2
)));
},
},
};
use std::convert::TryFrom;
let signature = Signature::try_from(signature_value.as_slice()).map_err(|e| {
GarrisonError::InvalidParam(format!("sso-saml-signature-bytes-decode::{}", e))
})?;
let verifying_key = VerifyingKey::<Sha256>::new(public_key);
match verifying_key.verify(signed_info_canonical.as_bytes(), &signature) {
Ok(()) => {},
Err(_) => {
tracing::warn!(
"SAML signature verification failed: signature value does not match SignedInfo"
);
return Ok(false);
},
}
let references = extract_reference_bindings(&signed_info_xml);
if references.is_empty() {
tracing::warn!("SAML SignedInfo missing <ds:Reference>, rejected (XSW protection)");
return Ok(false);
}
for binding in &references {
if let Err(reason) = validate_reference_binding(assertion_xml, binding) {
tracing::warn!(
reason = %reason,
uri = %binding.uri,
"SAML <ds:Reference> binding verification failed"
);
return Ok(false);
}
}
Ok(true)
}
#[cfg(feature = "protocol-saml")]
pub struct XmlSecSamlProvider {
idp_public_key_pem: String,
expected_destination: Option<String>,
expected_audience: Option<String>,
allow_unvalidated_destination: bool,
allow_unvalidated_audience: bool,
request_dao: Option<std::sync::Arc<dyn crate::dao::GarrisonDao>>,
warned_no_dao: std::sync::atomic::AtomicBool,
}
#[cfg(feature = "protocol-saml")]
impl XmlSecSamlProvider {
pub fn new(idp_public_key_pem: String) -> GarrisonResult<Self> {
let _ = parse_rsa_public_key(&idp_public_key_pem)?;
Ok(Self {
idp_public_key_pem,
expected_destination: None,
expected_audience: None,
allow_unvalidated_destination: false,
allow_unvalidated_audience: false,
request_dao: None,
warned_no_dao: std::sync::atomic::AtomicBool::new(false),
})
}
pub fn with_request_dao(mut self, dao: std::sync::Arc<dyn crate::dao::GarrisonDao>) -> Self {
self.request_dao = Some(dao);
self
}
pub fn with_expected_destination(mut self, destination: String) -> Self {
self.expected_destination = Some(destination);
self
}
pub fn with_expected_audience(mut self, audience: String) -> Self {
self.expected_audience = Some(audience);
self
}
#[must_use = "豁免 Destination 校验属显式安全决策,返回值必须被消费"]
pub fn allow_unvalidated_destination(mut self) -> Self {
self.allow_unvalidated_destination = true;
self
}
#[must_use = "豁免 Audience 校验属显式安全决策,返回值必须被消费"]
pub fn allow_unvalidated_audience(mut self) -> Self {
self.allow_unvalidated_audience = true;
self
}
}
#[cfg(feature = "protocol-saml")]
fn parse_rsa_public_key(pem: &str) -> GarrisonResult<rsa::RsaPublicKey> {
use rsa::pkcs1::DecodeRsaPublicKey;
use rsa::pkcs8::DecodePublicKey;
match rsa::RsaPublicKey::from_public_key_pem(pem) {
Ok(k) => Ok(k),
Err(e1) => match rsa::RsaPublicKey::from_pkcs1_pem(pem) {
Ok(k) => Ok(k),
Err(e2) => Err(GarrisonError::InvalidParam(format!(
"sso-saml-idp-public-key-parse-failed::pkcs8_err={}::pkcs1_err={}",
e1, e2
))),
},
}
}
#[cfg(feature = "protocol-saml")]
#[async_trait]
impl SamlProvider for XmlSecSamlProvider {
async fn build_authn_request(
&self,
sp_entity_id: &str,
acs_url: &str,
idp_sso_endpoint: &str,
) -> GarrisonResult<SamlRequest> {
let request = SamlRequest {
id: Uuid::new_v4().to_string(),
issue_instant: Utc::now().to_rfc3339(),
destination: idp_sso_endpoint.to_string(),
issuer: sp_entity_id.to_string(),
assertion_consumer_service_url: acs_url.to_string(),
};
if let Some(d) = &self.request_dao {
let key = format!("{}req:{}", DaoKeyPrefix::Saml, request.id);
d.set(&key, "1", 600).await?;
}
Ok(request)
}
async fn parse_response(&self, response_xml: &str) -> GarrisonResult<SamlResponse> {
const SAML_RESPONSE_MAX_SIZE: usize = 64 * 1024;
if response_xml.len() > SAML_RESPONSE_MAX_SIZE {
return Err(GarrisonError::InvalidParam(format!(
"saml-response-too-large::{}::{}",
response_xml.len(),
SAML_RESPONSE_MAX_SIZE
)));
}
let mut response = parse_saml_response_xml(response_xml)?;
validate_status_code(&response.status_code)?;
enforce_in_response_to(
self.request_dao.as_ref(),
&self.warned_no_dao,
&response.in_response_to,
)
.await?;
validate_destination(
&response.destination,
self.expected_destination.as_deref(),
self.allow_unvalidated_destination,
)?;
if let Some(ref assertion) = response.assertion {
validate_audience(
&assertion.audience,
self.expected_audience.as_deref(),
self.allow_unvalidated_audience,
)?;
}
if let Some(ref assertion) = response.assertion {
match self.validate_assertion(assertion).await {
Ok(true) => {
if let Some(d) = &self.request_dao {
if !check_assertion_replay(
&assertion.id,
&assertion.not_on_or_after,
d.as_ref(),
)
.await?
{
return Err(GarrisonError::InvalidParam(format!(
"sso-saml-assertion-replay::{}",
assertion.id
)));
}
}
},
Ok(false) => {
tracing::warn!("SAML Assertion signature verification failed, stripped");
response.assertion = None;
},
Err(e) => return Err(e),
}
}
Ok(response)
}
async fn validate_assertion(&self, assertion: &SamlAssertion) -> GarrisonResult<bool> {
let raw_xml = assertion.raw_xml.as_ref().ok_or_else(|| {
GarrisonError::InvalidParam(
"sso-saml-missing-raw-xml-for-signature-verification".to_string(),
)
})?;
verify_saml_signature(raw_xml, &self.idp_public_key_pem)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn saml_assertion_serde_roundtrip() {
let assertion = SamlAssertion {
id: "assertion-001".to_string(),
issuer: "https://idp.example.com".to_string(),
subject: "user@example.com".to_string(),
audience: "https://sp.example.com".to_string(),
not_on_or_after: "2026-07-10T12:00:00Z".to_string(),
not_before: String::new(),
attributes: vec![("email".to_string(), "user@example.com".to_string())],
raw_xml: None,
};
let json = serde_json::to_string(&assertion).unwrap();
let deserialized: SamlAssertion = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.id, assertion.id);
assert_eq!(deserialized.issuer, assertion.issuer);
assert_eq!(deserialized.subject, assertion.subject);
assert_eq!(deserialized.audience, assertion.audience);
assert_eq!(deserialized.not_on_or_after, assertion.not_on_or_after);
assert_eq!(deserialized.attributes.len(), 1);
assert_eq!(deserialized.attributes[0].0, "email");
assert_eq!(deserialized.attributes[0].1, "user@example.com");
assert!(deserialized.raw_xml.is_none());
}
#[test]
fn saml_response_serde_roundtrip() {
let response = SamlResponse {
destination: "https://sp.example.com/acs".to_string(),
issuer: "https://idp.example.com".to_string(),
assertion: None,
status_code: "urn:oasis:names:tc:SAML:2.0:status:Success".to_string(),
in_response_to: String::new(),
};
let json = serde_json::to_string(&response).unwrap();
let deserialized: SamlResponse = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.destination, response.destination);
assert_eq!(deserialized.issuer, response.issuer);
assert!(deserialized.assertion.is_none());
assert_eq!(deserialized.status_code, response.status_code);
}
#[test]
fn saml_request_serde_roundtrip() {
let request = SamlRequest {
id: "id-12345".to_string(),
issue_instant: "2026-07-10T12:00:00Z".to_string(),
destination: "https://idp.example.com/sso".to_string(),
issuer: "https://sp.example.com".to_string(),
assertion_consumer_service_url: "https://sp.example.com/acs".to_string(),
};
let json = serde_json::to_string(&request).unwrap();
let deserialized: SamlRequest = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized.id, request.id);
assert_eq!(deserialized.issue_instant, request.issue_instant);
assert_eq!(deserialized.destination, request.destination);
assert_eq!(deserialized.issuer, request.issuer);
assert_eq!(
deserialized.assertion_consumer_service_url,
request.assertion_consumer_service_url
);
}
#[test]
fn saml_assertion_implements_clone_debug() {
let assertion = SamlAssertion {
id: "assertion-002".to_string(),
issuer: "idp".to_string(),
subject: "user".to_string(),
audience: "sp".to_string(),
not_on_or_after: "2026-07-10T12:00:00Z".to_string(),
not_before: String::new(),
attributes: vec![],
raw_xml: None,
};
let cloned = assertion.clone();
assert_eq!(cloned.issuer, assertion.issuer);
let debug_str = format!("{:?}", assertion);
assert!(debug_str.contains("SamlAssertion"));
}
#[test]
fn default_saml_provider_new_returns_ok() {
let provider = DefaultSamlProvider::new();
assert!(provider.is_ok());
}
#[test]
fn default_saml_provider_implements_saml_provider() {
fn assert_saml_provider<T: SamlProvider>(_provider: &T) {}
let provider = DefaultSamlProvider::new().unwrap();
assert_saml_provider(&provider);
}
#[tokio::test]
async fn build_authn_request_returns_valid_request() {
let provider = DefaultSamlProvider::new().unwrap();
let request = provider
.build_authn_request(
"https://sp.example.com",
"https://sp.example.com/acs",
"https://idp.example.com/sso",
)
.await
.unwrap();
assert_eq!(request.issuer, "https://sp.example.com");
assert_eq!(
request.assertion_consumer_service_url,
"https://sp.example.com/acs"
);
assert_eq!(request.destination, "https://idp.example.com/sso");
assert!(!request.id.is_empty());
assert!(!request.issue_instant.is_empty());
}
#[tokio::test]
async fn build_authn_request_generates_unique_ids() {
let provider = DefaultSamlProvider::new().unwrap();
let r1 = provider
.build_authn_request("sp1", "https://sp1.example.com/acs", "https://idp/sso")
.await
.unwrap();
let r2 = provider
.build_authn_request("sp1", "https://sp1.example.com/acs", "https://idp/sso")
.await
.unwrap();
assert_ne!(r1.id, r2.id);
}
#[tokio::test]
async fn parse_response_success() {
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let xml = format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Destination="https://sp.example.com/acs">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<saml:Assertion ID="assertion-123">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<saml:Subject>
<saml:NameID>user@example.com</saml:NameID>
<saml:SubjectConfirmation>
<saml:SubjectConfirmationData NotOnOrAfter="{future_str}"/>
</saml:SubjectConfirmation>
</saml:Subject>
<saml:Conditions>
<saml:AudienceRestriction>
<saml:Audience>https://sp.example.com</saml:Audience>
</saml:AudienceRestriction>
</saml:Conditions>
<saml:AttributeStatement>
<saml:Attribute Name="email">
<saml:AttributeValue>user@example.com</saml:AttributeValue>
</saml:Attribute>
</saml:AttributeStatement>
</saml:Assertion>
</samlp:Response>"#
);
let provider = DefaultSamlProvider::new().unwrap();
let response = provider.parse_response(&xml).await.unwrap();
assert_eq!(response.destination, "https://sp.example.com/acs");
assert_eq!(response.issuer, "https://idp.example.com");
assert_eq!(
response.status_code,
"urn:oasis:names:tc:SAML:2.0:status:Success"
);
assert!(
response.assertion.is_none(),
"C-1: validate_assertion 未实现时应剥离 Assertion(fail-closed)"
);
}
#[tokio::test]
async fn parse_response_rejects_non_success_status_code() {
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Destination="https://sp.example.com/acs">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Requester"/>
</samlp:Status>
</samlp:Response>"#;
let provider = DefaultSamlProvider::new().unwrap();
let result = provider.parse_response(xml).await;
assert!(
matches!(result, Err(GarrisonError::InvalidParam(_))),
"非 Success 状态码应返回 InvalidParam,实际: {:?}",
result
);
}
#[tokio::test]
async fn parse_response_rejects_error_response_carrying_assertion() {
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let xml = format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Destination="https://sp.example.com/acs">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Responder"/>
</samlp:Status>
<saml:Assertion ID="evil-in-error">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<saml:Subject><saml:NameID>admin@evil.test</saml:NameID></saml:Subject>
<saml:Conditions>
<saml:AudienceRestriction><saml:Audience>https://sp.example.com</saml:Audience></saml:AudienceRestriction>
</saml:Conditions>
<saml:SubjectConfirmationData NotOnOrAfter="{}"/>
</saml:Assertion>
</samlp:Response>"#,
future_str
);
let provider = DefaultSamlProvider::new().unwrap();
let result = provider.parse_response(&xml).await;
assert!(
matches!(result, Err(GarrisonError::InvalidParam(ref m)) if m.contains("status-not-success")),
"错误响应夹带 Assertion 应被 StatusCode 校验拒绝,实际: {:?}",
result
);
}
#[tokio::test]
async fn parse_response_non_saml_xml_returns_empty_fields() {
let provider = DefaultSamlProvider::new().unwrap();
let result = provider.parse_response("not a saml response").await;
assert!(
matches!(result, Err(GarrisonError::InvalidParam(ref m)) if m.contains("status-not-success")),
"非 SAML 输入(空 StatusCode)应被 fail-closed 拒绝,实际: {:?}",
result
);
let parsed = parse_saml_response_xml("not a saml response").unwrap();
assert!(parsed.destination.is_empty());
assert!(parsed.status_code.is_empty());
assert!(parsed.assertion.is_none());
}
#[tokio::test]
async fn parse_response_rejects_expired_assertion() {
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Destination="https://sp.example.com/acs">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<saml:Assertion>
<saml:Issuer>https://idp.example.com</saml:Issuer>
<saml:Subject>
<saml:NameID>user@example.com</saml:NameID>
<saml:SubjectConfirmation>
<saml:SubjectConfirmationData NotOnOrAfter="2020-01-01T00:00:00Z"/>
</saml:SubjectConfirmation>
</saml:Subject>
</saml:Assertion>
</samlp:Response>"#;
let provider = DefaultSamlProvider::new().unwrap();
let result = provider.parse_response(xml).await;
assert!(
matches!(result, Err(GarrisonError::InvalidToken(_))),
"过期 Assertion 应返回 InvalidToken,实际: {:?}",
result
);
}
#[tokio::test]
async fn parse_response_accepts_valid_assertion() {
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let xml = format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Destination="https://sp.example.com/acs">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<saml:Assertion>
<saml:Issuer>https://idp.example.com</saml:Issuer>
<saml:Subject>
<saml:NameID>user@example.com</saml:NameID>
<saml:SubjectConfirmation>
<saml:SubjectConfirmationData NotOnOrAfter="{}"/>
</saml:SubjectConfirmation>
</saml:Subject>
</saml:Assertion>
</samlp:Response>"#,
future_str
);
let provider = DefaultSamlProvider::new().unwrap();
let response = provider.parse_response(&xml).await.unwrap();
assert_eq!(
response.status_code,
"urn:oasis:names:tc:SAML:2.0:status:Success"
);
}
#[tokio::test]
async fn validate_assertion_returns_not_implemented() {
let provider = DefaultSamlProvider::new().unwrap();
let assertion = SamlAssertion {
id: "assertion-003".to_string(),
issuer: "https://idp.example.com".to_string(),
subject: "user@example.com".to_string(),
audience: "https://sp.example.com".to_string(),
not_on_or_after: "2026-07-10T12:00:00Z".to_string(),
not_before: String::new(),
attributes: vec![],
raw_xml: None,
};
let result = provider.validate_assertion(&assertion).await;
assert!(result.is_err());
match result.err() {
Some(GarrisonError::NotImplemented(_)) => {},
other => panic!("期望 NotImplemented 错误,实际: {:?}", other),
}
}
#[tokio::test]
async fn check_assertion_replay_rejects_replay() {
let dao = crate::dao::tests::MockDao::new();
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let first = check_assertion_replay("assertion-replay-001", &future_str, &dao)
.await
.expect("首次 check 不应报错");
assert!(first, "首次消费应通过");
let second = check_assertion_replay("assertion-replay-001", &future_str, &dao)
.await
.expect("二次 check 不应报错");
assert!(
!second,
"同一 Assertion ID 二次消费应被拒绝(C-3 重放防护)"
);
}
#[tokio::test]
async fn check_assertion_replay_isolates_by_id() {
let dao = crate::dao::tests::MockDao::new();
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let a = check_assertion_replay("assertion-A", &future_str, &dao)
.await
.unwrap();
assert!(a, "assertion-A 首次应通过");
let b = check_assertion_replay("assertion-B", &future_str, &dao)
.await
.unwrap();
assert!(b, "assertion-B 首次应通过(不同 ID 隔离)");
}
#[tokio::test]
async fn check_assertion_replay_empty_id_passes() {
let dao = crate::dao::tests::MockDao::new();
let result = check_assertion_replay("", "", &dao)
.await
.expect("空 ID 不应报错");
assert!(result, "空 Assertion ID 应放行(无法做重放检查)");
}
#[test]
fn extract_local_name_strips_namespace() {
assert_eq!(extract_local_name("samlp:Response"), "Response");
assert_eq!(extract_local_name("saml:Issuer"), "Issuer");
assert_eq!(extract_local_name("Assertion"), "Assertion");
}
#[test]
fn check_saml_namespace_accepts_valid_prefixes() {
assert!(check_saml_namespace("samlp:Response"));
assert!(check_saml_namespace("saml:Assertion"));
assert!(check_saml_namespace("saml:Issuer"));
assert!(check_saml_namespace("ds:Signature"));
assert!(check_saml_namespace("Response")); assert!(check_saml_namespace("Assertion")); }
#[test]
fn check_saml_namespace_rejects_invalid_prefixes() {
assert!(!check_saml_namespace("evil:Assertion"));
assert!(!check_saml_namespace("foo:Response"));
assert!(!check_saml_namespace("xs:Issuer"));
assert!(!check_saml_namespace("attack:Attribute"));
}
#[tokio::test]
async fn parse_saml_response_skips_invalid_namespace_assertion() {
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<Response>
<evil:Assertion xmlns:evil="http://evil.example.com">
<Issuer>https://idp.example.com</Issuer>
<Subject>user@example.com</Subject>
<Audience>https://sp.example.com</Audience>
<SubjectConfirmationData NotOnOrAfter="2099-12-31T23:59:59Z"/>
</evil:Assertion>
</Response>"#;
let result = parse_saml_response_xml(xml).expect("解析不应报错");
assert!(
result.assertion.is_none(),
"非标准命名空间的 Assertion 应被跳过"
);
}
#[tokio::test]
async fn parse_saml_response_with_duplicate_attributes_preserves_both() {
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Assertion ID="attr-dup-001">
<Issuer>https://idp.example.com</Issuer>
<Subject>user@example.com</Subject>
<Audience>https://sp.example.com</Audience>
<SubjectConfirmationData NotOnOrAfter="2099-12-31T23:59:59Z"/>
<AttributeStatement>
<Attribute Name="role">
<AttributeValue>user</AttributeValue>
</Attribute>
<Attribute Name="role">
<AttributeValue>admin</AttributeValue>
</Attribute>
</AttributeStatement>
</Assertion>
</Response>"#;
let result = parse_saml_response_xml(xml).expect("解析不应报错");
let assertion = result
.assertion
.expect("parse_saml_response_xml 应返回 Assertion(剥离由 parse_response 负责)");
let roles: Vec<&str> = assertion
.attributes
.iter()
.filter(|(name, _)| name == "role")
.map(|(_, value)| value.as_str())
.collect();
assert_eq!(
roles.len(),
2,
"重复属性名应保留两个值(供消费方决策),实际: {:?}",
roles
);
assert!(roles.contains(&"user"));
assert!(roles.contains(&"admin"));
}
#[tokio::test]
async fn parse_response_destination_match_passes() {
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let xml = format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Destination="https://sp.example.com/acs">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<saml:Assertion>
<saml:Issuer>https://idp.example.com</saml:Issuer>
<saml:Subject>
<saml:NameID>user@example.com</saml:NameID>
<saml:SubjectConfirmation>
<saml:SubjectConfirmationData NotOnOrAfter="{}"/>
</saml:SubjectConfirmation>
</saml:Subject>
<saml:Conditions>
<saml:AudienceRestriction>
<saml:Audience>https://sp.example.com</saml:Audience>
</saml:AudienceRestriction>
</saml:Conditions>
</saml:Assertion>
</samlp:Response>"#,
future_str
);
let provider = DefaultSamlProvider::new()
.unwrap()
.with_expected_destination("https://sp.example.com/acs".to_string())
.with_expected_audience("https://sp.example.com".to_string());
let response = provider.parse_response(&xml).await.unwrap();
assert_eq!(response.destination, "https://sp.example.com/acs");
}
#[tokio::test]
async fn parse_response_destination_mismatch_returns_error() {
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let xml = format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Destination="https://evil.example.com/acs">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<saml:Assertion>
<saml:Issuer>https://idp.example.com</saml:Issuer>
<saml:Subject><saml:NameID>user@example.com</saml:NameID>
<saml:SubjectConfirmation>
<saml:SubjectConfirmationData NotOnOrAfter="{}"/>
</saml:SubjectConfirmation>
</saml:Subject>
</saml:Assertion>
</samlp:Response>"#,
future_str
);
let provider = DefaultSamlProvider::new()
.unwrap()
.with_expected_destination("https://sp.example.com/acs".to_string());
let result = provider.parse_response(&xml).await;
assert!(
matches!(result, Err(GarrisonError::InvalidParam(_))),
"Destination 不匹配应返回 InvalidParam(fail-loud),实际: {:?}",
result
);
}
#[tokio::test]
async fn parse_response_audience_mismatch_returns_error() {
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let xml = format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Destination="https://sp.example.com/acs">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<samlp:Status>
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/>
</samlp:Status>
<saml:Assertion>
<saml:Issuer>https://idp.example.com</saml:Issuer>
<saml:Subject>
<saml:NameID>user@example.com</saml:NameID>
<saml:SubjectConfirmation>
<saml:SubjectConfirmationData NotOnOrAfter="{}"/>
</saml:SubjectConfirmation>
</saml:Subject>
<saml:Conditions>
<saml:AudienceRestriction>
<saml:Audience>https://evil.example.com</saml:Audience>
</saml:AudienceRestriction>
</saml:Conditions>
</saml:Assertion>
</samlp:Response>"#,
future_str
);
let provider = DefaultSamlProvider::new()
.unwrap()
.with_expected_audience("https://sp.example.com".to_string());
let result = provider.parse_response(&xml).await;
assert!(
matches!(result, Err(GarrisonError::InvalidParam(_))),
"Audience 不匹配应返回 InvalidParam(fail-loud),实际: {:?}",
result
);
}
#[test]
fn validate_destination_audience_unit_tests() {
assert!(validate_destination("https://sp/acs", Some("https://sp/acs"), false).is_ok());
assert!(matches!(
validate_destination("https://evil/acs", Some("https://sp/acs"), false),
Err(GarrisonError::InvalidParam(_))
));
assert!(matches!(
validate_destination("https://sp/acs", None, false),
Err(GarrisonError::InvalidParam(ref m)) if m.contains("sso-saml-destination-not-configured")
));
assert!(matches!(
validate_destination("https://sp/acs", Some(""), false),
Err(GarrisonError::InvalidParam(ref m)) if m.contains("sso-saml-destination-not-configured")
));
assert!(validate_destination("https://sp/acs", None, true).is_ok());
assert!(validate_destination("https://sp/acs", Some(""), true).is_ok());
assert!(validate_audience("https://sp", Some("https://sp"), false).is_ok());
assert!(matches!(
validate_audience("https://evil", Some("https://sp"), false),
Err(GarrisonError::InvalidParam(_))
));
assert!(matches!(
validate_audience("https://sp", None, false),
Err(GarrisonError::InvalidParam(ref m)) if m.contains("sso-saml-audience-not-configured")
));
assert!(matches!(
validate_audience("https://sp", Some(""), false),
Err(GarrisonError::InvalidParam(ref m)) if m.contains("sso-saml-audience-not-configured")
));
assert!(validate_audience("https://sp", None, true).is_ok());
}
#[cfg(feature = "protocol-saml")]
mod signature_tests {
use super::*;
#[test]
fn signature_algorithm_whitelist_allows_strong_algorithms() {
assert!(
is_signature_algorithm_allowed(SIG_ALG_RSA_SHA256),
"rsa-sha256 应在白名单内"
);
assert!(
is_signature_algorithm_allowed(SIG_ALG_ECDSA_SHA256),
"ecdsa-sha256 应在白名单内"
);
}
#[test]
fn signature_algorithm_whitelist_rejects_weak_and_unknown_algorithms() {
assert!(
!is_signature_algorithm_allowed(SIG_ALG_RSA_1_5),
"rsa-1_5 应被拒绝(Bleichenbacher 攻击风险)"
);
assert!(
!is_signature_algorithm_allowed("http://www.w3.org/2000/09/xmldsig#dsa-sha1"),
"dsa-sha1 应被拒绝(未在白名单)"
);
assert!(
!is_signature_algorithm_allowed("unknown-algorithm"),
"未知算法应被拒绝"
);
assert!(!is_signature_algorithm_allowed(""), "空字符串应被拒绝");
}
fn build_test_signed_assertion() -> (String, String) {
build_test_signed_assertion_with_inner("")
}
fn build_test_signed_assertion_with_inner(extra_inner: &str) -> (String, String) {
use base64::Engine as _;
use rsa::pkcs1v15::SigningKey;
use rsa::pkcs8::EncodePublicKey;
use rsa::sha2::{Digest, Sha256};
use rsa::signature::{SignatureEncoding, Signer};
use rsa::RsaPrivateKey;
let mut rng = rsa::rand_core::OsRng;
let private_key =
RsaPrivateKey::new(&mut rng, 2048).expect("RSA 2048 密钥生成不应失败");
let public_key_pem = private_key
.to_public_key()
.to_public_key_pem(rsa::pkcs8::LineEnding::LF)
.expect("PEM 编码不应失败");
let assertion_id = "_test-assertion-001";
let inner = format!(
"<saml:Issuer>https://idp.example.com</saml:Issuer>\
<saml:Subject><saml:NameID>user@example.com</saml:NameID></saml:Subject>{extra_inner}"
);
let element_without_sig =
format!(r#"<Assertion ID="{}">{}</Assertion>"#, assertion_id, inner);
let digest_b64 = base64::engine::general_purpose::STANDARD
.encode(Sha256::digest(element_without_sig.as_bytes()));
let signed_info = format!(
r##"<ds:SignedInfo><ds:SignatureMethod Algorithm="{}"></ds:SignatureMethod><ds:Reference URI="#{}"><ds:DigestMethod Algorithm="{}"></ds:DigestMethod><ds:DigestValue>{}</ds:DigestValue></ds:Reference></ds:SignedInfo>"##,
SIG_ALG_RSA_SHA256, assertion_id, DIGEST_ALG_SHA256, digest_b64
);
let signing_key = SigningKey::<Sha256>::new(private_key);
let signature = signing_key.sign(signed_info.as_bytes());
let signature_b64 =
base64::engine::general_purpose::STANDARD.encode(signature.to_bytes());
let xml = format!(
r#"<Assertion ID="{}">{}<ds:Signature>{}<ds:SignatureValue>{}</ds:SignatureValue></ds:Signature></Assertion>"#,
assertion_id, inner, signed_info, signature_b64
);
(xml, public_key_pem)
}
#[test]
fn verify_saml_signature_rejects_xsw_copied_signature() {
let (legit_xml, public_key_pem) = build_test_signed_assertion();
let sig_start = legit_xml
.find("<ds:Signature>")
.expect("fixture 应含 Signature");
let sig_end = legit_xml
.find("</ds:Signature>")
.expect("fixture 应含 Signature 闭合")
+ "</ds:Signature>".len();
let stolen_signature = legit_xml[sig_start..sig_end].to_string();
let evil_xml = format!(
r#"<Assertion ID="evil-xsw"><saml:Issuer>https://idp.example.com</saml:Issuer><saml:Subject><saml:NameID>admin@evil.test</saml:NameID></saml:Subject>{}<saml:Attribute Name="role"><saml:AttributeValue>super-admin</saml:AttributeValue></saml:Attribute></Assertion>"#,
stolen_signature
);
let result = verify_saml_signature(&evil_xml, &public_key_pem);
assert!(
result.is_ok(),
"XSW 检测不应报错,应返回 Ok(false): {:?}",
result
);
assert!(
!result.unwrap(),
"XSW:拷贝的合法签名不得为未签名的攻击者 Assertion 背书"
);
}
fn build_xsw_cdata_evil_assertion() -> (String, String) {
let (victim_xml, public_key_pem) = build_test_signed_assertion();
let sig_start = victim_xml
.find("<ds:Signature>")
.expect("victim fixture 应含 Signature");
let sig_end = victim_xml
.find("</ds:Signature>")
.expect("victim fixture 应含 Signature 闭合")
+ "</ds:Signature>".len();
let stolen_signature = victim_xml[sig_start..sig_end].to_string();
let future = chrono::Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let evil_xml = format!(
r#"<Assertion ID="evil-cdata-xsw"><saml:Issuer>https://idp.example.com</saml:Issuer><saml:Subject><saml:NameID>admin@evil.test</saml:NameID><saml:SubjectConfirmationData NotOnOrAfter="{future_str}"/></saml:Subject>{}<![CDATA[{}]]><saml:Attribute Name="role"><saml:AttributeValue>super-admin</saml:AttributeValue></saml:Attribute></Assertion>"#,
stolen_signature, victim_xml
);
(evil_xml, public_key_pem)
}
#[test]
fn verify_saml_signature_rejects_xsw_cdata_hidden_variant() {
let (evil_xml, public_key_pem) = build_xsw_cdata_evil_assertion();
let result = verify_saml_signature(&evil_xml, &public_key_pem);
assert!(
result.is_ok(),
"XSW 检测不应报错,应返回 Ok(false): {:?}",
result
);
assert!(
!result.unwrap(),
"XSW CDATA 变体:拷贝的受害者签名不得为伪造断言背书"
);
}
#[test]
fn verify_saml_signature_rejects_xsw_cdata_same_id_variant() {
let (victim_xml, public_key_pem) = build_test_signed_assertion();
let sig_start = victim_xml
.find("<ds:Signature>")
.expect("victim fixture 应含 Signature");
let sig_end = victim_xml
.find("</ds:Signature>")
.expect("victim fixture 应含 Signature 闭合")
+ "</ds:Signature>".len();
let stolen_signature = victim_xml[sig_start..sig_end].to_string();
let evil_xml = format!(
r#"<Assertion ID="_test-assertion-001"><saml:Subject><saml:NameID>admin@evil.test</saml:NameID></saml:Subject>{}<![CDATA[{}]]></Assertion>"#,
stolen_signature, victim_xml
);
let result = verify_saml_signature(&evil_xml, &public_key_pem);
assert!(result.is_ok(), "不应报错,应返回 Ok(false): {:?}", result);
assert!(
!result.unwrap(),
"同 ID CDATA 变体:根子树摘要必须不匹配伪造内容"
);
}
#[test]
fn verify_saml_signature_accepts_valid_signature() {
let (xml, public_key_pem) = build_test_signed_assertion();
let result = verify_saml_signature(&xml, &public_key_pem);
assert!(
result.is_ok(),
"verify_saml_signature 不应报错: {:?}",
result
);
assert!(result.unwrap(), "合法 RSA-SHA256 签名应验证通过");
}
#[test]
fn verify_saml_signature_rejects_wrong_public_key() {
use rsa::pkcs8::EncodePublicKey;
let (xml, _original_public_pem) = build_test_signed_assertion();
let mut rng = rsa::rand_core::OsRng;
let other_key =
rsa::RsaPrivateKey::new(&mut rng, 2048).expect("RSA 2048 密钥生成不应失败");
let other_public_pem = other_key
.to_public_key()
.to_public_key_pem(rsa::pkcs8::LineEnding::LF)
.expect("PEM 编码不应失败");
let result = verify_saml_signature(&xml, &other_public_pem);
assert!(
result.is_ok(),
"verify_saml_signature 不应报错: {:?}",
result
);
assert!(!result.unwrap(), "用错误公钥验证应返回 false(签名不匹配)");
}
#[test]
fn verify_saml_signature_rejects_tampered_signed_info() {
let (xml, public_key_pem) = build_test_signed_assertion();
let tampered_xml =
xml.replacen("<ds:SignedInfo>", "<ds:SignedInfo><!-- tampered -->", 1);
let result = verify_saml_signature(&tampered_xml, &public_key_pem);
assert!(
result.is_ok(),
"verify_saml_signature 不应报错: {:?}",
result
);
assert!(!result.unwrap(), "篡改 SignedInfo 后签名验证应失败");
}
#[test]
fn verify_saml_signature_rejects_missing_signature() {
let xml = r#"<Assertion><Issuer>https://idp.example.com</Issuer></Assertion>"#;
let (_signed_xml, public_key_pem) = build_test_signed_assertion();
let result = verify_saml_signature(xml, &public_key_pem);
assert!(
result.is_ok(),
"verify_saml_signature 不应报错: {:?}",
result
);
assert!(!result.unwrap(), "缺少 <ds:Signature> 应返回 false");
}
#[test]
fn verify_saml_signature_rejects_rsa_1_5_algorithm() {
let xml = format!(
r#"<Assertion><ds:Signature><ds:SignedInfo><ds:SignatureMethod Algorithm="{}"></ds:SignatureMethod></ds:SignedInfo><ds:SignatureValue>dGVzdA==</ds:SignatureValue></ds:Signature></Assertion>"#,
SIG_ALG_RSA_1_5
);
let (_signed_xml, public_key_pem) = build_test_signed_assertion();
let result = verify_saml_signature(&xml, &public_key_pem);
assert!(
result.is_ok(),
"verify_saml_signature 不应报错: {:?}",
result
);
assert!(
!result.unwrap(),
"rsa-1_5 算法应被白名单拒绝(Bleichenbacher 攻击风险)"
);
}
#[test]
fn verify_saml_signature_rejects_invalid_public_key_pem() {
let (xml, _public_key_pem) = build_test_signed_assertion();
let invalid_pem = "-----BEGIN INVALID-----\nnot a real key\n-----END INVALID-----";
let result = verify_saml_signature(&xml, invalid_pem);
assert!(
result.is_err(),
"非法公钥 PEM 应返回 Err(fail-loud),实际: {:?}",
result
);
assert!(
matches!(result, Err(GarrisonError::InvalidParam(_))),
"应为 InvalidParam 错误"
);
}
#[tokio::test]
async fn xmlsec_provider_rejects_replayed_signed_assertion() {
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let (signed_assertion, public_key_pem) = build_test_signed_assertion_with_inner(
&format!("<SubjectConfirmationData NotOnOrAfter=\"{}\"/>", future_str),
);
let dao = std::sync::Arc::new(crate::dao::tests::MockDao::new());
let provider = XmlSecSamlProvider::new(public_key_pem)
.unwrap()
.with_request_dao(dao)
.allow_unvalidated_destination()
.allow_unvalidated_audience();
let xml = format!(
r#"<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
Destination="https://sp.example.com/acs">
<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">https://idp.example.com</saml:Issuer>
<Status><StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></Status>
{signed_assertion}
</samlp:Response>"#
);
let first = provider.parse_response(&xml).await.expect("首次解析应通过");
assert!(first.assertion.is_some(), "验签通过的 Assertion 不应被剥离");
let second = provider.parse_response(&xml).await;
assert!(
matches!(second, Err(GarrisonError::InvalidParam(ref m)) if m.contains("assertion-replay")),
"同一签名 Assertion 二次提交应被重放防护拒绝,实际: {:?}",
second
);
}
fn build_xsec_signed_response() -> (String, String) {
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let (signed_assertion, public_key_pem) = build_test_signed_assertion_with_inner(
&format!(
"<SubjectConfirmationData NotOnOrAfter=\"{future_str}\"/>\
<saml:Conditions><saml:AudienceRestriction><saml:Audience>https://sp.example.com</saml:Audience></saml:AudienceRestriction></saml:Conditions>"
),
);
let xml = format!(
r#"<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Destination="https://sp.example.com/acs">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<Status><StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></Status>
{signed_assertion}
</samlp:Response>"#
);
(xml, public_key_pem)
}
#[tokio::test]
async fn xmlsec_provider_default_rejects_unconfigured_destination() {
let (xml, public_key_pem) = build_xsec_signed_response();
let provider = XmlSecSamlProvider::new(public_key_pem).unwrap();
let result = provider.parse_response(&xml).await;
assert!(
matches!(result, Err(GarrisonError::InvalidParam(ref m)) if m.contains("sso-saml-destination-not-configured")),
"缺省未配置 Destination 应 fail-closed 拒绝,实际: {:?}",
result
);
}
#[tokio::test]
async fn xmlsec_provider_default_rejects_unconfigured_audience() {
let (xml, public_key_pem) = build_xsec_signed_response();
let provider = XmlSecSamlProvider::new(public_key_pem)
.unwrap()
.allow_unvalidated_destination();
let result = provider.parse_response(&xml).await;
assert!(
matches!(result, Err(GarrisonError::InvalidParam(ref m)) if m.contains("sso-saml-audience-not-configured")),
"缺省未配置 Audience 应 fail-closed 拒绝,实际: {:?}",
result
);
}
#[tokio::test]
async fn xmlsec_provider_with_expected_config_accepts_signed_assertion() {
let (xml, public_key_pem) = build_xsec_signed_response();
let provider = XmlSecSamlProvider::new(public_key_pem)
.unwrap()
.with_expected_destination("https://sp.example.com/acs".to_string())
.with_expected_audience("https://sp.example.com".to_string());
let response = provider.parse_response(&xml).await.unwrap();
let assertion = response.assertion.expect("显式配置 + 签名合法断言应保留");
assert_eq!(assertion.subject, "user@example.com");
}
#[tokio::test]
async fn xmlsec_provider_allow_unvalidated_opt_out_passes_signed_assertion() {
let (xml, public_key_pem) = build_xsec_signed_response();
let provider = XmlSecSamlProvider::new(public_key_pem)
.unwrap()
.allow_unvalidated_destination()
.allow_unvalidated_audience();
let response = provider.parse_response(&xml).await.unwrap();
assert!(
response.assertion.is_some(),
"显式豁免 Destination/Audience 后签名合法断言应放行"
);
}
#[tokio::test]
async fn xmlsec_provider_rejects_xsw_cdata_forged_assertion() {
let (evil_assertion, public_key_pem) = build_xsw_cdata_evil_assertion();
let xml = format!(
r#"<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
Destination="https://sp.example.com/acs">
<saml:Issuer>https://idp.example.com</saml:Issuer>
<Status><StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></Status>
{evil_assertion}
</samlp:Response>"#
);
let dao = std::sync::Arc::new(crate::dao::tests::MockDao::new());
let provider = XmlSecSamlProvider::new(public_key_pem)
.unwrap()
.with_request_dao(dao)
.with_expected_destination("https://sp.example.com/acs".to_string())
.allow_unvalidated_audience();
let out = provider.parse_response(&xml).await;
match out {
Ok(resp) => {
let a = resp.assertion.as_ref();
assert!(
a.is_none(),
"XSW CDATA 变体断言必须被剥离,实际保留: subject={:?} attrs={:?}",
a.map(|x| &x.subject),
a.map(|x| &x.attributes)
);
},
Err(e) => {
let msg = format!("{e:?}");
assert!(
!msg.contains("not-configured"),
"本用例已显式配置校验,不应因缺省配置拒绝: {msg}"
);
},
}
}
}
#[tokio::test]
async fn check_assertion_replay_concurrent_atomic_get_and_delete() {
let dao = std::sync::Arc::new(crate::dao::tests::MockDao::new());
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let assertion_id = "assertion-toctou-concurrent-001".to_string();
let mut handles = Vec::new();
for _ in 0..10 {
let dao_clone = dao.clone();
let id = assertion_id.clone();
let ttl = future_str.clone();
handles.push(tokio::spawn(async move {
check_assertion_replay(&id, &ttl, &*dao_clone).await
}));
}
let mut success_count = 0usize;
let mut failure_count = 0usize;
for handle in handles {
let result = handle.await.expect("并发任务不应 panic");
match result {
Ok(true) => success_count += 1,
Ok(false) => failure_count += 1,
Err(e) => panic!("check_assertion_replay 不应报错: {:?}", e),
}
}
assert!(
success_count >= 1,
"至少一个任务应成功消费,实际成功: {}",
success_count
);
assert!(
failure_count >= 1,
"至少一个任务应被拒绝(get_and_delete 原子性),实际拒绝: {}",
failure_count
);
assert_eq!(
success_count + failure_count,
10,
"所有 10 个任务都应正常完成"
);
}
#[tokio::test]
async fn check_assertion_replay_serial_replay_protection() {
let dao = crate::dao::tests::MockDao::new();
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let first = check_assertion_replay("assertion-serial-001", &future_str, &dao)
.await
.expect("首次 check 不应报错");
assert!(first, "首次消费应通过");
let second = check_assertion_replay("assertion-serial-001", &future_str, &dao)
.await
.expect("二次 check 不应报错");
assert!(
!second,
"同一 Assertion ID 二次消费应被拒绝(vuln-0003 重放防护)"
);
}
#[test]
fn parse_saml_response_xml_extracts_raw_xml() {
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let xml = format!(
r#"<Response>
<Assertion ID="raw-xml-001">
<Issuer>https://idp.example.com</Issuer>
<Subject><NameID>user@example.com</NameID></Subject>
<Audience>https://sp.example.com</Audience>
<SubjectConfirmationData NotOnOrAfter="{}"/>
</Assertion>
</Response>"#,
future_str
);
let result = parse_saml_response_xml(&xml).unwrap();
let assertion = result.assertion.expect("应解析出 Assertion");
let raw = assertion.raw_xml.expect("raw_xml 应被填充");
let trimmed = raw.trim_start();
assert!(
trimmed.starts_with("<Assertion"),
"raw_xml 应以 <Assertion 开头(trim 后),实际: {}",
&trimmed[..trimmed.len().min(50)]
);
assert!(
raw.contains("</Assertion>") || raw.contains("</Assertion >"),
"raw_xml 应包含 </Assertion> 闭合标签"
);
assert!(
raw.contains("ID=\"raw-xml-001\""),
"raw_xml 应包含 Assertion ID"
);
}
#[test]
fn parse_saml_response_xml_extracts_assertion_fields() {
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let xml = format!(
r#"<Response Destination="https://sp.example.com/acs">
<Issuer>https://idp.example.com</Issuer>
<Status><StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></Status>
<Assertion ID="fields-001">
<Issuer>https://idp-assertion.example.com</Issuer>
<Subject><NameID>user@example.com</NameID></Subject>
<Audience>https://sp.example.com</Audience>
<SubjectConfirmationData NotOnOrAfter="{}"/>
<AttributeStatement>
<Attribute Name="email"><AttributeValue>user@example.com</AttributeValue></Attribute>
<Attribute Name="role"><AttributeValue>admin</AttributeValue></Attribute>
</AttributeStatement>
</Assertion>
</Response>"#,
future_str
);
let result = parse_saml_response_xml(&xml).unwrap();
assert_eq!(result.destination, "https://sp.example.com/acs");
assert_eq!(result.issuer, "https://idp.example.com");
assert_eq!(
result.status_code,
"urn:oasis:names:tc:SAML:2.0:status:Success"
);
let assertion = result.assertion.expect("应解析出 Assertion");
assert_eq!(assertion.id, "fields-001");
assert_eq!(assertion.issuer, "https://idp-assertion.example.com");
assert_eq!(assertion.subject, "user@example.com");
assert_eq!(assertion.audience, "https://sp.example.com");
assert_eq!(assertion.attributes.len(), 2);
assert_eq!(
assertion.attributes[0],
("email".to_string(), "user@example.com".to_string())
);
assert_eq!(
assertion.attributes[1],
("role".to_string(), "admin".to_string())
);
}
#[test]
fn parse_saml_response_xml_handles_empty_assertion() {
let future = Utc::now().timestamp() + 3600;
let future_str = chrono::DateTime::from_timestamp(future, 0)
.unwrap()
.to_rfc3339();
let xml = format!(
r#"<Response>
<Assertion ID="empty-001">
<SubjectConfirmationData NotOnOrAfter="{}"/>
</Assertion>
</Response>"#,
future_str
);
let result = parse_saml_response_xml(&xml).unwrap();
let assertion = result.assertion.expect("空 Assertion 仍应被解析");
assert_eq!(assertion.id, "empty-001");
assert!(assertion.issuer.is_empty());
assert!(assertion.subject.is_empty());
assert!(assertion.audience.is_empty());
assert!(assertion.attributes.is_empty());
}
#[test]
fn parse_saml_response_rejects_missing_not_on_or_after() {
let xml = r#"<Response>
<Assertion ID="no-expiry-001">
<Issuer>https://idp.example.com</Issuer>
<Subject><NameID>user@example.com</NameID></Subject>
</Assertion>
</Response>"#;
let result = parse_saml_response_xml(xml);
assert!(
matches!(result, Err(GarrisonError::InvalidToken(ref m)) if m.contains("missing-not-on-or-after")),
"缺失 NotOnOrAfter 应 fail-closed 拒绝,实际: {:?}",
result
);
}
#[test]
fn parse_saml_response_rejects_future_not_before() {
let not_before = Utc::now().timestamp() + 3600;
let nb_str = chrono::DateTime::from_timestamp(not_before, 0)
.unwrap()
.to_rfc3339();
let expiry = Utc::now().timestamp() + 7200;
let exp_str = chrono::DateTime::from_timestamp(expiry, 0)
.unwrap()
.to_rfc3339();
let xml = format!(
r#"<Response>
<Assertion ID="future-nb-001">
<Issuer>https://idp.example.com</Issuer>
<Conditions NotBefore="{}"/>
<Subject><NameID>user@example.com</NameID></Subject>
<SubjectConfirmationData NotOnOrAfter="{}"/>
</Assertion>
</Response>"#,
nb_str, exp_str
);
let result = parse_saml_response_xml(&xml);
assert!(
matches!(result, Err(GarrisonError::InvalidToken(ref m)) if m.contains("not-yet-valid")),
"未来 NotBefore 应被拒绝,实际: {:?}",
result
);
}
#[test]
fn parse_saml_response_accepts_valid_not_before_window() {
let not_before = Utc::now().timestamp() - 300;
let nb_str = chrono::DateTime::from_timestamp(not_before, 0)
.unwrap()
.to_rfc3339();
let expiry = Utc::now().timestamp() + 3600;
let exp_str = chrono::DateTime::from_timestamp(expiry, 0)
.unwrap()
.to_rfc3339();
let xml = format!(
r#"<Response>
<Assertion ID="valid-window-001">
<Issuer>https://idp.example.com</Issuer>
<Conditions NotBefore="{}"/>
<Subject><NameID>user@example.com</NameID></Subject>
<SubjectConfirmationData NotOnOrAfter="{}"/>
</Assertion>
</Response>"#,
nb_str, exp_str
);
let result = parse_saml_response_xml(&xml);
assert!(result.is_ok(), "合法时间窗口不应报错,实际: {:?}", result);
}
#[test]
fn parse_saml_response_xml_no_assertion() {
let xml = r#"<Response Destination="https://sp.example.com">
<Issuer>https://idp.example.com</Issuer>
<Status><StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Responder"/></Status>
</Response>"#;
let result = parse_saml_response_xml(xml).unwrap();
assert!(result.assertion.is_none());
assert_eq!(result.destination, "https://sp.example.com");
assert_eq!(result.issuer, "https://idp.example.com");
}
fn build_sp_initiated_response(in_response_to: &str) -> String {
format!(
r#"<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
Destination="https://sp.example.com/acs" InResponseTo="{in_response_to}">
<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">https://idp.example.com</saml:Issuer>
<Status><StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></Status>
</samlp:Response>"#
)
}
#[tokio::test]
async fn in_response_to_binding_first_pass_then_rejected() {
use crate::dao::GarrisonDao as _;
let dao = std::sync::Arc::new(crate::dao::tests::MockDao::new());
let provider = DefaultSamlProvider::new()
.unwrap()
.with_request_dao(dao.clone());
let request = provider
.build_authn_request("sp", "https://sp/acs", "https://idp/sso")
.await
.unwrap();
assert!(
dao.get(&format!("{}req:{}", DaoKeyPrefix::Saml, request.id))
.await
.unwrap()
.is_some(),
"build_authn_request 应记录在途请求 ID"
);
let xml = build_sp_initiated_response(&request.id);
provider
.parse_response(&xml)
.await
.expect("首次消费在途请求 ID 应通过");
let second = provider.parse_response(&xml).await;
assert!(
matches!(second, Err(GarrisonError::InvalidParam(ref m)) if m.contains("in-response-to-unknown")),
"同一 InResponseTo 二次消费应被拒绝,实际: {:?}",
second
);
}
#[tokio::test]
async fn in_response_to_unknown_rejected() {
let dao = std::sync::Arc::new(crate::dao::tests::MockDao::new());
let provider = DefaultSamlProvider::new().unwrap().with_request_dao(dao);
let xml = build_sp_initiated_response("_never-issued-id");
let result = provider.parse_response(&xml).await;
assert!(
matches!(result, Err(GarrisonError::InvalidParam(ref m)) if m.contains("in-response-to-unknown")),
"未注册的 InResponseTo 应被拒绝,实际: {:?}",
result
);
}
#[tokio::test]
async fn in_response_to_without_dao_fail_closed() {
let provider = DefaultSamlProvider::new().unwrap();
let xml = build_sp_initiated_response("_some-request-id");
let result = provider.parse_response(&xml).await;
assert!(
matches!(result, Err(GarrisonError::InvalidParam(ref m)) if m.contains("unverifiable")),
"无 DAO 时 SP-initiated 响应应 fail-closed 拒绝,实际: {:?}",
result
);
}
#[tokio::test]
async fn idp_initiated_response_passes_without_dao() {
let provider = DefaultSamlProvider::new().unwrap();
let xml = r#"<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
Destination="https://sp.example.com/acs">
<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">https://idp.example.com</saml:Issuer>
<Status><StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></Status>
</samlp:Response>"#;
let result = provider.parse_response(xml).await;
assert!(
result.is_ok(),
"IdP-initiated 响应应放行,实际: {:?}",
result
);
}
}