Skip to content

Commit

Permalink
Merge pull request #122 from K-Phoen/fix-long-uid
Browse files Browse the repository at this point in the history
Prevent long UIDs from being used
  • Loading branch information
K-Phoen authored Jul 26, 2021
2 parents 92a793a + a7e5f26 commit bdd7ad6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
15 changes: 14 additions & 1 deletion dashboard/dashboard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package dashboard

import (
// We're not using it for security stuff, so it's fine.
//nolint:gosec
"crypto/sha1"
"encoding/hex"
"encoding/json"

"github.com/K-Phoen/grabana/row"
Expand Down Expand Up @@ -120,7 +124,16 @@ func ID(id uint) Option {
// UID sets the UID used by the dashboard.
func UID(uid string) Option {
return func(builder *Builder) {
builder.board.UID = uid
validUID := uid

if len(uid) > 40 {
// We're not using it for security stuff, so it's fine.
//nolint:gosec
sha := sha1.Sum([]byte(uid))
validUID = hex.EncodeToString(sha[:])
}

builder.board.UID = validUID
}
}

Expand Down
10 changes: 10 additions & 0 deletions dashboard/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ func TestDashboardUIDCanBeSet(t *testing.T) {
req.Equal("foo", panel.board.UID)
}

func TestDashboardUIDWillBeHashedWhenTooLongForGrafana(t *testing.T) {
req := require.New(t)

originalUID := "this-uid-is-more-than-forty-characters-and-grafana-does-not-like-it"
panel := New("", UID(originalUID))

req.NotEqual(originalUID, panel.board.UID)
req.Len(panel.board.UID, 40)
}

func TestDashboardCanBeMadeReadOnly(t *testing.T) {
req := require.New(t)

Expand Down

0 comments on commit bdd7ad6

Please sign in to comment.