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
//!
//! ## API Guide
//!
//! ### For End Users
//!
//! Most applications should use the high-level detection functions:
//!
//! - [`detect_sqli`] - Main SQL injection detection (recommended)
//! - [`detect_sqli_with_flags`] - SQL injection detection with custom flags
//! - [`detect_xss`] - Cross-site scripting detection
//! - [`version`] - Library version information
//!
//! These functions handle all the complexity of testing multiple contexts and
//! SQL dialects automatically, returning simple results.
//!
//! ### For Advanced Users and Debugging
//!
//! For debugging, performance analysis, or advanced customization, you can access
//! the lower-level APIs:
//!
//! - [`SqliState`] - Direct access to SQL parsing state and tokenization
//! - [`XssDetector`] - Direct XSS detection with context control
//! - [`Fingerprint`] - SQL injection fingerprint analysis
//!
//! These APIs expose the internal parsing state, tokens, and folding mechanisms
//! that power the detection logic. They are primarily intended for:
//!
//! - **Debugging**: Understanding why certain inputs are flagged
//! - **Performance**: Avoiding repeated parsing for multiple checks
//! - **Research**: Analyzing the tokenization and folding process
//! - **Testing**: Validating behavior against the C reference implementation
//!
//! Most applications should **not** use these lower-level APIs unless they have
//! specific requirements that the high-level functions cannot meet.
extern crate alloc;
use fmt;
use Error as StdError;
// Re-export types for advanced usage
pub use ;
pub use ;
/// The type of injection detected by libinjection.
/// Result of an injection detection operation.
///
/// This structure contains information about whether injection was detected,
/// what type it was, and additional metadata like the fingerprint.
// Fingerprint is now exported from sqli module
/// Detects SQL injection in the given input using default flags.
///
/// This is the main entry point for SQL injection detection. It tests the input
/// in multiple contexts (no quotes, single quotes, double quotes) and with both
/// ANSI and MySQL SQL modes to maximize detection accuracy.
///
/// # Arguments
///
/// * `input` - The byte slice to analyze for SQL injection
///
/// # Returns
///
/// Returns a [`DetectionResult`] indicating whether injection was detected,
/// along with the fingerprint if it was SQL injection.
///
/// # Examples
///
/// ```
/// use libinjectionrs::detect_sqli;
///
/// // Safe input
/// let result = detect_sqli(b"hello world");
/// assert!(!result.is_injection());
///
/// // SQL injection
/// let result = detect_sqli(b"1' OR '1'='1");
/// assert!(result.is_injection());
/// ```
/// Detects SQL injection in the given input with specific parsing flags.
///
/// This function allows you to customize the SQL parsing behavior by specifying
/// flags for quote context and SQL dialect. However, for most use cases,
/// [`detect_sqli`] is recommended as it automatically tests multiple contexts.
///
/// # Arguments
///
/// * `input` - The byte slice to analyze for SQL injection
/// * `flags` - Parsing flags to control quote context and SQL dialect
///
/// # Returns
///
/// Returns a [`DetectionResult`] indicating whether injection was detected.
///
/// # Examples
///
/// ```
/// use libinjectionrs::{detect_sqli_with_flags, SqliFlags};
///
/// let result = detect_sqli_with_flags(
/// b"1' OR '1'='1",
/// SqliFlags::FLAG_QUOTE_SINGLE | SqliFlags::FLAG_SQL_ANSI
/// );
/// assert!(result.is_injection());
/// ```
/// Detects Cross-Site Scripting (XSS) in the given input.
///
/// This function analyzes the input for XSS vectors by parsing it in multiple
/// HTML contexts (data state, unquoted attributes, single/double quoted attributes,
/// etc.) to detect potentially malicious HTML, JavaScript, or other markup.
///
/// # Arguments
///
/// * `input` - The byte slice to analyze for XSS
///
/// # Returns
///
/// Returns an [`XssResult`] indicating whether XSS was detected.
///
/// # Examples
///
/// ```
/// use libinjectionrs::detect_xss;
///
/// // Safe input
/// let result = detect_xss(b"Hello, world!");
/// assert!(!result.is_injection());
///
/// // XSS vector
/// let result = detect_xss(b"<script>alert('xss')</script>");
/// assert!(result.is_injection());
/// ```
/// Returns the version of the libinjection library.
///
/// # Examples
///
/// ```
/// use libinjectionrs::version;
///
/// println!("libinjection version: {}", version());
/// ```