Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stegaBOB opened this issue Jan 9, 2024 · 1 comment
Open

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

stegaBOB opened this issue Jan 9, 2024 · 1 comment

Comments

@stegaBOB
Copy link

stegaBOB commented Jan 9, 2024

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
Copy link
Owner

aprimadi commented Jan 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants