Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: validate role resource id (#10237)

p0ny465b2513e210
diff --git a/backend/api/v1/resource_id.go b/backend/api/v1/resource_id.go
new file mode 100644
index 0000000000..da49430e9d
--- /dev/null
+++ b/backend/api/v1/resource_id.go
@@ -0,0 +1,17 @@
+package v1
+
+import (
+ "regexp"
+
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
+)
+
+var resourceIDRegex = regexp.MustCompile(`^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$`)
+
+func validateResourceID(resourceID string) error {
+ if !resourceIDRegex.MatchString(resourceID) {
+ return status.Errorf(codes.InvalidArgument, "invalid resource ID %q", resourceID)
+ }
+ return nil
+}
diff --git a/backend/api/v1/resource_id_test.go b/backend/api/v1/resource_id_test.go
new file mode 100644
index 0000000000..fbd36591a6
--- /dev/null
+++ b/backend/api/v1/resource_id_test.go
@@ -0,0 +1,54 @@
+package v1
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestValidateResourceID(t *testing.T) {
+ a := require.New(t)
+
+ testCases := []struct {
+ resourceID string
+ wantErr bool
+ }{
+ {
+ resourceID: "",
+ wantErr: true,
+ },
+ {
+ resourceID: "a",
+ wantErr: false,
+ },
+ {
+ resourceID: "A",
+ wantErr: true,
+ },
+ {
+ resourceID: "a-1",
+ wantErr: false,
+ },
+ {
+ resourceID: "a-1-",
+ wantErr: true,
+ },
+ {
+ resourceID: "1a",
+ wantErr: true,
+ },
+ {
+ resourceID: "environment-ds8gr3lx",
+ wantErr: false,
+ },
+ }
+
+ for _, tc := range testCases {
+ err := validateResourceID(tc.resourceID)
+ if tc.wantErr {
+ a.Error(err)
+ } else {
+ a.NoError(err)
+ }
+ }
+}
diff --git a/backend/api/v1/role_service.go b/backend/api/v1/role_service.go
index 2e0e099115..f953390425 100644
--- a/backend/api/v1/role_service.go
+++ b/backend/api/v1/role_service.go
@@ -54,6 +54,11 @@ func (s *RoleService) CreateRole(ctx context.Context, request *v1pb.CreateRoleRe
if !ok {
return nil, status.Errorf(codes.Internal, "principal ID not found")
}
+
+ if err := validateResourceID(request.RoleId); err != nil {
+ return nil, err
+ }
+
create := &store.RoleMessage{
ResourceID: request.RoleId,
Name: request.Role.Title,