Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions src/stdlib/core.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::helpers::check_arg_count;
use crate::interpreter::environment::Environment;
use crate::interpreter::error::RuntimeError;
use crate::interpreter::value::Value;
Expand All @@ -15,26 +16,14 @@ pub fn native_print(args: Vec<Value>) -> Result<Value, RuntimeError> {
}

pub fn native_typeof(args: Vec<Value>) -> Result<Value, RuntimeError> {
if args.len() != 1 {
return Err(RuntimeError::new(
format!("typeof expects 1 argument, got {}", args.len()),
0,
0,
));
}
check_arg_count("typeof", &args, 1)?;

let type_name = args[0].type_name();
Ok(Value::Text(Rc::from(type_name)))
}

pub fn native_isnothing(args: Vec<Value>) -> Result<Value, RuntimeError> {
if args.len() != 1 {
return Err(RuntimeError::new(
format!("isnothing expects 1 argument, got {}", args.len()),
0,
0,
));
}
check_arg_count("isnothing", &args, 1)?;

match &args[0] {
Value::Null => Ok(Value::Bool(true)),
Expand Down
85 changes: 20 additions & 65 deletions src/stdlib/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::helpers::{check_arg_count, expect_text};
use crate::interpreter::environment::Environment;
use crate::interpreter::error::RuntimeError;
use crate::interpreter::value::Value;
Expand Down Expand Up @@ -417,20 +418,10 @@ fn bytes_to_hex(bytes: &[u8]) -> String {

/// WFLHASH-256 implementation with security fixes
pub fn native_wflhash256(args: Vec<Value>) -> Result<Value, RuntimeError> {
if args.len() != 1 {
return Err(RuntimeError::new(
"Invalid argument count".to_string(),
0,
0,
));
}
check_arg_count("wflhash256", &args, 1)?;

let input = match &args[0] {
Value::Text(text) => text.as_bytes(),
_ => {
return Err(RuntimeError::new("Invalid argument type".to_string(), 0, 0));
}
};
let text = expect_text(&args[0])?;
let input = text.as_bytes();

let params = WflHashParams::new(32); // 256 bits = 32 bytes
let hash_bytes = wflhash_core_text(input, &params)?; // Validate UTF-8 for text
Expand All @@ -441,20 +432,10 @@ pub fn native_wflhash256(args: Vec<Value>) -> Result<Value, RuntimeError> {

/// WFLHASH-512 implementation with security fixes
pub fn native_wflhash512(args: Vec<Value>) -> Result<Value, RuntimeError> {
if args.len() != 1 {
return Err(RuntimeError::new(
"Invalid argument count".to_string(),
0,
0,
));
}
check_arg_count("wflhash512", &args, 1)?;

let input = match &args[0] {
Value::Text(text) => text.as_bytes(),
_ => {
return Err(RuntimeError::new("Invalid argument type".to_string(), 0, 0));
}
};
let text = expect_text(&args[0])?;
let input = text.as_bytes();

let params = WflHashParams::new(64); // 512 bits = 64 bytes
let hash_bytes = wflhash_core_text(input, &params)?; // Validate UTF-8 for text
Expand All @@ -465,27 +446,13 @@ pub fn native_wflhash512(args: Vec<Value>) -> Result<Value, RuntimeError> {

/// WFLHASH-256 with personalization/salt support
pub fn native_wflhash256_with_salt(args: Vec<Value>) -> Result<Value, RuntimeError> {
if args.len() != 2 {
return Err(RuntimeError::new(
"Invalid argument count".to_string(),
0,
0,
));
}
check_arg_count("wflhash256_with_salt", &args, 2)?;

let input = match &args[0] {
Value::Text(text) => text.as_bytes(),
_ => {
return Err(RuntimeError::new("Invalid argument type".to_string(), 0, 0));
}
};
let text = expect_text(&args[0])?;
let input = text.as_bytes();

let salt = match &args[1] {
Value::Text(text) => text.as_bytes(),
_ => {
return Err(RuntimeError::new("Invalid argument type".to_string(), 0, 0));
}
};
let salt_text = expect_text(&args[1])?;
let salt = salt_text.as_bytes();

let params = WflHashParams::new_with_personalization(32, salt);
let hash_bytes = wflhash_core_text(input, &params)?;
Expand All @@ -497,27 +464,13 @@ pub fn native_wflhash256_with_salt(args: Vec<Value>) -> Result<Value, RuntimeErr
/// WFLHASH-256 with key for MAC functionality (WFLMAC-256)
/// Now uses proper HKDF key derivation for enhanced security
pub fn native_wflmac256(args: Vec<Value>) -> Result<Value, RuntimeError> {
if args.len() != 2 {
return Err(RuntimeError::new(
"Invalid argument count".to_string(),
0,
0,
));
}
check_arg_count("wflmac256", &args, 2)?;

let input = match &args[0] {
Value::Text(text) => text.as_bytes(),
_ => {
return Err(RuntimeError::new("Invalid argument type".to_string(), 0, 0));
}
};
let text = expect_text(&args[0])?;
let input = text.as_bytes();

let key = match &args[1] {
Value::Text(text) => text.as_bytes(),
_ => {
return Err(RuntimeError::new("Invalid argument type".to_string(), 0, 0));
}
};
let key_text = expect_text(&args[1])?;
let key = key_text.as_bytes();

// Use proper key derivation with error handling
let params = WflHashParams::new_with_key(32, key)?;
Expand Down Expand Up @@ -557,7 +510,9 @@ pub fn wflmac256_verify(

/// Generate a cryptographically secure random token (for CSRF, sessions, etc.)
/// Usage: generate_csrf_token() -> "a1b2c3d4e5f6..."
pub fn native_generate_csrf_token(_args: Vec<Value>) -> Result<Value, RuntimeError> {
pub fn native_generate_csrf_token(args: Vec<Value>) -> Result<Value, RuntimeError> {
check_arg_count("generate_csrf_token", &args, 0)?;
Comment on lines +513 to +514

Copilot AI Feb 4, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function signature changed from using an unused parameter prefix (_args) to a regular parameter (args), which is good. However, the validation is being added for a function that takes 0 arguments. While this makes the validation explicit, consider whether this check is necessary for zero-argument functions since it adds minimal value—calling with arguments would be caught at the call site in most cases.

Copilot uses AI. Check for mistakes.

use rand::RngCore;

// Generate 32 random bytes (256 bits)
Expand Down
Loading