Fix newline escaping in line protocol (#3992)
(cherry picked from commit e4f8a82ee6
)
This commit is contained in:
parent
5f0fbc7d30
commit
b5babc3f00
File diff suppressed because it is too large
Load Diff
|
@ -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;
|
||||
|
|
|
@ -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`),
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue