eggsec-nse 0.3.0

Standalone Lua-based Nmap Scripting Engine runtime
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
//! Declarative registry for NSE library modules.
//!
//! Each entry describes a standard Nmap Lua library module, its sandbox
//! side effects, optional dependencies, fallback behavior when missing,
//! and known compatibility notes. The registry is machine-readable and
//! used for policy evaluation, diagnostics, and compatibility reporting.
//!
//! The 43 entries cover Nmap's standard Lua library set (24 main + 19
//! auxiliary). Protocol-specific Rust implementations in `src/libraries/`
//! that are not part of the standard Nmap Lua API are not registered here.
//!
//! This module compiles with the `nse` feature **off** — it contains no
//! Lua or mlua dependencies.

use std::fmt;

// ---------------------------------------------------------------------------
// Enums
// ---------------------------------------------------------------------------

/// Categories for NSE library modules.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NseLibraryCategory {
    /// Core runtime libraries required by most scripts.
    Core,
    /// Protocol-specific libraries (HTTP, DNS, SMB, etc.).
    Protocol,
    /// General-purpose utility libraries (encoding, string, math).
    Utility,
    /// Exploit and vulnerability libraries.
    Exploit,
    /// Authentication and credential libraries.
    Auth,
}

impl fmt::Display for NseLibraryCategory {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Core => write!(f, "Core"),
            Self::Protocol => write!(f, "Protocol"),
            Self::Utility => write!(f, "Utility"),
            Self::Exploit => write!(f, "Exploit"),
            Self::Auth => write!(f, "Auth"),
        }
    }
}

/// Sandbox side effects a library may perform.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NseSandboxSideEffect {
    /// No side effects (pure computation).
    None,
    /// Reads from the filesystem.
    FileSystemRead,
    /// Writes to the filesystem.
    FileSystemWrite,
    /// Makes network connections.
    NetworkAccess,
    /// Spawns or executes external processes.
    ProcessExecution,
    /// Accesses environment variables.
    EnvAccess,
}

impl fmt::Display for NseSandboxSideEffect {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::None => write!(f, "None"),
            Self::FileSystemRead => write!(f, "FileSystemRead"),
            Self::FileSystemWrite => write!(f, "FileSystemWrite"),
            Self::NetworkAccess => write!(f, "NetworkAccess"),
            Self::ProcessExecution => write!(f, "ProcessExecution"),
            Self::EnvAccess => write!(f, "EnvAccess"),
        }
    }
}

/// Enforcement status of the NSE capability wrappers for this library.
///
/// Tracks whether a library's side-effecting operations have been routed
/// through `NseCapabilityContext` for policy enforcement.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EnforcementStatus {
    /// All side-effecting operations routed through NseCapabilityContext.
    Wrapped,
    /// Some operations wrapped, others not.
    PartiallyWrapped,
    /// Manual-only (CLI interactive), no capability enforcement.
    ManualOnly,
    /// Not yet migrated, deferred to future milestone.
    Deferred,
    /// No side-effecting operations.
    Pure,
}

impl fmt::Display for EnforcementStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Wrapped => write!(f, "Wrapped"),
            Self::PartiallyWrapped => write!(f, "PartiallyWrapped"),
            Self::ManualOnly => write!(f, "ManualOnly"),
            Self::Deferred => write!(f, "Deferred"),
            Self::Pure => write!(f, "Pure"),
        }
    }
}

/// Fallback behavior when a library is not available in the runtime.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NseFallbackBehavior {
    /// Script must have this library; absence is a hard error.
    HardFail,
    /// Script can degrade gracefully if the library is absent.
    GracefulDegrade,
    /// Script silently skips functionality requiring this library.
    Skip,
}

impl fmt::Display for NseFallbackBehavior {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::HardFail => write!(f, "HardFail"),
            Self::GracefulDegrade => write!(f, "GracefulDegrade"),
            Self::Skip => write!(f, "Skip"),
        }
    }
}

// ---------------------------------------------------------------------------
// Descriptor
// ---------------------------------------------------------------------------

/// Declarative descriptor for a single NSE library module.
#[derive(Debug, Clone)]
pub struct NseLibraryDescriptor {
    /// Library name as used in `require()` calls (e.g. `"stdnse"`).
    pub name: &'static str,
    /// Functional category.
    pub category: NseLibraryCategory,
    /// Sandbox side effects this library may perform.
    pub sandbox_side_effects: &'static [NseSandboxSideEffect],
    /// Optional system/library dependencies required at build or runtime.
    /// Empty if the library has no special dependencies.
    pub optional_deps: &'static [&'static str],
    /// Behavior when the library is unavailable.
    pub fallback_behavior: NseFallbackBehavior,
    /// Freeform notes about compatibility, known gaps, or special handling.
    pub notes: &'static str,
    /// Whether side-effecting operations are routed through NseCapabilityContext.
    pub enforcement_status: EnforcementStatus,
}

// ---------------------------------------------------------------------------
// Static registry
// ---------------------------------------------------------------------------

/// Complete registry of standard Nmap Lua library modules.
///
/// 43 entries total: 24 main + 19 auxiliary. `nse.lua` (the orchestrator)
/// is intentionally excluded.
pub static LIBRARY_REGISTRY: &[NseLibraryDescriptor] = &[
    // =====================================================================
    // Main libraries (24)
    // =====================================================================
    NseLibraryDescriptor {
        name: "stdnse",
        category: NseLibraryCategory::Core,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::HardFail,
        notes: "Core output formatting, verbose, and utility functions. Required by nearly all scripts.",
        enforcement_status: EnforcementStatus::PartiallyWrapped,
    },
    NseLibraryDescriptor {
        name: "nmap",
        category: NseLibraryCategory::Core,
        sandbox_side_effects: &[
            NseSandboxSideEffect::EnvAccess,
            NseSandboxSideEffect::NetworkAccess,
        ],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::HardFail,
        notes: "Nmap scan state, registry, and host/port data access. Provides nmap.target, nmap.registry, etc.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "socket",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::HardFail,
        notes: "Low-level TCP/UDP socket operations. Core networking primitive for protocol libraries.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "http",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::HardFail,
        notes: "HTTP client library. Supports GET/POST/PUT/DELETE/HEAD/OPTIONS/request, cookies, authentication, SSL/TLS. All network operations gated via check_network_tcp() (or maybe_denied_response helper); denied requests never reach reqwest.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "dns",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::HardFail,
        notes: "DNS resolution and packet construction. Supports A, AAAA, MX, TXT, and other record types.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "ssl",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &["openssl"],
        fallback_behavior: NseFallbackBehavior::HardFail,
        notes: "TLS/SSL handshake and certificate operations. Wraps OpenSSL via Lua bindings. Wrapped since Milestone 3 Phase 05.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "ssh",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &["libssh2"],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "SSH protocol operations. Requires libssh2 for real execution; stub available.",
        enforcement_status: EnforcementStatus::Deferred,
    },
    NseLibraryDescriptor {
        name: "smb",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "SMB/CIFS protocol operations. NTLM authentication, share enumeration.",
        enforcement_status: EnforcementStatus::Deferred,
    },
    NseLibraryDescriptor {
        name: "smb2",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "SMBv2/v3 protocol operations. Modern SMB dialect support.",
        enforcement_status: EnforcementStatus::Deferred,
    },
    NseLibraryDescriptor {
        name: "mysql",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "MySQL database protocol client. Authentication, query execution.",
        enforcement_status: EnforcementStatus::Deferred,
    },
    NseLibraryDescriptor {
        name: "postgres",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "PostgreSQL database protocol client. Startup, authentication, simple query.",
        enforcement_status: EnforcementStatus::Deferred,
    },
    NseLibraryDescriptor {
        name: "redis",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "Redis protocol client. RESP protocol, command execution.",
        enforcement_status: EnforcementStatus::Deferred,
    },
    NseLibraryDescriptor {
        name: "mongodb",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "MongoDB wire protocol client. isMaster, server status queries.",
        enforcement_status: EnforcementStatus::Deferred,
    },
    NseLibraryDescriptor {
        name: "ldap",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "LDAP protocol operations. Bind, search, enumeration.",
        enforcement_status: EnforcementStatus::Deferred,
    },
    NseLibraryDescriptor {
        name: "snmp",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "SNMP protocol operations. GET, GETNEXT, WALK for community strings.",
        enforcement_status: EnforcementStatus::Deferred,
    },
    NseLibraryDescriptor {
        name: "vulns",
        category: NseLibraryCategory::Exploit,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Vulnerability reporting and CVE database lookups (NVD, OSV, CISA KEV).",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "creds",
        category: NseLibraryCategory::Auth,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Pure in-memory credential store. No side effects.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "unpwdb",
        category: NseLibraryCategory::Auth,
        sandbox_side_effects: &[NseSandboxSideEffect::FileSystemRead],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Username/password database reader. Iterates credential lists for brute-force. Filesystem reads routed through NseCapabilityContext.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "brute",
        category: NseLibraryCategory::Auth,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Brute-force attack engine. Iterator pattern for credential testing.",
        enforcement_status: EnforcementStatus::Deferred,
    },
    NseLibraryDescriptor {
        name: "io",
        category: NseLibraryCategory::Core,
        sandbox_side_effects: &[
            NseSandboxSideEffect::FileSystemRead,
            NseSandboxSideEffect::FileSystemWrite,
            NseSandboxSideEffect::ProcessExecution,
        ],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "Standard Lua I/O library. File open/read/write, popen (process execution). Heavily sandboxed.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "os",
        category: NseLibraryCategory::Core,
        sandbox_side_effects: &[
            NseSandboxSideEffect::EnvAccess,
            NseSandboxSideEffect::ProcessExecution,
        ],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "Standard Lua OS library. Date/time, env vars, execute. Blocked in sandbox mode.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "lfs",
        category: NseLibraryCategory::Core,
        sandbox_side_effects: &[
            NseSandboxSideEffect::FileSystemRead,
            NseSandboxSideEffect::FileSystemWrite,
        ],
        optional_deps: &["luafilesystem"],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "LuaFileSystem directory operations. List, stat, symlink checks. Restricted to allowed_dir in sandbox.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "tab",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Table/array utility functions for structured data manipulation.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "json",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "JSON encode/decode library.",
        enforcement_status: EnforcementStatus::Pure,
    },
    // =====================================================================
    // Auxiliary libraries (19)
    // =====================================================================
    NseLibraryDescriptor {
        name: "base64",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Base64 encoding and decoding.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "base32",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Base32 encoding and decoding.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "bin",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Binary data packing/unpacking (little/big endian).",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "bit",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Bitwise operations library.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "stringaux",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Extended string utilities beyond standard Lua string lib.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "strbuf",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "String buffer for efficient string concatenation.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "nse_string",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "NSE-specific string helper functions.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "nse_table",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "NSE-specific table helper functions.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "pcre",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &["pcre"],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "PCRE regular expressions. Optional; falls back to Lua patterns if unavailable.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "openssl",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &["openssl"],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "OpenSSL bindings for crypto operations. Certificate parsing, hashing, HMAC.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "comm",
        category: NseLibraryCategory::Protocol,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::GracefulDegrade,
        notes: "Communication helpers for banner grabbing and service interaction.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "shortport",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Port number normalization and validation helpers.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "target",
        category: NseLibraryCategory::Core,
        sandbox_side_effects: &[NseSandboxSideEffect::NetworkAccess],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Target host/address resolution and manipulation.",
        enforcement_status: EnforcementStatus::Deferred,
    },
    NseLibraryDescriptor {
        name: "match_lib",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Pattern matching helpers for service detection.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "matchs",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Structured match operations for version detection.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "datetime",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Date and time parsing/formatting utilities.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "rand",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Random number generation helpers.",
        enforcement_status: EnforcementStatus::Wrapped,
    },
    NseLibraryDescriptor {
        name: "url",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "URL parsing and normalization utilities.",
        enforcement_status: EnforcementStatus::Pure,
    },
    NseLibraryDescriptor {
        name: "unicode",
        category: NseLibraryCategory::Utility,
        sandbox_side_effects: &[NseSandboxSideEffect::None],
        optional_deps: &[],
        fallback_behavior: NseFallbackBehavior::Skip,
        notes: "Unicode encoding/decoding and normalization.",
        enforcement_status: EnforcementStatus::Pure,
    },
];

// ---------------------------------------------------------------------------
// Lookup functions
// ---------------------------------------------------------------------------

/// Find a library descriptor by name.
///
/// Returns `None` if no library with that name exists in the registry.
pub fn find_library(name: &str) -> Option<&'static NseLibraryDescriptor> {
    LIBRARY_REGISTRY.iter().find(|lib| lib.name == name)
}

/// Return all registered library descriptors.
pub fn all_libraries() -> &'static [NseLibraryDescriptor] {
    LIBRARY_REGISTRY
}

/// Return all libraries in the given category.
pub fn libraries_by_category(category: NseLibraryCategory) -> Vec<&'static NseLibraryDescriptor> {
    LIBRARY_REGISTRY
        .iter()
        .filter(|lib| lib.category == category)
        .collect()
}

/// Return all libraries that have at least one sandbox side effect.
pub fn libraries_with_side_effects() -> Vec<&'static NseLibraryDescriptor> {
    LIBRARY_REGISTRY
        .iter()
        .filter(|lib| {
            !lib.sandbox_side_effects.is_empty()
                && lib.sandbox_side_effects[0] != NseSandboxSideEffect::None
        })
        .collect()
}

/// Return the effective sandbox policy for a library.
///
/// If the library has no side effects, returns `None`. Otherwise returns
/// the list of side effects that sandbox policy must account for.
pub fn sandbox_policy_for_library(name: &str) -> Option<&'static [NseSandboxSideEffect]> {
    find_library(name).and_then(|lib| {
        if lib.sandbox_side_effects == [NseSandboxSideEffect::None] {
            None
        } else {
            Some(lib.sandbox_side_effects)
        }
    })
}

/// Return libraries that are known to be missing from some Nmap builds
/// or have conditional availability.
pub fn libraries_missing_from_nmap() -> Vec<&'static NseLibraryDescriptor> {
    LIBRARY_REGISTRY
        .iter()
        .filter(|lib| !lib.optional_deps.is_empty())
        .collect()
}

/// Return the total number of registered libraries.
pub fn registry_count() -> usize {
    LIBRARY_REGISTRY.len()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn registry_has_43_entries() {
        assert_eq!(
            LIBRARY_REGISTRY.len(),
            43,
            "Expected exactly 43 registered libraries (24 main + 19 auxiliary)"
        );
    }

    #[test]
    fn no_duplicate_names() {
        let mut names: Vec<&str> = LIBRARY_REGISTRY.iter().map(|lib| lib.name).collect();
        let original_len = names.len();
        names.dedup();
        assert_eq!(
            names.len(),
            original_len,
            "Duplicate library names found in registry"
        );
    }

    #[test]
    fn every_library_has_descriptor() {
        for lib in LIBRARY_REGISTRY {
            assert!(!lib.name.is_empty(), "Library name must not be empty");
            assert!(
                !lib.notes.is_empty(),
                "Library '{}' must have notes",
                lib.name
            );
        }
    }

    #[test]
    fn find_library_works() {
        assert!(find_library("stdnse").is_some());
        assert!(find_library("http").is_some());
        assert!(find_library("json").is_some());
        assert!(find_library("nonexistent").is_none());
    }

    #[test]
    fn find_library_returns_correct_entry() {
        let lib = find_library("http").unwrap();
        assert_eq!(lib.name, "http");
        assert_eq!(lib.category, NseLibraryCategory::Protocol);
        assert!(lib
            .sandbox_side_effects
            .contains(&NseSandboxSideEffect::NetworkAccess));
    }

    #[test]
    fn all_libraries_returns_full_registry() {
        assert_eq!(all_libraries().len(), LIBRARY_REGISTRY.len());
    }

    #[test]
    fn libraries_by_category_core() {
        let core = libraries_by_category(NseLibraryCategory::Core);
        assert!(core.len() >= 6, "Expected at least 6 Core libraries");
        assert!(core.iter().any(|l| l.name == "stdnse"));
        assert!(core.iter().any(|l| l.name == "nmap"));
        assert!(core.iter().any(|l| l.name == "io"));
    }

    #[test]
    fn libraries_by_category_protocol() {
        let protos = libraries_by_category(NseLibraryCategory::Protocol);
        assert!(
            protos.len() >= 10,
            "Expected at least 10 Protocol libraries"
        );
        assert!(protos.iter().any(|l| l.name == "http"));
        assert!(protos.iter().any(|l| l.name == "dns"));
        assert!(protos.iter().any(|l| l.name == "smb"));
    }

    #[test]
    fn libraries_by_category_utility() {
        let utils = libraries_by_category(NseLibraryCategory::Utility);
        assert!(utils.len() >= 12, "Expected at least 12 Utility libraries");
        assert!(utils.iter().any(|l| l.name == "base64"));
        assert!(utils.iter().any(|l| l.name == "json"));
        assert!(utils.iter().any(|l| l.name == "tab"));
    }

    #[test]
    fn side_effects_coverage() {
        let with_effects = libraries_with_side_effects();
        assert!(
            with_effects.len() >= 15,
            "Expected at least 15 libraries with side effects, got {}",
            with_effects.len()
        );
        // Verify known side-effecting libraries
        assert!(with_effects.iter().any(|l| l.name == "socket"));
        assert!(with_effects.iter().any(|l| l.name == "http"));
        assert!(with_effects.iter().any(|l| l.name == "io"));
        assert!(with_effects.iter().any(|l| l.name == "os"));
    }

    #[test]
    fn sandbox_policy_for_known_library() {
        let policy = sandbox_policy_for_library("socket");
        assert!(policy.is_some());
        assert!(policy
            .unwrap()
            .contains(&NseSandboxSideEffect::NetworkAccess));
    }

    #[test]
    fn sandbox_policy_for_no_side_effects() {
        // "tab" has None side effects — sandbox_policy_for_library returns None
        let policy = sandbox_policy_for_library("tab");
        assert!(policy.is_none());
    }

    #[test]
    fn sandbox_policy_for_unknown_library() {
        assert!(sandbox_policy_for_library("nonexistent").is_none());
    }

    #[test]
    fn libraries_with_optional_deps() {
        let missing = libraries_missing_from_nmap();
        assert!(
            !missing.is_empty(),
            "Expected at least some libraries with optional deps"
        );
        assert!(missing.iter().any(|l| l.name == "ssl"));
        assert!(missing.iter().any(|l| l.name == "ssh"));
        assert!(missing.iter().any(|l| l.name == "pcre"));
    }

    #[test]
    fn registry_count_matches() {
        assert_eq!(registry_count(), LIBRARY_REGISTRY.len());
    }

    #[test]
    fn main_libraries_present() {
        let main_names = [
            "stdnse", "nmap", "socket", "http", "dns", "ssl", "ssh", "smb", "smb2", "mysql",
            "postgres", "redis", "mongodb", "ldap", "snmp", "vulns", "creds", "unpwdb", "brute",
            "io", "os", "lfs", "tab", "json",
        ];
        for name in &main_names {
            assert!(
                find_library(name).is_some(),
                "Main library '{}' not found in registry",
                name
            );
        }
    }

    #[test]
    fn auxiliary_libraries_present() {
        let aux_names = [
            "base64",
            "base32",
            "bin",
            "bit",
            "stringaux",
            "strbuf",
            "nse_string",
            "nse_table",
            "pcre",
            "openssl",
            "comm",
            "shortport",
            "target",
            "match_lib",
            "matchs",
            "datetime",
            "rand",
            "url",
            "unicode",
        ];
        for name in &aux_names {
            assert!(
                find_library(name).is_some(),
                "Auxiliary library '{}' not found in registry",
                name
            );
        }
    }

    #[test]
    fn all_categories_used() {
        let all_categories: Vec<NseLibraryCategory> =
            LIBRARY_REGISTRY.iter().map(|lib| lib.category).collect();
        assert!(all_categories.contains(&NseLibraryCategory::Core));
        assert!(all_categories.contains(&NseLibraryCategory::Protocol));
        assert!(all_categories.contains(&NseLibraryCategory::Utility));
        assert!(all_categories.contains(&NseLibraryCategory::Exploit));
        assert!(all_categories.contains(&NseLibraryCategory::Auth));
    }

    #[test]
    fn all_fallback_behaviors_used() {
        let all_behaviors: Vec<NseFallbackBehavior> = LIBRARY_REGISTRY
            .iter()
            .map(|lib| lib.fallback_behavior)
            .collect();
        assert!(all_behaviors.contains(&NseFallbackBehavior::HardFail));
        assert!(all_behaviors.contains(&NseFallbackBehavior::GracefulDegrade));
        assert!(all_behaviors.contains(&NseFallbackBehavior::Skip));
    }

    #[test]
    fn display_implementations() {
        assert_eq!(NseLibraryCategory::Core.to_string(), "Core");
        assert_eq!(NseLibraryCategory::Protocol.to_string(), "Protocol");
        assert_eq!(NseLibraryCategory::Utility.to_string(), "Utility");
        assert_eq!(NseLibraryCategory::Exploit.to_string(), "Exploit");
        assert_eq!(NseLibraryCategory::Auth.to_string(), "Auth");

        assert_eq!(NseSandboxSideEffect::None.to_string(), "None");
        assert_eq!(
            NseSandboxSideEffect::NetworkAccess.to_string(),
            "NetworkAccess"
        );

        assert_eq!(NseFallbackBehavior::HardFail.to_string(), "HardFail");
        assert_eq!(
            NseFallbackBehavior::GracefulDegrade.to_string(),
            "GracefulDegrade"
        );
        assert_eq!(NseFallbackBehavior::Skip.to_string(), "Skip");
    }

    #[test]
    fn enforcement_status_display() {
        assert_eq!(EnforcementStatus::Wrapped.to_string(), "Wrapped");
        assert_eq!(
            EnforcementStatus::PartiallyWrapped.to_string(),
            "PartiallyWrapped"
        );
        assert_eq!(EnforcementStatus::ManualOnly.to_string(), "ManualOnly");
        assert_eq!(EnforcementStatus::Deferred.to_string(), "Deferred");
        assert_eq!(EnforcementStatus::Pure.to_string(), "Pure");
    }

    #[test]
    fn every_library_has_enforcement_status() {
        for lib in LIBRARY_REGISTRY {
            // Exhaustive match ensures new variants are handled
            match lib.enforcement_status {
                EnforcementStatus::Wrapped
                | EnforcementStatus::PartiallyWrapped
                | EnforcementStatus::ManualOnly
                | EnforcementStatus::Deferred
                | EnforcementStatus::Pure => {}
            }
        }
    }

    #[test]
    fn wrapped_libraries_include_known_wrapped() {
        let wrapped: Vec<&str> = LIBRARY_REGISTRY
            .iter()
            .filter(|l| l.enforcement_status == EnforcementStatus::Wrapped)
            .map(|l| l.name)
            .collect();
        assert!(wrapped.contains(&"socket"), "socket should be Wrapped");
        assert!(wrapped.contains(&"io"), "io should be Wrapped");
        assert!(wrapped.contains(&"os"), "os should be Wrapped");
        assert!(wrapped.contains(&"lfs"), "lfs should be Wrapped");
        assert!(wrapped.contains(&"nmap"), "nmap should be Wrapped");
        assert!(wrapped.contains(&"dns"), "dns should be Wrapped");
        assert!(wrapped.contains(&"comm"), "comm should be Wrapped");
        assert!(wrapped.contains(&"openssl"), "openssl should be Wrapped");
        assert!(wrapped.contains(&"datetime"), "datetime should be Wrapped");
        assert!(wrapped.contains(&"rand"), "rand should be Wrapped");
        assert!(
            wrapped.contains(&"unpwdb"),
            "unpwdb should be Wrapped (Milestone 5 Phase 04)"
        );
        assert!(
            wrapped.contains(&"ssl"),
            "ssl should be Wrapped (Milestone 3 Phase 05)"
        );
    }

    #[test]
    fn partially_wrapped_libraries() {
        let partially: Vec<&str> = LIBRARY_REGISTRY
            .iter()
            .filter(|l| l.enforcement_status == EnforcementStatus::PartiallyWrapped)
            .map(|l| l.name)
            .collect();
        assert!(
            partially.contains(&"stdnse"),
            "stdnse should be PartiallyWrapped"
        );
        // http was PartiallyWrapped but is now Wrapped (Milestone 6: check_network_tcp blocks before reqwest)
    }

    #[test]
    fn deferred_libraries_include_auth_and_protocol() {
        let deferred: Vec<&str> = LIBRARY_REGISTRY
            .iter()
            .filter(|l| l.enforcement_status == EnforcementStatus::Deferred)
            .map(|l| l.name)
            .collect();
        assert!(deferred.contains(&"brute"), "brute should be Deferred");
        assert!(deferred.contains(&"ssh"), "ssh should be Deferred");
    }

    #[test]
    fn pure_libraries_have_no_side_effects() {
        let pure_with_effects: Vec<&str> = LIBRARY_REGISTRY
            .iter()
            .filter(|l| {
                l.enforcement_status == EnforcementStatus::Pure
                    && l.sandbox_side_effects != &[NseSandboxSideEffect::None]
            })
            .map(|l| l.name)
            .collect();
        // vulns has NetworkAccess but is Pure (CVE lookups only, no user data exfil)
        // This is acceptable and documented; all others should have None side effects
        for name in &pure_with_effects {
            assert_eq!(
                *name, "vulns",
                "Pure library '{}' unexpectedly has side effects",
                name
            );
        }
    }

    #[test]
    fn all_enforcement_statuses_used() {
        let statuses: Vec<EnforcementStatus> = LIBRARY_REGISTRY
            .iter()
            .map(|l| l.enforcement_status)
            .collect();
        assert!(
            statuses.contains(&EnforcementStatus::Wrapped),
            "At least one library should be Wrapped"
        );
        assert!(
            statuses.contains(&EnforcementStatus::PartiallyWrapped),
            "At least one library should be PartiallyWrapped"
        );
        assert!(
            statuses.contains(&EnforcementStatus::Deferred),
            "At least one library should be Deferred"
        );
        assert!(
            statuses.contains(&EnforcementStatus::Pure),
            "At least one library should be Pure"
        );
    }
}