Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Validate social ID characters

Neeraj Gupta902d05a2add8
diff --git a/server/pkg/controller/social/id_helper.go b/server/pkg/controller/social/id_helper.go
index 55bcd52727..57e5115cac 100644
--- a/server/pkg/controller/social/id_helper.go
+++ b/server/pkg/controller/social/id_helper.go
@@ -32,6 +32,11 @@ func normalizeOrGenerateID(prefix, id string) (string, error) {
if len(id) != len(prefix)+nanoidSuffixLength {
return "", ente.ErrBadRequest
}
+ for _, ch := range id[len(prefix):] {
+ if !(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9' || ch == '_' || ch == '-') {
+ return "", ente.ErrBadRequest
+ }
+ }
return id, nil
}
diff --git a/server/pkg/controller/social/id_helper_test.go b/server/pkg/controller/social/id_helper_test.go
new file mode 100644
index 0000000000..14cc7fde1c
--- /dev/null
+++ b/server/pkg/controller/social/id_helper_test.go
@@ -0,0 +1,41 @@
+package social
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/ente/museum/ente"
+ "github.com/stretchr/testify/require"
+)
+
+func TestNormalizeSocialID(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ prefix string
+ normalize func(string) (string, error)
+ }{
+ {"comment", commentIDPrefix, NormalizeCommentID},
+ {"reaction", reactionIDPrefix, NormalizeReactionID},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ valid := tc.prefix + strings.Repeat("a", nanoidSuffixLength-2) + "_-"
+ actual, err := tc.normalize(valid)
+ require.NoError(t, err)
+ require.Equal(t, valid, actual)
+
+ generated, err := tc.normalize("")
+ require.NoError(t, err)
+ actual, err = tc.normalize(generated)
+ require.NoError(t, err)
+ require.Equal(t, generated, actual)
+
+ for _, invalid := range []string{
+ tc.prefix + "/../../family/leave?a",
+ tc.prefix + strings.Repeat("a", nanoidSuffixLength-3) + "%2F",
+ } {
+ _, err := tc.normalize(invalid)
+ require.ErrorIs(t, err, ente.ErrBadRequest)
+ }
+ })
+ }
+}