aprimadi / influxdb2

Influxdb2 client library for Rust
MIT License
44 stars 39 forks source link

Strings aren't escaped properly in the derived `WriteDataPoint` #50

Open stegaBOB opened 10 months ago

stegaBOB commented 10 months ago

Strings that contain quotes aren't properly escaped and cause writes to fail.

Minimal example

#[derive(Default, WriteDataPoint)]
#[measurement = "quote_strings"]
struct QuoteStrings {
    #[influxdb(tag)]
    string_tag: String,
    #[influxdb(field)]
    string_field: String,
    #[influxdb(timestamp)]
    time: i64,
}

expands to:

impl ::influxdb2::models::WriteDataPoint for QuoteStrings {
    fn write_data_point_to<W>(&self, mut w: W) -> std::io::Result<()> where W: std::io::Write {
        w.write_all(format!("{},", "quote_strings").as_bytes())?;
        w.write_all(format!("{}", "string_tag").as_bytes())?;
        w.write_all(b"=")?;
        w.write_all(<String as ::influxdb2::writable::KeyWritable>::encode_key(&self.string_tag).into_bytes().as_slice())?;
        w.write_all(b" ")?;
        w.write_all(format!("{}", "string_field").as_bytes())?;
        w.write_all(b"=")?;
        w.write_all(<String as ::influxdb2::writable::ValueWritable>::encode_value(&self.string_field).into_bytes().as_slice())?;
        w.write_all(b" ")?;
        w.write_all(<i64 as ::influxdb2::writable::TimestampWritable>::encode_timestamp(&self.time).into_bytes().as_slice())?;
        w.write_all(b"\n")?;
        Ok(())
    }
}

Currently, the KeyWritable just clones the string, and the ValueWritable implementation formats the string with escaped quotes surrounding it. Both would result in errors if the values contain quotes.

https://docs.influxdata.com/influxdb/v1/write_protocols/line_protocol_reference/#special-characters Looks like there's a few other characters that need escaping too.

I'd be happy to add a fix for this and open a PR some time next week

aprimadi commented 10 months ago

@stegaBOB thank you for reporting this. Pull request is always welcome