Fix newline escaping in line protocol (#3992)

This commit is contained in:
Daniel Nelson 2018-04-09 15:29:52 -07:00 committed by GitHub
parent a28de4b5cd
commit e4f8a82ee6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 5302 additions and 5084 deletions

File diff suppressed because it is too large Load Diff

View File

@ -142,7 +142,7 @@ fieldfloat =
fieldinteger =
(integer 'i') >begin %integer;
fieldunsigned =
fieldunsigned =
(unsigned 'u') >begin %unsigned;
false =
@ -155,7 +155,7 @@ fieldbool =
(true | false) >begin %bool;
fieldstringchar =
[^\\"] | '\\' [\\"];
[^\n\f\r\\"] | '\\' [\\"];
fieldstring =
fieldstringchar* >begin %string;

View File

@ -1154,6 +1154,22 @@ var tests = []struct {
},
},
},
{
name: "invalid newline in string field",
input: []byte("cpu value=\"4\n2\""),
results: []Result{
Result{
Name: Measurement,
Value: []byte("cpu"),
},
Result{
err: ErrFieldParse,
},
Result{
err: ErrFieldParse,
},
},
},
{
name: "invalid field value",
input: []byte(`cpu value=howdy`),

View File

@ -3,30 +3,42 @@ package influx
import "strings"
const (
escapes = " ,="
nameEscapes = " ,"
stringFieldEscapes = `\"`
escapes = "\t\n\f\r ,="
nameEscapes = "\t\n\f\r ,"
stringFieldEscapes = "\t\n\f\r\\\""
)
var (
escaper = strings.NewReplacer(
"\t", `\t`,
"\n", `\n`,
"\f", `\f`,
"\r", `\r`,
`,`, `\,`,
`"`, `\"`, // ???
` `, `\ `,
`=`, `\=`,
)
nameEscaper = strings.NewReplacer(
"\t", `\t`,
"\n", `\n`,
"\f", `\f`,
"\r", `\r`,
`,`, `\,`,
` `, `\ `,
)
stringFieldEscaper = strings.NewReplacer(
"\t", `\t`,
"\n", `\n`,
"\f", `\f`,
"\r", `\r`,
`"`, `\"`,
`\`, `\\`,
)
)
// Escape a tagkey, tagvalue, or fieldkey
func escape(s string) string {
if strings.ContainsAny(s, escapes) {
return escaper.Replace(s)
@ -35,6 +47,7 @@ func escape(s string) string {
}
}
// Escape a measurement name
func nameEscape(s string) string {
if strings.ContainsAny(s, nameEscapes) {
return nameEscaper.Replace(s)
@ -43,6 +56,7 @@ func nameEscape(s string) string {
}
}
// Escape a string field
func stringFieldEscape(s string) string {
if strings.ContainsAny(s, stringFieldEscapes) {
return stringFieldEscaper.Replace(s)

View File

@ -261,6 +261,50 @@ var tests = []struct {
),
output: []byte("cpu abc=123i 1519194109000000042\ncpu def=456i 1519194109000000042\n"),
},
{
name: "name newline",
input: MustMetric(
metric.New(
"c\npu",
map[string]string{},
map[string]interface{}{
"value": 42,
},
time.Unix(0, 0),
),
),
output: []byte("c\\npu value=42i 0\n"),
},
{
name: "tag newline",
input: MustMetric(
metric.New(
"cpu",
map[string]string{
"host": "x\ny",
},
map[string]interface{}{
"value": 42,
},
time.Unix(0, 0),
),
),
output: []byte("cpu,host=x\\ny value=42i 0\n"),
},
{
name: "string newline",
input: MustMetric(
metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"value": "x\ny",
},
time.Unix(0, 0),
),
),
output: []byte("cpu value=\"x\\ny\" 0\n"),
},
{
name: "need more space",
maxBytes: 32,