dbnexus 0.6.0-rc.4

An enterprise-grade database abstraction layer for Rust with built-in permission control and connection pooling
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
// Copyright (c) 2026 Kirky.X
// SPDX-License-Identifier: MIT
//! 统一注入检测引擎
//!
//! 此前注入检测规则散落在三处独立实现中:
//! 1. `sql_parser::contains_sql_injection` 的关系型静态模式表;
//! 2. `sql_parser::contains_variables` 的动态 SQL 变量正则;
//! 3. `session::validate_cypher_safety` 的图查询危险过程黑名单
//!    与 `ddl_guard::FORBIDDEN_PATTERNS` 的 DDL 禁用模式。
//!
//! 本模块将全部规则合并为单一注册表(去重后),按 [`RuleCategory`] 分类,
//! 由各调用方的方言管线(预处理)复用同一匹配器:
//! - 关系型管线(NFKC 规范化 → 块注释标记 → 去字符串 → 去块注释 → 大写);
//! - DDL 管线(trim → 大写);
//! - 图管线(原样小写)。
//!
//! # 去重记录(可证明等价)
//!
//! 合并时按"子串包含即冗余"剪枝——若模式 B 是模式 A 的子串,则包含 A 的输入
//! 必然包含 B,A 永远不会独有命中,可安全删除。剪掉的 4 条关系型模式:
//! - `"; EXECUTE"`(被 `"; EXEC"` 包含)
//! - `"EXEC xp_"`、`"EXECUTE xp_"`(被 `" xp_"` 包含)
//! - `"ORDER BY 1#"`(被 `"#"` 包含)
//!
//! 误报对比见模块底部 `parity` 测试:引擎与合并前遗留规则表在攻击语料与
//! 良性语料上的判定完全一致(保留既有误报轮廓,未扩大也未缩小)。

use std::sync::LazyLock;

/// 注入规则类别
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RuleCategory {
    /// UNION 注入(UNION SELECT 等)
    Union,
    /// 布尔盲注(OR 1=1 等)
    BooleanBlind,
    /// 时间盲注(SLEEP/PG_SLEEP/WAITFOR 等)
    TimeBlind,
    /// 动态 SQL 执行(EXEC/XP_CMDSHELL 等)
    DynamicExec,
    /// 文件操作(LOAD_FILE/INTO OUTFILE 等)
    FileOps,
    /// 信息泄露(INFORMATION_SCHEMA/系统表等)
    InfoLeak,
    /// 编码绕过(CHAR/CHR/CONCAT/0X 等)
    Encoding,
    /// 堆叠查询(; DROP 等)
    Stacked,
    /// 注释注入(--、#、块注释标记)
    Comment,
    /// 其他危险模式(HAVING 1=1、EXTRACTVALUE 等)
    Other,
    /// 动态 SQL 变量(@var/:var/${var} 等,关系型管线专用正则)
    #[cfg(feature = "sql-parser")]
    DynamicVariable,
    /// DDL 禁用模式(DROP DATABASE/DROP ALL,DDL 管线)
    DdlForbidden,
    /// 图数据库危险过程(CALL apoc. 等,图管线)
    GraphProcedure,
}

/// 统一注入规则
#[derive(Debug)]
pub struct InjectionRule {
    /// 稳定规则 ID(审计/日志引用)
    pub id: &'static str,
    /// 规则类别
    pub category: RuleCategory,
    /// 匹配模式(子串匹配;关系型管线对大写文本匹配)
    pub pattern: &'static str,
}

/// 统一注入检测引擎
///
/// 持有去重后的全部规则;匹配器纯同步无状态,全局单例经 [`InjectionEngine::global`]。
pub struct InjectionEngine {
    rules: Vec<InjectionRule>,
    /// 动态 SQL 变量正则(sql-parser feature;`?` 占位符不算危险变量)
    #[cfg(feature = "sql-parser")]
    variable_regexes: Vec<regex::Regex>,
}

/// 全局引擎:合并三处遗留规则表并完成去重剪枝
static GLOBAL_ENGINE: LazyLock<InjectionEngine> = LazyLock::new(InjectionEngine::build_global);

impl InjectionEngine {
    /// 全局引擎单例
    pub fn global() -> &'static Self {
        &GLOBAL_ENGINE
    }

    /// 注册表规模(去重后),供诊断与测试
    pub fn rule_count(&self) -> usize {
        self.rules.len()
    }

    /// 对预处理的文本做子串扫描,返回命中的规则
    ///
    /// `prepared` 必须已按调用方方言管线预处理(大写化等);
    /// 仅匹配 `categories` 中列出的类别。
    pub fn scan(&self, prepared: &str, categories: &[RuleCategory]) -> Vec<&InjectionRule> {
        self.rules
            .iter()
            .filter(|rule| categories.contains(&rule.category) && prepared.contains(rule.pattern))
            .collect()
    }

    /// 关系型管线:完整预处理后扫描(sql-parser feature)
    ///
    /// 语义与合并前 `contains_sql_injection` 完全一致:
    /// NFKC 规范化 → 块注释标记检查(在移除前检测)→ 移除字符串字面量
    /// → 移除块注释 → 大写 → 子串扫描。
    #[cfg(feature = "sql-parser")]
    pub fn scan_relational(&self, sql: &str) -> Vec<&InjectionRule> {
        use crate::access::sql_parser::{
            normalize_unicode, remove_string_literals, strip_block_comments,
        };

        // 第一步:Unicode 规范化(NFKC),防止 Unicode 绕过
        let normalized = normalize_unicode(sql);

        // 第二步:块注释标记在移除前检测(注释本身即为注入迹象)
        if normalized.contains("/*") {
            return self
                .rules
                .iter()
                .filter(|rule| rule.id == "comment.block_marker")
                .collect();
        }

        // 第三至四步:移除字符串字面量与块注释后大写
        let without_strings = remove_string_literals(&normalized);
        let without_comments = strip_block_comments(&without_strings);
        let prepared = without_comments.to_uppercase();

        self.scan(
            &prepared,
            &[
                RuleCategory::Union,
                RuleCategory::BooleanBlind,
                RuleCategory::TimeBlind,
                RuleCategory::DynamicExec,
                RuleCategory::FileOps,
                RuleCategory::InfoLeak,
                RuleCategory::Encoding,
                RuleCategory::Stacked,
                RuleCategory::Comment,
                RuleCategory::Other,
            ],
        )
    }

    /// 关系型管线布尔口径(等价合并前的 `contains_sql_injection`)
    #[cfg(feature = "sql-parser")]
    pub fn is_suspicious_relational(&self, sql: &str) -> bool {
        !self.scan_relational(sql).is_empty()
    }

    /// 动态 SQL 变量检测(sql-parser feature;等价合并前的 `contains_variables`)
    ///
    /// `?` prepared-statement 占位符不算危险变量:它是参数绑定的安全形态。
    #[cfg(feature = "sql-parser")]
    pub fn has_dynamic_variables(&self, sql: &str) -> bool {
        use crate::access::sql_parser::remove_string_literals;

        // 先移除字符串字面量,避免误报
        let without_strings = remove_string_literals(sql);
        self.variable_regexes
            .iter()
            .any(|re| re.is_match(&without_strings))
    }

    /// DDL 管线:trim + 大写后扫描禁用模式
    ///
    /// 语义与合并前 `ddl_guard::FORBIDDEN_PATTERNS` 检查一致。
    pub fn scan_ddl(&self, sql: &str) -> Vec<&InjectionRule> {
        let prepared = sql.trim().to_uppercase();
        self.scan(&prepared, &[RuleCategory::DdlForbidden])
    }

    /// 图管线:原样小写后扫描危险过程与块注释
    ///
    /// 语义与合并前 `validate_cypher_safety` 的第 4/5 步一致
    /// (不做字符串剥离,图查询无对应预处理)。
    pub fn scan_graph(&self, cypher: &str) -> Vec<&InjectionRule> {
        let prepared = cypher.to_ascii_lowercase();
        self.scan(
            &prepared,
            &[RuleCategory::Comment, RuleCategory::GraphProcedure],
        )
        .into_iter()
        .filter(|rule| {
            rule.pattern.starts_with("call ") || rule.pattern == "/*" || rule.pattern == "*/"
        })
        .collect()
    }

    /// 构建全局规则表(合并 + 去重剪枝)
    fn build_global() -> Self {
        // --- 关系型管线规则(来源:sql_parser::INJECTION_PATTERNS,去重后) ---
        const RELATIONAL: &[(&str, RuleCategory, &str)] = &[
            // === UNION 注入 ===
            // ("UNION ALL SELECT"/"UNION DISTINCT SELECT" 与 "UNION SELECT" 无子串
            //   包含关系,各自独立保留)
            ("union.select", RuleCategory::Union, "UNION SELECT"),
            ("union.all_select", RuleCategory::Union, "UNION ALL SELECT"),
            (
                "union.distinct_select",
                RuleCategory::Union,
                "UNION DISTINCT SELECT",
            ),
            // === 布尔盲注 ===
            ("bool.or_1eq1", RuleCategory::BooleanBlind, " OR 1=1"),
            ("bool.or_1sp1", RuleCategory::BooleanBlind, " OR 1 =1"),
            ("bool.or_1sp_eq1", RuleCategory::BooleanBlind, " OR 1= 1"),
            (
                "bool.or_1sp_eq_sp1",
                RuleCategory::BooleanBlind,
                " OR 1 = 1",
            ),
            ("bool.or_true", RuleCategory::BooleanBlind, " OR TRUE"),
            ("bool.or_false", RuleCategory::BooleanBlind, " OR FALSE"),
            ("bool.and_1eq1", RuleCategory::BooleanBlind, " AND 1=1"),
            ("bool.and_true", RuleCategory::BooleanBlind, " AND TRUE"),
            ("bool.and_false", RuleCategory::BooleanBlind, " AND FALSE"),
            // === 时间盲注 - MySQL ===
            ("time.sleep", RuleCategory::TimeBlind, "SLEEP("),
            ("time.benchmark", RuleCategory::TimeBlind, "BENCHMARK("),
            // === 时间盲注 - PostgreSQL ===
            ("time.pg_sleep", RuleCategory::TimeBlind, "PG_SLEEP("),
            (
                "time.pg_sleep_for",
                RuleCategory::TimeBlind,
                "PG_SLEEP_FOR(",
            ),
            (
                "time.pg_sleep_until",
                RuleCategory::TimeBlind,
                "PG_SLEEP_UNTIL(",
            ),
            // === 时间盲注 - SQL Server ===
            (
                "time.waitfor_delay",
                RuleCategory::TimeBlind,
                "WAITFOR DELAY",
            ),
            ("time.waitfor_time", RuleCategory::TimeBlind, "WAITFOR TIME"),
            // === 时间盲注 - Oracle ===
            (
                "time.dbms_pipe",
                RuleCategory::TimeBlind,
                "DBMS_PIPE.RECEIVE_MESSAGE(",
            ),
            (
                "time.dbms_lock",
                RuleCategory::TimeBlind,
                "DBMS_LOCK.SLEEP(",
            ),
            // === 动态 SQL 执行 ===
            // ("; EXECUTE" 被 "; EXEC" 包含;"EXEC xp_"/"EXECUTE xp_" 被 " xp_"
            //   包含——三条冗余模式已在去重中剪除,见模块文档)
            ("exec.exec", RuleCategory::DynamicExec, "EXEC("),
            ("exec.execute", RuleCategory::DynamicExec, "EXECUTE("),
            (
                "exec.sp_executesql",
                RuleCategory::DynamicExec,
                "SP_EXECUTESQL",
            ),
            ("exec.xp_cmdshell", RuleCategory::DynamicExec, "XP_CMDSHELL"),
            ("exec.xp_generic", RuleCategory::DynamicExec, " xp_"),
            // === 文件操作 ===
            ("file.load_file", RuleCategory::FileOps, "LOAD_FILE("),
            ("file.into_outfile", RuleCategory::FileOps, "INTO OUTFILE"),
            ("file.into_dumpfile", RuleCategory::FileOps, "INTO DUMPFILE"),
            // === 信息泄露 ===
            (
                "info.information_schema",
                RuleCategory::InfoLeak,
                "INFORMATION_SCHEMA",
            ),
            ("info.sysobjects", RuleCategory::InfoLeak, "SYSOBJECTS"),
            ("info.syscolumns", RuleCategory::InfoLeak, "SYSCOLUMNS"),
            ("info.sys_tables", RuleCategory::InfoLeak, "SYS.TABLES"),
            ("info.sys_columns", RuleCategory::InfoLeak, "SYS.COLUMNS"),
            (
                "info.sys_databases",
                RuleCategory::InfoLeak,
                "SYS.DATABASES",
            ),
            ("info.mysql_user", RuleCategory::InfoLeak, "MYSQL.USER"),
            ("info.pg_user", RuleCategory::InfoLeak, "PG_USER"),
            ("info.pg_shadow", RuleCategory::InfoLeak, "PG_SHADOW"),
            ("info.all_tables", RuleCategory::InfoLeak, "ALL_TABLES"),
            ("info.all_columns", RuleCategory::InfoLeak, "ALL_COLUMNS"),
            (
                "info.all_tab_columns",
                RuleCategory::InfoLeak,
                "ALL_TAB_COLUMNS",
            ),
            ("info.user_tables", RuleCategory::InfoLeak, "USER_TABLES"),
            (
                "info.user_tab_columns",
                RuleCategory::InfoLeak,
                "USER_TAB_COLUMNS",
            ),
            // === 编码绕过 ===
            ("enc.char", RuleCategory::Encoding, "CHAR("),
            ("enc.chr", RuleCategory::Encoding, "CHR("),
            ("enc.concat", RuleCategory::Encoding, "CONCAT("),
            ("enc.concat_ws", RuleCategory::Encoding, "CONCAT_WS("),
            ("enc.hex_prefix", RuleCategory::Encoding, "0X"),
            // === 堆叠查询 ===
            // ("; EXECUTE" 剪除,见上)
            ("stack.drop", RuleCategory::Stacked, "; DROP"),
            ("stack.delete", RuleCategory::Stacked, "; DELETE"),
            ("stack.update", RuleCategory::Stacked, "; UPDATE"),
            ("stack.insert", RuleCategory::Stacked, "; INSERT"),
            ("stack.truncate", RuleCategory::Stacked, "; TRUNCATE"),
            ("stack.alter", RuleCategory::Stacked, "; ALTER"),
            ("stack.create", RuleCategory::Stacked, "; CREATE"),
            ("stack.exec", RuleCategory::Stacked, "; EXEC"),
            // === 注释注入 ===
            ("comment.dash_space", RuleCategory::Comment, "-- "),
            ("comment.dash_plus", RuleCategory::Comment, "--+"),
            ("comment.hash", RuleCategory::Comment, "#"),
            // === 其他危险模式 ===
            // ("ORDER BY 1#" 被 "#" 包含,剪除;"ORDER BY 1--" 无尾随空格,
            //   与 "-- " 无包含关系,保留)
            ("other.having_tautology", RuleCategory::Other, "HAVING 1=1"),
            ("other.order_by_dash", RuleCategory::Other, "ORDER BY 1--"),
            (
                "other.procedure_analyse",
                RuleCategory::Other,
                "PROCEDURE ANALYSE(",
            ),
            ("other.extractvalue", RuleCategory::Other, "EXTRACTVALUE("),
            ("other.updatexml", RuleCategory::Other, "UPDATEXML("),
            ("other.xmltype", RuleCategory::Other, "XMLTYPE("),
            ("other.utl_http", RuleCategory::Other, "UTL_HTTP.REQUEST("),
            (
                "other.utl_inaddr_host",
                RuleCategory::Other,
                "UTL_INADDR.GET_HOST_ADDRESS(",
            ),
            (
                "other.utl_inaddr_name",
                RuleCategory::Other,
                "UTL_INADDR.GET_HOST_NAME(",
            ),
        ];

        // --- DDL 管线规则(来源:ddl_guard::FORBIDDEN_PATTERNS) ---
        const DDL: &[(&str, RuleCategory, &str)] = &[
            (
                "ddl.drop_database",
                RuleCategory::DdlForbidden,
                "DROP DATABASE",
            ),
            ("ddl.drop_all", RuleCategory::DdlForbidden, "DROP ALL"),
        ];

        // --- 图管线规则(来源:validate_cypher_safety 第 4/5 步) ---
        const GRAPH: &[(&str, RuleCategory, &str)] = &[
            // 块注释标记为关系型与图管线共享的唯一 "/*" 规则
            // (关系型管线经 scan_relational 的早退分支消费,见模块文档)
            ("comment.block_marker", RuleCategory::Comment, "/*"),
            ("graph.block_comment_close", RuleCategory::Comment, "*/"),
            (
                "graph.call_apoc",
                RuleCategory::GraphProcedure,
                "call apoc.",
            ),
            (
                "graph.call_dbms",
                RuleCategory::GraphProcedure,
                "call dbms.",
            ),
            ("graph.call_db", RuleCategory::GraphProcedure, "call db."),
            ("graph.call_tx", RuleCategory::GraphProcedure, "call tx."),
        ];

        let mut rules: Vec<InjectionRule> = RELATIONAL
            .iter()
            .chain(DDL.iter())
            .chain(GRAPH.iter())
            .map(|(id, category, pattern)| InjectionRule {
                id,
                category: *category,
                pattern,
            })
            .collect();

        // 防御性去重:精确重复(同 pattern 同类别)只保留首个
        let mut seen: std::collections::HashSet<(&str, &str)> = std::collections::HashSet::new();
        rules.retain(|rule| seen.insert((rule.pattern, category_key(&rule.category))));

        Self {
            rules,
            #[cfg(feature = "sql-parser")]
            variable_regexes: vec![
                // 命名参数:@variable
                regex::Regex::new(r"@[\w]+").expect("Regex pattern should be valid"),
                // 命名参数::variable
                regex::Regex::new(r":[a-zA-Z_][\w]*").expect("Regex pattern should be valid"),
                // Shell/PHP 变量:$variable、${variable}
                regex::Regex::new(r"\$\{?[\w]+\}?").expect("Regex pattern should be valid"),
                // 百分号编码参数:%variable%
                regex::Regex::new(r"%[\w]+%").expect("Regex pattern should be valid"),
                // 可能用于绕过过滤的十六进制字面量
                regex::Regex::new(r"0x[0-9A-Fa-f]+").expect("Regex pattern should be valid"),
                // 注意:`?` prepared-statement 占位符不算危险变量(参数绑定的安全形态)
            ],
        }
    }
}

/// RuleCategory 的比较键(无 DynamicVariable 的 cfg 组合下 Hash 可用性一致)
fn category_key(category: &RuleCategory) -> &'static str {
    match category {
        RuleCategory::Union => "union",
        RuleCategory::BooleanBlind => "bool",
        RuleCategory::TimeBlind => "time",
        RuleCategory::DynamicExec => "exec",
        RuleCategory::FileOps => "file",
        RuleCategory::InfoLeak => "info",
        RuleCategory::Encoding => "enc",
        RuleCategory::Stacked => "stack",
        RuleCategory::Comment => "comment",
        RuleCategory::Other => "other",
        #[cfg(feature = "sql-parser")]
        RuleCategory::DynamicVariable => "dynvar",
        RuleCategory::DdlForbidden => "ddl",
        RuleCategory::GraphProcedure => "graph",
    }
}

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

    /// 合并前的遗留关系型规则表(去重前原样副本)——用于误报对比(parity)测试
    const LEGACY_RELATIONAL: &[&str] = &[
        "UNION SELECT",
        "UNION ALL SELECT",
        "UNION DISTINCT SELECT",
        " OR 1=1",
        " OR 1 =1",
        " OR 1= 1",
        " OR 1 = 1",
        " OR TRUE",
        " OR FALSE",
        " AND 1=1",
        " AND TRUE",
        " AND FALSE",
        "SLEEP(",
        "BENCHMARK(",
        "PG_SLEEP(",
        "PG_SLEEP_FOR(",
        "PG_SLEEP_UNTIL(",
        "WAITFOR DELAY",
        "WAITFOR TIME",
        "DBMS_PIPE.RECEIVE_MESSAGE(",
        "DBMS_LOCK.SLEEP(",
        "EXEC(",
        "EXECUTE(",
        "SP_EXECUTESQL",
        "XP_CMDSHELL",
        " xp_",
        "EXEC xp_",
        "EXECUTE xp_",
        "LOAD_FILE(",
        "INTO OUTFILE",
        "INTO DUMPFILE",
        "INFORMATION_SCHEMA",
        "SYSOBJECTS",
        "SYSCOLUMNS",
        "SYS.TABLES",
        "SYS.COLUMNS",
        "SYS.DATABASES",
        "MYSQL.USER",
        "PG_USER",
        "PG_SHADOW",
        "ALL_TABLES",
        "ALL_COLUMNS",
        "ALL_TAB_COLUMNS",
        "USER_TABLES",
        "USER_TAB_COLUMNS",
        "CHAR(",
        "CHR(",
        "CONCAT(",
        "CONCAT_WS(",
        "0X",
        "; DROP",
        "; DELETE",
        "; UPDATE",
        "; INSERT",
        "; TRUNCATE",
        "; ALTER",
        "; CREATE",
        "; EXEC",
        "; EXECUTE",
        "-- ",
        "--+",
        "#",
        "HAVING 1=1",
        "ORDER BY 1--",
        "ORDER BY 1#",
        "PROCEDURE ANALYSE(",
        "EXTRACTVALUE(",
        "UPDATEXML(",
        "XMLTYPE(",
        "UTL_HTTP.REQUEST(",
        "UTL_INADDR.GET_HOST_ADDRESS(",
        "UTL_INADDR.GET_HOST_NAME(",
    ];

    /// 遗留匹配口径:任意模式命中即真
    fn legacy_contains(prepared_upper: &str) -> bool {
        LEGACY_RELATIONAL
            .iter()
            .any(|pattern| prepared_upper.contains(pattern))
    }

    /// 引擎匹配口径(关系型类别全集)
    fn engine_contains(prepared_upper: &str) -> bool {
        !InjectionEngine::global()
            .scan(
                prepared_upper,
                &[
                    RuleCategory::Union,
                    RuleCategory::BooleanBlind,
                    RuleCategory::TimeBlind,
                    RuleCategory::DynamicExec,
                    RuleCategory::FileOps,
                    RuleCategory::InfoLeak,
                    RuleCategory::Encoding,
                    RuleCategory::Stacked,
                    RuleCategory::Comment,
                    RuleCategory::Other,
                ],
            )
            .is_empty()
    }

    /// 误报对比(攻击语料 + 良性语料):引擎与遗留表判定逐一一致
    #[test]
    fn test_parity_with_legacy_rule_set() {
        let attack_corpus = [
            "SELECT * FROM USERS WHERE ID = 1 UNION SELECT PASSWORD FROM CREDENTIALS",
            "SELECT 1 OR 1=1",
            "SELECT 1 AND TRUE",
            "SELECT SLEEP(10)",
            "SELECT PG_SLEEP(5)",
            "WAITFOR DELAY 0:0:10",
            "EXEC XP_CMDSHELL DIR",
            "EXECUTE SP_EXECUTESQL X",
            "SELECT USER_X FROM T", // 含 " xp_" 形态
            "SELECT LOAD_FILE('/ETC/PASSWD')",
            "SELECT * INTO OUTFILE '/TMP/X'",
            "SELECT * FROM INFORMATION_SCHEMA.TABLES",
            "SELECT CHR(65)",
            "SELECT CONCAT(A, B) FROM T",
            "SELECT 1; DROP TABLE USERS",
            "SELECT 1 -- COMMENT",
            "SELECT 1 #COMMENT",
            "SELECT * FROM T HAVING 1=1",
            "SELECT EXTRACTVALUE(1, CONCAT(0X5C))",
        ];
        let benign_corpus = [
            "SELECT ID, NAME FROM USERS WHERE ID = ?",
            "SELECT COUNT(*) AS C FROM ORDERS WHERE STATUS = PAID",
            "INSERT INTO LOGS (LEVEL, MESSAGE) VALUES (INFO, OK)",
            "UPDATE USERS SET LAST_SEEN = NOW() WHERE ID = 42",
            "SELECT NAME FROM CATEGORIES WHERE PARENT_ID IS NULL",
            "SELECT A FROM T WHERE B IN (1, 2, 3)",
        ];

        let corpora: [&[&str]; 2] = [&attack_corpus, &benign_corpus];
        for corpus in corpora {
            for input in corpus {
                assert_eq!(
                    engine_contains(input),
                    legacy_contains(input),
                    "引擎与遗留规则表判定不一致: {input}"
                );
            }
        }

        // 块注释标记走管线早退分支(与遗留 contains_sql_injection 的前置检查同位),
        // 不参与匹配阶段语料对比(scan_relational 仅在 sql-parser 下编译)
        #[cfg(feature = "sql-parser")]
        assert!(
            !InjectionEngine::global()
                .scan_relational("SELECT /* HIDDEN */ 1")
                .is_empty()
        );
    }

    /// 去重剪枝正确性:被包含模式不存在独立命中
    #[test]
    fn test_dedup_pruned_rules_have_no_unique_matches() {
        let engine = InjectionEngine::global();
        // 关系型类别规则数 = 遗留表 72 条 - 剪除的 4 条冗余
        // ("; EXECUTE"/"EXEC xp_"/"EXECUTE xp_"/"ORDER BY 1#")
        // 共享的块注释标记规则(关系型早退分支与图管线复用)与图管线专属规则
        // (graph.* 前缀)不计入关系型匹配口径
        let relational_count = engine
            .rules
            .iter()
            .filter(|rule| !rule.id.starts_with("graph.") && rule.id != "comment.block_marker")
            .filter(|rule| {
                matches!(
                    rule.category,
                    RuleCategory::Union
                        | RuleCategory::BooleanBlind
                        | RuleCategory::TimeBlind
                        | RuleCategory::DynamicExec
                        | RuleCategory::FileOps
                        | RuleCategory::InfoLeak
                        | RuleCategory::Encoding
                        | RuleCategory::Stacked
                        | RuleCategory::Comment
                        | RuleCategory::Other
                )
            })
            .count();
        assert_eq!(
            relational_count,
            LEGACY_RELATIONAL.len() - 4,
            "关系型规则应恰剪除 4 条冗余"
        );
        // "; EXECUTE" 场景仍由 "; EXEC" 命中
        assert!(engine_contains("SELECT 1; EXECUTE SP_X"));
        // "#" 场景仍命中("ORDER BY 1#" 冗余副本剪除后行为不变)
        assert!(engine_contains("SELECT 1 ORDER BY 1#"));
    }

    /// 命中结果携带稳定规则 ID 与类别(供审计消费)
    #[test]
    fn test_scan_reports_rule_ids_and_categories() {
        let engine = InjectionEngine::global();
        let findings = engine.scan("SELECT 1; DROP TABLE T", &[RuleCategory::Stacked]);
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].id, "stack.drop");
        assert_eq!(findings[0].category, RuleCategory::Stacked);
    }

    /// DDL 管线与图管线的独立可用性
    #[test]
    fn test_ddl_and_graph_pipelines() {
        let engine = InjectionEngine::global();
        let ddl = engine.scan_ddl("  drop database production  ");
        assert_eq!(ddl.len(), 1);
        assert_eq!(ddl[0].id, "ddl.drop_database");

        let graph = engine.scan_graph("MATCH (n) CALL dbms.components() YIELD * RETURN n");
        assert!(graph.iter().any(|r| r.id == "graph.call_dbms"));

        let comment = engine.scan_graph("MATCH (n) RETURN n /* sneaky */");
        assert!(comment.iter().any(|r| r.id == "comment.block_marker"));
    }

    #[cfg(feature = "sql-parser")]
    #[test]
    fn test_dynamic_variables_via_engine() {
        let engine = InjectionEngine::global();
        assert!(engine.has_dynamic_variables("SELECT * FROM t WHERE a = @var"));
        assert!(engine.has_dynamic_variables("SELECT ${col} FROM t"));
        assert!(!engine.has_dynamic_variables("SELECT * FROM t WHERE a = ?"));
    }
}