Skip to main content

validate_method_name

Function validate_method_name 

Source
pub fn validate_method_name(method_name: &str) -> Result<(), String>
Expand description

Validates a JSON-RPC method name for security and correctness

This function prevents DoS attacks through method name validation by enforcing:

  • Maximum length of 255 characters to prevent resource exhaustion
  • Only allowed characters: alphanumeric (a-z, A-Z, 0-9), dot (.), underscore (_), hyphen (-)
  • No control characters (0x00-0x1F, 0x7F) to prevent injection attacks
  • No leading or trailing whitespace to ensure proper formatting

§Arguments

  • method_name - The method name to validate

§Returns

  • Ok(()) - If the method name is valid
  • Err(String) - If the method name is invalid with a descriptive error message

§Example

use spikard_http::jsonrpc::validate_method_name;

// Valid method names
assert!(validate_method_name("user.getById").is_ok());
assert!(validate_method_name("calculate_sum").is_ok());
assert!(validate_method_name("api-v1-handler").is_ok());
assert!(validate_method_name("rpc1").is_ok());

// Invalid method names
assert!(validate_method_name("").is_err());  // Empty
assert!(validate_method_name(" method").is_err());  // Leading whitespace
assert!(validate_method_name("method ").is_err());  // Trailing whitespace
assert!(validate_method_name("method\x00name").is_err());  // Control character
assert!(validate_method_name("a".repeat(256)).is_err());  // Too long