sanitization-secrecy 2.1.0

Migration-compatible secrecy-style wrappers backed by sanitization.
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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
#![no_std]
#![deny(unsafe_code)]
#![deny(unsafe_op_in_unsafe_fn)]
#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]

//! Compatibility-oriented secret wrappers backed by [`sanitization`].
//!
//! This crate provides the familiar `SecretBox`, `SecretSlice`,
//! `SecretString`, `ExposeSecret`, and `ExposeSecretMut` API shape for projects
//! migrating from `secrecy` 0.10. It deliberately keeps reference-returning
//! exposure, cloning, and plaintext serialization out of the hardened
//! `sanitization` containers.
//!
//! The compatibility wrapper provides redacted formatting and sanitizes its
//! currently boxed value on drop. It does not provide memory locking, guard
//! pages, storage-history recovery, or the scoped-exposure guarantees of the
//! native `sanitization` container families.
//!
//! Reference exposure requires the core storage-stability contracts under
//! every feature combination. The `hazmat-unrestricted-exposure` feature adds
//! a visibly distinct [`UnrestrictedSecretBox`] for legacy types that cannot
//! satisfy those contracts; it never broadens [`SecretBox`].

extern crate alloc;

use alloc::{boxed::Box, string::String, vec::Vec};
use core::{
    any, fmt,
    mem::{size_of, MaybeUninit},
};
use sanitization::{ct, SecureSanitize};

#[cfg(feature = "serde")]
use serde::{de, ser, Deserialize, Serialize};

/// The `zeroize` API used by `secrecy` callers, available through the default
/// `zeroize-interop` compatibility feature.
#[cfg(feature = "zeroize-interop")]
pub use zeroize;

/// A boxed secret value with explicit reference exposure and clearing on drop.
///
/// This compatibility type intentionally exposes references through
/// [`ExposeSecret`] and [`ExposeSecretMut`], with stable-storage bounds by
/// default. Prefer native `sanitization` containers when scoped closure
/// exposure, memory locking, canaries, or guard pages are required.
pub struct SecretBox<S: SecureSanitize + ?Sized> {
    inner_secret: Box<S>,
}

impl<S: SecureSanitize + ?Sized> SecretBox<S> {
    /// Wrap an already boxed secret without copying it.
    #[must_use]
    #[inline]
    pub const fn new(boxed_secret: Box<S>) -> Self {
        Self {
            inner_secret: boxed_secret,
        }
    }

    /// Sanitize the currently boxed value while retaining its allocation.
    #[inline]
    pub fn clear_secret(&mut self) {
        self.inner_secret.secure_sanitize();
    }

    /// Consume this wrapper after sanitizing its value.
    #[inline]
    pub fn into_cleared(self) {
        drop(self);
    }
}

impl<S: sanitization::StableMutableSecretStorage + Default> SecretBox<S> {
    /// Allocate a default value and initialize it directly through a mutable
    /// reference.
    ///
    /// If `initialize` panics, the partially initialized boxed value is
    /// sanitized during unwinding.
    #[must_use]
    #[inline]
    pub fn init_with_mut(initialize: impl FnOnce(&mut S)) -> Self {
        let mut secret = Self::default();
        initialize(secret.expose_secret_mut());
        secret
    }

    /// Fallible in-place initialization of the final boxed value.
    ///
    /// Returning an error or unwinding sanitizes the partially initialized
    /// value before its allocation is released. Allocation failure can still
    /// abort and is not represented by `E`.
    #[inline]
    pub fn try_init_with_mut<E>(
        initialize: impl FnOnce(&mut S) -> Result<(), E>,
    ) -> Result<Self, E> {
        let mut secret = Self::default();
        initialize(secret.expose_secret_mut())?;
        Ok(secret)
    }
}

impl<S: CloneableSecret> SecretBox<S> {
    /// Construct a value, clone it into its final allocation, and sanitize the
    /// temporary value.
    ///
    /// Prefer [`SecretBox::new`] or [`SecretBox::init_with_mut`] because this
    /// operation necessarily creates a temporary copy. The temporary is also
    /// sanitized if cloning panics and Rust unwinds. Cleanup is not guaranteed
    /// after an abort, including an allocation failure handled by aborting.
    /// `CloneableSecret` requires custom clone implementations to sanitize
    /// secret-bearing partial destination state if cloning unwinds; this
    /// wrapper cannot recover state discarded inside arbitrary `Clone` code.
    #[must_use]
    #[inline]
    pub fn init_with(construct: impl FnOnce() -> S) -> Self {
        let temporary = TemporarySecret::new(construct());
        Self::new(Box::new(temporary.value().clone()))
    }

    /// Fallible form of [`SecretBox::init_with`].
    ///
    /// Generator errors are returned directly. A successfully generated
    /// temporary is sanitized on success and during panic unwinding. Allocation
    /// failure and a panic from `S::clone` are not represented by `E`.
    ///
    /// `CloneableSecret` is an explicit security contract: if `S::clone`
    /// unwinds after constructing secret-bearing destination state, that
    /// partial state must sanitize itself during ordinary destruction. This
    /// wrapper can sanitize the complete source temporary, but Rust does not
    /// expose an arbitrary clone's discarded partial destination to it.
    #[inline]
    pub fn try_init_with<E>(construct: impl FnOnce() -> Result<S, E>) -> Result<Self, E> {
        let temporary = TemporarySecret::new(construct()?);
        Ok(Self::new(Box::new(temporary.value().clone())))
    }
}

impl<S: SecureSanitize + ?Sized> From<Box<S>> for SecretBox<S> {
    #[inline]
    fn from(source: Box<S>) -> Self {
        Self::new(source)
    }
}

impl<S: SecureSanitize + Default> Default for SecretBox<S> {
    #[inline]
    fn default() -> Self {
        Self::new(Box::<S>::default())
    }
}

impl<S: SecureSanitize + ?Sized> SecureSanitize for SecretBox<S> {
    #[inline]
    fn secure_sanitize(&mut self) {
        self.clear_secret();
    }
}

#[cfg(feature = "zeroize-interop")]
impl<S: SecureSanitize + ?Sized> zeroize::Zeroize for SecretBox<S> {
    #[inline]
    fn zeroize(&mut self) {
        self.clear_secret();
    }
}

#[cfg(feature = "zeroize-interop")]
impl<S: SecureSanitize + ?Sized> zeroize::ZeroizeOnDrop for SecretBox<S> {}

impl<S: SecureSanitize + ?Sized> Drop for SecretBox<S> {
    #[inline]
    fn drop(&mut self) {
        self.clear_secret();
    }
}

impl<S: SecureSanitize + ?Sized> fmt::Debug for SecretBox<S> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "SecretBox<{}>([REDACTED])",
            any::type_name::<S>()
        )
    }
}

impl<S: SecureSanitize + ct::ConstantTimeEq + ?Sized> SecretBox<S> {
    /// Compare two wrapped values without secret-dependent early exit.
    ///
    /// For dynamically sized values, length is public. The returned choice
    /// must be explicitly declassified before it can control normal program
    /// flow. Prefer this method over comparing exposed references with `==`.
    #[must_use]
    #[inline]
    pub fn ct_eq(&self, other: &Self) -> ct::Choice {
        ct::ConstantTimeEq::ct_eq(self.inner_secret.as_ref(), other.inner_secret.as_ref())
    }
}

impl<S: CloneableSecret> Clone for SecretBox<S> {
    #[inline]
    fn clone(&self) -> Self {
        Self::new(self.inner_secret.clone())
    }
}

impl<S: sanitization::StableSharedSecretStorage + ?Sized> ExposeSecret<S> for SecretBox<S> {
    #[inline]
    fn expose_secret(&self) -> &S {
        self.inner_secret.as_ref()
    }
}

impl<S: sanitization::StableMutableSecretStorage + ?Sized> ExposeSecretMut<S> for SecretBox<S> {
    #[inline]
    fn expose_secret_mut(&mut self) -> &mut S {
        self.inner_secret.as_mut()
    }
}

/// Explicit reduced-assurance wrapper for legacy unrestricted exposure.
///
/// This type is available only with `hazmat-unrestricted-exposure`. Unlike
/// [`SecretBox`], it does not require storage-stability attestations before
/// returning references or invoking mutable initialization callbacks. Safe
/// methods reached through those references can therefore replace or release
/// secret-bearing storage without clearing historical allocations first.
///
/// Cargo feature unification can make this type available transitively, but it
/// cannot change any method or trait implementation on [`SecretBox`]. A caller
/// must name and construct `UnrestrictedSecretBox` explicitly.
#[cfg(feature = "hazmat-unrestricted-exposure")]
pub struct UnrestrictedSecretBox<S: SecureSanitize + ?Sized> {
    inner: SecretBox<S>,
}

#[cfg(feature = "hazmat-unrestricted-exposure")]
impl<S: SecureSanitize + ?Sized> UnrestrictedSecretBox<S> {
    /// Wrap an already boxed secret without copying it.
    #[must_use]
    #[inline]
    pub const fn new(boxed_secret: Box<S>) -> Self {
        Self {
            inner: SecretBox::new(boxed_secret),
        }
    }

    /// Sanitize the currently boxed value while retaining its allocation.
    #[inline]
    pub fn clear_secret(&mut self) {
        self.inner.clear_secret();
    }

    /// Convert to the storage-gated wrapper without copying the secret.
    #[must_use]
    #[inline]
    pub fn into_restricted(self) -> SecretBox<S> {
        self.inner
    }

    /// Consume this wrapper after sanitizing its value.
    #[inline]
    pub fn into_cleared(self) {
        drop(self);
    }
}

#[cfg(feature = "hazmat-unrestricted-exposure")]
impl<S: SecureSanitize + Default> UnrestrictedSecretBox<S> {
    /// Allocate a default value and initialize it through an unrestricted
    /// mutable reference.
    ///
    /// If the callback reallocates or replaces owned storage, this wrapper
    /// cannot recover and sanitize the released allocation.
    #[must_use]
    #[inline]
    pub fn init_with_mut(initialize: impl FnOnce(&mut S)) -> Self {
        let mut secret = Self::default();
        initialize(secret.inner.inner_secret.as_mut());
        secret
    }

    /// Fallible unrestricted initialization of the final boxed value.
    ///
    /// Returning an error or unwinding sanitizes the currently owned value,
    /// but cannot recover storage released by the callback.
    #[inline]
    pub fn try_init_with_mut<E>(
        initialize: impl FnOnce(&mut S) -> Result<(), E>,
    ) -> Result<Self, E> {
        let mut secret = Self::default();
        initialize(secret.inner.inner_secret.as_mut())?;
        Ok(secret)
    }
}

#[cfg(feature = "hazmat-unrestricted-exposure")]
impl<S: SecureSanitize + ?Sized> From<Box<S>> for UnrestrictedSecretBox<S> {
    #[inline]
    fn from(source: Box<S>) -> Self {
        Self::new(source)
    }
}

#[cfg(feature = "hazmat-unrestricted-exposure")]
impl<S: SecureSanitize + Default> Default for UnrestrictedSecretBox<S> {
    #[inline]
    fn default() -> Self {
        Self {
            inner: SecretBox::default(),
        }
    }
}

#[cfg(feature = "hazmat-unrestricted-exposure")]
impl<S: SecureSanitize + ?Sized> SecureSanitize for UnrestrictedSecretBox<S> {
    #[inline]
    fn secure_sanitize(&mut self) {
        self.clear_secret();
    }
}

#[cfg(all(feature = "hazmat-unrestricted-exposure", feature = "zeroize-interop"))]
impl<S: SecureSanitize + ?Sized> zeroize::Zeroize for UnrestrictedSecretBox<S> {
    #[inline]
    fn zeroize(&mut self) {
        self.clear_secret();
    }
}

#[cfg(all(feature = "hazmat-unrestricted-exposure", feature = "zeroize-interop"))]
impl<S: SecureSanitize + ?Sized> zeroize::ZeroizeOnDrop for UnrestrictedSecretBox<S> {}

#[cfg(feature = "hazmat-unrestricted-exposure")]
impl<S: SecureSanitize + ?Sized> fmt::Debug for UnrestrictedSecretBox<S> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "UnrestrictedSecretBox<{}>([REDACTED])",
            any::type_name::<S>()
        )
    }
}

#[cfg(feature = "hazmat-unrestricted-exposure")]
impl<S: SecureSanitize + ct::ConstantTimeEq + ?Sized> UnrestrictedSecretBox<S> {
    /// Compare two wrapped values without secret-dependent early exit.
    #[must_use]
    #[inline]
    pub fn ct_eq(&self, other: &Self) -> ct::Choice {
        self.inner.ct_eq(&other.inner)
    }
}

#[cfg(feature = "hazmat-unrestricted-exposure")]
impl<S: CloneableSecret> Clone for UnrestrictedSecretBox<S> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

#[cfg(feature = "hazmat-unrestricted-exposure")]
impl<S: SecureSanitize + ?Sized> ExposeSecret<S> for UnrestrictedSecretBox<S> {
    #[inline]
    fn expose_secret(&self) -> &S {
        self.inner.inner_secret.as_ref()
    }
}

#[cfg(feature = "hazmat-unrestricted-exposure")]
impl<S: SecureSanitize + ?Sized> ExposeSecretMut<S> for UnrestrictedSecretBox<S> {
    #[inline]
    fn expose_secret_mut(&mut self) -> &mut S {
        self.inner.inner_secret.as_mut()
    }
}

/// A boxed secret slice.
pub type SecretSlice<S> = SecretBox<[S]>;

/// Error returned when a no-copy vector transfer would discard capacity.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SecretSliceCapacityError {
    /// Number of initialized elements.
    pub length: usize,
    /// Number of elements in the source allocation.
    pub capacity: usize,
}

impl fmt::Display for SecretSliceCapacityError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "secret slice source has length {} but capacity {}; no-copy boxing would discard secret-bearing capacity",
            self.length, self.capacity
        )
    }
}

impl<S: SecureSanitize> SecretSlice<S> {
    /// Transfer a vector without copying when its allocation is already exact.
    ///
    /// A non-zero-sized vector with excess capacity is sanitized and rejected
    /// before `into_boxed_slice` can release that allocation. Do not call
    /// `shrink_to_fit` on secret-bearing input to satisfy this precondition;
    /// construct it with exact capacity or use the cloning [`From<Vec<S>>`]
    /// implementation when `S` implements [`CloneableSecret`].
    #[inline]
    pub fn try_from_vec_exact(mut source: Vec<S>) -> Result<Self, SecretSliceCapacityError> {
        let length = source.len();
        let capacity = source.capacity();
        if size_of::<S>() != 0 && capacity != length {
            source.secure_sanitize();
            return Err(SecretSliceCapacityError { length, capacity });
        }

        Ok(Self::new(source.into_boxed_slice()))
    }
}

impl SecretSlice<u8> {
    /// Allocate a zero-filled byte slice and initialize its final boxed storage.
    ///
    /// `length` must be trusted and bounded. Allocation failure may abort.
    #[must_use]
    #[inline]
    pub fn init_with_len(length: usize, initialize: impl FnOnce(&mut [u8])) -> Self {
        let mut secret = Self::new(alloc::vec![0_u8; length].into_boxed_slice());
        initialize(secret.expose_secret_mut());
        secret
    }

    /// Fallible allocation and initialization of a runtime-length byte slice.
    ///
    /// A returned error or unwind sanitizes the final boxed byte allocation.
    /// Allocation refusal and initializer failure remain distinguishable. This
    /// method does not enforce an application limit; use
    /// [`SecretSlice::try_init_with_len_bounded`] at untrusted length
    /// boundaries.
    #[inline]
    pub fn try_init_with_len<E>(
        length: usize,
        initialize: impl FnOnce(&mut [u8]) -> Result<(), E>,
    ) -> Result<Self, SecretSliceInitError<E>> {
        let mut secret =
            try_allocate_exact_zeroed_slice(length).map_err(SecretSliceInitError::Build)?;
        initialize(secret.expose_secret_mut()).map_err(SecretSliceInitError::Initialize)?;
        Ok(secret)
    }

    /// Fallibly allocate and initialize a byte slice under a compile-time
    /// public length ceiling.
    ///
    /// Oversized lengths are rejected before allocation and before
    /// `initialize` runs. Allocation refusal, a non-exact allocator capacity,
    /// and initializer failure remain distinct typed outcomes.
    #[inline]
    pub fn try_init_with_len_bounded<const MAX: usize, E>(
        length: usize,
        initialize: impl FnOnce(&mut [u8]) -> Result<(), E>,
    ) -> Result<Self, SecretSliceInitError<E>> {
        if length > MAX {
            return Err(SecretSliceInitError::Build(
                SecretSliceAllocationError::TooLong {
                    maximum: MAX,
                    actual: length,
                },
            ));
        }

        Self::try_init_with_len(length, initialize)
    }
}

/// Error returned while allocating final storage for a runtime-length secret
/// slice.
#[derive(Debug)]
pub enum SecretSliceAllocationError {
    /// The requested public length exceeded the compile-time maximum.
    TooLong {
        /// Maximum accepted byte length.
        maximum: usize,
        /// Rejected requested byte length.
        actual: usize,
    },
    /// The allocator could not reserve the requested byte length.
    Allocation(alloc::collections::TryReserveError),
    /// The allocator exposed more logical capacity than requested.
    ///
    /// The allocation is released before it receives secret input.
    NonExactCapacity {
        /// Requested byte length.
        requested: usize,
        /// Capacity returned by the allocator.
        actual: usize,
    },
}

impl fmt::Display for SecretSliceAllocationError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TooLong { maximum, actual } => write!(
                formatter,
                "secret slice length exceeds limit: maximum {maximum} bytes, got {actual} bytes"
            ),
            Self::Allocation(error) => write!(formatter, "secret slice allocation failed: {error}"),
            Self::NonExactCapacity { requested, actual } => write!(
                formatter,
                "secret slice allocator returned capacity {actual} for requested length {requested}"
            ),
        }
    }
}

impl core::error::Error for SecretSliceAllocationError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            Self::Allocation(error) => Some(error),
            Self::TooLong { .. } | Self::NonExactCapacity { .. } => None,
        }
    }
}

/// Error returned by fallible runtime-length secret-slice initialization.
#[derive(Debug)]
pub enum SecretSliceInitError<E> {
    /// Public-length validation or allocation failed before initialization.
    Build(SecretSliceAllocationError),
    /// The caller's initializer returned an error.
    Initialize(E),
}

impl<E> From<SecretSliceAllocationError> for SecretSliceInitError<E> {
    #[inline]
    fn from(error: SecretSliceAllocationError) -> Self {
        Self::Build(error)
    }
}

impl<E: fmt::Display> fmt::Display for SecretSliceInitError<E> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Build(error) => error.fmt(formatter),
            Self::Initialize(error) => {
                write!(formatter, "secret slice initialization failed: {error}")
            }
        }
    }
}

impl<E> core::error::Error for SecretSliceInitError<E>
where
    E: core::error::Error + 'static,
{
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            Self::Build(error) => Some(error),
            Self::Initialize(error) => Some(error),
        }
    }
}

fn try_allocate_exact_zeroed_slice(
    length: usize,
) -> Result<SecretSlice<u8>, SecretSliceAllocationError> {
    let mut bytes = Vec::new();
    bytes
        .try_reserve_exact(length)
        .map_err(SecretSliceAllocationError::Allocation)?;

    if bytes.capacity() != length {
        return Err(SecretSliceAllocationError::NonExactCapacity {
            requested: length,
            actual: bytes.capacity(),
        });
    }

    bytes.resize(length, 0);
    Ok(SecretSlice::new(bytes.into_boxed_slice()))
}

impl<S: CloneableSecret> From<Vec<S>> for SecretSlice<S> {
    /// Clone into guarded destination storage, then sanitize the source vector.
    ///
    /// This intentionally copies the live elements so the source allocation,
    /// including excess capacity, can be cleared before release. Completed
    /// destination elements are also sanitized if a later clone panics.
    #[inline]
    fn from(source: Vec<S>) -> Self {
        let source = TemporarySecret::new(source);
        Self::new(clone_slice_into_box(source.value()))
    }
}

impl<S: CloneableSecret> Clone for SecretSlice<S> {
    #[inline]
    fn clone(&self) -> Self {
        Self::new(clone_slice_into_box(self.inner_secret.as_ref()))
    }
}

impl<S: SecureSanitize> Default for SecretSlice<S> {
    #[inline]
    fn default() -> Self {
        Self::new(Vec::new().into_boxed_slice())
    }
}

/// A boxed UTF-8 secret string.
pub type SecretString = SecretBox<str>;

impl From<String> for SecretString {
    /// Copy into final boxed string storage and sanitize the source allocation.
    ///
    /// Copying is intentional: it permits the complete source capacity to be
    /// cleared before release instead of asking `String::into_boxed_str` to
    /// discard excess capacity without sanitizing it.
    #[inline]
    fn from(source: String) -> Self {
        let source = TemporarySecret::new(source);
        Self::new(Box::<str>::from(source.value().as_str()))
    }
}

impl From<&str> for SecretString {
    /// Copy a borrowed string into compatibility storage.
    ///
    /// The source remains outside this wrapper's ownership and clearing
    /// guarantee.
    #[inline]
    fn from(source: &str) -> Self {
        Self::new(Box::<str>::from(source))
    }
}

/// Compatibility exception: `SecretString` is always cloneable to match
/// `secrecy` 0.10. Other generic `SecretBox<S>` values require
/// [`CloneableSecret`].
impl Clone for SecretString {
    #[inline]
    fn clone(&self) -> Self {
        Self::new(self.inner_secret.clone())
    }
}

impl Default for SecretString {
    #[inline]
    fn default() -> Self {
        String::new().into()
    }
}

/// Marker for secret values whose owners explicitly permit duplication.
///
/// Types do not become cloneable merely by implementing [`SecureSanitize`]:
/// `SecretString` is the intentional compatibility exception and remains
/// unconditionally cloneable to match `secrecy` 0.10.
///
/// ```compile_fail
/// use sanitization::SecureSanitize;
/// use sanitization_secrecy::SecretBox;
///
/// #[derive(Clone)]
/// struct RequiresOptIn(u8);
/// impl SecureSanitize for RequiresOptIn {
///     fn secure_sanitize(&mut self) {
///         self.0.secure_sanitize();
///     }
/// }
///
/// let secret = SecretBox::new(Box::new(RequiresOptIn(7)));
/// let duplicate = secret.clone();
/// # let _ = duplicate;
/// ```
///
/// Implementors are responsible for ensuring that partially cloned owned
/// state is sanitized if their `Clone` implementation unwinds. The wrapper can
/// guard completed slice elements, but it cannot inspect partial state inside
/// an arbitrary `S::clone()` implementation.
///
/// This crate implements the marker for reviewed integer primitives, `String`,
/// and arrays whose elements are those integer primitives. A custom `Copy`
/// implementation does not authorize its array automatically because `Copy`
/// does not prevent a separate manually implemented `Clone` from panicking.
pub trait CloneableSecret: Clone + SecureSanitize {}

macro_rules! impl_cloneable_secret {
    ($($type:ty),+ $(,)?) => {
        $(impl CloneableSecret for $type {})+
    };
}

impl_cloneable_secret!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize,);

impl CloneableSecret for String {}

macro_rules! impl_cloneable_secret_arrays {
    ($($type:ty),+ $(,)?) => {
        $(impl<const N: usize> CloneableSecret for [$type; N] {})+
    };
}

impl_cloneable_secret_arrays!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize,);

/// Expose a shared reference to an inner secret.
///
/// The returned reference can remain live for the full borrow of `self`. By
/// default, `SecretBox` implements this trait only for
/// [`sanitization::StableSharedSecretStorage`]. The
/// `hazmat-unrestricted-exposure` feature adds [`UnrestrictedSecretBox`]
/// rather than weakening this implementation.
pub trait ExposeSecret<S: ?Sized> {
    /// Borrow the secret value.
    fn expose_secret(&self) -> &S;
}

/// Expose a mutable reference to an inner secret.
///
/// `SecretBox` implements this trait only for
/// [`sanitization::StableMutableSecretStorage`]. The
/// `hazmat-unrestricted-exposure` feature adds [`UnrestrictedSecretBox`]
/// rather than weakening this implementation.
pub trait ExposeSecretMut<S: ?Sized> {
    /// Mutably borrow the secret value.
    fn expose_secret_mut(&mut self) -> &mut S;
}

/// Marker for secret values whose owners explicitly permit plaintext Serde
/// serialization.
///
/// Implementing this trait is an exfiltration decision. Serialized output and
/// serializer-owned intermediate buffers are outside this crate's clearing
/// guarantees.
///
/// Serialization is unavailable without an explicit implementation:
///
/// ```compile_fail
/// use sanitization_secrecy::SecretBox;
///
/// let secret = SecretBox::new(Box::new(String::from("token")));
/// let encoded = serde_json::to_string(&secret).unwrap();
/// # let _ = encoded;
/// ```
#[cfg(feature = "serde")]
pub trait SerializableSecret: Serialize {}

#[cfg(feature = "serde-compat-unbounded")]
impl<'de, T> Deserialize<'de> for SecretBox<T>
where
    T: CloneableSecret + de::DeserializeOwned + Sized,
{
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        Self::try_init_with(|| T::deserialize(deserializer))
    }
}

#[cfg(all(
    feature = "serde-compat-unbounded",
    feature = "hazmat-unrestricted-exposure"
))]
impl<'de, T> Deserialize<'de> for UnrestrictedSecretBox<T>
where
    T: CloneableSecret + de::DeserializeOwned + Sized,
{
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        SecretBox::deserialize(deserializer).map(|inner| Self { inner })
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for SecretString {
    /// Deserialize with the core crate's default 1 MiB UTF-8 byte ceiling.
    ///
    /// This limit is checked when the deserializer invokes the visitor. Enforce
    /// a separate transport or parser limit because parser-owned buffers may
    /// already exist by then. Use `sanitization::BoundedSecretString` when the
    /// storage type must enforce a smaller permanent maximum.
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        deserializer.deserialize_string(SecretStringVisitor)
    }
}

#[cfg(feature = "serde")]
struct SecretStringVisitor;

#[cfg(feature = "serde")]
impl<'de> de::Visitor<'de> for SecretStringVisitor {
    type Value = SecretString;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "at most {} UTF-8 bytes of secret text",
            sanitization::DEFAULT_SECRET_STRING_SERDE_MAX_LEN
        )
    }

    fn visit_str<E>(self, text: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        self.validate_len::<E>(text.len())?;
        Ok(SecretString::from(text))
    }

    fn visit_string<E>(self, mut text: String) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        if let Err(error) = self.validate_len::<E>(text.len()) {
            text.secure_sanitize();
            return Err(error);
        }
        Ok(SecretString::from(text))
    }
}

#[cfg(feature = "serde")]
impl SecretStringVisitor {
    fn validate_len<E: de::Error>(&self, actual: usize) -> Result<(), E> {
        if actual > sanitization::DEFAULT_SECRET_STRING_SERDE_MAX_LEN {
            Err(E::invalid_length(actual, self))
        } else {
            Ok(())
        }
    }
}

#[cfg(feature = "serde")]
impl<T> Serialize for SecretBox<T>
where
    T: sanitization::StableSharedSecretStorage + SerializableSecret + Serialize + Sized,
{
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: ser::Serializer,
    {
        self.expose_secret().serialize(serializer)
    }
}

#[cfg(all(feature = "serde", feature = "hazmat-unrestricted-exposure"))]
impl<T> Serialize for UnrestrictedSecretBox<T>
where
    T: SecureSanitize + SerializableSecret + Serialize + Sized,
{
    #[inline]
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: ser::Serializer,
    {
        self.inner.inner_secret.as_ref().serialize(serializer)
    }
}

struct TemporarySecret<T: SecureSanitize>(T);

impl<T: SecureSanitize> TemporarySecret<T> {
    #[inline]
    const fn new(value: T) -> Self {
        Self(value)
    }

    #[inline]
    const fn value(&self) -> &T {
        &self.0
    }
}

fn clone_slice_into_box<S: CloneableSecret>(source: &[S]) -> Box<[S]> {
    let mut pending = PendingCloneSlice::new(source.len());
    for item in source {
        pending.push(item.clone());
    }
    pending.into_box()
}

struct PendingCloneSlice<S: SecureSanitize> {
    storage: Option<Box<[MaybeUninit<S>]>>,
    initialized: usize,
}

impl<S: SecureSanitize> PendingCloneSlice<S> {
    fn new(length: usize) -> Self {
        Self {
            storage: Some(Box::<[S]>::new_uninit_slice(length)),
            initialized: 0,
        }
    }

    fn push(&mut self, value: S) {
        let storage = self.storage.as_mut().expect("pending clone storage");
        storage[self.initialized].write(value);
        self.initialized += 1;
    }

    #[allow(unsafe_code)]
    fn into_box(mut self) -> Box<[S]> {
        let expected = self.storage.as_ref().expect("pending clone storage").len();
        assert_eq!(
            self.initialized, expected,
            "pending clone slice is not fully initialized"
        );
        let storage = self.storage.take().expect("pending clone storage");
        // SAFETY: the mandatory check above proves that `push` initialized
        // every slot in the consecutive prefix before the guard was disarmed.
        unsafe { storage.assume_init() }
    }
}

impl<S: SecureSanitize> Drop for PendingCloneSlice<S> {
    #[allow(unsafe_code)]
    fn drop(&mut self) {
        let Some(storage) = self.storage.as_mut() else {
            return;
        };

        for slot in &mut storage[..self.initialized] {
            // SAFETY: only the consecutive prefix counted by `initialized`
            // has been written. Each value is sanitized before its destructor
            // runs, and `MaybeUninit` prevents a second automatic drop.
            unsafe {
                slot.assume_init_mut().secure_sanitize();
                slot.assume_init_drop();
            }
        }
    }
}

impl<T: SecureSanitize> Drop for TemporarySecret<T> {
    #[inline]
    fn drop(&mut self) {
        self.0.secure_sanitize();
    }
}

#[cfg(test)]
extern crate std;

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec;
    use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
    use std::{
        format, panic,
        sync::{Arc, Mutex},
    };

    static DEFAULT_VALUE_CLEARED: AtomicBool = AtomicBool::new(false);
    static DEFAULT_VALUE_TEST_LOCK: Mutex<()> = Mutex::new(());

    #[cfg(feature = "serde")]
    impl SerializableSecret for [u8; 4] {}

    #[cfg(all(feature = "serde", feature = "hazmat-unrestricted-exposure"))]
    impl SerializableSecret for String {}

    #[test]
    fn string_and_slice_round_trip() {
        let mut source = String::with_capacity(64);
        source.push_str("token");
        let mut text = SecretString::from(source);
        assert_eq!(text.expose_secret(), "token");
        text.expose_secret_mut().make_ascii_uppercase();
        assert_eq!(text.expose_secret(), "TOKEN");
        text.clear_secret();
        assert_eq!(text.expose_secret().as_bytes(), &[0; 5]);

        let bytes = SecretSlice::from(vec![1_u8, 2, 3]);
        assert_eq!(bytes.expose_secret(), &[1, 2, 3]);
    }

    #[cfg(feature = "hazmat-unrestricted-exposure")]
    #[test]
    fn hazmat_newtype_provides_explicit_unrestricted_string_exposure() {
        let mut secret =
            UnrestrictedSecretBox::<String>::init_with_mut(|text| text.push_str("token"));
        secret.expose_secret_mut().push_str("-rotated");
        assert_eq!(secret.expose_secret(), "token-rotated");

        let restricted = secret.into_restricted();
        assert_eq!(restricted.inner_secret.as_ref(), "token-rotated");
    }

    #[test]
    fn exact_vector_transfer_rejects_and_sanitizes_excess_capacity() {
        struct NonCloneable {
            cleared: Arc<AtomicBool>,
            byte: u8,
        }

        impl SecureSanitize for NonCloneable {
            fn secure_sanitize(&mut self) {
                self.byte = 0;
                self.cleared.store(true, Ordering::SeqCst);
            }
        }

        let cleared = Arc::new(AtomicBool::new(false));
        let mut source = Vec::with_capacity(8);
        source.push(NonCloneable {
            cleared: Arc::clone(&cleared),
            byte: 23,
        });

        let error = SecretSlice::try_from_vec_exact(source).unwrap_err();
        assert_eq!(error.length, 1);
        assert_eq!(error.capacity, 8);
        assert!(cleared.load(Ordering::SeqCst));
    }

    #[test]
    fn exact_vector_transfer_preserves_live_elements() {
        let mut source = Vec::with_capacity(4);
        source.extend_from_slice(&[3_u8, 5, 7, 11]);
        assert_eq!(source.len(), source.capacity());

        let secret = SecretSlice::try_from_vec_exact(source).unwrap();
        assert_eq!(secret.expose_secret(), &[3, 5, 7, 11]);
    }

    #[test]
    fn vector_conversion_clears_source_with_excess_capacity() {
        let cleared = Arc::new(AtomicBool::new(false));
        let mut source = Vec::with_capacity(8);
        source.push(TrackedSecret {
            cleared: Arc::clone(&cleared),
            byte: 31,
        });

        let secret = SecretSlice::from(source);
        assert!(cleared.load(Ordering::SeqCst));
        assert_eq!(secret.expose_secret()[0].byte, 31);
    }

    #[test]
    fn debug_is_redacted() {
        let secret = SecretString::from("do-not-print");
        let rendered = format!("{secret:?}");
        assert!(rendered.contains("REDACTED"));
        assert!(!rendered.contains("do-not-print"));
    }

    #[test]
    fn init_with_mut_writes_final_allocation() {
        let secret = SecretBox::<[u8; 4]>::init_with_mut(|bytes| {
            bytes.copy_from_slice(b"test");
        });
        assert_eq!(secret.expose_secret(), b"test");
    }

    #[test]
    fn runtime_slice_initializer_uses_final_length_and_propagates_errors() {
        let secret = SecretSlice::<u8>::init_with_len(5, |bytes| {
            assert_eq!(bytes.len(), 5);
            bytes.copy_from_slice(b"token");
        });
        assert_eq!(secret.expose_secret(), b"token");

        let result = SecretSlice::<u8>::try_init_with_len(4, |bytes| {
            bytes[0] = 17;
            Err::<(), _>("injected initialization failure")
        });
        assert!(matches!(
            result,
            Err(SecretSliceInitError::Initialize(
                "injected initialization failure"
            ))
        ));
    }

    #[test]
    fn bounded_runtime_slice_rejects_length_before_initialization() {
        let initializer_called = AtomicBool::new(false);
        let result = SecretSlice::<u8>::try_init_with_len_bounded::<4, &str>(5, |_| {
            initializer_called.store(true, Ordering::SeqCst);
            Ok(())
        });

        assert!(matches!(
            result,
            Err(SecretSliceInitError::Build(
                SecretSliceAllocationError::TooLong {
                    maximum: 4,
                    actual: 5
                }
            ))
        ));
        assert!(!initializer_called.load(Ordering::SeqCst));
    }

    #[test]
    fn runtime_slice_reports_capacity_overflow_as_allocation_failure() {
        let result = SecretSlice::<u8>::try_init_with_len(usize::MAX, |_| Ok::<(), &str>(()));
        assert!(matches!(
            result,
            Err(SecretSliceInitError::Build(
                SecretSliceAllocationError::Allocation(_)
            ))
        ));
    }

    #[test]
    fn reviewed_primitive_arrays_are_cloneable_secrets() {
        fn assert_cloneable<T: CloneableSecret>() {}

        assert_cloneable::<[u8; 32]>();
        assert_cloneable::<[i128; 2]>();
    }

    #[derive(Clone)]
    struct TrackedSecret {
        cleared: Arc<AtomicBool>,
        byte: u8,
    }

    impl SecureSanitize for TrackedSecret {
        fn secure_sanitize(&mut self) {
            self.byte = 0;
            self.cleared.store(true, Ordering::SeqCst);
        }
    }

    // STORAGE CONTRACT: all secret bytes are inline; shared and mutable
    // operations in this test type neither replace nor release storage.
    impl sanitization::StableSharedSecretStorage for TrackedSecret {}
    impl sanitization::StableMutableSecretStorage for TrackedSecret {}

    impl CloneableSecret for TrackedSecret {}

    #[derive(Default)]
    struct DefaultTrackedSecret(u8);

    impl SecureSanitize for DefaultTrackedSecret {
        fn secure_sanitize(&mut self) {
            self.0 = 0;
            DEFAULT_VALUE_CLEARED.store(true, Ordering::SeqCst);
        }
    }

    // STORAGE CONTRACT: the only storage is one inline byte.
    impl sanitization::StableSharedSecretStorage for DefaultTrackedSecret {}
    impl sanitization::StableMutableSecretStorage for DefaultTrackedSecret {}

    #[test]
    fn drop_sanitizes_boxed_value() {
        let cleared = Arc::new(AtomicBool::new(false));
        let secret = SecretBox::new(Box::new(TrackedSecret {
            cleared: Arc::clone(&cleared),
            byte: 42,
        }));

        drop(secret);
        assert!(cleared.load(Ordering::SeqCst));
    }

    #[test]
    fn cloned_secret_is_independent() {
        let source = SecretBox::new(Box::new(7_u32));
        let mut duplicate = source.clone();
        *duplicate.expose_secret_mut() = 9;

        assert_eq!(*source.expose_secret(), 7);
        assert_eq!(*duplicate.expose_secret(), 9);
    }

    #[test]
    fn custom_clone_marker_is_the_positive_opt_in() {
        let cleared = Arc::new(AtomicBool::new(false));
        let source = SecretBox::new(Box::new(TrackedSecret { cleared, byte: 19 }));
        let duplicate = source.clone();
        assert_eq!(duplicate.expose_secret().byte, 19);
    }

    #[test]
    fn completed_slice_clones_are_sanitized_when_a_later_clone_panics() {
        struct PanicCloneElement {
            byte: u8,
            clone_attempts: Arc<AtomicUsize>,
            leaked_on_drop: Arc<AtomicBool>,
            cloned: bool,
        }

        impl Clone for PanicCloneElement {
            fn clone(&self) -> Self {
                if self.clone_attempts.fetch_add(1, Ordering::SeqCst) == 1 {
                    panic!("injected element clone panic");
                }
                Self {
                    byte: self.byte,
                    clone_attempts: Arc::clone(&self.clone_attempts),
                    leaked_on_drop: Arc::clone(&self.leaked_on_drop),
                    cloned: true,
                }
            }
        }

        impl SecureSanitize for PanicCloneElement {
            fn secure_sanitize(&mut self) {
                self.byte = 0;
            }
        }

        impl CloneableSecret for PanicCloneElement {}

        impl Drop for PanicCloneElement {
            fn drop(&mut self) {
                if self.cloned && self.byte != 0 {
                    self.leaked_on_drop.store(true, Ordering::SeqCst);
                }
            }
        }

        let clone_attempts = Arc::new(AtomicUsize::new(0));
        let leaked_on_drop = Arc::new(AtomicBool::new(false));
        let source = SecretBox::new(
            vec![
                PanicCloneElement {
                    byte: 11,
                    clone_attempts: Arc::clone(&clone_attempts),
                    leaked_on_drop: Arc::clone(&leaked_on_drop),
                    cloned: false,
                },
                PanicCloneElement {
                    byte: 13,
                    clone_attempts,
                    leaked_on_drop: Arc::clone(&leaked_on_drop),
                    cloned: false,
                },
            ]
            .into_boxed_slice(),
        );

        let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
            let _ = source.clone();
        }));
        assert!(result.is_err());
        assert!(!leaked_on_drop.load(Ordering::SeqCst));
    }

    #[test]
    fn incomplete_pending_clone_keeps_cleanup_guard_armed() {
        let cleared = Arc::new(AtomicBool::new(false));
        let mut pending = PendingCloneSlice::new(2);
        pending.push(TrackedSecret {
            cleared: Arc::clone(&cleared),
            byte: 47,
        });

        let result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
            let _ = pending.into_box();
        }));

        assert!(result.is_err());
        assert!(cleared.load(Ordering::SeqCst));
    }

    #[test]
    fn compatibility_comparison_uses_explicit_declassification() {
        let left = SecretString::from("same-token");
        let equal = SecretString::from("same-token");
        let different = SecretString::from("other-token");

        assert!(left
            .ct_eq(&equal)
            .declassify("authentication token equality is intentionally public"));
        assert!(!left
            .ct_eq(&different)
            .declassify("authentication token inequality is intentionally public"));
    }

    #[test]
    fn init_temporary_is_sanitized_on_success() {
        let cleared = Arc::new(AtomicBool::new(false));
        let secret = SecretBox::init_with(|| TrackedSecret {
            cleared: Arc::clone(&cleared),
            byte: 42,
        });

        assert!(cleared.load(Ordering::SeqCst));
        assert_eq!(secret.expose_secret().byte, 42);
    }

    #[test]
    fn init_temporary_is_sanitized_when_clone_panics() {
        #[derive(Default)]
        struct PanicClone {
            cleared: Arc<AtomicBool>,
        }

        impl Clone for PanicClone {
            fn clone(&self) -> Self {
                panic!("injected clone panic")
            }
        }

        impl SecureSanitize for PanicClone {
            fn secure_sanitize(&mut self) {
                self.cleared.store(true, Ordering::SeqCst);
            }
        }

        impl CloneableSecret for PanicClone {}

        let cleared = Arc::new(AtomicBool::new(false));
        let result = panic::catch_unwind({
            let cleared = Arc::clone(&cleared);
            move || {
                let _ = SecretBox::init_with(|| PanicClone { cleared });
            }
        });

        assert!(result.is_err());
        assert!(cleared.load(Ordering::SeqCst));
    }

    #[test]
    fn init_with_mut_sanitizes_partial_value_during_unwind() {
        let _test_guard = DEFAULT_VALUE_TEST_LOCK
            .lock()
            .expect("default-value cleanup test lock");
        DEFAULT_VALUE_CLEARED.store(false, Ordering::SeqCst);
        let result = panic::catch_unwind(|| {
            let _ = SecretBox::<DefaultTrackedSecret>::init_with_mut(|secret| {
                secret.0 = 42;
                panic!("injected initializer panic");
            });
        });

        assert!(result.is_err());
        assert!(DEFAULT_VALUE_CLEARED.load(Ordering::SeqCst));
    }

    #[test]
    fn try_init_with_mut_sanitizes_partial_value_on_error() {
        let _test_guard = DEFAULT_VALUE_TEST_LOCK
            .lock()
            .expect("default-value cleanup test lock");
        DEFAULT_VALUE_CLEARED.store(false, Ordering::SeqCst);
        let result = SecretBox::<DefaultTrackedSecret>::try_init_with_mut(|secret| {
            secret.0 = 29;
            Err::<(), _>("injected initialization failure")
        });

        assert_eq!(result.unwrap_err(), "injected initialization failure");
        assert!(DEFAULT_VALUE_CLEARED.load(Ordering::SeqCst));
    }

    #[test]
    fn into_cleared_sanitizes_exactly_once() {
        struct CountedClear(Arc<AtomicUsize>);

        impl SecureSanitize for CountedClear {
            fn secure_sanitize(&mut self) {
                self.0.fetch_add(1, Ordering::SeqCst);
            }
        }

        let clear_count = Arc::new(AtomicUsize::new(0));
        SecretBox::new(Box::new(CountedClear(Arc::clone(&clear_count)))).into_cleared();
        assert_eq!(clear_count.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn fallible_constructor_preserves_generator_error() {
        let result = SecretBox::<[u8; 4]>::try_init_with(|| Err::<[u8; 4], _>("failure"));
        assert_eq!(result.unwrap_err(), "failure");
    }

    #[cfg(feature = "serde")]
    #[test]
    fn serde_plaintext_requires_explicit_marker() {
        let secret = SecretBox::new(Box::new([1_u8, 2, 3, 4]));
        assert_eq!(secret.expose_secret(), &[1, 2, 3, 4]);
        assert_eq!(serde_json::to_string(&secret).unwrap(), "[1,2,3,4]");
    }

    #[cfg(feature = "serde")]
    #[test]
    #[cfg_attr(
        miri,
        ignore = "large JSON boundary fixture is covered natively; exact limit arithmetic has a Miri-safe unit test"
    )]
    fn serde_secret_string_enforces_default_input_ceiling() {
        let accepted_json = format!(
            "\"{}\"",
            "a".repeat(sanitization::DEFAULT_SECRET_STRING_SERDE_MAX_LEN)
        );
        let accepted: SecretString = serde_json::from_str(&accepted_json).unwrap();
        assert_eq!(
            accepted.expose_secret().len(),
            sanitization::DEFAULT_SECRET_STRING_SERDE_MAX_LEN
        );

        let rejected_json = format!(
            "\"{}\"",
            "b".repeat(sanitization::DEFAULT_SECRET_STRING_SERDE_MAX_LEN + 1)
        );
        assert!(serde_json::from_str::<SecretString>(&rejected_json).is_err());
    }

    #[cfg(feature = "serde")]
    #[test]
    fn serde_secret_string_limit_accepts_max_and_rejects_next_byte() {
        let visitor = SecretStringVisitor;
        assert!(visitor
            .validate_len::<de::value::Error>(sanitization::DEFAULT_SECRET_STRING_SERDE_MAX_LEN)
            .is_ok());
        assert!(visitor
            .validate_len::<de::value::Error>(sanitization::DEFAULT_SECRET_STRING_SERDE_MAX_LEN + 1)
            .is_err());
    }

    #[cfg(feature = "serde-compat-unbounded")]
    #[test]
    fn generic_serde_deserialization_requires_unbounded_compatibility_opt_in() {
        let secret: SecretBox<String> = serde_json::from_str("\"token\"").unwrap();
        assert_eq!(secret.inner_secret.as_ref(), "token");
    }

    #[cfg(all(
        feature = "serde-compat-unbounded",
        feature = "hazmat-unrestricted-exposure"
    ))]
    #[test]
    fn unrestricted_newtype_serde_requires_both_explicit_features() {
        let secret: UnrestrictedSecretBox<String> = serde_json::from_str("\"token\"").unwrap();
        assert_eq!(secret.expose_secret(), "token");
        assert_eq!(serde_json::to_string(&secret).unwrap(), "\"token\"");
    }
}