xml-sec 0.1.16

Pure Rust XML Security: XMLDSig, XMLEnc, C14N. Drop-in replacement for libxmlsec1.
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
use std::collections::HashSet;

use xml_sec::c14n::{C14nAlgorithm, C14nMode, canonicalize};
use xml_sec::policy::{PolicyViolation, SigningPolicy};
use xml_sec::xmldsig::transforms::MAX_TRANSFORMS_PER_REFERENCE;
use xml_sec::xmldsig::{
    DigestAlgorithm, ReferenceBuilder, SignatureAlgorithm, SignatureBuilder, SignatureBuilderError,
    Transform, UriTypeSet, XPathExpression, XPathFilter, XPathFilterOperation, parse_transforms,
};

const DSIG_NS: &str = "http://www.w3.org/2000/09/xmldsig#";
const XMLNS_NS: &str = "http://www.w3.org/2000/xmlns/";

fn exclusive_c14n() -> C14nAlgorithm {
    C14nAlgorithm::new(C14nMode::Exclusive1_0, false)
}

#[test]
fn builds_parseable_prefixed_template_in_required_order() {
    // This guards the schema order consumed by xmlsec1 and our strict parser.
    let xml = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .ns_prefix("ds")
        .signature_id("sig-1")
        .add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256)
                .uri("#assertion&1")
                .id("ref-1")
                .ref_type("urn:test:kind")
                .transform(Transform::Enveloped)
                .transform(Transform::C14n(exclusive_c14n())),
        )
        .key_info(true)
        .build_template()
        .expect("valid template");

    let document = roxmltree::Document::parse(&xml).expect("builder must emit valid XML");
    let signature = document.root_element();
    assert_eq!(signature.tag_name().namespace(), Some(DSIG_NS));
    assert_eq!(signature.attribute("Id"), Some("sig-1"));
    let children: Vec<_> = signature
        .children()
        .filter(roxmltree::Node::is_element)
        .map(|node| node.tag_name().name())
        .collect();
    assert_eq!(children, ["SignedInfo", "SignatureValue", "KeyInfo"]);

    let signed_info = signature
        .children()
        .find(|node| node.has_tag_name((DSIG_NS, "SignedInfo")))
        .expect("SignedInfo");
    let reference = signed_info
        .children()
        .find(|node| node.has_tag_name((DSIG_NS, "Reference")))
        .expect("Reference");
    assert_eq!(reference.attribute("URI"), Some("#assertion&1"));
    let reference_children: Vec<_> = reference
        .children()
        .filter(roxmltree::Node::is_element)
        .map(|node| node.tag_name().name())
        .collect();
    assert_eq!(
        reference_children,
        ["Transforms", "DigestMethod", "DigestValue"]
    );
    let transforms = reference
        .children()
        .find(|node| node.has_tag_name((DSIG_NS, "Transforms")))
        .expect("Transforms");
    assert_eq!(
        parse_transforms(transforms)
            .expect("valid transforms")
            .len(),
        2
    );
    let digest_value = reference
        .children()
        .find(|node| node.has_tag_name((DSIG_NS, "DigestValue")))
        .expect("DigestValue");
    assert_eq!(digest_value.text(), None);
}

#[test]
fn preserves_reference_order_and_default_namespace() {
    // Reference order is signed data and must never be normalized or sorted.
    let xml = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::EcdsaSha256)
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha384).uri("#first"))
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha512).uri("#second"))
        .build_template()
        .expect("valid template");
    let document = roxmltree::Document::parse(&xml).expect("valid XML");
    let signature = document.root_element();
    assert_eq!(signature.lookup_namespace_uri(None), Some(DSIG_NS));
    let reference_uris: Vec<_> = signature
        .first_element_child()
        .expect("SignedInfo")
        .children()
        .filter(|node| node.has_tag_name((DSIG_NS, "Reference")))
        .map(|node| node.attribute("URI"))
        .collect();
    assert_eq!(reference_uris, [Some("#first"), Some("#second")]);
}

#[test]
fn default_reference_serializes_explicit_same_document_uri() {
    // The signing parser requires a URI attribute. The builder's ergonomic
    // default therefore denotes the empty same-document URI rather than an
    // omitted attribute that would fail only after template serialization.
    let xml = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256))
        .build_template()
        .expect("the default reference must remain signable");
    let document = roxmltree::Document::parse(&xml).expect("valid XML");
    let reference = document
        .descendants()
        .find(|node| node.has_tag_name((DSIG_NS, "Reference")))
        .expect("Reference");

    assert_eq!(reference.attribute("URI"), Some(""));
}

#[test]
fn rejects_incomplete_or_unsafe_signing_templates() {
    // Builders fail before serialization rather than producing unusable templates.
    let missing = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .build_template()
        .expect_err("Reference is mandatory");
    assert!(matches!(missing, SignatureBuilderError::MissingReference));

    let invalid_prefix = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .ns_prefix("bad:prefix")
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256))
        .build_template()
        .expect_err("prefix must be an NCName");
    assert!(matches!(
        invalid_prefix,
        SignatureBuilderError::InvalidNamespacePrefix(_)
    ));

    let sha1_signature = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha1)
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256))
        .build_template()
        .expect_err("SHA-1 signatures are disabled by the default policy");
    assert!(matches!(
        sha1_signature,
        SignatureBuilderError::Policy(PolicyViolation::Algorithm {
            operation: "signing",
            ..
        })
    ));

    let sha1_digest = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha1))
        .build_template()
        .expect_err("SHA-1 digests are disabled by the default policy");
    assert!(matches!(
        sha1_digest,
        SignatureBuilderError::Policy(PolicyViolation::Algorithm {
            operation: "signing",
            ..
        })
    ));
}

#[test]
fn rejects_reserved_signature_namespace_prefixes() {
    // XML Namespaces reserves both spellings independently of whether a parser
    // happens to accept the resulting declaration syntax. Neither may be bound
    // to the XMLDSig namespace by the public template builder.
    for prefix in ["xml", "xmlns"] {
        let error = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
            .ns_prefix(prefix)
            .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256))
            .build_template()
            .expect_err("reserved namespace prefixes must fail before serialization");

        assert!(matches!(
            error,
            SignatureBuilderError::InvalidNamespacePrefix(value) if value == prefix
        ));
    }
}

#[test]
fn rejects_too_many_references_before_serialization() {
    // Builder output must obey the same cardinality bound as strict parsing and
    // execution instead of producing a template that signing later rejects.
    let mut builder = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256);
    for index in 0..65 {
        builder = builder.add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256).uri(format!("#item-{index}")),
        );
    }

    let error = builder
        .build_template()
        .expect_err("the builder must reject the 65th Reference");

    assert!(matches!(
        error,
        SignatureBuilderError::Policy(PolicyViolation::ResourceLimit {
            resource: "signature references",
            maximum: 64,
            actual: 65,
        })
    ));
}

#[test]
fn rejects_transform_chains_that_execution_cannot_accept() {
    // A builder-produced template must remain parseable and executable by the
    // same crate instead of deferring an oversized chain failure until signing.
    let mut reference = ReferenceBuilder::new(DigestAlgorithm::Sha256);
    for _ in 0..=MAX_TRANSFORMS_PER_REFERENCE {
        reference = reference.transform(Transform::Enveloped);
    }
    let error = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(reference)
        .build_template()
        .expect_err("builder must reject an oversized transform chain");

    assert!(matches!(
        error,
        SignatureBuilderError::Policy(PolicyViolation::ResourceLimit {
            resource: "reference transforms",
            ..
        })
    ));
}

#[test]
fn rejects_node_set_reference_when_generation_c14n11_is_disallowed() {
    // Second Edition generation serializes inclusive C14N 1.1 explicitly, so
    // builder policy must authorize that emitted transform.
    let mut policy = SigningPolicy::default();
    policy.transforms.allowed_algorithms = Some(
        [
            exclusive_c14n().uri().to_owned(),
            xml_sec::xmldsig::ENVELOPED_SIGNATURE_URI.to_owned(),
        ]
        .into_iter()
        .collect(),
    );
    let error = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256).transform(Transform::Enveloped),
        )
        .build_template_with_policy(&policy)
        .expect_err("builder must enforce the generated canonicalization transform");

    assert!(matches!(
        error,
        SignatureBuilderError::Policy(PolicyViolation::Algorithm {
            operation: "signing transform",
            algorithm,
        }) if algorithm == "http://www.w3.org/2006/12/xml-c14n11"
    ));
}

#[test]
fn rejects_xpath_sources_that_parsing_cannot_accept() {
    // Programmatic XPath and Filter 2.0 parameters must pass the same source
    // contract as serialized parameters parsed back during signing.
    let invalid_transforms = [
        ("empty", Transform::XPath(XPathExpression::new(""))),
        ("malformed", Transform::XPath(XPathExpression::new("("))),
        (
            "oversized",
            Transform::XPath(XPathExpression::new("x".repeat(16 * 1024 + 1))),
        ),
        (
            "filter2 malformed",
            Transform::XPathFilter2(vec![XPathFilter::new(
                XPathFilterOperation::Intersect,
                XPathExpression::new("("),
            )]),
        ),
    ];

    for (case, transform) in invalid_transforms {
        let error = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
            .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256).transform(transform))
            .build_template()
            .expect_err("builder must reject an unusable XPath source");
        assert!(error.to_string().contains("XPath"), "case: {case}");
    }
}

#[test]
fn rejects_xpath_namespace_budget_before_serialization() {
    // Parsing enforces the namespace budget for each XPath expression. The
    // builder must reject the same oversized expression before serialization.
    let mut expression = XPathExpression::new("true()");
    for index in 0..=1_024 {
        expression = expression.with_namespace(format!("p{index}"), "urn:test");
    }

    let error = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256).transform(Transform::XPath(expression)),
        )
        .build_template()
        .expect_err("builder must enforce the parser's per-expression namespace budget");

    assert!(matches!(
        error,
        SignatureBuilderError::Policy(PolicyViolation::ResourceLimit {
            resource: "XPath namespace bindings",
            ..
        })
    ));
}

#[test]
fn policy_aware_builder_validates_optimized_xpath_wire_form() {
    // The optimized variant still serializes one XPath expression with one
    // namespace binding. Builder validation must account for that wire form so
    // signing cannot reject a template accepted under the same policy.
    let builder = || {
        SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256).add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256)
                .transform(Transform::XpathExcludeAllSignatures),
        )
    };
    let mut policies = Vec::new();

    let expression_count = SigningPolicy {
        resources: xml_sec::policy::ResourcePolicy {
            max_xpath_expressions: 0,
            ..xml_sec::policy::ResourcePolicy::default()
        },
        ..SigningPolicy::default()
    };
    policies.push(("XPath expressions", expression_count));

    let expression_bytes = SigningPolicy {
        resources: xml_sec::policy::ResourcePolicy {
            max_xpath_expression_bytes: 1,
            ..xml_sec::policy::ResourcePolicy::default()
        },
        ..SigningPolicy::default()
    };
    policies.push(("XPath expression bytes", expression_bytes));

    let expression_complexity = SigningPolicy {
        resources: xml_sec::policy::ResourcePolicy {
            max_xpath_expression_complexity: 0,
            ..xml_sec::policy::ResourcePolicy::default()
        },
        ..SigningPolicy::default()
    };
    policies.push(("XPath expression complexity", expression_complexity));

    let namespace_bindings = SigningPolicy {
        resources: xml_sec::policy::ResourcePolicy {
            max_xpath_namespace_bindings: 0,
            ..xml_sec::policy::ResourcePolicy::default()
        },
        ..SigningPolicy::default()
    };
    policies.push(("XPath namespace bindings", namespace_bindings));

    for (expected_resource, policy) in policies {
        let error = builder()
            .build_template_with_policy(&policy)
            .expect_err("optimized XPath wire content must obey builder policy");
        let SignatureBuilderError::Policy(PolicyViolation::ResourceLimit { resource, .. }) = error
        else {
            panic!("expected {expected_resource} resource rejection, got {error}");
        };
        assert_eq!(resource, expected_resource);
    }
}

#[test]
fn rejects_signature_wide_xpath_expression_budget() {
    // Builder output must obey the parser's aggregate bound rather than emit a
    // template that becomes unacceptable only when signing reparses SignedInfo.
    let filters = (0..64)
        .map(|_| {
            XPathFilter::new(
                XPathFilterOperation::Intersect,
                XPathExpression::new("true()"),
            )
        })
        .collect::<Vec<_>>();
    let mut max_reference = ReferenceBuilder::new(DigestAlgorithm::Sha256).uri("#item-0");
    for _ in 0..63 {
        max_reference = max_reference.transform(Transform::XPathFilter2(filters.clone()));
    }
    let mut builder = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(max_reference);
    builder = builder.add_reference(
        ReferenceBuilder::new(DigestAlgorithm::Sha256)
            .uri("#item-1")
            .transform(Transform::XPathFilter2(filters)),
    );
    builder
        .clone()
        .build_template()
        .expect("the exact signature-wide expression maximum must remain accepted");
    builder = builder.add_reference(
        ReferenceBuilder::new(DigestAlgorithm::Sha256)
            .uri("#item-2")
            .transform(Transform::XPath(XPathExpression::new("true()"))),
    );

    let error = builder
        .build_template()
        .expect_err("builder must enforce the signature-wide XPath expression budget");

    assert!(matches!(
        error,
        SignatureBuilderError::Policy(PolicyViolation::ResourceLimit {
            resource: "XPath expressions",
            ..
        })
    ));
}

#[test]
fn accepts_repeated_inherited_prefix_within_each_xpath_budget() {
    // Every serialized Filter2 XPath inherits the signature prefix independently;
    // one expression must not consume another expression's namespace allowance.
    let filters = (0..64)
        .map(|_| {
            XPathFilter::new(
                XPathFilterOperation::Intersect,
                XPathExpression::new("true()"),
            )
        })
        .collect::<Vec<_>>();
    let reference = (0..17).fold(
        ReferenceBuilder::new(DigestAlgorithm::Sha256),
        |reference, _| reference.transform(Transform::XPathFilter2(filters.clone())),
    );

    let policy = SigningPolicy {
        resources: xml_sec::policy::ResourcePolicy {
            max_xpath_namespace_bindings: 1,
            ..xml_sec::policy::ResourcePolicy::default()
        },
        ..SigningPolicy::default()
    };
    let xml = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .ns_prefix("ds")
        .add_reference(reference)
        .build_template_with_policy(&policy)
        .expect("each XPath contains exactly one inherited namespace binding");

    let document = roxmltree::Document::parse(&xml).expect("builder output must remain valid XML");
    assert_eq!(
        document
            .descendants()
            .filter(
                |node| node.has_tag_name((xml_sec::xmldsig::XPATH_FILTER2_TRANSFORM_URI, "XPath"))
            )
            .count(),
        17 * 64
    );
}

#[test]
fn policy_aware_builder_uses_the_callers_resource_snapshot() {
    // The high-level signing path must not silently validate against defaults
    // before applying its caller-selected immutable policy.
    let mut policy = SigningPolicy::default();
    policy.resources.max_references = 1;
    let builder = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256).uri("#first"))
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256).uri("#second"));

    let error = builder
        .build_template_with_policy(&policy)
        .expect_err("the selected signing policy must reach the builder");

    assert!(matches!(
        error,
        SignatureBuilderError::Policy(PolicyViolation::ResourceLimit {
            resource: "signature references",
            maximum: 1,
            actual: 2,
        })
    ));
}

#[test]
fn policy_aware_builder_bounds_the_serialized_template() {
    // Builder validation must cover the completed XML artifact, not only the
    // individual fields that contributed to it. Otherwise the same policy
    // accepts a template here and rejects it immediately when signing starts.
    let builder = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256));

    let mut byte_policy = SigningPolicy::default();
    byte_policy.resources.max_xml_document_bytes = 1;
    let byte_error = builder
        .build_template_with_policy(&byte_policy)
        .expect_err("serialized template must obey the XML byte limit");
    assert!(matches!(
        byte_error,
        SignatureBuilderError::Policy(PolicyViolation::ResourceLimit {
            resource: "XML document",
            maximum: 1,
            actual,
        }) if actual > 1
    ));

    let mut node_policy = SigningPolicy::default();
    node_policy.resources.max_xml_nodes = 1;
    let node_error = builder
        .build_template_with_policy(&node_policy)
        .expect_err("serialized template must obey the XML node limit");
    assert!(matches!(
        node_error,
        SignatureBuilderError::Policy(PolicyViolation::ResourceLimit {
            resource: "XML nodes",
            maximum: 1,
            actual: 2,
        })
    ));
}

#[test]
fn policy_aware_builder_bounds_signed_info_canonicalization() {
    // The policy-aware builder must not emit a template that the signing path
    // immediately rejects while canonicalizing the generated SignedInfo.
    let builder = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256));
    let mut policy = SigningPolicy::default();
    policy.resources.max_canonicalized_bytes = 0;

    let error = builder
        .build_template_with_policy(&policy)
        .expect_err("generated SignedInfo must obey the canonicalization budget");

    assert!(matches!(
        error,
        SignatureBuilderError::Policy(PolicyViolation::ResourceLimit {
            maximum: 0,
            actual,
            ..
        }) if actual > 0
    ));
}

#[test]
fn policy_aware_builder_accounts_for_filled_digest_values() {
    // Signing fills each DigestValue before canonicalizing SignedInfo, so an
    // empty-template measurement would undercount every reference digest.
    let builder = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256));
    let template = builder.build_template().expect("valid empty template");
    let document = roxmltree::Document::parse(&template).expect("builder output must parse");
    let signed_info = document
        .descendants()
        .find(|node| node.has_tag_name((DSIG_NS, "SignedInfo")))
        .expect("generated SignedInfo");
    let signed_info_subtree: HashSet<_> = signed_info.descendants().map(|node| node.id()).collect();
    let mut empty_canonical = Vec::new();
    canonicalize(
        &document,
        Some(&|node| signed_info_subtree.contains(&node.id())),
        &exclusive_c14n(),
        &mut empty_canonical,
    )
    .expect("empty SignedInfo must canonicalize");
    let mut policy = SigningPolicy::default();
    policy.resources.max_canonicalized_bytes = empty_canonical.len();

    let error = builder
        .build_template_with_policy(&policy)
        .expect_err("filled SHA-256 digest text must exceed the empty-form budget");

    assert!(matches!(
        error,
        SignatureBuilderError::Policy(PolicyViolation::ResourceLimit {
            maximum,
            actual,
            ..
        }) if maximum == empty_canonical.len() && actual > maximum
    ));
}

#[test]
fn policy_aware_builder_accepts_external_references_without_claiming_request_availability() {
    // The builder validates static URI policy but has no request context.
    // SignContext separately requires caller-owned bytes before digest work.
    let builder = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256).uri("https://example.invalid/payload"),
        );
    let mut policy = SigningPolicy::default();
    policy.uris.references = UriTypeSet::ALL;

    let template = builder
        .build_template_with_policy(&policy)
        .expect("static policy allows the external reference template");
    assert!(template.contains("URI=\"https://example.invalid/payload\""));
}

#[test]
fn serializes_xpath_and_exclusive_prefix_list() {
    // Complex transforms retain the child content required by their specifications.
    let c14n = exclusive_c14n().with_prefix_list("saml #default ds");
    let xml = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .ns_prefix("ds")
        .add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256)
                .transform(Transform::XpathExcludeAllSignatures)
                .transform(Transform::C14n(c14n)),
        )
        .build_template()
        .expect("valid template");
    let document = roxmltree::Document::parse(&xml).expect("valid XML");
    let xpath = document
        .descendants()
        .find(|node| node.has_tag_name((DSIG_NS, "XPath")))
        .expect("XPath child");
    assert_eq!(xpath.text(), Some("not(ancestor-or-self::dsig:Signature)"));
    let transforms = xpath
        .parent()
        .and_then(|node| node.parent())
        .expect("Transforms");
    assert!(matches!(
        parse_transforms(transforms).as_deref(),
        Ok([Transform::XpathExcludeAllSignatures, Transform::C14n(_)])
    ));
    let inclusive = document
        .descendants()
        .find(|node| node.tag_name().name() == "InclusiveNamespaces")
        .expect("InclusiveNamespaces child");
    assert_eq!(inclusive.attribute("PrefixList"), Some("#default ds saml"));
}

#[test]
fn serializes_and_reparses_general_xpath_filter2_parameters() {
    // Builder output must preserve expression order, operations, escaping,
    // and prefix bindings because all four affect the resulting node set.
    let filters = vec![
        XPathFilter::new(
            XPathFilterOperation::Intersect,
            XPathExpression::new("//doc:Record[@active = 'yes']")
                .with_namespace("doc", "urn:documents"),
        ),
        XPathFilter::new(
            XPathFilterOperation::Subtract,
            XPathExpression::new("//doc:Secret").with_namespace("doc", "urn:documents"),
        ),
    ];
    let xml = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .ns_prefix("ds")
        .add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256)
                .transform(Transform::XPathFilter2(filters)),
        )
        .build_template()
        .expect("valid XPath Filter 2.0 template");
    let document = roxmltree::Document::parse(&xml).expect("builder must emit valid XML");
    let transforms = document
        .descendants()
        .find(|node| node.has_tag_name((DSIG_NS, "Transforms")))
        .expect("Transforms element");
    let parsed = parse_transforms(transforms).expect("serialized filters must parse");
    let [
        Transform::XPathFilter2(parsed_filters),
        Transform::C14n(generated_c14n),
    ] = parsed.as_slice()
    else {
        panic!("expected XPath Filter 2.0 followed by generated C14N 1.1");
    };
    assert_eq!(generated_c14n.uri(), "http://www.w3.org/2006/12/xml-c14n11");

    assert_eq!(parsed_filters.len(), 2);
    assert_eq!(
        parsed_filters[0].operation(),
        XPathFilterOperation::Intersect
    );
    assert_eq!(
        parsed_filters[0].xpath().expression(),
        "//doc:Record[@active = 'yes']"
    );
    assert_eq!(
        parsed_filters[0]
            .xpath()
            .namespaces()
            .get("doc")
            .map(String::as_str),
        Some("urn:documents")
    );
    assert_eq!(
        parsed_filters[1].operation(),
        XPathFilterOperation::Subtract
    );
}

#[test]
fn allows_filter2_binding_that_matches_signature_prefix() {
    // Filter2 XPath parameters use the Filter2 namespace as their default
    // namespace, so a local `ds` binding cannot rebind XMLDSig elements.
    let filters = vec![XPathFilter::new(
        XPathFilterOperation::Intersect,
        XPathExpression::new("//ds:Record").with_namespace("ds", "urn:documents"),
    )];
    let xml = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .ns_prefix("ds")
        .add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256)
                .transform(Transform::XPathFilter2(filters)),
        )
        .build_template()
        .expect("Filter2-local bindings must not conflict with the signature prefix");
    let document = roxmltree::Document::parse(&xml).expect("builder must emit valid XML");
    let xpath = document
        .descendants()
        .find(|node| node.has_tag_name(("http://www.w3.org/2002/06/xmldsig-filter2", "XPath")))
        .expect("Filter2 XPath child");

    assert_eq!(
        xpath.lookup_namespace_uri(Some("ds")),
        Some("urn:documents")
    );
}

#[test]
fn rejects_xml_forbidden_characters_in_xpath_namespace_uris() {
    // XML escaping cannot legalize control characters forbidden by XML 1.0;
    // a successful builder result must therefore always remain parseable.
    for uri in ["urn:invalid:\0", "urn:invalid:\u{1}"] {
        let error = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
            .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256).transform(
                Transform::XPath(XPathExpression::new("//doc:item").with_namespace("doc", uri)),
            ))
            .build_template()
            .expect_err("XML-forbidden namespace URI characters must fail at the builder");

        assert!(error.to_string().contains("forbidden by XML 1.0"));
    }
}

#[test]
fn rejects_xml_forbidden_characters_in_xpath_expressions() {
    // XPath grammar accepts string literals that XML 1.0 cannot serialize. The
    // builder must reject both parameter forms rather than return an XML string
    // that the signing pipeline cannot parse again.
    let expression = "contains(., '\u{B}')";
    let transforms = [
        Transform::XPath(XPathExpression::new(expression)),
        Transform::XPathFilter2(vec![XPathFilter::new(
            XPathFilterOperation::Intersect,
            XPathExpression::new(expression),
        )]),
    ];

    for transform in transforms {
        let error = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
            .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256).transform(transform))
            .build_template()
            .expect_err("XML-forbidden XPath characters must fail at the builder boundary");

        assert!(error.to_string().contains("forbidden by XML 1.0"));
    }
}

#[test]
fn rejects_invalid_filter2_expression_counts() {
    // Builder output is reparsed by signing, so it must enforce the same
    // non-empty, bounded Filter2 sequence accepted by the transform parser.
    let oversized = (0..65)
        .map(|_| {
            XPathFilter::new(
                XPathFilterOperation::Intersect,
                XPathExpression::new("/root"),
            )
        })
        .collect::<Vec<_>>();

    let build = |filters| {
        SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
            .add_reference(
                ReferenceBuilder::new(DigestAlgorithm::Sha256)
                    .transform(Transform::XPathFilter2(filters)),
            )
            .build_template()
    };

    assert!(matches!(
        build(Vec::new()).expect_err("empty Filter2 must fail at the builder boundary"),
        SignatureBuilderError::InvalidXPath(_)
    ));
    assert!(matches!(
        build(oversized).expect_err("oversized Filter2 must fail at the builder boundary"),
        SignatureBuilderError::Policy(PolicyViolation::ResourceLimit {
            resource: "XPath filters",
            maximum: 64,
            actual: 65,
        })
    ));
}

#[test]
fn rejects_xpath_binding_that_shadows_signature_prefix() {
    // Rebinding the prefix used for ds:XPath changes the element namespace and
    // produces a template that neither the strict parser nor a signer can use.
    let error = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .ns_prefix("ds")
        .add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256).transform(Transform::XPath(
                XPathExpression::new("//ds:item").with_namespace("ds", "urn:payload"),
            )),
        )
        .build_template()
        .expect_err("XPath namespace bindings must not shadow the signature prefix");

    assert!(error.to_string().contains("ds"));
}

#[test]
fn rejects_xpath_binding_that_redefines_xml_prefix() {
    // XML Namespaces permanently binds `xml` to the XML namespace. Accepting
    // another URI would change the XPath context when serialization omits the
    // reserved declaration.
    let error = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256).transform(Transform::XPath(
                XPathExpression::new("//xml:item").with_namespace("xml", "urn:not-xml"),
            )),
        )
        .build_template()
        .expect_err("the xml prefix cannot be rebound to another namespace");

    assert!(matches!(
        error,
        SignatureBuilderError::InvalidNamespacePrefix(prefix) if prefix == "xml"
    ));
}

#[test]
fn rejects_xpath_binding_that_aliases_xml_namespace() {
    // The XML namespace is reserved for the `xml` prefix; declaring an alias
    // would produce a document that violates the namespace constraints.
    let error = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(
            ReferenceBuilder::new(DigestAlgorithm::Sha256).transform(Transform::XPath(
                XPathExpression::new("//alias:item")
                    .with_namespace("alias", "http://www.w3.org/XML/1998/namespace"),
            )),
        )
        .build_template()
        .expect_err("the XML namespace cannot be assigned to another prefix");

    assert!(matches!(
        error,
        SignatureBuilderError::InvalidNamespacePrefix(prefix) if prefix == "alias"
    ));
}

#[test]
fn rejects_xpath_bindings_with_invalid_namespace_uris() {
    // Empty namespace names and the namespace-declaration namespace cannot be
    // bound to an XPath prefix in either XMLDSig XPath parameter form.
    let transforms = [
        Transform::XPath(XPathExpression::new("//doc:item").with_namespace("doc", "")),
        Transform::XPath(XPathExpression::new("//doc:item").with_namespace("doc", XMLNS_NS)),
        Transform::XPathFilter2(vec![XPathFilter::new(
            XPathFilterOperation::Intersect,
            XPathExpression::new("//doc:item").with_namespace("doc", ""),
        )]),
        Transform::XPathFilter2(vec![XPathFilter::new(
            XPathFilterOperation::Intersect,
            XPathExpression::new("//doc:item").with_namespace("doc", XMLNS_NS),
        )]),
    ];

    for transform in transforms {
        let error = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
            .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256).transform(transform))
            .build_template()
            .expect_err("invalid namespace URI must be rejected before serialization");
        assert!(matches!(
            error,
            SignatureBuilderError::InvalidNamespaceUri(uri)
                if uri.is_empty() || uri == XMLNS_NS
        ));
    }
}

#[test]
fn accepts_unicode_xml_namespace_prefixes() {
    // XML 1.0 NCNames permit Unicode letters; prefix validation must not be ASCII-only.
    let xml = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .ns_prefix("подпись")
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256))
        .build_template()
        .expect("Unicode prefix is a valid XML NCName");
    let document = roxmltree::Document::parse(&xml).expect("valid XML");
    assert_eq!(
        document.root_element().tag_name().namespace(),
        Some(DSIG_NS)
    );
}

#[test]
fn rejects_non_ncname_signature_and_reference_ids() {
    // xsd:ID derives from NCName, so escaping an invalid value is not sufficient.
    let signature_id = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .signature_id("sig&1")
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256))
        .build_template()
        .expect_err("Signature Id must be an NCName");
    assert!(signature_id.to_string().contains("Signature Id"));

    let reference_id = SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
        .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256).id("ref:1"))
        .build_template()
        .expect_err("Reference Id must be an NCName");
    assert!(reference_id.to_string().contains("Reference Id"));

    // Interpolation turns these into valid documents with comment/PI nodes before
    // `<valid/>`; parser success alone accepted them before the exact-name check.
    let injected_signature_id =
        SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
            .signature_id("!--comment--><valid")
            .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256))
            .build_template()
            .expect_err("markup must not satisfy Signature Id validation");
    assert!(injected_signature_id.to_string().contains("Signature Id"));

    let injected_reference_id =
        SignatureBuilder::new(exclusive_c14n(), SignatureAlgorithm::RsaSha256)
            .add_reference(ReferenceBuilder::new(DigestAlgorithm::Sha256).id("?check?><valid"))
            .build_template()
            .expect_err("markup must not satisfy Reference Id validation");
    assert!(injected_reference_id.to_string().contains("Reference Id"));
}
use xml_sec as roxmltree;