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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright 2026 Paolo Vella
// SPDX-License-Identifier: MPL-2.0
//! ETDI (Enhanced Tool Definition Interface) types — cryptographic verification
//! of MCP tool definitions based on arxiv:2506.01333.
use serde::{Deserialize, Serialize};
use std::fmt;
/// Signature algorithm for tool definitions (ETDI).
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(rename_all = "snake_case")]
pub enum SignatureAlgorithm {
/// Ed25519 signatures (recommended, default).
#[default]
Ed25519,
/// ECDSA with P-256 curve.
EcdsaP256,
}
impl fmt::Display for SignatureAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SignatureAlgorithm::Ed25519 => write!(f, "ed25519"),
SignatureAlgorithm::EcdsaP256 => write!(f, "ecdsa_p256"),
}
}
}
/// SECURITY (FIND-R51-002): Validate that a string is a well-formed ISO 8601
/// basic timestamp in the format `YYYY-MM-DDTHH:MM:SSZ`.
///
/// Checks structure, character classes, and value ranges for all components.
/// Returns `false` for any malformed input, which callers use for fail-closed
/// behavior (treating malformed timestamps as expired).
fn is_valid_iso8601_basic(s: &str) -> bool {
// Must be exactly 20 bytes: "YYYY-MM-DDTHH:MM:SSZ"
if s.len() != 20 {
return false;
}
let b = s.as_bytes();
// Structural checks: separators at fixed positions
if b[4] != b'-'
|| b[7] != b'-'
|| b[10] != b'T'
|| b[13] != b':'
|| b[16] != b':'
|| b[19] != b'Z'
{
return false;
}
// All digit positions must be ASCII digits
let digit_positions = [0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18];
for &pos in &digit_positions {
if !b[pos].is_ascii_digit() {
return false;
}
}
// Parse and validate numeric ranges
// Safe: we verified all positions are ASCII digits above, so from_utf8 and parse cannot fail.
let year: u16 = s[0..4].parse().unwrap_or(0);
let month: u8 = s[5..7].parse().unwrap_or(0);
let day: u8 = s[8..10].parse().unwrap_or(0);
let hour: u8 = s[11..13].parse().unwrap_or(0);
let minute: u8 = s[14..16].parse().unwrap_or(0);
let second: u8 = s[17..19].parse().unwrap_or(0);
if year < 1970 {
return false;
}
if !(1..=12).contains(&month) {
return false;
}
// R231-TYP-1: Calendar-aware day-of-month validation.
let max_day = match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 => {
if (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400) {
29
} else {
28
}
}
_ => return false,
};
if !(1..=max_day).contains(&day) {
return false;
}
if hour > 23 {
return false;
}
if minute > 59 {
return false;
}
if second > 59 {
return false;
}
true
}
/// A cryptographic signature on a tool definition.
///
/// Part of the ETDI (Enhanced Tool Definition Interface) system.
/// Tool providers sign their tool definitions, and Vellaveto verifies
/// these signatures before allowing tool registration.
#[derive(Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ToolSignature {
/// Unique identifier for this signature.
pub signature_id: String,
/// Hex-encoded cryptographic signature.
pub signature: String,
/// Algorithm used to create the signature.
pub algorithm: SignatureAlgorithm,
/// Hex-encoded public key of the signer.
pub public_key: String,
/// Optional fingerprint of the public key (for key management).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub key_fingerprint: Option<String>,
/// ISO 8601 timestamp when the signature was created.
pub signed_at: String,
/// ISO 8601 timestamp when the signature expires (optional).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub expires_at: Option<String>,
/// SPIFFE ID of the signer for workload identity (optional).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub signer_spiffe_id: Option<String>,
/// Optional Rekor transparency log entry for tool provenance (Phase 23.4).
/// Contains a serialized `RekorEntry` for offline inclusion proof verification.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rekor_entry: Option<serde_json::Value>,
}
// SECURITY (FIND-R52-016): Custom Debug to redact signature and public_key.
impl std::fmt::Debug for ToolSignature {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ToolSignature")
.field("signature_id", &self.signature_id)
.field("signature", &"[REDACTED]")
.field("algorithm", &self.algorithm)
.field("public_key", &"[REDACTED]")
.field("key_fingerprint", &self.key_fingerprint)
.field("signed_at", &self.signed_at)
.field("expires_at", &self.expires_at)
.field("signer_spiffe_id", &self.signer_spiffe_id)
.field(
"rekor_entry",
&self.rekor_entry.as_ref().map(|_| "[PRESENT]"),
)
.finish()
}
}
impl ToolSignature {
/// Maximum serialized size of `rekor_entry` in bytes.
pub const MAX_REKOR_ENTRY_SIZE: usize = 65_536;
/// Maximum length for `signature_id` (bytes).
const MAX_SIGNATURE_ID_LEN: usize = 256;
/// Maximum length for `signature` hex string (bytes).
const MAX_SIGNATURE_LEN: usize = 512;
/// Maximum length for `public_key` hex string (bytes).
const MAX_PUBLIC_KEY_LEN: usize = 512;
/// Maximum length for ISO 8601 timestamp fields (`signed_at`, `expires_at`).
const MAX_TIMESTAMP_LEN: usize = 64;
/// Maximum length for `key_fingerprint` (bytes).
const MAX_KEY_FINGERPRINT_LEN: usize = 256;
/// Maximum length for `signer_spiffe_id` URI (bytes).
const MAX_SPIFFE_ID_LEN: usize = 2048;
/// Validate structural bounds on all string fields.
///
/// SECURITY (FIND-R52-002): Prevents memory exhaustion from oversized
/// signature payloads deserialized from untrusted input.
pub fn validate(&self) -> Result<(), String> {
// SECURITY (FIND-R52-002): Validate all string field lengths.
if self.signature_id.len() > Self::MAX_SIGNATURE_ID_LEN {
return Err(format!(
"ToolSignature signature_id length {} exceeds max {}",
self.signature_id.len(),
Self::MAX_SIGNATURE_ID_LEN,
));
}
if self.signature.len() > Self::MAX_SIGNATURE_LEN {
return Err(format!(
"ToolSignature signature length {} exceeds max {}",
self.signature.len(),
Self::MAX_SIGNATURE_LEN,
));
}
if self.public_key.len() > Self::MAX_PUBLIC_KEY_LEN {
return Err(format!(
"ToolSignature public_key length {} exceeds max {}",
self.public_key.len(),
Self::MAX_PUBLIC_KEY_LEN,
));
}
// SECURITY (FIND-R196-006): Validate that signature and public_key contain
// only hex characters. These fields are documented as "Hex-encoded" — accepting
// non-hex content could cause downstream parsing failures or be used for
// injection if the values are logged or interpolated without sanitization.
if !self.signature.is_empty() && !self.signature.bytes().all(|b| b.is_ascii_hexdigit()) {
return Err("ToolSignature signature contains non-hex characters".to_string());
}
if !self.public_key.is_empty() && !self.public_key.bytes().all(|b| b.is_ascii_hexdigit()) {
return Err("ToolSignature public_key contains non-hex characters".to_string());
}
if self.signed_at.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"ToolSignature signed_at length {} exceeds max {}",
self.signed_at.len(),
Self::MAX_TIMESTAMP_LEN,
));
}
if let Some(ref ea) = self.expires_at {
if ea.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"ToolSignature expires_at length {} exceeds max {}",
ea.len(),
Self::MAX_TIMESTAMP_LEN,
));
}
}
if let Some(ref kf) = self.key_fingerprint {
if kf.len() > Self::MAX_KEY_FINGERPRINT_LEN {
return Err(format!(
"ToolSignature key_fingerprint length {} exceeds max {}",
kf.len(),
Self::MAX_KEY_FINGERPRINT_LEN,
));
}
}
if let Some(ref spiffe) = self.signer_spiffe_id {
if spiffe.len() > Self::MAX_SPIFFE_ID_LEN {
return Err(format!(
"ToolSignature signer_spiffe_id length {} exceeds max {}",
spiffe.len(),
Self::MAX_SPIFFE_ID_LEN,
));
}
}
if let Some(ref entry) = self.rekor_entry {
let size = serde_json::to_string(entry)
.map_err(|e| format!("rekor_entry serialization failed: {e}"))?
.len();
if size > Self::MAX_REKOR_ENTRY_SIZE {
return Err(format!(
"rekor_entry serialized size {} exceeds max {}",
size,
Self::MAX_REKOR_ENTRY_SIZE
));
}
}
// SECURITY (FIND-R141-003): Validate control and Unicode format characters
// in all string fields. Without this, a signature_id containing newlines could
// cause log injection, and a signer_spiffe_id with zero-width chars could bypass
// SPIFFE-based identity matching. This matches the pattern in ToolVersionPin::validate().
for (name, value) in [
("signature_id", Some(&self.signature_id)),
("signed_at", Some(&self.signed_at)),
] {
if let Some(v) = value {
if v.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"ToolSignature {name} contains control or format characters"
));
}
}
}
for (name, value) in [
("expires_at", self.expires_at.as_ref()),
("key_fingerprint", self.key_fingerprint.as_ref()),
("signer_spiffe_id", self.signer_spiffe_id.as_ref()),
] {
if let Some(v) = value {
if v.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"ToolSignature {name} contains control or format characters"
));
}
}
}
Ok(())
}
/// Returns true if the signature has expired.
///
/// SAFETY: Lexicographic comparison is correct for ISO 8601 timestamps
/// in the canonical format `YYYY-MM-DDTHH:MM:SSZ` because:
/// - All fields are fixed-width with zero-padding
/// - Fields are ordered from most significant (year) to least (second)
/// - The character ordering of digits matches numeric ordering
///
/// This holds for any consistent ISO 8601 format (with or without 'Z',
/// with or without fractional seconds) as long as both timestamps use
/// the same format and timezone.
pub fn is_expired(&self, now: &str) -> bool {
// SECURITY (FIND-R49-004): Reject non-UTC timestamps as expired (fail-closed).
if !now.ends_with('Z') {
return true;
}
// SECURITY (FIND-R51-002): Reject malformed `now` timestamps as expired (fail-closed).
if !is_valid_iso8601_basic(now) {
return true;
}
self.expires_at.as_ref().is_some_and(|exp| {
if !exp.ends_with('Z') {
return true;
}
// SECURITY (FIND-R51-002): Reject malformed `expires_at` as expired (fail-closed).
if !is_valid_iso8601_basic(exp) {
return true;
}
now >= exp.as_str()
})
}
}
/// Verification result for a tool signature.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct SignatureVerification {
/// Whether the cryptographic signature is valid.
pub valid: bool,
/// Whether the signer is in the trusted signers list.
pub signer_trusted: bool,
/// Whether the signature has expired.
pub expired: bool,
/// Human-readable message about the verification result.
pub message: String,
}
impl SignatureVerification {
/// Maximum length for `message` field (bytes).
const MAX_MESSAGE_LEN: usize = 4096;
/// Returns true if the signature passes all checks.
pub fn is_fully_verified(&self) -> bool {
self.valid && self.signer_trusted && !self.expired
}
/// Validate structural bounds on string fields.
///
/// SECURITY (FIND-R146-TE-002): Prevents memory exhaustion from oversized
/// message fields and control/format character injection from untrusted payloads.
pub fn validate(&self) -> Result<(), String> {
if self.message.len() > Self::MAX_MESSAGE_LEN {
return Err(format!(
"SignatureVerification message length {} exceeds max {}",
self.message.len(),
Self::MAX_MESSAGE_LEN,
));
}
if crate::core::has_dangerous_chars(&self.message) {
return Err(
"SignatureVerification message contains control or format characters".to_string(),
);
}
Ok(())
}
}
/// An attestation record in a tool's provenance chain.
///
/// Attestations form a chain of custody for tool definitions,
/// allowing verification that a tool has not been modified
/// since it was first registered.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ToolAttestation {
/// Unique identifier for this attestation.
pub attestation_id: String,
/// Type of attestation (e.g., "initial", "version_update", "security_audit").
pub attestation_type: String,
/// Entity that created this attestation.
pub attester: String,
/// ISO 8601 timestamp when the attestation was created.
pub timestamp: String,
/// SHA-256 hash of the tool definition at attestation time.
pub tool_hash: String,
/// ID of the previous attestation in the chain (None for first attestation).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub previous_attestation: Option<String>,
/// Cryptographic signature on this attestation.
pub signature: ToolSignature,
/// Optional reference to a transparency log entry.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub transparency_log_entry: Option<String>,
}
impl ToolAttestation {
/// Maximum attestation chain depth to prevent DoS during chain traversal.
pub const MAX_ATTESTATION_CHAIN_DEPTH: usize = 64;
/// Maximum length for `attestation_id`.
const MAX_ATTESTATION_ID_LEN: usize = 256;
/// Maximum length for `attestation_type`.
const MAX_ATTESTATION_TYPE_LEN: usize = 128;
/// Maximum length for `attester`.
const MAX_ATTESTER_LEN: usize = 512;
/// Maximum length for ISO 8601 timestamp fields.
const MAX_TIMESTAMP_LEN: usize = 64;
/// Maximum length for `tool_hash` (hex-encoded SHA-256 = 64 chars).
const MAX_TOOL_HASH_LEN: usize = 256;
/// Maximum length for `previous_attestation` ID.
const MAX_PREV_ATTESTATION_LEN: usize = 256;
/// Maximum length for `transparency_log_entry` reference.
const MAX_LOG_ENTRY_LEN: usize = 2048;
/// Validate structural bounds on string fields and the nested signature.
///
/// SECURITY: Prevents memory exhaustion from oversized attestation payloads
/// deserialized from untrusted input. Delegates to [`ToolSignature::validate()`]
/// for the inner signature.
pub fn validate(&self) -> Result<(), String> {
if self.attestation_id.len() > Self::MAX_ATTESTATION_ID_LEN {
return Err(format!(
"ToolAttestation attestation_id length {} exceeds max {}",
self.attestation_id.len(),
Self::MAX_ATTESTATION_ID_LEN,
));
}
if self.attestation_type.len() > Self::MAX_ATTESTATION_TYPE_LEN {
return Err(format!(
"ToolAttestation attestation_type length {} exceeds max {}",
self.attestation_type.len(),
Self::MAX_ATTESTATION_TYPE_LEN,
));
}
if self.attester.len() > Self::MAX_ATTESTER_LEN {
return Err(format!(
"ToolAttestation attester length {} exceeds max {}",
self.attester.len(),
Self::MAX_ATTESTER_LEN,
));
}
if self.timestamp.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"ToolAttestation timestamp length {} exceeds max {}",
self.timestamp.len(),
Self::MAX_TIMESTAMP_LEN,
));
}
if self.tool_hash.len() > Self::MAX_TOOL_HASH_LEN {
return Err(format!(
"ToolAttestation tool_hash length {} exceeds max {}",
self.tool_hash.len(),
Self::MAX_TOOL_HASH_LEN,
));
}
if let Some(ref prev) = self.previous_attestation {
if prev.len() > Self::MAX_PREV_ATTESTATION_LEN {
return Err(format!(
"ToolAttestation previous_attestation length {} exceeds max {}",
prev.len(),
Self::MAX_PREV_ATTESTATION_LEN,
));
}
}
if let Some(ref entry) = self.transparency_log_entry {
if entry.len() > Self::MAX_LOG_ENTRY_LEN {
return Err(format!(
"ToolAttestation transparency_log_entry length {} exceeds max {}",
entry.len(),
Self::MAX_LOG_ENTRY_LEN,
));
}
}
// SECURITY (FIND-R141-004): Validate control and Unicode format characters
// in all string fields. Without this, an attestation_id or attester with
// newlines could cause log injection, and a tool_hash with zero-width chars
// could cause integrity check inconsistencies. Matches ToolVersionPin::validate() pattern.
for (name, value) in [
("attestation_id", &self.attestation_id as &str),
("attestation_type", &self.attestation_type),
("attester", &self.attester),
("timestamp", &self.timestamp),
("tool_hash", &self.tool_hash),
] {
if value
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"ToolAttestation {name} contains control or format characters"
));
}
}
for (name, value) in [
("previous_attestation", self.previous_attestation.as_ref()),
(
"transparency_log_entry",
self.transparency_log_entry.as_ref(),
),
] {
if let Some(v) = value {
if v.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(format!(
"ToolAttestation {name} contains control or format characters"
));
}
}
}
self.signature.validate()?;
Ok(())
}
/// Returns true if this is the first attestation in the chain.
pub fn is_initial(&self) -> bool {
self.previous_attestation.is_none()
}
}
/// Version pinning record for a tool.
///
/// Pins allow administrators to lock tools to specific versions
/// or version constraints, preventing unauthorized updates.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ToolVersionPin {
/// Name of the tool being pinned.
pub tool_name: String,
/// Exact version to pin to (e.g., "1.2.3").
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pinned_version: Option<String>,
/// Semver constraint (e.g., "^1.2.0", ">=1.0,<2.0").
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version_constraint: Option<String>,
/// SHA-256 hash of the pinned tool definition.
pub definition_hash: String,
/// ISO 8601 timestamp when the pin was created.
pub pinned_at: String,
/// Identity of who created this pin.
pub pinned_by: String,
}
impl ToolVersionPin {
/// Maximum length for `tool_name` (bytes).
const MAX_TOOL_NAME_LEN: usize = 256;
/// Maximum length for `pinned_version` (bytes).
const MAX_VERSION_LEN: usize = 128;
/// Maximum length for `version_constraint` (bytes).
const MAX_CONSTRAINT_LEN: usize = 256;
/// Maximum length for `definition_hash` (bytes).
const MAX_HASH_LEN: usize = 256;
/// Maximum length for ISO 8601 timestamp fields (bytes).
const MAX_TIMESTAMP_LEN: usize = 64;
/// Maximum length for `pinned_by` (bytes).
const MAX_PINNED_BY_LEN: usize = 256;
/// Returns true if the pin uses an exact version match.
pub fn is_exact(&self) -> bool {
self.pinned_version.is_some()
}
/// Returns true if the pin uses a constraint.
pub fn is_constraint(&self) -> bool {
self.version_constraint.is_some()
}
/// Validate structural bounds on fields.
///
/// SECURITY (FIND-R53-P3-008): Prevents memory exhaustion and control character
/// injection from untrusted `ToolVersionPin` payloads.
pub fn validate(&self) -> Result<(), String> {
if self.tool_name.is_empty() {
return Err("ToolVersionPin tool_name must not be empty".to_string());
}
if self.tool_name.len() > Self::MAX_TOOL_NAME_LEN {
return Err(format!(
"ToolVersionPin tool_name length {} exceeds max {}",
self.tool_name.len(),
Self::MAX_TOOL_NAME_LEN,
));
}
if crate::core::has_dangerous_chars(&self.tool_name) {
return Err(
"ToolVersionPin tool_name contains control or format characters".to_string(),
);
}
if let Some(ref ver) = self.pinned_version {
if ver.len() > Self::MAX_VERSION_LEN {
return Err(format!(
"ToolVersionPin pinned_version length {} exceeds max {}",
ver.len(),
Self::MAX_VERSION_LEN,
));
}
// SECURITY: Reject control/format chars in pinned_version to prevent
// version comparison bypass via zero-width characters.
if crate::core::has_dangerous_chars(ver) {
return Err(
"ToolVersionPin pinned_version contains control or format characters"
.to_string(),
);
}
}
if let Some(ref constraint) = self.version_constraint {
if constraint.len() > Self::MAX_CONSTRAINT_LEN {
return Err(format!(
"ToolVersionPin version_constraint length {} exceeds max {}",
constraint.len(),
Self::MAX_CONSTRAINT_LEN,
));
}
// SECURITY: Reject control/format chars in version_constraint to prevent
// constraint parsing bypass via invisible characters.
if crate::core::has_dangerous_chars(constraint) {
return Err(
"ToolVersionPin version_constraint contains control or format characters"
.to_string(),
);
}
}
if self.definition_hash.len() > Self::MAX_HASH_LEN {
return Err(format!(
"ToolVersionPin definition_hash length {} exceeds max {}",
self.definition_hash.len(),
Self::MAX_HASH_LEN,
));
}
// SECURITY: Reject control/format chars in definition_hash to prevent
// integrity check inconsistencies via zero-width characters in hash comparison.
if crate::core::has_dangerous_chars(&self.definition_hash) {
return Err(
"ToolVersionPin definition_hash contains control or format characters".to_string(),
);
}
if self.pinned_at.len() > Self::MAX_TIMESTAMP_LEN {
return Err(format!(
"ToolVersionPin pinned_at length {} exceeds max {}",
self.pinned_at.len(),
Self::MAX_TIMESTAMP_LEN,
));
}
// SECURITY: Reject control/format chars in pinned_at to prevent
// log injection and timestamp comparison bypass.
if crate::core::has_dangerous_chars(&self.pinned_at) {
return Err(
"ToolVersionPin pinned_at contains control or format characters".to_string(),
);
}
if self.pinned_by.is_empty() {
return Err("ToolVersionPin pinned_by must not be empty".to_string());
}
if self.pinned_by.len() > Self::MAX_PINNED_BY_LEN {
return Err(format!(
"ToolVersionPin pinned_by length {} exceeds max {}",
self.pinned_by.len(),
Self::MAX_PINNED_BY_LEN,
));
}
if crate::core::has_dangerous_chars(&self.pinned_by) {
return Err(
"ToolVersionPin pinned_by contains control or format characters".to_string(),
);
}
Ok(())
}
}
/// Result of version drift detection.
///
/// Generated when a tool's version or definition changes
/// from the pinned state.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct VersionDriftAlert {
/// Name of the tool with drift.
pub tool: String,
/// Expected version string or definition hash from the pin.
///
/// When `drift_type` is `"version_mismatch"`, this holds the pinned semver
/// version (e.g., `"1.2.3"`). When `drift_type` is `"hash_mismatch"`, this
/// holds the pinned SHA-256 definition hash.
pub expected_version: String,
/// Actual version string or definition hash observed at evaluation time.
///
/// Interpretation depends on `drift_type` — see [`Self::expected_version`].
pub actual_version: String,
/// Type of drift detected: `"version_mismatch"` (semver changed) or
/// `"hash_mismatch"` (definition content changed while version stayed the same).
pub drift_type: String,
/// Whether this drift should block the tool from being used.
pub blocking: bool,
/// ISO 8601 timestamp when the drift was detected.
pub detected_at: String,
}
impl VersionDriftAlert {
/// Maximum length for string fields (`tool`, `expected_version`,
/// `actual_version`, `drift_type`, `detected_at`).
const MAX_FIELD_LEN: usize = 512;
/// Validate structural bounds on all string fields.
///
/// SECURITY (FIND-R146-TE-001): Prevents memory exhaustion from oversized
/// fields and control/format character injection from untrusted payloads.
pub fn validate(&self) -> Result<(), String> {
for (name, value) in [
("tool", &self.tool),
("expected_version", &self.expected_version),
("actual_version", &self.actual_version),
("drift_type", &self.drift_type),
("detected_at", &self.detected_at),
] {
if value.is_empty() {
return Err(format!("VersionDriftAlert {name} must not be empty"));
}
if value.len() > Self::MAX_FIELD_LEN {
return Err(format!(
"VersionDriftAlert {} length {} exceeds max {}",
name,
value.len(),
Self::MAX_FIELD_LEN,
));
}
if crate::core::has_dangerous_chars(value) {
return Err(format!(
"VersionDriftAlert {name} contains control or format characters"
));
}
}
Ok(())
}
/// Create a version mismatch alert.
pub fn version_mismatch(
tool: impl Into<String>,
expected: impl Into<String>,
actual: impl Into<String>,
blocking: bool,
detected_at: impl Into<String>,
) -> Self {
Self {
tool: tool.into(),
expected_version: expected.into(),
actual_version: actual.into(),
drift_type: "version_mismatch".to_string(),
blocking,
detected_at: detected_at.into(),
}
}
/// Create a hash mismatch alert.
pub fn hash_mismatch(
tool: impl Into<String>,
expected_hash: impl Into<String>,
actual_hash: impl Into<String>,
blocking: bool,
detected_at: impl Into<String>,
) -> Self {
Self {
tool: tool.into(),
expected_version: expected_hash.into(),
actual_version: actual_hash.into(),
drift_type: "hash_mismatch".to_string(),
blocking,
detected_at: detected_at.into(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_r231_iso8601_rejects_feb_31() {
assert!(!is_valid_iso8601_basic("2026-02-31T00:00:00Z"));
}
#[test]
fn test_r231_iso8601_rejects_feb_30_non_leap() {
assert!(!is_valid_iso8601_basic("2025-02-30T00:00:00Z"));
}
#[test]
fn test_r231_iso8601_rejects_feb_29_non_leap() {
assert!(!is_valid_iso8601_basic("2025-02-29T00:00:00Z"));
}
#[test]
fn test_r231_iso8601_accepts_feb_29_leap() {
assert!(is_valid_iso8601_basic("2024-02-29T00:00:00Z"));
}
#[test]
fn test_r231_iso8601_accepts_feb_28_non_leap() {
assert!(is_valid_iso8601_basic("2025-02-28T12:30:45Z"));
}
#[test]
fn test_r231_iso8601_rejects_apr_31() {
assert!(!is_valid_iso8601_basic("2026-04-31T00:00:00Z"));
}
#[test]
fn test_r231_iso8601_accepts_apr_30() {
assert!(is_valid_iso8601_basic("2026-04-30T00:00:00Z"));
}
#[test]
fn test_r231_iso8601_rejects_jun_31() {
assert!(!is_valid_iso8601_basic("2026-06-31T10:00:00Z"));
}
#[test]
fn test_r231_iso8601_accepts_jan_31() {
assert!(is_valid_iso8601_basic("2026-01-31T23:59:59Z"));
}
#[test]
fn test_r231_iso8601_century_leap_year() {
// 2000 is divisible by 400 → leap
assert!(is_valid_iso8601_basic("2000-02-29T00:00:00Z"));
// 1900 is divisible by 100 but not 400 → NOT leap
// But year < 1970 is rejected separately
}
}