nyx-scanner 0.6.1

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
//! Constraint solver: apply conditions to [`PathEnv`] and check satisfiability.
//!
//! The solver operates on structured [`ConditionExpr`] values, never on raw
//! text. Negation is always structural (via [`ConditionExpr::negate`] /
//! [`CompOp::negate`]), not via a generic "negate ValueFact" operation.

use crate::ssa::type_facts::TypeKind;

use super::domain::{BoolState, ConstValue, Nullability, PathEnv, RelOp, TypeSet, ValueFact};
use super::lower::{CompOp, ConditionExpr, Operand};

/// Apply a condition to a [`PathEnv`], producing the refined environment
/// for the branch where the condition has the given polarity.
///
/// `polarity = true`: condition holds (true branch).
/// `polarity = false`: condition does NOT hold (false branch), negate
/// the condition structurally, then apply.
pub fn refine_env(env: &PathEnv, cond: &ConditionExpr, polarity: bool) -> PathEnv {
    if env.is_unsat() {
        return env.clone();
    }

    let effective = if polarity {
        cond.clone()
    } else {
        cond.negate()
    };

    let mut result = env.clone();
    apply_condition(&mut result, &effective);
    result
}

/// Check if a [`PathEnv`] is satisfiable.
///
/// Unsatisfiability is detected incrementally during [`PathEnv::refine`],
/// so this is just a flag check.
pub fn is_satisfiable(env: &PathEnv) -> bool {
    !env.is_unsat()
}

// ── Internal dispatch ───────────────────────────────────────────────────

fn apply_condition(env: &mut PathEnv, cond: &ConditionExpr) {
    match cond {
        ConditionExpr::NullCheck { var, is_null } => {
            let mut fact = ValueFact::top();
            if *is_null {
                fact.null = Nullability::Null;
                fact.types = TypeSet::singleton(&TypeKind::Null);
            } else {
                fact.null = Nullability::NonNull;
            }
            env.refine(*var, &fact);
        }

        ConditionExpr::TypeCheck {
            var,
            type_name,
            positive,
        } => {
            if let Some(kind) = parse_type_name(type_name) {
                let ts = TypeSet::singleton(&kind);
                let mut fact = ValueFact::top();
                if *positive {
                    fact.types = ts;
                    if kind != TypeKind::Null {
                        fact.null = Nullability::NonNull;
                    }
                } else {
                    fact.types = ts.complement();
                }
                env.refine(*var, &fact);
            }
            // Unknown type name → no refinement (conservative)
        }

        ConditionExpr::BoolTest { var } => {
            // Conservative: only refine NonNull for known boolean-typed values.
            // Truthiness is language-specific (0, "", empty containers are
            // falsy in some languages). Over-constraining would be unsound.
            //
            // We check the existing fact: if the value is already known
            // boolean-typed, we can safely refine to True.
            let existing = env.get(*var);
            if existing.types == TypeSet::singleton(&TypeKind::Bool) {
                let mut fact = ValueFact::top();
                fact.bool_state = BoolState::True;
                fact.null = Nullability::NonNull;
                env.refine(*var, &fact);
            }
            // Otherwise: no refinement (conservative)
        }

        ConditionExpr::Comparison { lhs, op, rhs } => {
            apply_comparison(env, lhs, *op, rhs);
        }

        ConditionExpr::Unknown => {
            // No information, no refinement
        }
    }
}

fn apply_comparison(env: &mut PathEnv, lhs: &Operand, op: CompOp, rhs: &Operand) {
    match (lhs, rhs) {
        (Operand::Value(v), Operand::Const(c)) => {
            apply_value_const(env, *v, op, c);
        }
        (Operand::Const(c), Operand::Value(v)) => {
            // Flip: const op var → var (flipped_op) const
            apply_value_const(env, *v, op.flip(), c);
        }
        (Operand::Value(a), Operand::Value(b)) => match op {
            CompOp::Eq => env.assert_equal(*a, *b),
            CompOp::Neq => env.assert_not_equal(*a, *b),
            CompOp::Lt => env.assert_relational(*a, RelOp::Lt, *b),
            CompOp::Gt => env.assert_relational(*b, RelOp::Lt, *a),
            CompOp::Le => env.assert_relational(*a, RelOp::Le, *b),
            CompOp::Ge => env.assert_relational(*b, RelOp::Le, *a),
        },
        // At least one Unknown operand: no refinement
        _ => {}
    }
}

/// Apply a value-vs-constant comparison to the environment.
fn apply_value_const(env: &mut PathEnv, v: crate::ssa::ir::SsaValue, op: CompOp, c: &ConstValue) {
    let mut fact = ValueFact::top();

    match op {
        CompOp::Eq => {
            fact.exact = Some(c.clone());
            match c {
                ConstValue::Int(i) => {
                    fact.lo = Some(*i);
                    fact.hi = Some(*i);
                    fact.types = TypeSet::singleton(&TypeKind::Int);
                    fact.null = Nullability::NonNull;
                }
                ConstValue::Null => {
                    fact.null = Nullability::Null;
                    fact.types = TypeSet::singleton(&TypeKind::Null);
                }
                ConstValue::Bool(b) => {
                    fact.bool_state = if *b {
                        BoolState::True
                    } else {
                        BoolState::False
                    };
                    fact.types = TypeSet::singleton(&TypeKind::Bool);
                    fact.null = Nullability::NonNull;
                }
                ConstValue::Str(_) => {
                    fact.types = TypeSet::singleton(&TypeKind::String);
                    fact.null = Nullability::NonNull;
                }
            }
        }
        CompOp::Neq => {
            if c == &ConstValue::Null {
                fact.null = Nullability::NonNull;
            }
            fact.excluded.push(c.clone());
        }
        CompOp::Lt => {
            if let ConstValue::Int(i) = c {
                fact.hi = Some(*i);
                fact.hi_strict = true;
                fact.null = Nullability::NonNull;
            }
            // Non-Int Lt: no refinement (V1)
        }
        CompOp::Le => {
            if let ConstValue::Int(i) = c {
                fact.hi = Some(*i);
                fact.null = Nullability::NonNull;
            }
        }
        CompOp::Gt => {
            if let ConstValue::Int(i) = c {
                fact.lo = Some(*i);
                fact.lo_strict = true;
                fact.null = Nullability::NonNull;
            }
        }
        CompOp::Ge => {
            if let ConstValue::Int(i) = c {
                fact.lo = Some(*i);
                fact.null = Nullability::NonNull;
            }
        }
    }

    env.refine(v, &fact);
}

/// Map typeof / type-name strings to [`TypeKind`].
///
/// Resolution order:
/// 1. Cross-language primitive aliases (case-insensitive)
/// 2. Java/Ruby/Go class and framework names (case-sensitive)
/// 3. Java type hierarchy fallback (case-sensitive, via [`crate::ssa::type_facts::TypeHierarchy`])
pub fn parse_type_name(name: &str) -> Option<TypeKind> {
    use crate::ssa::type_facts::TypeHierarchy;

    primitive_type_alias(name)
        .or_else(|| class_name_to_type_kind(name))
        .or_else(|| TypeHierarchy::resolve_kind(name))
}

/// Tier 1: Cross-language primitive type aliases (case-insensitive).
fn primitive_type_alias(name: &str) -> Option<TypeKind> {
    match name.to_ascii_lowercase().as_str() {
        "string" | "str" => Some(TypeKind::String),
        "number" | "int" | "integer" | "i32" | "i64" | "u32" | "u64" | "float" | "double"
        | "numeric" => Some(TypeKind::Int),
        "boolean" | "bool" => Some(TypeKind::Bool),
        "object" => Some(TypeKind::Object),
        "array" | "list" => Some(TypeKind::Array),
        "null" | "nil" | "none" | "undefined" => Some(TypeKind::Null),
        _ => None,
    }
}

/// Tier 2: Java/Ruby/Go class and framework type names (case-sensitive).
pub fn class_name_to_type_kind(name: &str) -> Option<TypeKind> {
    match name {
        "String" | "CharSequence" | "StringBuilder" | "StringBuffer" => Some(TypeKind::String),
        "Integer" | "Long" | "Short" | "Byte" | "Number" | "BigInteger" | "BigDecimal"
        | "Double" | "Float" => Some(TypeKind::Int),
        "Boolean" => Some(TypeKind::Bool),
        "List" | "ArrayList" | "Collection" | "Set" | "HashSet" => Some(TypeKind::Array),
        "URL" | "URI" => Some(TypeKind::Url),
        // Framework HTTP clients, also listed in JAVA_HIERARCHY (type_facts.rs)
        // for subtype resolution. Both locations needed: this function is called
        // directly by the constraint solver, while the hierarchy provides
        // is_subtype_of() for instanceof checks.
        "HttpClient" | "CloseableHttpClient" | "OkHttpClient" | "WebClient"
        | "RestTemplate" => Some(TypeKind::HttpClient),
        "HttpServletResponse" | "HttpResponse" | "ServletResponse"
        // Spring HTTP response
        | "ResponseEntity" => {
            Some(TypeKind::HttpResponse)
        }
        "Connection" | "DataSource" | "MongoClient"
        // JDBC statement types (execute SQL, same suppression semantics)
        | "Statement" | "PreparedStatement" => Some(TypeKind::DatabaseConnection),
        "File" | "Path"
        // Java I/O supertypes (enables hierarchy fallback for subtypes)
        | "InputStream" | "OutputStream" | "Reader" | "Writer" | "PrintWriter"
        | "BufferedInputStream" | "BufferedOutputStream" => Some(TypeKind::FileHandle),
        // Python qualified type names.
        // Only covers raw lowered names from isinstance(). The lowering in lower.rs
        // extracts the literal type text: isinstance(x, requests.Session) produces
        // type_name = "requests.Session". Does NOT handle import aliasing
        // (e.g., `from requests import Session as S` → "S" is not resolved).
        "requests.Response" | "http.client.HTTPResponse" | "urllib3.response.HTTPResponse"
        | "httpx.Response" | "aiohttp.ClientResponse" => Some(TypeKind::HttpResponse),
        "requests.Session" | "urllib3.PoolManager" | "aiohttp.ClientSession"
        | "httpx.Client" | "httpx.AsyncClient" => Some(TypeKind::HttpClient),
        "sqlite3.Connection" | "psycopg2.connection" | "mysql.connector.connection"
        | "pymongo.MongoClient" | "redis.Redis" => Some(TypeKind::DatabaseConnection),
        "io.TextIOWrapper" | "io.BufferedReader" | "io.BufferedWriter" | "io.FileIO"
        | "io.BytesIO" | "io.StringIO" => Some(TypeKind::FileHandle),
        _ => None,
    }
}

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

    // ── parse_type_name tier 1 (primitive aliases) ───────────────────────

    #[test]
    fn parse_numeric_to_int() {
        // PHP-style "numeric" → Int via tier 1 primitive alias
        assert_eq!(parse_type_name("numeric"), Some(TypeKind::Int));
    }

    #[test]
    fn parse_string_case_insensitive() {
        // Tier 1 lowercase "string" matches "String" via case-insensitive
        assert_eq!(parse_type_name("String"), Some(TypeKind::String));
    }

    // ── parse_type_name tier 2 (class names) ────────────────────────────

    #[test]
    fn parse_integer_class_name() {
        // Java boxed class "Integer" → Int via tier 2
        assert_eq!(parse_type_name("Integer"), Some(TypeKind::Int));
    }

    #[test]
    fn parse_http_servlet_response() {
        // Java framework class → HttpResponse via tier 2
        assert_eq!(
            parse_type_name("HttpServletResponse"),
            Some(TypeKind::HttpResponse)
        );
    }

    // ── parse_type_name tier 3 (hierarchy fallback) ─────────────────────

    #[test]
    fn parse_closeable_http_client_via_hierarchy() {
        // CloseableHttpClient is in tier 2 class_name_to_type_kind directly
        assert_eq!(
            parse_type_name("CloseableHttpClient"),
            Some(TypeKind::HttpClient)
        );
    }

    #[test]
    fn parse_file_input_stream_via_hierarchy() {
        // FileInputStream: not in tier 1 or tier 2 directly.
        // Tier 3 hierarchy: FileInputStream → supertypes ["InputStream"].
        // "InputStream" IS in tier 2 → FileHandle.
        assert_eq!(
            parse_type_name("FileInputStream"),
            Some(TypeKind::FileHandle)
        );
    }

    // ── Java I/O and JDBC types ─────────────────────────────────────────

    #[test]
    fn parse_java_statement_to_db() {
        assert_eq!(
            parse_type_name("Statement"),
            Some(TypeKind::DatabaseConnection)
        );
        assert_eq!(
            parse_type_name("PreparedStatement"),
            Some(TypeKind::DatabaseConnection)
        );
    }

    #[test]
    fn parse_java_io_stream_to_file_handle() {
        assert_eq!(parse_type_name("InputStream"), Some(TypeKind::FileHandle));
        assert_eq!(parse_type_name("OutputStream"), Some(TypeKind::FileHandle));
        assert_eq!(parse_type_name("Reader"), Some(TypeKind::FileHandle));
        assert_eq!(parse_type_name("Writer"), Some(TypeKind::FileHandle));
        assert_eq!(parse_type_name("PrintWriter"), Some(TypeKind::FileHandle));
    }

    #[test]
    fn parse_java_response_entity() {
        assert_eq!(
            parse_type_name("ResponseEntity"),
            Some(TypeKind::HttpResponse)
        );
    }

    // ── Python qualified type names ─────────────────────────────────────

    #[test]
    fn parse_python_qualified_http_client() {
        assert_eq!(
            parse_type_name("requests.Session"),
            Some(TypeKind::HttpClient)
        );
        assert_eq!(
            parse_type_name("aiohttp.ClientSession"),
            Some(TypeKind::HttpClient)
        );
        assert_eq!(parse_type_name("httpx.Client"), Some(TypeKind::HttpClient));
        assert_eq!(
            parse_type_name("httpx.AsyncClient"),
            Some(TypeKind::HttpClient)
        );
        assert_eq!(
            parse_type_name("urllib3.PoolManager"),
            Some(TypeKind::HttpClient)
        );
    }

    #[test]
    fn parse_python_qualified_db_connection() {
        assert_eq!(
            parse_type_name("sqlite3.Connection"),
            Some(TypeKind::DatabaseConnection)
        );
        assert_eq!(
            parse_type_name("psycopg2.connection"),
            Some(TypeKind::DatabaseConnection)
        );
        assert_eq!(
            parse_type_name("mysql.connector.connection"),
            Some(TypeKind::DatabaseConnection)
        );
    }

    #[test]
    fn parse_python_qualified_http_response() {
        assert_eq!(
            parse_type_name("requests.Response"),
            Some(TypeKind::HttpResponse)
        );
        assert_eq!(
            parse_type_name("httpx.Response"),
            Some(TypeKind::HttpResponse)
        );
        assert_eq!(
            parse_type_name("aiohttp.ClientResponse"),
            Some(TypeKind::HttpResponse)
        );
    }

    #[test]
    fn parse_python_qualified_file_handle() {
        assert_eq!(
            parse_type_name("io.TextIOWrapper"),
            Some(TypeKind::FileHandle)
        );
        assert_eq!(parse_type_name("io.BytesIO"), Some(TypeKind::FileHandle));
        assert_eq!(parse_type_name("io.StringIO"), Some(TypeKind::FileHandle));
    }
}