Skip to content

Commit

Permalink
Add function safe wrappers and test file
Browse files Browse the repository at this point in the history
  • Loading branch information
yaa110 committed Oct 24, 2018
1 parent 08e88b8 commit 4e040a4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
clean:
rm -rf libinjection
rm -f src/bindings.rs
cargo clean

fix-python:
sed -i 's/python$$/python2/g' libinjection/src/*.py
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod bindings;
mod wrapper;

pub use wrapper::{sqli, xss};
26 changes: 26 additions & 0 deletions src/wrapper.rs
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)
}
22 changes: 22 additions & 0 deletions tests/lib_test.rs
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);
}

0 comments on commit 4e040a4

Please sign in to comment.