saml-rs 0.5.0

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

#[derive(Debug)]
enum OutboundLogoutElement {
    Root,
    Issuer,
    Signature,
    Extensions { child_seen: bool },
    Status,
    StatusCode { child_seen: bool },
    StatusMessage,
    StatusDetail,
    ExtensionContent,
}

#[derive(Debug, Default)]
enum RootStage {
    #[default]
    ExpectIssuer,
    AfterIssuer,
    AfterSignature,
    AfterExtensions,
    AfterStatus,
}

#[derive(Debug, Default)]
enum StatusStage {
    #[default]
    ExpectStatusCode,
    AfterStatusCode,
    AfterStatusMessage,
    AfterStatusDetail,
}

#[derive(Debug, Default)]
struct OutboundLogoutState {
    root_stage: RootStage,
    status_stage: StatusStage,
    saw_signature: bool,
    destination: Option<String>,
}

struct OutboundLogoutExpectation<'a> {
    id: &'a str,
    destination: &'a str,
    issuer: &'a str,
    in_response_to: Option<&'a str>,
    validation: OutboundLogoutValidation,
}

#[derive(Debug, Clone, Copy)]
pub(crate) enum OutboundLogoutValidation {
    BeforeSigning { destination_required: bool },
    AfterPostSigning,
}

impl OutboundLogoutValidation {
    fn destination_required(self) -> bool {
        match self {
            Self::BeforeSigning {
                destination_required,
            } => destination_required,
            Self::AfterPostSigning => true,
        }
    }

    fn root_signature_required(self) -> bool {
        matches!(self, Self::AfterPostSigning)
    }

    fn root_signature_allowed(self) -> bool {
        matches!(self, Self::AfterPostSigning)
    }
}

fn is_xml_name_start_char(value: char) -> bool {
    matches!(
        value,
        'A'..='Z'
            | '_'
            | 'a'..='z'
            | '\u{c0}'..='\u{d6}'
            | '\u{d8}'..='\u{f6}'
            | '\u{f8}'..='\u{2ff}'
            | '\u{370}'..='\u{37d}'
            | '\u{37f}'..='\u{1fff}'
            | '\u{200c}'..='\u{200d}'
            | '\u{2070}'..='\u{218f}'
            | '\u{2c00}'..='\u{2fef}'
            | '\u{3001}'..='\u{d7ff}'
            | '\u{f900}'..='\u{fdcf}'
            | '\u{fdf0}'..='\u{fffd}'
            | '\u{10000}'..='\u{effff}'
    )
}

fn is_xml_name_char(value: char) -> bool {
    is_xml_name_start_char(value)
        || matches!(
            value,
            '-' | '.'
                | '0'..='9'
                | '\u{b7}'
                | '\u{300}'..='\u{36f}'
                | '\u{203f}'..='\u{2040}'
        )
}

fn is_ncname(value: &str) -> bool {
    let mut chars = value.chars();
    chars.next().is_some_and(is_xml_name_start_char) && chars.all(is_xml_name_char)
}

fn attribute_value<'a>(values: &'a [(Vec<u8>, String)], name: &[u8]) -> Option<&'a str> {
    values
        .iter()
        .find_map(|(candidate, value)| (candidate == name).then_some(value.as_str()))
}

fn require_generated_issue_instant(
    values: &[(Vec<u8>, String)],
    element: &BytesStart<'_>,
) -> Result<(), SamlError> {
    let issue_instant = attribute_value(values, b"IssueInstant").ok_or_else(|| {
        profile_error(format!(
            "{} is missing required unqualified attribute IssueInstant",
            element_label(element),
        ))
    })?;
    if parse_generated_saml_utc_date_time(issue_instant).is_none() {
        return Err(profile_error(format!(
            "{} IssueInstant must use the SAML-conformant UTC xs:dateTime form ending in Z",
            element_label(element),
        )));
    }
    Ok(())
}

fn validate_outbound_logout_root(
    reader: &NsReader<&[u8]>,
    element: &BytesStart<'_>,
    element_namespace: NamespaceKind,
    expectation: &OutboundLogoutExpectation<'_>,
    state: &mut OutboundLogoutState,
) -> Result<(), SamlError> {
    if element.local_name().as_ref() != b"LogoutResponse"
        || element_namespace != NamespaceKind::Protocol
    {
        return Err(profile_error(format!(
            "expected root {{{}}}LogoutResponse, got {}",
            namespace::PROTOCOL,
            element_label(element),
        )));
    }
    let attributes = validate_closed_unqualified_attributes(
        reader,
        element,
        &[
            b"ID",
            b"InResponseTo",
            b"Version",
            b"IssueInstant",
            b"Destination",
            b"Consent",
        ],
        &[b"ID", b"Version", b"IssueInstant"],
    )?;
    require_version_2(&attributes, element)?;
    require_generated_issue_instant(&attributes, element)?;

    let id = attribute_value(&attributes, b"ID").ok_or_else(|| {
        profile_error("LogoutResponse is missing required unqualified attribute ID")
    })?;
    if !is_ncname(id) {
        return Err(profile_error(
            "LogoutResponse ID must use the XML Schema xs:ID lexical form",
        ));
    }
    if id != expectation.id {
        return Err(SamlError::Invalid(format!(
            "outbound LogoutResponse ID mismatch: expected {}, got {id}",
            expectation.id
        )));
    }

    state.destination = attribute_value(&attributes, b"Destination").map(str::to_string);
    if let Some(actual) = state.destination.as_deref() {
        if !is_absolute_saml_uri(actual) {
            return Err(profile_error(
                "LogoutResponse Destination must be a non-empty absolute URI without whitespace",
            ));
        }
        if actual != expectation.destination {
            return Err(SamlError::destination_mismatch(
                expectation.destination,
                Some(actual),
            ));
        }
    }

    if let Some(consent) = attribute_value(&attributes, b"Consent") {
        if !is_absolute_saml_uri(consent) {
            return Err(profile_error(
                "LogoutResponse Consent must be a non-empty absolute URI without whitespace",
            ));
        }
    }

    let actual_in_response_to = attribute_value(&attributes, b"InResponseTo");
    if actual_in_response_to.is_some_and(|value| !is_ncname(value)) {
        return Err(profile_error(
            "LogoutResponse InResponseTo must use the XML Schema NCName lexical form",
        ));
    }
    // SAML Core 2.0 §3.2.2 requires correlation when the request is known and
    // requires omission when there is no known request to reference.
    if actual_in_response_to != expectation.in_response_to {
        return Err(SamlError::in_response_to_mismatch(
            expectation.in_response_to,
            actual_in_response_to,
        ));
    }
    Ok(())
}

fn validate_outbound_issuer(
    reader: &NsReader<&[u8]>,
    element: &BytesStart<'_>,
    element_namespace: NamespaceKind,
) -> Result<(), SamlError> {
    if element_namespace != NamespaceKind::Assertion {
        return Err(profile_error("Issuer has an invalid namespace"));
    }
    let attributes = validate_closed_unqualified_attributes(
        reader,
        element,
        &[
            b"Format",
            b"NameQualifier",
            b"SPNameQualifier",
            b"SPProvidedID",
        ],
        &[],
    )?;
    if let Some(format) = attribute_value(&attributes, b"Format") {
        if format != name_id_format::ENTITY {
            return Err(profile_error(
                "LogoutResponse Issuer Format must be omitted or use the entity identifier format",
            ));
        }
    }
    if [
        b"NameQualifier".as_slice(),
        b"SPNameQualifier".as_slice(),
        b"SPProvidedID".as_slice(),
    ]
    .iter()
    .any(|name| attribute_value(&attributes, name).is_some())
    {
        return Err(profile_error(
            "entity-format LogoutResponse Issuer must omit NameQualifier, SPNameQualifier, and SPProvidedID",
        ));
    }
    Ok(())
}

fn is_absolute_saml_uri(value: &str) -> bool {
    !value.is_empty() && !value.chars().any(char::is_whitespace) && Url::parse(value).is_ok()
}

fn is_entity_identifier(value: &str) -> bool {
    value.chars().count() <= 1024 && is_absolute_saml_uri(value)
}

fn validate_outbound_status_code(
    reader: &NsReader<&[u8]>,
    element: &BytesStart<'_>,
    element_namespace: NamespaceKind,
    top_level: bool,
) -> Result<(), SamlError> {
    if element_namespace != NamespaceKind::Protocol {
        return Err(profile_error("StatusCode has an invalid namespace"));
    }
    let attributes =
        validate_closed_unqualified_attributes(reader, element, &[b"Value"], &[b"Value"])?;
    let value = attribute_value(&attributes, b"Value").ok_or_else(|| {
        profile_error("StatusCode is missing required unqualified attribute Value")
    })?;
    if !is_absolute_saml_uri(value) {
        return Err(profile_error(
            "StatusCode Value must be a non-empty absolute URI without whitespace",
        ));
    }
    if top_level
        && !matches!(
            value,
            status_code::SUCCESS
                | status_code::REQUESTER
                | status_code::RESPONDER
                | status_code::VERSION_MISMATCH
        )
    {
        return Err(profile_error(
            "top-level StatusCode Value must be Success, Requester, Responder, or VersionMismatch",
        ));
    }
    Ok(())
}

fn validate_outbound_root_child(
    reader: &NsReader<&[u8]>,
    element: &BytesStart<'_>,
    element_namespace: NamespaceKind,
    expectation: &OutboundLogoutExpectation<'_>,
    state: &mut OutboundLogoutState,
) -> Result<OutboundLogoutElement, SamlError> {
    let local = element.local_name();
    match (local.as_ref(), element_namespace, &state.root_stage) {
        (b"Issuer", NamespaceKind::Assertion, RootStage::ExpectIssuer) => {
            validate_outbound_issuer(reader, element, element_namespace)?;
            state.root_stage = RootStage::AfterIssuer;
            Ok(OutboundLogoutElement::Issuer)
        }
        (b"Signature", NamespaceKind::Dsig, RootStage::AfterIssuer) => {
            if !expectation.validation.root_signature_allowed() {
                // Library policy centralizes root signature construction. This
                // also ensures Redirect can meet Bindings 2.0 §3.4.4.1 before
                // DEFLATE without teaching generic binding helpers about XML.
                return Err(profile_error(
                    "outbound LogoutResponse templates must not contain a root ds:Signature before library signing",
                ));
            }
            state.root_stage = RootStage::AfterSignature;
            state.saw_signature = true;
            Ok(OutboundLogoutElement::Signature)
        }
        (
            b"Extensions",
            NamespaceKind::Protocol,
            RootStage::AfterIssuer | RootStage::AfterSignature,
        ) => {
            validate_closed_unqualified_attributes(reader, element, &[], &[])?;
            state.root_stage = RootStage::AfterExtensions;
            Ok(OutboundLogoutElement::Extensions { child_seen: false })
        }
        (
            b"Status",
            NamespaceKind::Protocol,
            RootStage::AfterIssuer | RootStage::AfterSignature | RootStage::AfterExtensions,
        ) => {
            validate_closed_unqualified_attributes(reader, element, &[], &[])?;
            state.root_stage = RootStage::AfterStatus;
            Ok(OutboundLogoutElement::Status)
        }
        _ => Err(profile_error(
            "LogoutResponse children must be Issuer, optional Signature, optional Extensions, and exactly one final Status",
        )),
    }
}

fn validate_outbound_status_child(
    reader: &NsReader<&[u8]>,
    element: &BytesStart<'_>,
    element_namespace: NamespaceKind,
    state: &mut OutboundLogoutState,
) -> Result<OutboundLogoutElement, SamlError> {
    let local = element.local_name();
    match (local.as_ref(), element_namespace, &state.status_stage) {
        (
            b"StatusCode",
            NamespaceKind::Protocol,
            StatusStage::ExpectStatusCode,
        ) => {
            validate_outbound_status_code(reader, element, element_namespace, true)?;
            state.status_stage = StatusStage::AfterStatusCode;
            Ok(OutboundLogoutElement::StatusCode { child_seen: false })
        }
        (
            b"StatusMessage",
            NamespaceKind::Protocol,
            StatusStage::AfterStatusCode,
        ) => {
            validate_closed_unqualified_attributes(reader, element, &[], &[])?;
            state.status_stage = StatusStage::AfterStatusMessage;
            Ok(OutboundLogoutElement::StatusMessage)
        }
        (
            b"StatusDetail",
            NamespaceKind::Protocol,
            StatusStage::AfterStatusCode | StatusStage::AfterStatusMessage,
        ) => {
            validate_closed_unqualified_attributes(reader, element, &[], &[])?;
            state.status_stage = StatusStage::AfterStatusDetail;
            Ok(OutboundLogoutElement::StatusDetail)
        }
        _ => Err(profile_error(
            "Status children must be exactly one StatusCode, optional StatusMessage, and optional StatusDetail in that order",
        )),
    }
}

fn validate_outbound_logout_start(
    reader: &NsReader<&[u8]>,
    element: &BytesStart<'_>,
    element_namespace: NamespaceKind,
    stack: &mut [OutboundLogoutElement],
    expectation: &OutboundLogoutExpectation<'_>,
    state: &mut OutboundLogoutState,
) -> Result<OutboundLogoutElement, SamlError> {
    let Some(parent) = stack.last_mut() else {
        validate_outbound_logout_root(reader, element, element_namespace, expectation, state)?;
        return Ok(OutboundLogoutElement::Root);
    };

    match parent {
        OutboundLogoutElement::Root => {
            validate_outbound_root_child(reader, element, element_namespace, expectation, state)
        }
        OutboundLogoutElement::Issuer => Err(profile_error(
            "LogoutResponse Issuer must not contain child elements",
        )),
        OutboundLogoutElement::Status => {
            validate_outbound_status_child(reader, element, element_namespace, state)
        }
        OutboundLogoutElement::StatusCode { child_seen } => {
            if *child_seen
                || element.local_name().as_ref() != b"StatusCode"
                || element_namespace != NamespaceKind::Protocol
            {
                return Err(profile_error(
                    "StatusCode may contain at most one subordinate StatusCode",
                ));
            }
            validate_outbound_status_code(reader, element, element_namespace, false)?;
            *child_seen = true;
            Ok(OutboundLogoutElement::StatusCode { child_seen: false })
        }
        OutboundLogoutElement::StatusMessage => Err(profile_error(
            "StatusMessage must not contain child elements",
        )),
        OutboundLogoutElement::Extensions { child_seen } => {
            // Core 2.0 §3.2.2 narrows the protocol schema's ##other wildcard:
            // direct extension elements must use a namespace not defined by SAML.
            if !matches!(
                element_namespace,
                NamespaceKind::Dsig | NamespaceKind::XmlEncryption | NamespaceKind::Other
            ) {
                return Err(profile_error(
                    "Extensions direct children must use a namespace not defined by SAML",
                ));
            }
            *child_seen = true;
            Ok(OutboundLogoutElement::ExtensionContent)
        }
        OutboundLogoutElement::StatusDetail
        | OutboundLogoutElement::Signature
        | OutboundLogoutElement::ExtensionContent => Ok(OutboundLogoutElement::ExtensionContent),
    }
}

fn finish_outbound_logout_element(
    element: &OutboundLogoutElement,
    state: &OutboundLogoutState,
) -> Result<(), SamlError> {
    match element {
        OutboundLogoutElement::Root if !matches!(state.root_stage, RootStage::AfterStatus) => Err(
            profile_error("LogoutResponse must contain Issuer and exactly one final Status"),
        ),
        OutboundLogoutElement::Status
            if matches!(state.status_stage, StatusStage::ExpectStatusCode) =>
        {
            Err(profile_error(
                "Status must contain exactly one StatusCode as its first child",
            ))
        }
        OutboundLogoutElement::Extensions { child_seen: false } => Err(profile_error(
            "Extensions must contain at least one extension element",
        )),
        _ => Ok(()),
    }
}

fn validate_structural_text(
    parent: Option<&OutboundLogoutElement>,
    text: &[u8],
) -> Result<(), SamlError> {
    if is_structural_element(parent)
        && !text
            .iter()
            .all(|byte| matches!(byte, b' ' | b'\t' | b'\r' | b'\n'))
    {
        return Err(profile_error(
            "structural LogoutResponse elements may contain only whitespace text",
        ));
    }
    Ok(())
}

fn is_structural_element(parent: Option<&OutboundLogoutElement>) -> bool {
    matches!(
        parent,
        Some(
            OutboundLogoutElement::Root
                | OutboundLogoutElement::Status
                | OutboundLogoutElement::StatusCode { .. }
                | OutboundLogoutElement::Extensions { .. }
                | OutboundLogoutElement::StatusDetail
        )
    )
}

fn validate_structural_reference(
    parent: Option<&OutboundLogoutElement>,
    reference: &BytesRef<'_>,
) -> Result<(), SamlError> {
    if is_structural_element(parent)
        && !matches!(
            reference
                .resolve_char_ref()
                .map_err(|error| SamlError::Xml(error.to_string()))?,
            Some(' ' | '\t' | '\r' | '\n')
        )
    {
        return Err(profile_error(
            "structural LogoutResponse elements may contain only whitespace text",
        ));
    }
    Ok(())
}

fn validate_outbound_logout_stream(
    xml: &str,
    expectation: &OutboundLogoutExpectation<'_>,
) -> Result<OutboundLogoutState, SamlError> {
    let mut reader = NsReader::from_str(xml);
    let mut stack = Vec::new();
    let mut state = OutboundLogoutState::default();

    loop {
        let (resolved, event) = reader
            .read_resolved_event()
            .map_err(|error| SamlError::Xml(error.to_string()))?;
        let element_namespace = classify_namespace(resolved);
        match event {
            Event::Start(element) => {
                let current = validate_outbound_logout_start(
                    &reader,
                    &element,
                    element_namespace,
                    &mut stack,
                    expectation,
                    &mut state,
                )?;
                stack.push(current);
            }
            Event::Empty(element) => {
                let current = validate_outbound_logout_start(
                    &reader,
                    &element,
                    element_namespace,
                    &mut stack,
                    expectation,
                    &mut state,
                )?;
                finish_outbound_logout_element(&current, &state)?;
            }
            Event::End(_) => {
                let current = stack
                    .pop()
                    .ok_or_else(|| SamlError::Xml("unexpected closing element".into()))?;
                finish_outbound_logout_element(&current, &state)?;
            }
            Event::Text(text) => {
                let text = text
                    .decode()
                    .map_err(|error| SamlError::Xml(error.to_string()))?;
                validate_structural_text(stack.last(), text.as_bytes())?;
            }
            Event::CData(text) => {
                validate_structural_text(stack.last(), text.as_ref())?;
            }
            Event::GeneralRef(reference) => {
                validate_structural_reference(stack.last(), &reference)?;
            }
            Event::DocType(_) => return Err(SamlError::Xml("DOCTYPE is not allowed".into())),
            Event::Eof => break,
            Event::Decl(_) | Event::Comment(_) | Event::PI(_) => {}
        }
    }
    Ok(state)
}

pub(crate) fn validate_logout_response_outbound(
    xml: &str,
    expected_id: &str,
    expected_destination: &str,
    expected_issuer: &str,
    expected_in_response_to: Option<&str>,
    validation: OutboundLogoutValidation,
) -> Result<Document, SamlError> {
    let document = parse_with_limits(xml, XmlLimits::unbounded())?;
    let expectation = OutboundLogoutExpectation {
        id: expected_id,
        destination: expected_destination,
        issuer: expected_issuer,
        in_response_to: expected_in_response_to,
        validation,
    };
    let state = validate_outbound_logout_stream(xml, &expectation)?;

    let issuer = document.root.children.first().ok_or_else(|| {
        profile_error("LogoutResponse must contain an Issuer before all other children")
    })?;
    if issuer.text.is_empty() {
        return Err(profile_error(
            "LogoutResponse Issuer must identify the responding entity",
        ));
    }
    if !is_entity_identifier(&issuer.text) {
        return Err(profile_error(
            "entity-format LogoutResponse Issuer must be an absolute URI no longer than 1024 characters",
        ));
    }
    if issuer.text != expectation.issuer {
        return Err(SamlError::issuer_mismatch(
            expectation.issuer,
            Some(&issuer.text),
        ));
    }
    if (expectation.validation.destination_required() || state.saw_signature)
        && state.destination.is_none()
    {
        return Err(SamlError::destination_mismatch(
            expectation.destination,
            None,
        ));
    }
    if expectation.validation.root_signature_required() && !state.saw_signature {
        return Err(profile_error(
            "signed POST LogoutResponse must contain a root ds:Signature in schema order",
        ));
    }
    Ok(document)
}

#[cfg(test)]
#[path = "outbound_logout/tests.rs"]
mod tests;