pub struct SqlSecurity;Expand description
SQL identifier validation and escaping utilities.
Implementations§
Source§impl SqlSecurity
impl SqlSecurity
Sourcepub fn escape_identifier(identifier: &str) -> Result<String>
pub fn escape_identifier(identifier: &str) -> Result<String>
Validates and escapes a SQL identifier (table name, column name, etc.).
This function ensures that user-provided identifiers are safe to use in SQL queries by validating their format and properly escaping them.
§Arguments
identifier- The identifier to validate and escape
§Returns
Ok(String)- The safely escaped identifier ready for SQL useErr(TermError)- If the identifier is invalid or potentially malicious
§Security
This function prevents SQL injection by:
- Validating identifier format against allowed patterns
- Checking against a blocklist of dangerous patterns
- Properly escaping identifiers using double quotes
- Limiting identifier length to prevent DoS attacks
§Examples
use term_guard::security::SqlSecurity;
// Valid identifiers
assert!(SqlSecurity::escape_identifier("customer_id").is_ok());
assert!(SqlSecurity::escape_identifier("table1").is_ok());
// Invalid identifiers
assert!(SqlSecurity::escape_identifier("id; DROP TABLE users--").is_err());
assert!(SqlSecurity::escape_identifier(&"very_long_name_".repeat(100)).is_err());Sourcepub fn validate_identifier(identifier: &str) -> Result<()>
pub fn validate_identifier(identifier: &str) -> Result<()>
Validates a SQL identifier without escaping it.
This function checks if an identifier is safe to use but doesn’t escape it. Useful for cases where you need validation but will use the identifier in a different context.
Sourcepub fn validate_regex_pattern(pattern: &str) -> Result<String>
pub fn validate_regex_pattern(pattern: &str) -> Result<String>
Validates a regex pattern for safety.
This function ensures that user-provided regex patterns are safe to use in SQL queries and won’t cause ReDoS attacks or other security issues.
Sourcepub fn validate_sql_expression(expression: &str) -> Result<()>
pub fn validate_sql_expression(expression: &str) -> Result<()>
Validates a custom SQL expression for safety.
This function performs security validation on user-provided SQL expressions to prevent SQL injection and other attacks while still allowing legitimate validation expressions.
Auto Trait Implementations§
impl Freeze for SqlSecurity
impl RefUnwindSafe for SqlSecurity
impl Send for SqlSecurity
impl Sync for SqlSecurity
impl Unpin for SqlSecurity
impl UnwindSafe for SqlSecurity
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more