Skip to content

Commit fa2d061

Browse files
fix: apply consistent input validation for env names
1 parent fcec138 commit fa2d061

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

backend/backend/graphene/mutations/environment.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from django.utils import timezone
23
from django.db.models import Max
34
from api.utils.rest import get_resolver_request_meta
@@ -10,7 +11,6 @@
1011
)
1112
from api.utils.audit_logging import log_secret_event
1213
from api.utils.secrets import create_environment_folder_structure, normalize_path_string
13-
1414
from backend.quotas import can_add_environment, can_use_custom_envs
1515
import graphene
1616
from graphql import GraphQLError
@@ -115,6 +115,11 @@ def mutate(
115115
"You don't have permission to create environments in this organisation"
116116
)
117117

118+
if not re.match(r"^[a-zA-Z0-9\-_]+$", environment_data.name):
119+
raise GraphQLError(
120+
"Environment name is invalid! Environment names can only includes letters, numbers, hyphens and underscores."
121+
)
122+
118123
if Environment.objects.filter(
119124
app=app, name__iexact=environment_data.name
120125
).exists():
@@ -209,6 +214,11 @@ def mutate(cls, root, info, environment_id, name):
209214
"Your Organisation doesn't have access to Custom Environments"
210215
)
211216

217+
if not re.match(r"^[a-zA-Z0-9\-_]+$", name):
218+
raise GraphQLError(
219+
"Environment name is invalid! Environment names can only includes letters, numbers, hyphens and underscores."
220+
)
221+
212222
if (
213223
Environment.objects.filter(app=environment.app, name__iexact=name)
214224
.exclude(id=environment_id)

frontend/components/environments/CreateEnvironmentDialog.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { toast } from 'react-toastify'
1414
import Spinner from '../common/Spinner'
1515
import { Alert } from '../common/Alert'
1616
import { UpsellDialog } from '../settings/organisation/UpsellDialog'
17+
import { sanitizeInput } from '@/utils/environment'
1718

1819
export const CreateEnvironmentDialog = (props: { appId: string }) => {
1920
const { activeOrganisation: organisation } = useContext(organisationContext)
@@ -66,7 +67,7 @@ export const CreateEnvironmentDialog = (props: { appId: string }) => {
6667
appData.sseEnabled ? appData.serverPublicKey : null
6768
)
6869

69-
await createEnvironment({
70+
const { data } = await createEnvironment({
7071
variables: {
7172
envInput: newEnvData.createEnvPayload,
7273
adminKeys: newEnvData.adminKeysPayload,
@@ -76,15 +77,17 @@ export const CreateEnvironmentDialog = (props: { appId: string }) => {
7677
refetchQueries: [{ query: GetAppEnvironments, variables: { appId: props.appId } }],
7778
})
7879

80+
if (!data) {
81+
return
82+
}
83+
7984
setName('')
8085

8186
toast.success('Environment created!')
8287

8388
closeModal()
8489
}
8590

86-
const sanitizeInput = (value: string) => value.replace(/[^a-zA-Z0-9]/g, '')
87-
8891
const closeModal = () => {
8992
if (dialogRef.current) {
9093
dialogRef.current.closeModal()

frontend/components/environments/ManageEnvironmentDialog.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { isCloudHosted } from '@/utils/appConfig'
2222
import { UpgradeRequestForm } from '../forms/UpgradeRequestForm'
2323
import { UpsellDialog } from '../settings/organisation/UpsellDialog'
2424
import { userHasPermission } from '@/utils/access/permissions'
25+
import { sanitizeInput } from '@/utils/environment'
2526

2627
const RenameEnvironment = (props: { environment: EnvironmentType }) => {
2728
const { activeOrganisation: organisation } = useContext(organisationContext)
@@ -65,7 +66,7 @@ const RenameEnvironment = (props: { environment: EnvironmentType }) => {
6566
: "You don't have the permissions required to rename this Environment"}
6667
</Alert>
6768
<Input
68-
value={name}
69+
value={sanitizeInput(name)}
6970
setValue={setName}
7071
label="Environment name"
7172
required

frontend/utils/environment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const sanitizeInput = (value: string) => value.replace(/[^a-zA-Z0-9\-_]/g, '')

0 commit comments

Comments
 (0)