-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function safe wrappers and test file
- Loading branch information
Showing
4 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod bindings; | ||
mod wrapper; | ||
|
||
pub use wrapper::{sqli, xss}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use bindings; | ||
use std::ffi::CString; | ||
|
||
/// Checks `input` for SQL injection detection, and returns an option of (is_sqli, fingerprint) | ||
pub fn sqli(input: &str) -> Option<(bool, String)> { | ||
let fingerprint_cstring = CString::new("").ok()?; | ||
let fingerprint_raw_ptr = fingerprint_cstring.into_raw(); | ||
let input_cstring = CString::new(input).ok()?; | ||
let input_ptr = input_cstring.as_ptr(); | ||
let is_sqli = | ||
unsafe { bindings::libinjection_sqli(input_ptr, input.len(), fingerprint_raw_ptr) }; | ||
Some(( | ||
is_sqli == 1, | ||
unsafe { CString::from_raw(fingerprint_raw_ptr) } | ||
.into_string() | ||
.ok()?, | ||
)) | ||
} | ||
|
||
/// Checks `input` for XSS detection, and returns an option of is_xss | ||
pub fn xss(input: &str) -> Option<bool> { | ||
let input_cstring = CString::new(input).ok()?; | ||
let input_ptr = input_cstring.as_ptr(); | ||
let is_xss = unsafe { bindings::libinjection_xss(input_ptr, input.len()) }; | ||
Some(is_xss == 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
extern crate libinjection; | ||
|
||
use libinjection::{sqli, xss}; | ||
|
||
#[test] | ||
fn test_sqli() { | ||
let (is_sqli, fingerprint) = sqli("' OR '2'='2' --").unwrap(); | ||
assert!(is_sqli); | ||
assert_eq!("s&sos", fingerprint); | ||
|
||
let (is_sqli, fingerprint) = sqli("SELECT * FROM users").unwrap(); | ||
assert!(!is_sqli); | ||
assert!(fingerprint.is_empty()); | ||
} | ||
|
||
fn test_xss() { | ||
let is_xss = xss("<script type='text/javascript'>alert('xss');</script>").unwrap(); | ||
assert!(is_xss); | ||
|
||
let is_xss = xss("is not XSS").unwrap(); | ||
assert!(!is_xss); | ||
} |