Skip to content

Commit

Permalink
Properly escape json strings.
Browse files Browse the repository at this point in the history
Although most likely not officially supported, we might end up with some
weird metric names in the DB. icinga2 is a good candidate for a source
of broken metrics.

Add a simple test like and marshal the strings properly.
  • Loading branch information
bzed committed Dec 4, 2023
1 parent 2935529 commit 0d93b24
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -81,12 +82,13 @@ func (i *Index) WriteJSON(w http.ResponseWriter) error {
continue
}

quote := []byte{'"'}
json_b, err := json.Marshal(string(b))
if err != nil {
return err
}
jsonParts := [][]byte{
nil,
quote,
b,
quote,
json_b,
}
if idx != 0 {
jsonParts[0] = []byte{','}
Expand Down
3 changes: 2 additions & 1 deletion index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ func TestWriteJSONNonleafRows(t *testing.T) {
"testing.leaf",
"testing.nonleaf.",
"testing.leaf.node",
"testing.\"broken\".node",
}
metrics, err := writeRows(rows)
if err != nil {
t.Fatalf("Error during transform or unmarshal: %s", err)
}
if len(metrics) != 2 {
if len(metrics) != 3 {
t.Fatalf("Wrong metrics slice length = %d: %s", len(metrics), metrics)
}
if metrics[0] != "testing.leaf" || metrics[1] != "testing.leaf.node" {
Expand Down

0 comments on commit 0d93b24

Please sign in to comment.