Skip to content

Commit

Permalink
Escape friendly name double quote (#84)
Browse files Browse the repository at this point in the history
* escape friendly name

* Updated README
  • Loading branch information
MindFlavor authored Nov 3, 2021
1 parent 5b709b1 commit 72d3f73
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prometheus_wireguard_exporter"
version = "3.6.0"
version = "3.6.1"
authors = ["Francesco Cogno <[email protected]>"]
description = "Prometheus WireGuard Exporter"
edition = "2018"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ A Prometheus exporter for [WireGuard](https://www.wireguard.com), written in Rus

## Changelog

* **BREAKING** From release [3.6.0](https://github.com/MindFlavor/prometheus_wireguard_exporter/releases/tag/3.6.0) the exporter takes fallback configuration values from the environment variables. Thanks to [j_r0dd](https://github.com/jr0dd) for the idea. This changes how the exporter evaluates the command line parameters: make sure to consult the documentation on how to convert your command line to the new format. Basically every switch (for example verbose `-v`) not expect values, either `true` or `false`. This is necessary because there is no way to discriminate between an empty environment variable and one that has not been set.
* From release [3.6.1](https://github.com/MindFlavor/prometheus_wireguard_exporter/releases/tag/3.6.1) the exporter correctly escapes the double quotes in `friendly_name`. Thanks to [Steven Wood](https://github.com/stvnw) for finding the bug in #82.
* **BREAKING** From version `3.6.0` the exporter takes fallback configuration values from the environment variables. Thanks to [j_r0dd](https://github.com/jr0dd) for the idea. This changes how the exporter evaluates the command line parameters: make sure to consult the documentation on how to convert your command line to the new format. Basically every switch (for example verbose `-v`) not expect values, either `true` or `false`. This is necessary because there is no way to discriminate between an empty environment variable and one that has not been set.
* From release [3.5.1](https://github.com/MindFlavor/prometheus_wireguard_exporter/releases/tag/3.5.1) the exporter supports multiple peer files. Thanks to [Tobias Krischer](https://github.com/tobikris) for the idea.
* From release [3.5.0](https://github.com/MindFlavor/prometheus_wireguard_exporter/releases/tag/3.5.0) the exporter supports the `friendly_json` tag. Entries prepended with the `friendly_json` tag will output all the entries in the specificed json as Prometheus attributes. Thanks to [DrProxyProSupport](https://github.com/iqdoctor) for the idea.
* From release [3.4.1](https://github.com/MindFlavor/prometheus_wireguard_exporter/releases/tag/3.4.0) the exporter supports prepending `sudo` to the `wg` command. This allows to run the exporter as a non root user (although sudoer without password). Thanks to [Jonas Seydel](https://github.com/Thor77) for the idea.
Expand Down
22 changes: 21 additions & 1 deletion src/friendly_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<'a> TryFrom<(&'a str, &'a str)> for FriendlyDescription<'a> {

fn try_from((header_name, value): (&'a str, &'a str)) -> Result<Self, Self::Error> {
Ok(match header_name {
"friendly_name" => FriendlyDescription::Name(value.into()),
"friendly_name" => FriendlyDescription::Name(value.replace("\"", "\\\"").into()),
"friendly_json" => {
let ret: HashMap<&str, serde_json::Value> = serde_json::from_str(value)?;
FriendlyDescription::Json(ret)
Expand All @@ -29,3 +29,23 @@ impl<'a> TryFrom<(&'a str, &'a str)> for FriendlyDescription<'a> {
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::convert::TryInto;

#[test]
fn test_no_escape_friendly_name() {
let fd: FriendlyDescription = ("friendly_name", "no escaping").try_into().unwrap();
assert_eq!(fd, FriendlyDescription::Name("no escaping".into()));
}

#[test]
fn test_escape_friendly_name() {
const TO_ESCAPE: &str = r#"man this is a quote ""#;
const ESCAPED: &str = r#"man this is a quote \""#;
let fd: FriendlyDescription = ("friendly_name", TO_ESCAPE).try_into().unwrap();
assert_eq!(fd, FriendlyDescription::Name(ESCAPED.into()));
}
}

0 comments on commit 72d3f73

Please sign in to comment.