nyx-scanner 0.5.0

A multi-language static analysis tool for detecting security vulnerabilities
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
pub mod checks;
pub mod config;
pub mod extract;
pub mod model;
pub mod sql_semantics;

use crate::commands::scan::Diag;
use crate::evidence::{Confidence, Evidence, SpanEvidence};
use crate::patterns::FindingCategory;
use crate::ssa::type_facts::TypeKind;
use crate::summary::GlobalSummaries;
use crate::symbol::{FuncKey, Lang, normalize_namespace};
use crate::utils::Config;
use std::collections::HashMap;
use std::path::Path;
use tree_sitter::Tree;

fn byte_offset_to_point(tree: &Tree, byte: usize) -> tree_sitter::Point {
    tree.root_node()
        .descendant_for_byte_range(byte, byte)
        .map(|node| node.start_position())
        .unwrap_or(tree_sitter::Point { row: 0, column: 0 })
}

/// Per-file snapshot of SSA-derived variable types, keyed by
/// source-level variable name.  Built at `run_auth_analysis` call sites
/// by merging type facts across all bodies in the file; a variable name
/// with conflicting types in different bodies is dropped (absence is
/// safe — the sink gate just falls back to name-based classification).
pub type VarTypes = HashMap<String, TypeKind>;

#[allow(clippy::too_many_arguments)]
pub fn run_auth_analysis(
    tree: &Tree,
    source: &[u8],
    lang: &str,
    file_path: &Path,
    cfg: &Config,
    var_types: Option<&VarTypes>,
    global_summaries: Option<&GlobalSummaries>,
    scan_root: Option<&Path>,
) -> Vec<Diag> {
    let rules = config::build_auth_rules(cfg, lang);
    if !rules.enabled {
        return Vec::new();
    }

    let mut model = extract::extract_authorization_model(
        lang,
        cfg.framework_ctx.as_ref(),
        tree,
        source,
        file_path,
        &rules,
    );

    // Refine `SensitiveOperation::sink_class` using SSA-derived
    // variable types.  Runs only when the caller supplied `var_types`
    // (skipped for slug-lookup / unit-test call sites).
    if let Some(types) = var_types {
        apply_var_types_to_model(&mut model, &rules, types);
        apply_typed_bounded_params(&mut model, types);
    }

    // Lift per-function auth-check summaries and synthesise call-site
    // `AuthCheck`s in callers, so a handler that delegates to a helper
    // which internally validates ownership is recognised as
    // auth-checked.  Iterated to a small fixpoint so transitive helper
    // chains are also covered; consults `global_summaries.auth_by_key`
    // (when provided) for cross-file helpers that live in other files.
    apply_helper_lifting(&mut model, lang, file_path, scan_root, global_summaries);

    if model.routes.is_empty() && model.units.is_empty() {
        return Vec::new();
    }

    checks::run_checks(&model, &rules)
        .into_iter()
        .map(|finding| auth_finding_to_diag(&finding, tree, file_path))
        .collect()
}

/// Build per-function [`model::AuthCheckSummary`] entries for every
/// unit in `model`, keyed by a canonical [`FuncKey`] derived from the
/// enclosing file's path and the unit's leaf name + arity.
///
/// Used by pass 1 to persist per-file auth summaries for cross-file
/// helper lifting.  Only returns summaries for units whose body
/// already proves at least one positional parameter under ownership /
/// membership / admin / authorization check — i.e. the exact
/// single-file lift set, so the cross-file variant does not widen what
/// counts as a helper.
pub fn extract_auth_summaries_by_key(
    tree: &Tree,
    source: &[u8],
    lang: &str,
    file_path: &Path,
    cfg: &Config,
    scan_root: Option<&Path>,
) -> Vec<(FuncKey, model::AuthCheckSummary)> {
    let rules = config::build_auth_rules(cfg, lang);
    if !rules.enabled {
        return Vec::new();
    }
    let model = extract::extract_authorization_model(
        lang,
        cfg.framework_ctx.as_ref(),
        tree,
        source,
        file_path,
        &rules,
    );
    summaries_keyed_by_func(&model, lang, file_path, scan_root)
}

/// Convert an already-built [`model::AuthorizationModel`] into a
/// canonical `(FuncKey, AuthCheckSummary)` list suitable for
/// persistence.  Shares the per-unit summary-building logic with
/// [`build_helper_summaries`] so single-file and cross-file lifts
/// accept the exact same set of helpers.
fn summaries_keyed_by_func(
    model: &model::AuthorizationModel,
    lang: &str,
    file_path: &Path,
    scan_root: Option<&Path>,
) -> Vec<(FuncKey, model::AuthCheckSummary)> {
    let Some(lang_enum) = Lang::from_slug(lang) else {
        return Vec::new();
    };
    let path_str = file_path.to_string_lossy();
    let root_str = scan_root.map(|r| r.to_string_lossy().into_owned());
    let namespace = normalize_namespace(&path_str, root_str.as_deref());

    let mut out = Vec::new();
    for unit in &model.units {
        let Some(name) = unit.name.as_deref() else {
            continue;
        };
        if name.is_empty() {
            continue;
        }
        let Some(summary) = build_unit_summary(unit) else {
            continue;
        };
        let leaf = name.rsplit('.').next().unwrap_or(name).to_string();
        let key = FuncKey {
            lang: lang_enum,
            namespace: namespace.clone(),
            container: String::new(),
            name: leaf,
            arity: Some(unit.params.len()),
            disambig: None,
            kind: crate::symbol::FuncKind::Function,
        };
        out.push((key, summary));
    }
    out
}

/// Build an [`model::AuthCheckSummary`] for a single
/// [`model::AnalysisUnit`].  Returns `None` when the unit produces no
/// usable param → auth-kind mapping, so callers can cheaply skip
/// persisting empty entries.
fn build_unit_summary(unit: &model::AnalysisUnit) -> Option<model::AuthCheckSummary> {
    use model::{AuthCheckKind, AuthCheckSummary};
    if unit.params.is_empty() {
        return None;
    }
    let mut summary = AuthCheckSummary::default();
    for check in &unit.auth_checks {
        if matches!(
            check.kind,
            AuthCheckKind::LoginGuard | AuthCheckKind::TokenExpiry | AuthCheckKind::TokenRecipient
        ) {
            continue;
        }
        for subject in &check.subjects {
            let Some(candidate) = subject_lift_key(subject) else {
                continue;
            };
            if let Some(idx) = unit.params.iter().position(|p| p == &candidate) {
                summary
                    .param_auth_kinds
                    .entry(idx)
                    .and_modify(|existing| {
                        *existing = stronger_check_kind(*existing, check.kind);
                    })
                    .or_insert(check.kind);
            }
        }
    }
    if summary.param_auth_kinds.is_empty() {
        None
    } else {
        Some(summary)
    }
}

/// Walk every `SensitiveOperation` in the model and, when the call's
/// receiver root variable has a known SSA type, override `sink_class`
/// to the type-implied class.  Strictly additive — only overrides
/// when the type map produces a definite class, otherwise leaves the
/// name/prefix-derived classification intact.
fn apply_var_types_to_model(
    model: &mut model::AuthorizationModel,
    rules: &config::AuthAnalysisRules,
    var_types: &VarTypes,
) {
    for unit in &mut model.units {
        for op in &mut unit.operations {
            let Some(first) = receiver_root(&op.callee) else {
                continue;
            };
            let Some(ty) = var_types.get(first) else {
                continue;
            };
            if let Some(new_class) = sink_class_for_type(ty, &op.callee, rules) {
                op.sink_class = Some(new_class);
            }
        }
    }
}

/// Populate each [`model::AnalysisUnit::typed_bounded_vars`] with the
/// names of formal parameters whose SSA-inferred [`TypeKind`] is a
/// payload-incompatible scalar ([`TypeKind::Int`] or
/// [`TypeKind::Bool`]).  Only parameter-rooted entries are considered;
/// function-local bindings stay outside this set so a downstream
/// reassignment from user input (`let id = req.params.id`) never gets
/// suppressed by accident.
///
/// Phase 6: when a parameter's type is a [`TypeKind::Dto`], lift each
/// of its `Int`/`Bool` fields as `typed_bounded_dto_fields[<param>]`
/// so member-access subjects like `dto.age` are recognised as
/// payload-incompatible.  Only fires when the base param itself was
/// recognised as a typed extractor by a Phase 1-2 matcher — bare
/// parameters with no framework gate never lift their fields.
fn apply_typed_bounded_params(model: &mut model::AuthorizationModel, var_types: &VarTypes) {
    for unit in &mut model.units {
        for name in &unit.params {
            let Some(ty) = var_types.get(name) else {
                continue;
            };
            match ty {
                TypeKind::Int | TypeKind::Bool => {
                    unit.typed_bounded_vars.insert(name.clone());
                }
                TypeKind::Dto(dto) => {
                    let mut bounded = Vec::new();
                    for (field_name, field_kind) in &dto.fields {
                        if matches!(field_kind, TypeKind::Int | TypeKind::Bool) {
                            bounded.push(field_name.clone());
                        }
                    }
                    if !bounded.is_empty() {
                        unit.typed_bounded_dto_fields.insert(name.clone(), bounded);
                    }
                }
                _ => {}
            }
        }
    }
}

/// First segment of a callee's receiver chain (`map.insert` → `"map"`,
/// `self.cache.set` → `"self"`).  Returns `None` when the callee has no
/// receiver (e.g. a free function call).
fn receiver_root(callee: &str) -> Option<&str> {
    let (first, rest) = callee.split_once('.')?;
    if rest.is_empty() {
        return None;
    }
    if first.is_empty() { None } else { Some(first) }
}

/// Map an inferred [`TypeKind`] to the [`model::SinkClass`] that should
/// supersede the callee-name classification.  The DB case disambiguates
/// read vs mutation using the callee's verb; non-security types return
/// `None` so the caller leaves the existing class in place.
fn sink_class_for_type(
    ty: &TypeKind,
    callee: &str,
    rules: &config::AuthAnalysisRules,
) -> Option<model::SinkClass> {
    match ty {
        TypeKind::LocalCollection => Some(model::SinkClass::InMemoryLocal),
        TypeKind::HttpClient => Some(model::SinkClass::OutboundNetwork),
        TypeKind::DatabaseConnection => {
            if rules.is_read(callee) && !rules.is_mutation(callee) {
                Some(model::SinkClass::DbCrossTenantRead)
            } else {
                Some(model::SinkClass::DbMutation)
            }
        }
        _ => None,
    }
}

/// Build per-function `AuthCheckSummary` and synthesise `AuthCheck`s
/// at every call site that targets a known helper whose summary names
/// auth-checked params.  Iterated to a small fixpoint
/// so transitive helper chains (`handler → validate → require_member`)
/// are also covered.
///
/// The synthesised AuthCheck inherits the helper-param's check kind
/// and is anchored at the call site's line, with subjects = the
/// caller's value-refs from the corresponding positional argument.
/// `auth_check_covers_subject` then matches them against downstream
/// sensitive operations exactly like a real prior auth check.
///
/// When `global_summaries` is `Some`, cross-file helpers are looked up
/// via [`GlobalSummaries::get_auth`] after the same-file summary
/// gather — this recovers the handler-in-file-A calling
/// `require_owner`-in-file-B case that single-file lifting cannot see.
fn apply_helper_lifting(
    model: &mut model::AuthorizationModel,
    lang: &str,
    file_path: &Path,
    scan_root: Option<&Path>,
    global_summaries: Option<&GlobalSummaries>,
) {
    use std::collections::HashSet;

    let caller_lang = Lang::from_slug(lang);
    let path_str = file_path.to_string_lossy();
    let root_str = scan_root.map(|r| r.to_string_lossy().into_owned());
    let caller_namespace = normalize_namespace(&path_str, root_str.as_deref());

    const MAX_ROUNDS: usize = 4;
    for _ in 0..MAX_ROUNDS {
        let summaries = build_helper_summaries(model);
        let have_same_file = !summaries.is_empty();
        let have_cross_file =
            global_summaries.is_some_and(|gs| gs.auth_by_key().is_some()) && caller_lang.is_some();
        if !have_same_file && !have_cross_file {
            return;
        }
        let mut added = false;
        // For each unit, compute synthetic checks BEFORE mutating, so
        // a helper-call inside one unit doesn't see synthetic checks
        // we add to a sibling in the same round (those land in the
        // next iteration via the rebuilt summaries).
        let synth: Vec<(usize, Vec<model::AuthCheck>)> = model
            .units
            .iter()
            .enumerate()
            .map(|(idx, unit)| {
                let mut out = synthesise_checks_for_unit(unit, &summaries);
                if have_cross_file
                    && let (Some(gs), Some(lang_enum)) = (global_summaries, caller_lang)
                {
                    out.extend(synthesise_cross_file_checks_for_unit(
                        unit,
                        &summaries,
                        gs,
                        lang_enum,
                        &caller_namespace,
                    ));
                }
                (idx, out)
            })
            .collect();
        let mut existing_keys_per_unit: Vec<HashSet<((usize, usize), model::AuthCheckKind)>> =
            model
                .units
                .iter()
                .map(|u| {
                    u.auth_checks
                        .iter()
                        .map(|c| (c.span, c.kind))
                        .collect::<HashSet<_>>()
                })
                .collect();
        for (idx, checks) in synth {
            for check in checks {
                let key = (check.span, check.kind);
                if existing_keys_per_unit[idx].insert(key) {
                    model.units[idx].auth_checks.push(check);
                    added = true;
                }
            }
        }
        if !added {
            return;
        }
    }
}

/// Build a `name → AuthCheckSummary` map by walking each unit's auth
/// checks and recording, for every check subject whose value-ref name
/// matches a positional parameter name of the unit, that param index
/// → check kind.  Same key with different kinds collapses to the most
/// specific (Ownership/Membership wins over Other).
fn build_helper_summaries(
    model: &model::AuthorizationModel,
) -> std::collections::HashMap<String, model::AuthCheckSummary> {
    use model::{AuthCheckKind, AuthCheckSummary};
    use std::collections::HashMap;

    let mut summaries: HashMap<String, AuthCheckSummary> = HashMap::new();
    for unit in &model.units {
        let Some(name) = unit.name.as_deref() else {
            continue;
        };
        if name.is_empty() || unit.params.is_empty() {
            continue;
        }
        let mut summary = AuthCheckSummary::default();
        for check in &unit.auth_checks {
            // We only lift checks that actively prove ownership /
            // membership / admin-rights / authorize-helper — login
            // and token-validity checks don't justify foreign-id
            // mutations and we want to keep parity with
            // `has_prior_subject_auth`'s filter.
            if matches!(
                check.kind,
                AuthCheckKind::LoginGuard
                    | AuthCheckKind::TokenExpiry
                    | AuthCheckKind::TokenRecipient
            ) {
                continue;
            }
            for subject in &check.subjects {
                let candidate = subject_lift_key(subject);
                let Some(candidate) = candidate else { continue };
                if let Some(idx) = unit.params.iter().position(|p| p == &candidate) {
                    summary
                        .param_auth_kinds
                        .entry(idx)
                        .and_modify(|existing| {
                            *existing = stronger_check_kind(*existing, check.kind);
                        })
                        .or_insert(check.kind);
                }
            }
        }
        if !summary.param_auth_kinds.is_empty() {
            // Deduplicate by last segment of the function name — the
            // lifting site matches the call's last segment too.
            let last = name.rsplit('.').next().unwrap_or(name).to_string();
            summaries
                .entry(last)
                .or_default()
                .param_auth_kinds
                .extend(summary.param_auth_kinds);
        }
    }
    summaries
}

/// Pick the identifier name for a check subject for purposes of
/// matching to the enclosing function's parameters.  We prefer the
/// `base` segment of a member-chain subject (`row.user_id` → `row`)
/// because helpers usually receive the full struct, not the field;
/// fall back to the raw `name` for plain identifiers.
fn subject_lift_key(subject: &model::ValueRef) -> Option<String> {
    if let Some(base) = subject.base.as_deref() {
        let first = base.split('.').next().unwrap_or(base).trim();
        if !first.is_empty() {
            return Some(first.to_string());
        }
    }
    if subject.name.is_empty() {
        None
    } else {
        Some(
            subject
                .name
                .split('.')
                .next()
                .unwrap_or(&subject.name)
                .to_string(),
        )
    }
}

fn stronger_check_kind(a: model::AuthCheckKind, b: model::AuthCheckKind) -> model::AuthCheckKind {
    use model::AuthCheckKind::*;
    fn rank(k: model::AuthCheckKind) -> u8 {
        match k {
            Ownership => 5,
            Membership => 4,
            AdminGuard => 3,
            Other => 2,
            LoginGuard => 1,
            TokenExpiry | TokenRecipient => 0,
        }
    }
    if rank(a) >= rank(b) { a } else { b }
}

/// For one unit, synthesise an `AuthCheck` at every call site that
/// targets a helper with a non-trivial summary.  Subjects are taken
/// from `call_site.args_value_refs[K]` for each auth-checked param
/// position K — these are the caller's concrete subjects passed at
/// that arg slot, exactly what `auth_check_covers_subject` needs.
fn synthesise_checks_for_unit(
    unit: &model::AnalysisUnit,
    summaries: &std::collections::HashMap<String, model::AuthCheckSummary>,
) -> Vec<model::AuthCheck> {
    let line_of = |span: (usize, usize)| -> usize {
        // Span is byte offsets; we don't have direct access to a Tree
        // here. Caller assigns line via `line` field on call_site
        // through CallSite metadata absence — fall back to the unit's
        // line since covers_subject uses `check.line <= op.line` and
        // helper calls are typically near the unit start.
        let _ = span;
        unit.line
    };

    let mut out = Vec::new();
    for call in &unit.call_sites {
        let last = call.name.rsplit('.').next().unwrap_or(&call.name);
        let Some(summary) = summaries.get(last) else {
            continue;
        };
        // A call to the unit itself shouldn't lift anything (would
        // produce a tautological self-cover).
        if unit.name.as_deref() == Some(last) {
            continue;
        }
        // Build subjects from the auth-checked param positions.
        let mut subjects: Vec<model::ValueRef> = Vec::new();
        let mut effective_kind = model::AuthCheckKind::Other;
        for (param_idx, kind) in &summary.param_auth_kinds {
            let Some(arg_refs) = call.args_value_refs.get(*param_idx) else {
                continue;
            };
            subjects.extend(arg_refs.iter().cloned());
            effective_kind = stronger_check_kind(effective_kind, *kind);
        }
        if subjects.is_empty() {
            continue;
        }
        let line = call_site_line(unit, call).unwrap_or_else(|| line_of(call.span));
        out.push(model::AuthCheck {
            kind: effective_kind,
            callee: format!("(lifted {})", call.name),
            subjects,
            span: call.span,
            line,
            args: call.args.clone(),
            condition_text: None,
        });
    }
    out
}

/// Approximate the call site's line.  We don't have tree access here,
/// so we walk the unit's existing operations / call_sites to find one
/// whose span starts at the same byte offset and reuse its line; if
/// nothing matches we conservatively report the unit's start line so
/// the synthetic check still satisfies `check.line <= op.line` for
/// operations declared after it.  In practice, helper calls always
/// resolve via the operations match because handlers register their
/// own call_site too.
fn call_site_line(unit: &model::AnalysisUnit, call: &model::CallSite) -> Option<usize> {
    for op in &unit.operations {
        if op.span.0 == call.span.0 {
            return Some(op.line);
        }
    }
    None
}

/// Cross-file variant of [`synthesise_checks_for_unit`] — for each
/// call site in `unit`, resolve the callee against `GlobalSummaries`
/// and look up an `AuthCheckSummary` that was persisted by some other
/// file's pass-1 extraction.  Skips call sites already handled by the
/// single-file map (`same_file_summaries`) so we do not double-lift
/// the same call.
///
/// The synthesised check carries the same shape as the single-file
/// version: subjects come from `call.args_value_refs[K]` at each
/// auth-checked param position K, effective kind is the strongest
/// check kind across those positions, and the `(lifted cross-file
/// <name>)` callee string distinguishes cross-file lifts in
/// diagnostics.
fn synthesise_cross_file_checks_for_unit(
    unit: &model::AnalysisUnit,
    same_file_summaries: &std::collections::HashMap<String, model::AuthCheckSummary>,
    gs: &GlobalSummaries,
    caller_lang: Lang,
    caller_namespace: &str,
) -> Vec<model::AuthCheck> {
    let mut out = Vec::new();
    for call in &unit.call_sites {
        let last = call.name.rsplit('.').next().unwrap_or(&call.name);
        if unit.name.as_deref() == Some(last) {
            continue;
        }
        // Skip if the single-file map already handled this callee —
        // that path has richer same-file context (existing
        // summaries from sibling units in this model) and its
        // synthesised check is strictly more precise.
        if same_file_summaries.contains_key(last) {
            continue;
        }

        let arity_hint = Some(call.args.len());
        let key = match gs.resolve_callee_key(last, caller_lang, caller_namespace, arity_hint) {
            crate::summary::CalleeResolution::Resolved(key) => key,
            _ => continue,
        };
        // Auth summaries are persisted with a canonical key:
        // `disambig=None`, `container=""`, `kind=Function`.  Normalise
        // the resolver's key to that canonical shape before looking up
        // so a byte-offset or DFS-index `disambig` on the resolved key
        // doesn't cause a trivial miss.
        let mut canonical = key.clone();
        canonical.disambig = None;
        canonical.container = String::new();
        canonical.kind = crate::symbol::FuncKind::Function;
        let Some(summary) = gs.get_auth(&canonical) else {
            continue;
        };

        let mut subjects: Vec<model::ValueRef> = Vec::new();
        let mut effective_kind = model::AuthCheckKind::Other;
        for (param_idx, kind) in &summary.param_auth_kinds {
            let Some(arg_refs) = call.args_value_refs.get(*param_idx) else {
                continue;
            };
            subjects.extend(arg_refs.iter().cloned());
            effective_kind = stronger_check_kind(effective_kind, *kind);
        }
        if subjects.is_empty() {
            continue;
        }
        let line = call_site_line(unit, call).unwrap_or(unit.line);
        out.push(model::AuthCheck {
            kind: effective_kind,
            callee: format!("(lifted cross-file {})", call.name),
            subjects,
            span: call.span,
            line,
            args: call.args.clone(),
            condition_text: None,
        });
    }
    out
}

fn auth_finding_to_diag(finding: &checks::AuthFinding, tree: &Tree, file_path: &Path) -> Diag {
    let point = byte_offset_to_point(tree, finding.span.0);
    Diag {
        path: file_path.to_string_lossy().into_owned(),
        line: point.row + 1,
        col: point.column + 1,
        severity: finding.severity,
        id: finding.rule_id.clone(),
        category: FindingCategory::Security,
        path_validated: false,
        guard_kind: None,
        message: Some(finding.message.clone()),
        labels: vec![],
        confidence: Some(Confidence::Medium),
        evidence: Some(Evidence {
            source: None,
            sink: Some(SpanEvidence {
                path: file_path.to_string_lossy().into_owned(),
                line: (point.row + 1) as u32,
                col: (point.column + 1) as u32,
                kind: "sink".into(),
                snippet: None,
            }),
            guards: vec![],
            sanitizers: vec![],
            state: None,
            notes: vec![],
            ..Default::default()
        }),
        rank_score: None,
        rank_reason: None,
        suppressed: false,
        suppression: None,
        rollup: None,
        finding_id: String::new(),
        alternative_finding_ids: Vec::new(),
    }
}

#[cfg(test)]
mod tests {
    use super::{VarTypes, apply_var_types_to_model, receiver_root, sink_class_for_type};
    use crate::auth_analysis::config::build_auth_rules;
    use crate::auth_analysis::model::{
        AnalysisUnit, AnalysisUnitKind, AuthorizationModel, OperationKind, SensitiveOperation,
        SinkClass,
    };
    use crate::ssa::type_facts::TypeKind;
    use crate::utils::config::Config;
    use std::collections::{HashMap, HashSet};

    fn sample_op(callee: &str, initial: Option<SinkClass>) -> SensitiveOperation {
        SensitiveOperation {
            kind: OperationKind::Mutation,
            sink_class: initial,
            callee: callee.to_string(),
            subjects: Vec::new(),
            span: (0, 0),
            line: 1,
            text: callee.to_string(),
        }
    }

    fn sample_unit(op: SensitiveOperation) -> AnalysisUnit {
        AnalysisUnit {
            kind: AnalysisUnitKind::Function,
            name: Some("handle".into()),
            span: (0, 0),
            params: Vec::new(),
            context_inputs: Vec::new(),
            call_sites: Vec::new(),
            auth_checks: Vec::new(),
            operations: vec![op],
            value_refs: Vec::new(),
            condition_texts: Vec::new(),
            line: 1,
            row_field_vars: HashMap::new(),
            var_alias_chain: HashMap::new(),
            row_population_data: HashMap::new(),
            self_actor_vars: HashSet::new(),
            self_actor_id_vars: HashSet::new(),
            authorized_sql_vars: HashSet::new(),
            const_bound_vars: HashSet::new(),
            typed_bounded_vars: HashSet::new(),
            typed_bounded_dto_fields: HashMap::new(),
            self_scoped_session_bases: HashSet::new(),
        }
    }

    #[test]
    fn receiver_root_returns_first_segment_only_for_chain_calls() {
        assert_eq!(receiver_root("map.insert"), Some("map"));
        assert_eq!(receiver_root("self.cache.insert"), Some("self"));
        // Free function call (no receiver) → None.
        assert_eq!(receiver_root("HashMap"), None);
        assert_eq!(receiver_root("free_fn"), None);
        // Empty chain segments → None.
        assert_eq!(receiver_root("."), None);
        assert_eq!(receiver_root(""), None);
    }

    #[test]
    fn sink_class_for_type_maps_security_typekinds() {
        let cfg = Config::default();
        let rules = build_auth_rules(&cfg, "rust");
        // LocalCollection always → InMemoryLocal.
        assert_eq!(
            sink_class_for_type(&TypeKind::LocalCollection, "whatever.insert", &rules),
            Some(SinkClass::InMemoryLocal)
        );
        // HttpClient → OutboundNetwork.
        assert_eq!(
            sink_class_for_type(&TypeKind::HttpClient, "client.send", &rules),
            Some(SinkClass::OutboundNetwork)
        );
        // DatabaseConnection: mutation verb → DbMutation.
        assert_eq!(
            sink_class_for_type(&TypeKind::DatabaseConnection, "conn.insert", &rules),
            Some(SinkClass::DbMutation)
        );
        // DatabaseConnection: read-only verb → DbCrossTenantRead.
        assert_eq!(
            sink_class_for_type(&TypeKind::DatabaseConnection, "conn.get", &rules),
            Some(SinkClass::DbCrossTenantRead)
        );
        // DatabaseConnection: unrecognized verb (`execute`) → DbMutation
        // (conservative default — treat as write-shaped).
        assert_eq!(
            sink_class_for_type(&TypeKind::DatabaseConnection, "conn.execute", &rules),
            Some(SinkClass::DbMutation)
        );
        // Non-security types → None (don't override).
        assert_eq!(
            sink_class_for_type(&TypeKind::String, "s.len", &rules),
            None
        );
        assert_eq!(
            sink_class_for_type(&TypeKind::Unknown, "x.frobnicate", &rules),
            None
        );
    }

    #[test]
    fn apply_var_types_overrides_sink_class_for_known_receiver() {
        let cfg = Config::default();
        let rules = build_auth_rules(&cfg, "rust");
        let mut model = AuthorizationModel::default();
        // Initial sink class from B1 name-based classification (e.g.
        // `results.insert` → DbMutation because `insert` matches the
        // mutation list and `results` doesn't match any non-sink prefix).
        model.units.push(sample_unit(sample_op(
            "results.insert",
            Some(SinkClass::DbMutation),
        )));

        let mut var_types: VarTypes = HashMap::new();
        var_types.insert("results".into(), TypeKind::LocalCollection);

        apply_var_types_to_model(&mut model, &rules, &var_types);

        // B2 overrode to InMemoryLocal based on the SSA type.
        assert_eq!(
            model.units[0].operations[0].sink_class,
            Some(SinkClass::InMemoryLocal)
        );
    }

    #[test]
    fn apply_var_types_leaves_classification_untouched_when_receiver_unknown() {
        let cfg = Config::default();
        let rules = build_auth_rules(&cfg, "rust");
        let mut model = AuthorizationModel::default();
        model.units.push(sample_unit(sample_op(
            "db.insert",
            Some(SinkClass::DbMutation),
        )));
        let var_types: VarTypes = HashMap::new();
        apply_var_types_to_model(&mut model, &rules, &var_types);
        // Unchanged — no entry in var_types for `db`.
        assert_eq!(
            model.units[0].operations[0].sink_class,
            Some(SinkClass::DbMutation)
        );
    }
}