fix(clients): reject spaces, '/', '\' and control chars in subscription ID

Like the client email, the subId is embedded directly in subscription
URLs, so the same characters break it. Validate it on the backend
(Create + Update) and the frontend (Zod), with a localized message
across all 13 locales. An empty subId stays allowed (it is then
auto-generated).
This commit is contained in:
MHSanaei committed 2026-05-30 23:28:58 +02:00 Unverified
1 parent a0865a67fd
commit 2fa7be86dc
16 files changed
+64 -6

No files matched your search

+3 -3
View File
@@ -119,7 +119,7 @@ export const GroupSummarySchema = z.object({
export const GroupSummaryListSchema = z.array(GroupSummarySchema).nullable().transform((v) => v ?? []);
export function emailHasForbiddenChars(value: string): boolean {
export function hasForbiddenClientChars(value: string): boolean {
if (value.includes('/') || value.includes('\\') || value.includes(' ')) return true;
for (let i = 0; i < value.length; i++) {
const code = value.charCodeAt(i);
@@ -133,8 +133,8 @@ export const ClientFormSchema = z.object({
.string()
.trim()
.min(1, 'pages.clients.email')
.refine((v) => !emailHasForbiddenChars(v), 'pages.clients.emailInvalidChars'),
subId: z.string(),
.refine((v) => !hasForbiddenClientChars(v), 'pages.clients.emailInvalidChars'),
subId: z.string().refine((v) => !hasForbiddenClientChars(v), 'pages.clients.subIdInvalidChars'),
uuid: z.string(),
password: z.string(),
auth: z.string(),
+23 -3
View File
@@ -408,12 +408,26 @@ type ClientCreatePayload struct {
InboundIds []int `json:"inboundIds"`
}
func validateClientEmail(email string) error {
for _, r := range email {
func hasForbiddenClientChar(s string) bool {
for _, r := range s {
if r == '/' || r == '\\' || r == ' ' || r < 0x20 || r == 0x7f {
return common.NewError("client email contains an invalid character:", email)
return true
}
}
return false
}
func validateClientEmail(email string) error {
if hasForbiddenClientChar(email) {
return common.NewError("client email contains an invalid character:", email)
}
return nil
}
func validateClientSubID(subID string) error {
if hasForbiddenClientChar(subID) {
return common.NewError("client subId contains an invalid character:", subID)
}
return nil
}
@@ -428,6 +442,9 @@ func (s *ClientService) Create(inboundSvc *InboundService, payload *ClientCreate
if err := validateClientEmail(client.Email); err != nil {
return false, err
}
if err := validateClientSubID(client.SubID); err != nil {
return false, err
}
if len(payload.InboundIds) == 0 {
return false, common.NewError("at least one inbound is required")
}
@@ -596,6 +613,9 @@ func (s *ClientService) Update(inboundSvc *InboundService, id int, updated model
if err := validateClientEmail(updated.Email); err != nil {
return false, err
}
if err := validateClientSubID(updated.SubID); err != nil {
return false, err
}
if updated.SubID == "" {
updated.SubID = existing.SubID
}
@@ -30,3 +30,28 @@ func TestValidateClientEmail(t *testing.T) {
}
}
}
func TestValidateClientSubID(t *testing.T) {
valid := []string{
"",
"abc123",
"sub-id_value",
}
for _, subID := range valid {
if err := validateClientSubID(subID); err != nil {
t.Errorf("validateClientSubID(%q) = %v, want nil", subID, err)
}
}
invalid := []string{
"a/b",
"with space",
"back\\slash",
"new\nline",
}
for _, subID := range invalid {
if err := validateClientSubID(subID); err == nil {
t.Errorf("validateClientSubID(%q) = nil, want error", subID)
}
}
}
Loaded 3 of 16 files, more files were not shown because too many files have changed in this diff. Show more