Skip to content
Snippets Groups Projects
Commit 7521afa6 authored by stratact's avatar stratact Committed by Michael Aaron Murphy
Browse files

:boom: :ambulance: Check non-alphanumeric variable names and first char for digits

parent 580009b3
No related branches found
No related tags found
No related merge requests found
...@@ -3,7 +3,7 @@ mod loops; ...@@ -3,7 +3,7 @@ mod loops;
pub(crate) mod pipelines; pub(crate) mod pipelines;
mod quotes; mod quotes;
pub(crate) mod shell_expand; pub(crate) mod shell_expand;
mod statement; pub(crate) mod statement;
pub use self::quotes::Terminator; pub use self::quotes::Terminator;
pub(crate) use self::{ pub(crate) use self::{
......
mod case; mod case;
mod functions; mod functions;
#[cfg(not(fuzzing))] #[cfg(not(fuzzing))]
mod parse; pub mod parse;
#[cfg(fuzzing)] #[cfg(fuzzing)]
pub mod parse; pub mod parse;
mod splitter; mod splitter;
......
...@@ -26,7 +26,13 @@ where ...@@ -26,7 +26,13 @@ where
} }
} }
fn is_valid_name(name: &str) -> bool { !name.chars().any(|c| !(c.is_alphanumeric() || c == '_')) } pub fn is_valid_name(name: &str) -> bool {
if !(name.as_bytes()[0] as char).is_numeric() {
!name.chars().any(|c| !(c.is_alphanumeric() || c == '_'))
} else {
false
}
}
pub(crate) fn parse(code: &str) -> Statement { pub(crate) fn parse(code: &str) -> Statement {
let cmd = code.trim(); let cmd = code.trim();
......
...@@ -5,7 +5,7 @@ use super::{ ...@@ -5,7 +5,7 @@ use super::{
}; };
use crate::{ use crate::{
lexers::assignments::{Key, Operator, Primitive}, lexers::assignments::{Key, Operator, Primitive},
parser::assignments::*, parser::{assignments::*, statement::parse::is_valid_name},
shell::{history::ShellHistory, variables::Value}, shell::{history::ShellHistory, variables::Value},
types, types,
}; };
...@@ -124,6 +124,10 @@ impl VariableStore for Shell { ...@@ -124,6 +124,10 @@ impl VariableStore for Shell {
// sanitize variable names // sanitize variable names
if ["HOME", "HOST", "PWD", "MWD", "SWD", "?"].contains(&key.name) { if ["HOME", "HOST", "PWD", "MWD", "SWD", "?"].contains(&key.name) {
Err(format!("not allowed to set `{}`", key.name)) Err(format!("not allowed to set `{}`", key.name))
} else if !is_valid_name(key.name) {
Err("invalid variable name\nVariable names may only have A-Z, a-z, 0-9 \
and _\nThe first character cannot be a digit"
.into())
} else { } else {
Ok((key, operator, expression)) Ok((key, operator, expression))
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment