daml-lint 0.6.2

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
use std::process::Command;
use std::sync::atomic::{AtomicUsize, Ordering};

static NEXT_TEMP: AtomicUsize = AtomicUsize::new(0);

fn cmd() -> Command {
    Command::new(env!("CARGO_BIN_EXE_daml-lint"))
}

fn temp_file(name: &str, contents: &str) -> std::path::PathBuf {
    let id = NEXT_TEMP.fetch_add(1, Ordering::Relaxed);
    let path = std::env::temp_dir().join(format!(
        "daml-lint-cli-{}-{}-{}",
        std::process::id(),
        id,
        name
    ));
    std::fs::write(&path, contents).unwrap();
    path
}

fn temp_dir(name: &str) -> std::path::PathBuf {
    let id = NEXT_TEMP.fetch_add(1, Ordering::Relaxed);
    let path = std::env::temp_dir().join(format!(
        "daml-lint-cli-{}-{}-{}",
        std::process::id(),
        id,
        name
    ));
    std::fs::create_dir_all(&path).unwrap();
    path
}

fn clean_file() -> std::path::PathBuf {
    temp_file(
        "clean.daml",
        r#"module Clean where

template Holding
  with
    owner : Party
    amount : Decimal
  where
    signatory owner
    ensure amount > 0.0

    choice Transfer : ContractId Holding
      with
        newOwner : Party
      controller owner
      do
        create this with owner = newOwner
"#,
    )
}

#[test]
#[cfg(feature = "custom-rules")]
fn installed_plugin_manifest_rule_can_be_enabled_from_config() {
    let project = temp_dir("plugin-project");
    std::fs::create_dir_all(project.join("node_modules/daml-lint-plugin-template/dist")).unwrap();
    std::fs::write(
        project.join(".daml-lint.json"),
        r#"{
  "plugins": ["template"],
  "rules": {
    "template/template-name-blocklist": ["medium", { "names": ["Iou"] }]
  }
}
"#,
    )
    .unwrap();
    std::fs::write(
        project.join("node_modules/daml-lint-plugin-template/package.json"),
        r#"{
  "name": "daml-lint-plugin-template",
  "version": "1.0.0",
  "damlLint": {
    "rules": {
      "template-name-blocklist": "dist/template-name-blocklist.js"
    }
  }
}
"#,
    )
    .unwrap();
    std::fs::write(
        project.join("node_modules/daml-lint-plugin-template/dist/template-name-blocklist.js"),
        r#"
const NAME = "template-name-blocklist";
const SEVERITY = "low";

function on_template(template) {
  const config = typeof CONFIG === "object" && CONFIG !== null ? CONFIG : {};
  const names = Array.isArray(config.names) ? config.names : [];
  if (names.includes(template.name)) {
    report(template, `Template '${template.name}' is blocked by config`);
  }
}
"#,
    )
    .unwrap();
    std::fs::write(
        project.join("Iou.daml"),
        r#"module Iou where

template Iou
  with
    owner : Party
  where
    signatory owner
    ensure True
"#,
    )
    .unwrap();

    let output = cmd()
        .current_dir(&project)
        .arg("Iou.daml")
        .arg("--fail-on")
        .arg("medium")
        .output()
        .unwrap();
    std::fs::remove_dir_all(&project).ok();

    assert_eq!(output.status.code(), Some(1));
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("MEDIUM"), "stdout was:\n{stdout}");
    assert!(
        stdout.contains("template/template-name-blocklist"),
        "stdout was:\n{stdout}"
    );
    assert!(
        stdout.contains("blocked by config"),
        "stdout was:\n{stdout}"
    );
}

#[test]
#[cfg(feature = "custom-rules")]
fn config_can_disable_builtin_rule() {
    let project = temp_dir("disabled-builtin-project");
    std::fs::write(
        project.join(".daml-lint.json"),
        r#"{
  "rules": {
    "missing-ensure-decimal": "off"
  }
}
"#,
    )
    .unwrap();
    std::fs::write(
        project.join("Iou.daml"),
        r#"module Iou where

template Iou
  with
    issuer : Party
    amount : Decimal
  where
    signatory issuer
"#,
    )
    .unwrap();

    let output = cmd()
        .current_dir(&project)
        .arg("Iou.daml")
        .arg("--fail-on")
        .arg("info")
        .output()
        .unwrap();
    std::fs::remove_dir_all(&project).ok();

    assert!(output.status.success());
    assert!(String::from_utf8_lossy(&output.stdout).contains("No findings."));
}

#[test]
#[cfg(feature = "custom-rules")]
fn config_unknown_enabled_rule_exits_two() {
    let project = temp_dir("unknown-rule-project");
    std::fs::write(
        project.join(".daml-lint.json"),
        r#"{
  "rules": {
    "unknown-rule": "medium"
  }
}
"#,
    )
    .unwrap();
    std::fs::write(
        project.join("Clean.daml"),
        r#"module Clean where

template Holding
  with
    owner : Party
  where
    signatory owner
    ensure True
"#,
    )
    .unwrap();

    let output = cmd()
        .current_dir(&project)
        .arg("Clean.daml")
        .output()
        .unwrap();
    std::fs::remove_dir_all(&project).ok();

    assert_eq!(output.status.code(), Some(2));
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("unknown-rule"), "stderr was:\n{stderr}");
}

#[test]
fn help_exits_successfully() {
    let output = cmd().arg("--help").output().unwrap();
    assert!(output.status.success());
    assert!(String::from_utf8_lossy(&output.stdout).contains("Static analysis scanner"));
}

#[cfg(not(feature = "custom-rules"))]
#[test]
fn help_omits_rules_when_custom_rules_disabled() {
    let output = cmd().arg("--help").output().unwrap();
    assert!(output.status.success());
    assert!(!String::from_utf8_lossy(&output.stdout).contains("--rules"));
}

#[test]
fn clean_file_exits_zero() {
    let path = clean_file();
    let output = cmd().arg(&path).output().unwrap();
    std::fs::remove_file(&path).ok();

    assert!(output.status.success());
    assert!(String::from_utf8_lossy(&output.stdout).contains("No findings."));
}

#[test]
#[cfg(unix)]
fn unreadable_daml_file_exits_two() {
    use std::os::unix::fs::PermissionsExt;

    let bad = temp_file(
        "unreadable.daml",
        r#"module Bad where
f = ()"#,
    );
    let good = temp_file(
        "good.daml",
        r#"module Good where
f = 1"#,
    );
    let parent = bad.parent().unwrap().to_path_buf();
    let original_bad = std::fs::metadata(&bad).unwrap().permissions();
    let mut bad_perm = original_bad.clone();
    bad_perm.set_mode(0o000);
    std::fs::set_permissions(&bad, bad_perm).unwrap();

    let output = cmd()
        .current_dir(&parent)
        .arg(&good)
        .arg(&bad)
        .output()
        .unwrap();

    std::fs::set_permissions(&bad, original_bad).unwrap();
    std::fs::remove_file(&bad).ok();
    std::fs::remove_file(&good).ok();

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert_eq!(output.status.code(), Some(2));
    assert!(String::from_utf8_lossy(&output.stderr).contains("could not read"));
    assert!(stdout.contains("Parse Errors (1)"), "stdout was:\n{stdout}");
    assert!(!String::from_utf8_lossy(&output.stdout).contains("No findings."));
}

#[test]
#[cfg(unix)]
fn unreadable_subdirectory_exits_two() {
    use std::os::unix::fs::PermissionsExt;

    let root = temp_dir("unreadable-dir-project");
    let unreadable = root.join("unreadable");
    std::fs::create_dir(&unreadable).unwrap();
    let original_dir = std::fs::metadata(&unreadable).unwrap().permissions();
    let mut dir_perm = original_dir.clone();
    dir_perm.set_mode(0o000);
    std::fs::set_permissions(&unreadable, dir_perm).unwrap();

    let output = cmd().current_dir(&root).arg("unreadable").output().unwrap();

    std::fs::set_permissions(&unreadable, original_dir).unwrap();
    std::fs::remove_dir_all(&unreadable).ok();
    std::fs::remove_dir_all(&root).ok();

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert_eq!(output.status.code(), Some(2));
    assert!(String::from_utf8_lossy(&output.stderr).contains("could not read directory"));
    assert!(stdout.contains("Parse Errors (1)"), "stdout was:\n{stdout}");
    assert!(!String::from_utf8_lossy(&output.stdout).contains("No findings."));
}

#[test]
fn missing_input_exits_two() {
    let root = temp_dir("missing-input-project");
    let missing = root.join("missing.daml");
    let output = cmd().current_dir(&root).arg(&missing).output().unwrap();

    std::fs::remove_dir_all(&root).ok();

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert_eq!(output.status.code(), Some(2));
    assert!(String::from_utf8_lossy(&output.stderr).contains("does not exist"));
    assert!(stdout.contains("Parse Errors (1)"), "stdout was:\n{stdout}");
    assert!(!String::from_utf8_lossy(&output.stdout).contains("No findings."));
}

#[test]
fn findings_at_threshold_exit_one() {
    let path = temp_file(
        "finding.daml",
        r#"module Bad where

template Iou
  with
    issuer : Party
    amount : Decimal
  where
    signatory issuer
"#,
    );
    let output = cmd()
        .arg(&path)
        .arg("--fail-on")
        .arg("high")
        .output()
        .unwrap();
    std::fs::remove_file(&path).ok();

    assert_eq!(output.status.code(), Some(1));
    assert!(String::from_utf8_lossy(&output.stdout).contains("missing-ensure-decimal"));
}

#[test]
#[cfg(feature = "custom-rules")]
fn findings_at_high_threshold_exit_one_for_high_or_critical() {
    let path = temp_file(
        "finding-high.daml",
        r#"module Threshold where

template Threshold
  with
    owner : Party
  where
    signatory owner
    ensure owner != owner
"#,
    );
    let low_rule = temp_file(
        "low-rule.js",
        r#"
const NAME = "cli-low";
const SEVERITY = "low";

function on_template(template) {
  report(template, "low severity report");
}
"#,
    );
    let high_rule = temp_file(
        "high-rule.js",
        r#"
const NAME = "cli-high";
const SEVERITY = "high";

function on_template(template) {
  report(template, "high severity report");
}
"#,
    );

    let low_output = cmd()
        .arg(&path)
        .arg("--rules")
        .arg(&low_rule)
        .arg("--fail-on")
        .arg("high")
        .output()
        .unwrap();
    let high_output = cmd()
        .arg(&path)
        .arg("--rules")
        .arg(&high_rule)
        .arg("--fail-on")
        .arg("high")
        .output()
        .unwrap();
    let mixed_output = cmd()
        .arg(&path)
        .arg("--rules")
        .arg(&low_rule)
        .arg("--rules")
        .arg(&high_rule)
        .arg("--fail-on")
        .arg("info")
        .output()
        .unwrap();

    std::fs::remove_file(&path).ok();
    std::fs::remove_file(&low_rule).ok();
    std::fs::remove_file(&high_rule).ok();

    assert_eq!(low_output.status.code(), Some(0));
    assert!(String::from_utf8_lossy(&low_output.stdout).contains("low severity report"));
    assert_eq!(high_output.status.code(), Some(1));
    assert!(String::from_utf8_lossy(&high_output.stdout).contains("high severity report"));
    assert!(String::from_utf8_lossy(&high_output.stdout).contains("cli-high"));

    let mixed_stdout = String::from_utf8_lossy(&mixed_output.stdout);
    let high_pos = mixed_stdout.find("high severity report").unwrap();
    let low_pos = mixed_stdout.find("low severity report").unwrap();
    assert!(
        high_pos < low_pos,
        "higher severity finding should be reported first: {mixed_stdout}"
    );
}

#[test]
fn parse_error_exits_three() {
    let path = temp_file("malformed.daml", "module Bad where\nf = \"unterminated\n");
    let output = cmd().arg(&path).output().unwrap();
    std::fs::remove_file(&path).ok();

    assert_eq!(output.status.code(), Some(3));
    assert!(String::from_utf8_lossy(&output.stderr).contains("daml-lint: parse"));
}

#[test]
#[cfg(feature = "custom-rules")]
fn custom_rule_runtime_error_exits_two() {
    let path = clean_file();
    let rule = temp_file(
        "runtime-rule.js",
        r#"
const NAME = "runtime-boom";
const SEVERITY = "low";
function on_template(template) {
  template.does.not.exist;
}
"#,
    );
    let output = cmd().arg(&path).arg("--rules").arg(&rule).output().unwrap();
    std::fs::remove_file(&path).ok();
    std::fs::remove_file(&rule).ok();

    assert_eq!(output.status.code(), Some(2));
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("runtime-boom"), "stderr was:\n{stderr}");
}