daml-lint 0.9.3

Static analysis scanner for Daml smart contracts
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
use crate::ir::DamlModule;
use daml_syntax::{CharColumn, LineNumber};
use serde::{Serialize, Serializer};
use std::error::Error;
use std::path::PathBuf;

fn serialize_line_number<S>(line: &LineNumber, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_u64(line.get() as u64)
}

fn serialize_char_column<S>(column: &CharColumn, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_u64(column.get() as u64)
}

/// Error returned by fallible detector execution.
///
/// Built-in detectors are currently infallible. Custom JavaScript rules can
/// fail at runtime or be interrupted, and library callers should handle those
/// failures through [`Detector::try_detect`] instead of letting a rule terminate
/// the host process.
#[derive(Debug)]
#[non_exhaustive]
pub struct DetectError {
    detector: String,
    message: String,
    source: Option<Box<dyn Error + Send + Sync + 'static>>,
}

impl DetectError {
    /// Build a detector error for `detector` with a human-readable `message`.
    #[must_use]
    pub fn new(detector: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            detector: detector.into(),
            message: message.into(),
            source: None,
        }
    }

    /// Build a detector error that preserves the recoverable cause that made
    /// the detector fail.
    #[must_use]
    pub fn with_source<E>(
        detector: impl Into<String>,
        message: impl Into<String>,
        source: E,
    ) -> Self
    where
        E: Error + Send + Sync + 'static,
    {
        Self {
            detector: detector.into(),
            message: message.into(),
            source: Some(Box::new(source)),
        }
    }

    /// Detector or custom-rule name that produced the error.
    #[must_use]
    pub fn detector(&self) -> &str {
        &self.detector
    }

    /// Human-readable error detail.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }

    /// Recoverable source error that caused detector execution to fail, when
    /// one is available.
    #[must_use]
    pub fn source(&self) -> Option<&(dyn Error + Send + Sync + 'static)> {
        self.source.as_deref()
    }
}

impl std::fmt::Display for DetectError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "detector '{}': {}", self.detector, self.message)
    }
}

impl std::error::Error for DetectError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.source
            .as_deref()
            .map(|source| source as &(dyn std::error::Error + 'static))
    }
}

/// Error returned when parsing an unsupported severity value.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SeverityParseError {
    value: String,
}

impl SeverityParseError {
    /// Unsupported severity text that was rejected.
    #[must_use]
    pub fn value(&self) -> &str {
        &self.value
    }
}

impl std::fmt::Display for SeverityParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "invalid severity: {} (expected one of critical|high|medium|low|info)",
            self.value
        )
    }
}

impl std::error::Error for SeverityParseError {}

/// Severity assigned to a detector finding.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub enum Severity {
    /// Critical issue that should fail any release or CI gate.
    Critical,
    /// High-risk issue; this is the default fail threshold for the CLI.
    High,
    /// Medium-risk issue that may indicate nondeterminism or policy drift.
    Medium,
    /// Low-risk issue.
    Low,
    /// Informational issue.
    Info,
}

impl Severity {
    /// Relative risk ordering used by report sorting and threshold checks.
    ///
    /// `Critical` has the highest rank (`5`) and `Info` the lowest (`1`).
    /// `Severity` intentionally does not implement `Ord`; use `rank()` or
    /// `meets_or_exceeds()` for risk-based ordering and threshold checks.
    #[must_use]
    pub const fn rank(self) -> u8 {
        match self {
            Self::Critical => 5,
            Self::High => 4,
            Self::Medium => 3,
            Self::Low => 2,
            Self::Info => 1,
        }
    }

    /// Returns `true` when `self` is at least as severe as `threshold`.
    ///
    /// For example, with threshold `High`, `Critical` and `High` both return
    /// `true`, while `Medium` and below return `false`.
    #[must_use]
    pub const fn meets_or_exceeds(self, threshold: Self) -> bool {
        self.rank() >= threshold.rank()
    }
}

impl std::fmt::Display for Severity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Critical => write!(f, "CRITICAL"),
            Self::High => write!(f, "HIGH"),
            Self::Medium => write!(f, "MEDIUM"),
            Self::Low => write!(f, "LOW"),
            Self::Info => write!(f, "INFO"),
        }
    }
}

impl std::str::FromStr for Severity {
    type Err = SeverityParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "critical" => Ok(Self::Critical),
            "high" => Ok(Self::High),
            "medium" => Ok(Self::Medium),
            "low" => Ok(Self::Low),
            "info" => Ok(Self::Info),
            _ => Err(SeverityParseError {
                value: s.to_string(),
            }),
        }
    }
}

/// A single detector result reported for one source location.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[non_exhaustive]
pub struct Finding {
    /// Detector or custom-rule name.
    pub detector: String,
    /// Finding severity.
    pub severity: Severity,
    /// Source file where the finding was reported.
    pub file: PathBuf,
    /// 1-based source line.
    #[serde(serialize_with = "serialize_line_number")]
    pub line: LineNumber,
    /// 1-based source column.
    #[serde(serialize_with = "serialize_char_column")]
    pub column: CharColumn,
    /// Human-readable finding message.
    pub message: String,
    /// Source excerpt or structural evidence for the finding.
    pub evidence: String,
}

/// Bundle of source location metadata for a finding.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct FindingLocation {
    /// Source file where the finding was reported.
    pub file: PathBuf,
    /// 1-based source line.
    pub line: LineNumber,
    /// 1-based source column.
    pub column: CharColumn,
}

impl FindingLocation {
    /// Construct a finding source location without relying on struct literal syntax.
    #[must_use]
    pub fn new(file: impl Into<PathBuf>, line: LineNumber, column: CharColumn) -> Self {
        Self {
            file: file.into(),
            line,
            column,
        }
    }
}

impl Finding {
    /// Construct a `Finding` without relying on struct literal syntax.
    #[must_use]
    pub fn new(
        detector: impl Into<String>,
        severity: Severity,
        location: FindingLocation,
        message: impl Into<String>,
        evidence: impl Into<String>,
    ) -> Self {
        Self {
            detector: detector.into(),
            severity,
            file: location.file,
            line: location.line,
            column: location.column,
            message: message.into(),
            evidence: evidence.into(),
        }
    }
}

// Scanning is single-threaded; detectors hold per-rule QuickJS state.
/// Static analysis rule over a lowered Daml module.
///
/// Implement [`Detector::try_detect`] for all rules; it is the required API and
/// communicates runtime/script failures to callers. Implementations that cannot
/// fail can use the default [`Detector::detect`] convenience adapter or provide
/// a fallible implementation that returns `Ok`.
pub trait Detector {
    /// Stable detector name used in reports and duplicate-rule checks.
    fn name(&self) -> &str;
    /// Severity assigned to findings from this detector.
    fn severity(&self) -> Severity;
    /// One-line detector description.
    fn description(&self) -> &str;
    /// Run an infallible detector over `module`.
    ///
    /// Implementations may continue to implement this directly; for most rules
    /// this adapter provides a panic-first convenience layer over
    /// [`Detector::try_detect`].
    ///
    /// # Panics
    ///
    /// Panics when [`Detector::try_detect`] returns [`DetectError`]. Use
    /// [`Detector::try_detect`] directly for custom JavaScript rules or other
    /// recoverable detector failures that should not terminate the caller.
    fn detect(&self, module: &DamlModule) -> Vec<Finding> {
        self.try_detect(module)
            .unwrap_or_else(|e| panic!("detector '{}' failed: {}", self.name(), e))
    }
    /// Run a detector that may fail without terminating the caller.
    ///
    /// # Errors
    ///
    /// Returns [`DetectError`] when the detector cannot analyze `module`.
    fn try_detect(&self, module: &DamlModule) -> Result<Vec<Finding>, DetectError>;
}

/// Detector wrapper that can rename a rule and/or override finding severity.
pub struct ConfiguredDetector {
    inner: Box<dyn Detector>,
    name_override: Option<String>,
    severity_override: Option<Severity>,
}

impl ConfiguredDetector {
    fn new(
        inner: Box<dyn Detector>,
        name_override: Option<String>,
        severity_override: Option<Severity>,
    ) -> Self {
        Self {
            inner,
            name_override,
            severity_override,
        }
    }

    /// Construct a configured detector that only renames the wrapped rule.
    #[must_use]
    pub fn with_name(inner: Box<dyn Detector>, name: impl Into<String>) -> Self {
        Self::new(inner, Some(name.into()), None)
    }

    /// Construct a configured detector that only overrides the wrapped rule
    /// severity.
    #[must_use]
    pub fn with_severity(inner: Box<dyn Detector>, severity: Severity) -> Self {
        Self::new(inner, None, Some(severity))
    }

    fn apply_overrides(&self, mut findings: Vec<Finding>) -> Vec<Finding> {
        let name = self.name().to_string();
        let severity = self.severity();
        for finding in &mut findings {
            if self.name_override.is_some() {
                finding.detector = name.clone();
            }
            if self.severity_override.is_some() {
                finding.severity = severity;
            }
        }
        findings
    }
}

impl Detector for ConfiguredDetector {
    fn name(&self) -> &str {
        self.name_override
            .as_deref()
            .unwrap_or_else(|| self.inner.name())
    }

    fn severity(&self) -> Severity {
        self.severity_override
            .unwrap_or_else(|| self.inner.severity())
    }

    fn description(&self) -> &str {
        self.inner.description()
    }

    fn try_detect(&self, module: &DamlModule) -> Result<Vec<Finding>, DetectError> {
        self.inner
            .try_detect(module)
            .map(|findings| self.apply_overrides(findings))
            .map_err(|e| {
                let wrapped_detector = e.detector().to_string();
                let message = format!(
                    "wrapped detector '{wrapped_detector}' failed: {}",
                    e.message()
                );
                DetectError::with_source(self.name(), message, e)
            })
    }
}

/// Returns the first detector name that appears more than once, if any.
#[must_use]
pub fn find_duplicate_detector_name(detectors: &[Box<dyn Detector>]) -> Option<String> {
    let mut seen = std::collections::HashSet::new();
    for det in detectors {
        if !seen.insert(det.name()) {
            return Some(det.name().to_string());
        }
    }
    None
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod configured_detector_tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn configured_detector_with_name_rewrites_detector_name_and_findings() {
        struct NamedDetector;

        impl Detector for NamedDetector {
            fn name(&self) -> &str {
                "named-base"
            }

            fn severity(&self) -> Severity {
                Severity::High
            }

            fn description(&self) -> &str {
                "base description"
            }

            fn try_detect(
                &self,
                module: &crate::ir::DamlModule,
            ) -> Result<Vec<Finding>, DetectError> {
                let _ = module;
                Ok(vec![Finding::new(
                    self.name(),
                    self.severity(),
                    FindingLocation::new("named.daml", LineNumber::new(9), CharColumn::new(11)),
                    "rewritable finding",
                    "x",
                )])
            }
        }

        let detector: Box<dyn Detector> = Box::new(ConfiguredDetector::with_name(
            Box::new(NamedDetector),
            "rewrite",
        ));
        assert_eq!(detector.name(), "rewrite");
        let module = crate::ir::DamlModule {
            ir_version: 8,
            name: String::from("Main"),
            file: PathBuf::from("named.daml"),
            source: String::new(),
            imports: Vec::new(),
            templates: Vec::new(),
            interfaces: Vec::new(),
            functions: Vec::new(),
        };
        let findings = detector.try_detect(&module).unwrap();
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].detector, "rewrite");
        assert_eq!(findings[0].line, LineNumber::new(9));
    }

    #[test]
    fn configured_detector_with_severity_overrides_reported_severity() {
        struct SeverityDetector;

        impl Detector for SeverityDetector {
            fn name(&self) -> &str {
                "severity-base"
            }

            fn severity(&self) -> Severity {
                Severity::Critical
            }

            fn description(&self) -> &str {
                "base description"
            }

            fn try_detect(
                &self,
                module: &crate::ir::DamlModule,
            ) -> Result<Vec<Finding>, DetectError> {
                let _ = module;
                Ok(vec![Finding::new(
                    self.name(),
                    self.severity(),
                    FindingLocation::new("severity.daml", LineNumber::new(3), CharColumn::new(7)),
                    "high-severity finding",
                    "y",
                )])
            }
        }

        let detector: Box<dyn Detector> = Box::new(ConfiguredDetector::with_severity(
            Box::new(SeverityDetector),
            Severity::Info,
        ));
        assert_eq!(detector.severity(), Severity::Info);
        let module = crate::ir::DamlModule {
            ir_version: 8,
            name: String::from("Main"),
            file: PathBuf::from("severity.daml"),
            source: String::new(),
            imports: Vec::new(),
            templates: Vec::new(),
            interfaces: Vec::new(),
            functions: Vec::new(),
        };
        let findings = detector.try_detect(&module).unwrap();
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].severity, Severity::Info);
    }

    #[test]
    fn configured_detector_preserves_wrapped_error_source_chain() {
        struct FailingDetector;

        impl Detector for FailingDetector {
            fn name(&self) -> &str {
                "failing-base"
            }

            fn severity(&self) -> Severity {
                Severity::High
            }

            fn description(&self) -> &str {
                "base description"
            }

            fn try_detect(
                &self,
                module: &crate::ir::DamlModule,
            ) -> Result<Vec<Finding>, DetectError> {
                let _ = module;
                Err(DetectError::with_source(
                    self.name(),
                    "could not run visitor",
                    std::io::Error::new(std::io::ErrorKind::Interrupted, "runtime stopped"),
                ))
            }
        }

        let detector: Box<dyn Detector> = Box::new(ConfiguredDetector::with_name(
            Box::new(FailingDetector),
            "configured-name",
        ));
        let module = crate::ir::DamlModule {
            ir_version: 8,
            name: String::from("Main"),
            file: PathBuf::from("failing.daml"),
            source: String::new(),
            imports: Vec::new(),
            templates: Vec::new(),
            interfaces: Vec::new(),
            functions: Vec::new(),
        };

        let err = detector.try_detect(&module).unwrap_err();
        assert_eq!(err.detector(), "configured-name");
        assert!(err.message().contains("failing-base"));
        let wrapped = std::error::Error::source(&err)
            .and_then(|source| source.downcast_ref::<DetectError>())
            .expect("configured detector should preserve the inner DetectError");
        assert_eq!(wrapped.detector(), "failing-base");
        assert!(std::error::Error::source(wrapped)
            .is_some_and(|source| source.to_string() == "runtime stopped"));
    }
}