Fix json parsing bug

Increase version for building a new Debian package.
This commit is contained in:
finga 2021-11-16 14:39:22 +01:00
parent 3a482a3eb9
commit 4b9186b10d
3 changed files with 42 additions and 17 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "webhookey"
version = "0.1.3"
version = "0.1.4"
authors = ["finga <webhookey@onders.org>"]
edition = "2021"
license = "GPL-3.0-or-later"

View file

@ -82,12 +82,35 @@ impl Hook {
&self,
hook_name: &str,
request: &Request,
data: &serde_json::Value,
data: &mut serde_json::Value,
) -> Result<String> {
trace!("Replacing parameters for command of hook `{}`", hook_name);
for parameter in self.get_parameter()? {
let parameter = parameter.trim();
if let Some(json_value) = data.pointer(parameter) {
*data.pointer_mut(parameter).ok_or_else(|| {
WebhookeyError::InvalidParameterPointer(parameter.to_string())
})? = serde_json::Value::String(webhooks::get_string(json_value)?);
}
}
replace_parameters(&self.command, request.headers(), data)
}
fn get_parameter(&self) -> Result<Vec<&str>> {
let parse: IResult<&str, Vec<&str>> = many0(alt((
delimited(tag("{{"), take_until("}}"), tag("}}")),
take_until("{{"),
)))(&self.command);
let (_last, result) = parse
.finish()
.map_err(|e| anyhow!("Could not get parameters from command: {}", e))?;
Ok(result)
}
}
#[derive(Debug)]
@ -142,11 +165,11 @@ impl Hooks {
valid = true;
let data: serde_json::Value =
let mut data: serde_json::Value =
serde_json::from_slice(&buffer).map_err(WebhookeyError::Serde)?;
match hook.filter.evaluate(&data) {
Ok(true) => match hook.get_command(hook_name, request, &data) {
Ok(true) => match hook.get_command(hook_name, request, &mut data) {
Ok(command) => {
info!("Filter for `{}` matched", &hook_name);
result.insert(hook_name.to_string(), command);

View file

@ -14,6 +14,8 @@ pub enum WebhookeyError {
Unauthorized(IpAddr),
#[error("Unmatched hook from `{0}`")]
UnmatchedHook(IpAddr),
#[error("Could not find field refered to in parameter `{0}`")]
InvalidParameterPointer(String),
#[error("Could not evaluate filter request")]
InvalidFilter,
#[error("IO Error")]
@ -74,7 +76,7 @@ impl JsonFilter {
let regex = Regex::new(&self.regex).map_err(WebhookeyError::Regex)?;
if let Some(value) = data.pointer(&self.pointer) {
if regex.is_match(&self.get_string(value)?) {
if regex.is_match(&get_string(value)?) {
debug!("Regex `{}` for `{}` matches", &self.regex, &self.pointer);
return Ok(true);
@ -88,18 +90,6 @@ impl JsonFilter {
Ok(false)
}
fn get_string(&self, data: &serde_json::Value) -> Result<String, WebhookeyError> {
match &data {
serde_json::Value::Bool(bool) => Ok(bool.to_string()),
serde_json::Value::Number(number) => Ok(number.to_string()),
serde_json::Value::String(string) => Ok(string.as_str().to_string()),
x => {
error!("Could not get string from: {:?}", x);
unimplemented!()
}
}
}
}
#[derive(Debug, Deserialize, Serialize)]
@ -161,3 +151,15 @@ impl FilterType {
}
}
}
pub fn get_string(data: &serde_json::Value) -> Result<String, WebhookeyError> {
match &data {
serde_json::Value::Bool(bool) => Ok(bool.to_string()),
serde_json::Value::Number(number) => Ok(number.to_string()),
serde_json::Value::String(string) => Ok(string.as_str().to_string()),
x => {
error!("Could not get string from: {:?}", x);
unimplemented!()
}
}
}