aegis-scan 0.3.0

Supply chain security CLI for npm — detect malicious packages before installing
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
//! Detects packages that look like AI-hallucinated or typosquatted names.
//!
//! AI code assistants sometimes suggest package names that don't actually exist.
//! Attackers register these fake names with malicious payloads. This analyzer
//! flags packages exhibiting signals consistent with that attack pattern.

use chrono::Utc;

use crate::registry::package::PackageMetadata;
use crate::types::{AnalysisContext, Finding, FindingCategory, Severity};

use super::Analyzer;

/// Top popular npm packages used for Levenshtein distance comparison.
const TOP_PACKAGES: &[&str] = &[
    "react",
    "express",
    "lodash",
    "axios",
    "chalk",
    "commander",
    "debug",
    "dotenv",
    "fs-extra",
    "glob",
    "inquirer",
    "jest",
    "moment",
    "mongoose",
    "next",
    "nodemon",
    "prettier",
    "socket.io",
    "typescript",
    "uuid",
    "webpack",
    "yargs",
    "bluebird",
    "body-parser",
    "cheerio",
    "cors",
    "dayjs",
    "eslint",
    "fastify",
    "helmet",
    "jsonwebtoken",
    "knex",
    "luxon",
    "marked",
    "mysql2",
    "nanoid",
    "passport",
    "pg",
    "pino",
    "ramda",
    "redis",
    "rimraf",
    "rxjs",
    "semver",
    "sequelize",
    "sharp",
    "underscore",
    "validator",
    "vue",
    "zod",
];

/// Generic name fragments that AI models tend to hallucinate.
const HALLUCINATION_PATTERNS: &[&str] = &[
    "utils-helper",
    "data-processor",
    "string-formatter",
    "json-utils",
    "file-helper",
    "array-utils",
    "object-helper",
    "math-helper",
    "date-utils",
    "config-helper",
    "log-helper",
    "http-helper",
    "api-helper",
    "parse-helper",
    "format-helper",
    "convert-helper",
    "validate-helper",
    "transform-utils",
    "common-utils",
    "simple-utils",
];

/// Known legitimate packages that are close in edit distance to top packages
/// but are NOT typosquats (e.g., "reakt" is a real package).
const KNOWN_LEGITIMATE: &[&str] = &[
    "preact",
    "reakt",
    "rax",
    "inferno",
    "mithril",
    "chokidar",
    "chalk-cli",
    "expressjs",
    "koa",
    "hapi",
    "fastify",
    "restify",
    "micro",
    "polka",
    "morgan",
    "cors",
    "helmet",
    "pino-pretty",
];

/// Check if a package name is a known legitimate package (not a typosquat).
fn is_known_legitimate(name: &str) -> bool {
    KNOWN_LEGITIMATE.contains(&name)
}

/// Check if a package looks like a plugin/extension of a popular package
/// (e.g., "react-router", "express-session").
fn is_plugin_or_extension(name: &str, top: &str) -> bool {
    // "react-router" is a plugin of "react"
    if name.starts_with(&format!("{}-", top)) || name.starts_with(&format!("{}.", top)) {
        return true;
    }
    // "@scope/react" or "eslint-plugin-react"
    if name.contains(&format!("-{}", top)) && name.len() > top.len() + 5 {
        return true;
    }
    false
}

/// Check for homoglyph-based typosquatting (e.g., "1odash" for "lodash").
fn has_homoglyphs(name: &str, top: &str) -> bool {
    if name.len() != top.len() {
        return false;
    }
    let mut diff_count = 0;
    let mut has_glyph_swap = false;
    for (a, b) in name.chars().zip(top.chars()) {
        if a != b {
            diff_count += 1;
            if diff_count > 2 {
                return false;
            }
            // Check common homoglyph pairs
            let is_homoglyph = matches!(
                (a.to_ascii_lowercase(), b.to_ascii_lowercase()),
                ('0', 'o')
                    | ('o', '0')
                    | ('1', 'l')
                    | ('l', '1')
                    | ('1', 'i')
                    | ('i', '1')
                    | ('l', 'i')
                    | ('i', 'l')
                    | ('m', 'n')
                    | ('n', 'm')
                    | ('v', 'u')
                    | ('u', 'v')
                    | ('d', 'b')
                    | ('b', 'd')
                    | ('q', 'p')
                    | ('p', 'q')
            );
            if is_homoglyph {
                has_glyph_swap = true;
            }
        }
    }
    has_glyph_swap && diff_count <= 2
}

/// Detect specific typosquat technique used.
fn detect_typosquat_technique(name: &str, top: &str) -> Option<String> {
    if has_homoglyphs(name, top) {
        return Some(format!("homoglyph substitution (looks like '{}')", top));
    }

    // Hyphen manipulation: "ex-press" vs "express"
    let name_no_hyphen: String = name.chars().filter(|c| *c != '-').collect();
    let top_no_hyphen: String = top.chars().filter(|c| *c != '-').collect();
    if name_no_hyphen == top_no_hyphen && name != top {
        return Some(format!("hyphen manipulation of '{}'", top));
    }

    // Scope-squatting: "@evil/lodash" for "lodash"
    if name.contains('/') {
        let bare = name.rsplit('/').next().unwrap_or("");
        if bare == top {
            return Some(format!("scope-squatting of '{}'", top));
        }
    }

    None
}

/// Compute normalized Levenshtein distance (0.0 = identical, 1.0 = completely different).
fn normalized_levenshtein(a: &str, b: &str) -> f64 {
    let max_len = a.len().max(b.len());
    if max_len == 0 {
        return 0.0;
    }
    levenshtein(a, b) as f64 / max_len as f64
}

/// Compute the Levenshtein edit distance between two strings.
fn levenshtein(a: &str, b: &str) -> usize {
    let a_len = a.len();
    let b_len = b.len();
    if a_len == 0 {
        return b_len;
    }
    if b_len == 0 {
        return a_len;
    }

    let mut prev: Vec<usize> = (0..=b_len).collect();
    let mut curr = vec![0usize; b_len + 1];

    for (i, ca) in a.chars().enumerate() {
        curr[0] = i + 1;
        for (j, cb) in b.chars().enumerate() {
            let cost = if ca == cb { 0 } else { 1 };
            curr[j + 1] = (prev[j] + cost).min(prev[j + 1] + 1).min(curr[j] + 1);
        }
        std::mem::swap(&mut prev, &mut curr);
    }

    prev[b_len]
}

/// Try to parse an ISO-8601 timestamp from the npm `time` map and return the
/// number of days between that timestamp and now. Returns `None` if unparsable.
fn days_since(iso: &str) -> Option<i64> {
    let parsed = chrono::DateTime::parse_from_rfc3339(iso).ok()?;
    let duration = Utc::now().signed_duration_since(parsed);
    Some(duration.num_days())
}

/// Analyzer for AI-hallucinated / typosquatted package names.
pub struct HallucinationAnalyzer;

impl Default for HallucinationAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

impl HallucinationAnalyzer {
    pub fn new() -> Self {
        Self
    }

    /// Analyse a `PackageMetadata` and return findings.
    pub fn analyze_metadata(&self, metadata: &PackageMetadata) -> Vec<Finding> {
        let mut findings: Vec<Finding> = Vec::new();

        let pkg_name = metadata.name.as_deref().unwrap_or("");

        // ── Derive helper values ──────────────────────────────────────
        let version_count = metadata.versions.len();

        // Weekly downloads: npm includes this in the packument `extra` as a
        // nested value; fall back to 0 when absent.
        let weekly_downloads: u64 = metadata
            .extra
            .get("weeklyDownloads")
            .or_else(|| metadata.extra.get("weekly_downloads"))
            .and_then(|v| v.as_u64())
            .unwrap_or(0);

        // Days since the package was first created (from `time.created`).
        let days_since_creation: Option<i64> =
            metadata.time.get("created").and_then(|ts| days_since(ts));

        let latest_info = metadata.latest_version_info();

        let has_install_scripts = latest_info
            .map(|v| !v.install_scripts().is_empty())
            .unwrap_or(false);

        let description = metadata
            .description
            .as_deref()
            .or_else(|| latest_info.and_then(|v| v.description.as_deref()))
            .unwrap_or("");

        let has_repo = metadata
            .extra
            .get("repository")
            .map(|v| !v.is_null())
            .unwrap_or(false);

        let maintainer_has_email = metadata
            .maintainers
            .as_ref()
            .and_then(|ms| ms.first())
            .and_then(|m| m.email.as_ref())
            .map(|e| !e.is_empty())
            .unwrap_or(false);

        let latest_version_str = metadata.latest_version().unwrap_or("");

        // ── CRITICAL: new + low downloads + install scripts ───────────
        if let Some(age) = days_since_creation {
            if age <= 30 && weekly_downloads == 0 && has_install_scripts {
                findings.push(Finding {
                    severity: Severity::Critical,
                    category: FindingCategory::HallucinatedPackage,
                    title: "Likely malicious hallucinated package".into(),
                    description: format!(
                        "Package '{}' was created {} days ago, has 0 weekly downloads, \
                         and contains install scripts. This is a strong signal of a \
                         malicious package registered to exploit AI hallucinations.",
                        pkg_name, age
                    ),
                    file: None,
                    line: None,
                    snippet: None,
                });
            }
        }

        // ── HIGH: very new + very low downloads ───────────────────────
        if let Some(age) = days_since_creation {
            if age < 7 && weekly_downloads < 100 {
                findings.push(Finding {
                    severity: Severity::High,
                    category: FindingCategory::HallucinatedPackage,
                    title: "Very new package with almost no downloads".into(),
                    description: format!(
                        "Package '{}' was created only {} days ago and has {} weekly \
                         downloads. Newly created packages with negligible adoption \
                         are a hallucination/typosquat risk.",
                        pkg_name, age, weekly_downloads
                    ),
                    file: None,
                    line: None,
                    snippet: None,
                });
            }
        }

        // ── HIGH: name suspiciously close to a top package ────────────
        if !is_known_legitimate(pkg_name) {
            for &top in TOP_PACKAGES {
                if pkg_name == top {
                    continue; // exact match means it IS the real package
                }

                // Skip if this looks like a plugin/extension (e.g., "react-router")
                if is_plugin_or_extension(pkg_name, top) {
                    continue;
                }

                // Check for homoglyph-based typosquatting first
                if let Some(technique) = detect_typosquat_technique(pkg_name, top) {
                    findings.push(Finding {
                        severity: Severity::Critical,
                        category: FindingCategory::HallucinatedPackage,
                        title: format!("Typosquat of '{}' detected", top),
                        description: format!(
                            "Package '{}' appears to be a typosquat of the popular \
                             package '{}' via {}.",
                            pkg_name, top, technique
                        ),
                        file: None,
                        line: None,
                        snippet: None,
                    });
                    break;
                }

                // Use normalized Levenshtein to reduce false positives for short names
                let norm_dist = normalized_levenshtein(pkg_name, top);
                let dist = levenshtein(pkg_name, top);
                if dist > 0 && dist <= 2 && norm_dist < 0.4 {
                    findings.push(Finding {
                        severity: Severity::High,
                        category: FindingCategory::HallucinatedPackage,
                        title: format!("Name suspiciously similar to '{}'", top),
                        description: format!(
                            "Package '{}' is only {} edit(s) away from the popular \
                             package '{}' (normalized distance: {:.2}). \
                             This may be a typosquat or hallucinated variant.",
                            pkg_name, dist, top, norm_dist
                        ),
                        file: None,
                        line: None,
                        snippet: None,
                    });
                    break; // one match is enough
                }
            }
        }

        // ── MEDIUM: no/short description ──────────────────────────────
        if description.is_empty() || description.len() < 10 {
            findings.push(Finding {
                severity: Severity::Medium,
                category: FindingCategory::HallucinatedPackage,
                title: "Missing or very short description".into(),
                description: format!(
                    "Package '{}' has no README or a description shorter than 10 \
                     characters, which is unusual for legitimate packages.",
                    pkg_name
                ),
                file: None,
                line: None,
                snippet: None,
            });
        }

        // ── MEDIUM: no repository URL ─────────────────────────────────
        if !has_repo {
            findings.push(Finding {
                severity: Severity::Medium,
                category: FindingCategory::HallucinatedPackage,
                title: "No repository URL".into(),
                description: format!(
                    "Package '{}' does not declare a source repository, making it \
                     harder to verify its legitimacy.",
                    pkg_name
                ),
                file: None,
                line: None,
                snippet: None,
            });
        }

        // ── MEDIUM: suspicious first version ──────────────────────────
        if version_count == 1 && (latest_version_str == "0.0.1" || latest_version_str == "1.0.0") {
            findings.push(Finding {
                severity: Severity::Medium,
                category: FindingCategory::HallucinatedPackage,
                title: "Single version published at 0.0.1 or 1.0.0".into(),
                description: format!(
                    "Package '{}' has exactly one published version ({}). \
                     Combined with other signals this can indicate a placeholder \
                     or malicious registration.",
                    pkg_name, latest_version_str
                ),
                file: None,
                line: None,
                snippet: None,
            });
        }

        // ── MEDIUM: maintainer without email ──────────────────────────
        if !maintainer_has_email {
            findings.push(Finding {
                severity: Severity::Medium,
                category: FindingCategory::HallucinatedPackage,
                title: "Maintainer has no published email".into(),
                description: format!(
                    "The primary maintainer of '{}' has no public email address, \
                     which can be a signal of a throwaway account.",
                    pkg_name
                ),
                file: None,
                line: None,
                snippet: None,
            });
        }

        // ── LOW: generic AI hallucination name patterns ───────────────
        let lower = pkg_name.to_lowercase();
        for &pattern in HALLUCINATION_PATTERNS {
            if lower.contains(pattern) {
                findings.push(Finding {
                    severity: Severity::Low,
                    category: FindingCategory::HallucinatedPackage,
                    title: "Name matches common AI hallucination pattern".into(),
                    description: format!(
                        "Package name '{}' contains the generic fragment '{}', \
                         which AI assistants frequently hallucinate as a package \
                         name.",
                        pkg_name, pattern
                    ),
                    file: None,
                    line: None,
                    snippet: None,
                });
                break; // one match is enough
            }
        }

        findings
    }
}

impl Analyzer for HallucinationAnalyzer {
    fn name(&self) -> &str {
        "hallucination"
    }

    fn analyze(&self, ctx: &AnalysisContext) -> Vec<Finding> {
        self.analyze_metadata(ctx.metadata)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_levenshtein_identical() {
        assert_eq!(levenshtein("react", "react"), 0);
    }

    #[test]
    fn test_levenshtein_one_edit() {
        assert_eq!(levenshtein("react", "reakt"), 1);
    }

    #[test]
    fn test_levenshtein_two_edits() {
        assert_eq!(levenshtein("lodash", "lodasg"), 1);
        assert_eq!(levenshtein("axos", "axios"), 1);
    }

    #[test]
    fn test_levenshtein_different() {
        assert!(levenshtein("react", "totally-different") > 2);
    }
}