Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor: consolidate and enhance validation rules for Windows local account and SMB connection schemas to improve security and clarity

Victor Santosd71ccf20379a
diff --git a/backend/src/ee/services/secret-rotation-v2/windows-local-account-rotation/windows-local-account-rotation-schemas.ts b/backend/src/ee/services/secret-rotation-v2/windows-local-account-rotation/windows-local-account-rotation-schemas.ts
index d220f94ad6..f590ccfff8 100644
--- a/backend/src/ee/services/secret-rotation-v2/windows-local-account-rotation/windows-local-account-rotation-schemas.ts
+++ b/backend/src/ee/services/secret-rotation-v2/windows-local-account-rotation/windows-local-account-rotation-schemas.ts
@@ -26,27 +26,25 @@ export const WindowsLocalAccountRotationGeneratedCredentialsSchema = z
.min(1)
.max(2);
+// Username validation for Windows local accounts
+const validateWindowsUsername = characterValidator([
+ CharacterType.AlphaNumeric,
+ CharacterType.Hyphen,
+ CharacterType.Underscore,
+ CharacterType.Period
+]);
+
const WindowsLocalAccountRotationParametersSchema = z.object({
username: z
.string()
.trim()
.min(1, "Username required")
.max(20, "Username too long - Windows local accounts are limited to 20 characters")
- .refine(
- (val) =>
- characterValidator([
- CharacterType.AlphaNumeric,
- CharacterType.Hyphen,
- CharacterType.Underscore,
- CharacterType.Period
- ])(val),
- "Username can only contain alphanumeric characters, underscores, hyphens, and periods"
- )
- .refine((val) => !val.startsWith("."), {
- message: "Username cannot start with a period"
+ .refine((val) => validateWindowsUsername(val), {
+ message: "Username can only contain alphanumeric characters, underscores, hyphens, and periods"
})
- .refine((val) => !val.startsWith("-"), {
- message: "Username cannot start with a hyphen"
+ .refine((val) => !val.startsWith("-") && !val.startsWith(".") && !val.endsWith("."), {
+ message: "Username cannot start with a hyphen or period, and cannot end with a period"
})
.describe(SecretRotations.PARAMETERS.WINDOWS_LOCAL_ACCOUNT.username),
passwordRequirements: PasswordRequirementsSchema.optional(),
diff --git a/backend/src/lib/smb-rpc/smb-rpc-client.ts b/backend/src/lib/smb-rpc/smb-rpc-client.ts
index 7209a3a334..05affb3091 100644
--- a/backend/src/lib/smb-rpc/smb-rpc-client.ts
+++ b/backend/src/lib/smb-rpc/smb-rpc-client.ts
@@ -60,11 +60,8 @@ const validateHost = (host: string): void => {
if (!validateHostname(host)) {
throw new Error("Host can only contain alphanumeric characters, dots, and hyphens");
}
- if (host.startsWith("-")) {
- throw new Error("Host cannot start with a hyphen");
- }
- if (host.startsWith(".")) {
- throw new Error("Host cannot start with a period");
+ if (host.startsWith("-") || host.startsWith(".")) {
+ throw new Error("Host cannot start with a hyphen or period");
}
};
@@ -80,8 +77,8 @@ const validateDomainInput = (domain: string | undefined): void => {
if (!validateDomain(domain)) {
throw new Error("Domain can only contain alphanumeric characters, dots, hyphens, and underscores");
}
- if (domain.startsWith("-")) {
- throw new Error("Domain cannot start with a hyphen");
+ if (domain.startsWith("-") || domain.startsWith(".")) {
+ throw new Error("Domain cannot start with a hyphen or period");
}
};
@@ -107,11 +104,8 @@ const validateAdminUsername = (username: string): void => {
if (!validateUsername(username)) {
throw new Error("Admin username can only contain alphanumeric characters, underscores, hyphens, and periods");
}
- if (username.startsWith("-")) {
- throw new Error("Admin username cannot start with a hyphen");
- }
- if (username.startsWith(".")) {
- throw new Error("Admin username cannot start with a period");
+ if (username.startsWith("-") || username.startsWith(".") || username.endsWith(".")) {
+ throw new Error("Admin username cannot start with a hyphen or period, and cannot end with a period");
}
};
@@ -291,6 +285,7 @@ const escapePasswordForRpc = (password: string): string => {
* Validate that a target username contains only safe characters to prevent command injection
* Windows local account username validation:
* - Cannot start with a period or hyphen (prevents flag injection and Windows rules)
+ * - Cannot end with a period (Windows rule)
* - Can contain alphanumeric, underscore, hyphen, and period
* - Max 20 characters for local accounts
*/
@@ -298,8 +293,8 @@ export const isValidWindowsUsername = (username: string): boolean => {
if (!username || username.length === 0 || username.length > MAX_USERNAME_LENGTH) {
return false;
}
- // Cannot start with period or hyphen (period is Windows rule, hyphen prevents flag injection)
- if (username.startsWith(".") || username.startsWith("-")) {
+ // Cannot start with period or hyphen, cannot end with period
+ if (username.startsWith(".") || username.startsWith("-") || username.endsWith(".")) {
return false;
}
return validateUsername(username);
@@ -318,7 +313,7 @@ export const changeWindowsPassword = async (
if (!isValidWindowsUsername(targetUser)) {
throw new Error(
- "Invalid username format - must be 1-20 characters, only alphanumeric characters, underscores, hyphens, and periods allowed, and cannot start with a period or hyphen"
+ "Invalid username format - must be 1-20 characters, only alphanumeric characters, underscores, hyphens, and periods allowed, and cannot start or end with a period or start with a hyphen"
);
}
diff --git a/backend/src/services/app-connection/smb/smb-connection-schemas.ts b/backend/src/services/app-connection/smb/smb-connection-schemas.ts
index 30adbac564..14d52bde59 100644
--- a/backend/src/services/app-connection/smb/smb-connection-schemas.ts
+++ b/backend/src/services/app-connection/smb/smb-connection-schemas.ts
@@ -12,11 +12,11 @@ import {
import { APP_CONNECTION_NAME_MAP } from "../app-connection-maps";
import { SmbConnectionMethod } from "./smb-connection-enums";
-// Hostname validation: alphanumeric, dots, hyphens (cannot start with hyphen)
+// Hostname validation: alphanumeric, dots, hyphens (cannot start with hyphen or period)
// Supports: hostnames (server.domain.com), IPv4 (192.168.1.1)
const validateHostname = characterValidator([CharacterType.AlphaNumeric, CharacterType.Period, CharacterType.Hyphen]);
-// Domain validation: alphanumeric, dots, hyphens, underscores (cannot start with hyphen)
+// Domain validation: alphanumeric, dots, hyphens, underscores (cannot start with hyphen or period)
const validateDomain = characterValidator([
CharacterType.AlphaNumeric,
CharacterType.Period,
@@ -25,7 +25,7 @@ const validateDomain = characterValidator([
]);
// Username validation for SMB admin user
-// Windows usernames: alphanumeric, underscores, hyphens, periods (cannot start with period or hyphen)
+// Windows usernames: alphanumeric, underscores, hyphens, periods (cannot start with period or hyphen, cannot end with period)
const validateSmbUsername = characterValidator([
CharacterType.AlphaNumeric,
CharacterType.Hyphen,
@@ -35,7 +35,7 @@ const validateSmbUsername = characterValidator([
// Dangerous characters that could enable command/RPC injection
// These are blocked to prevent:
-// - Command separators: ; | &
+// - Command separators: ; | &
// - Command substitution: ` $ ( )
// - Newlines: \n \r (auth file directive injection)
// - Null bytes: \0 (string termination attacks)
@@ -54,11 +54,8 @@ export const SmbConnectionCredentialsSchema = z.object({
.refine((val) => validateHostname(val), {
message: "Host can only contain alphanumeric characters, dots, and hyphens"
})
- .refine((val) => !val.startsWith("-"), {
- message: "Host cannot start with a hyphen"
- })
- .refine((val) => !val.startsWith("."), {
- message: "Host cannot start with a period"
+ .refine((val) => !val.startsWith("-") && !val.startsWith("."), {
+ message: "Host cannot start with a hyphen or period"
})
.describe(AppConnections.CREDENTIALS.SMB.host),
port: z.coerce.number().int().min(1).max(65535).describe(AppConnections.CREDENTIALS.SMB.port),
@@ -69,8 +66,8 @@ export const SmbConnectionCredentialsSchema = z.object({
.refine((val) => val === "" || validateDomain(val), {
message: "Domain can only contain alphanumeric characters, dots, hyphens, and underscores"
})
- .refine((val) => val === "" || !val.startsWith("-"), {
- message: "Domain cannot start with a hyphen"
+ .refine((val) => val === "" || (!val.startsWith("-") && !val.startsWith(".")), {
+ message: "Domain cannot start with a hyphen or period"
})
.optional()
.describe(AppConnections.CREDENTIALS.SMB.domain),
@@ -82,16 +79,12 @@ export const SmbConnectionCredentialsSchema = z.object({
.refine((val) => validateSmbUsername(val), {
message: "Username can only contain alphanumeric characters, underscores, hyphens, and periods"
})
- .refine((val) => !val.startsWith("-"), {
- message: "Username cannot start with a hyphen"
- })
- .refine((val) => !val.startsWith("."), {
- message: "Username cannot start with a period"
+ .refine((val) => !val.startsWith("-") && !val.startsWith(".") && !val.endsWith("."), {
+ message: "Username cannot start with a hyphen or period, and cannot end with a period"
})
.describe(AppConnections.CREDENTIALS.SMB.username),
password: z
.string()
- .trim()
.min(1, "Password required")
.refine((val) => validateSmbPassword(val), {
message: "Password cannot contain the following characters: ; | & ` $ ( ) or newlines"
diff --git a/docs/documentation/platform/secret-rotation/windows-local-account.mdx b/docs/documentation/platform/secret-rotation/windows-local-account.mdx
index 5edf156b9b..6a15aca445 100644
--- a/docs/documentation/platform/secret-rotation/windows-local-account.mdx
+++ b/docs/documentation/platform/secret-rotation/windows-local-account.mdx
@@ -43,7 +43,7 @@ description: "Learn how to automatically rotate Windows local account passwords
- **Rotation Method** - The method to use when rotating the target user's password.
- **Login as Target** - Infisical will use the target user's current credentials to authenticate and rotate its own password.
- - **Login as Root** - Infisical will use the administrator credentials provided in the Windows App Connection to rotate the target user's password.
+ - **Login as Root** - Infisical will use the administrator credentials provided in the Windows Connection to rotate the target user's password.
- **Username** - The target Windows username whose password will be rotated.
- **Current Password** - The current password of the target user (required when **Rotation Method** is set to **Login as Target**).
- **Password Requirements** - The constraints to apply when generating new passwords.
@@ -68,7 +68,7 @@ description: "Learn how to automatically rotate Windows local account passwords
### Reconcile Windows Local Account
- If you suspect the credentials are out of sync (for example, after a manual password change on the server), you can regain access by using **Reconcile**. This will use the configured Windows Connection's administrator account to reset the target user's password and sync it with Infisical.
+ If you suspect the credentials are out of sync (for example, after a manual password change on the server), you can regain access by using **Reconcile**. This will use the configured Windows App Connection's administrator account to reset the target user's password and sync it with Infisical.
![Reconcile Option](/images/secret-rotations-v2/windows-local-account/windows-local-account-reconcile.png)
![Reconcile Confirmation](/images/secret-rotations-v2/windows-local-account/windows-local-account-reconcile-confirm.png)
diff --git a/frontend/src/pages/organization/AppConnections/AppConnectionsPage/components/AppConnectionForm/SmbConnectionForm.tsx b/frontend/src/pages/organization/AppConnections/AppConnectionsPage/components/AppConnectionForm/SmbConnectionForm.tsx
index 2a51856dbb..babceb029e 100644
--- a/frontend/src/pages/organization/AppConnections/AppConnectionsPage/components/AppConnectionForm/SmbConnectionForm.tsx
+++ b/frontend/src/pages/organization/AppConnections/AppConnectionsPage/components/AppConnectionForm/SmbConnectionForm.tsx
@@ -31,15 +31,62 @@ type Props = {
onSubmit: (formData: FormData) => Promise<void>;
};
+// Character validation regex patterns (must match backend validation)
+const HOSTNAME_REGEX = /^[a-zA-Z0-9.-]+$/;
+const DOMAIN_REGEX = /^[a-zA-Z0-9._-]+$/;
+const USERNAME_REGEX = /^[a-zA-Z0-9._-]+$/;
+
+// Dangerous characters that could enable command/RPC injection
+const DANGEROUS_PASSWORD_CHARS = [";", "|", "&", "`", "$", "(", ")", "\n", "\r", "\0"];
+
+const validatePassword = (password: string): boolean => {
+ return !DANGEROUS_PASSWORD_CHARS.some((char) => password.includes(char));
+};
+
const formSchema = genericAppConnectionFieldsSchema.extend({
app: z.literal(AppConnection.SMB),
method: z.literal(SmbConnectionMethod.Credentials),
credentials: z.object({
- host: z.string().trim().min(1, "Host required"),
+ host: z
+ .string()
+ .trim()
+ .min(1, "Host required")
+ .max(253, "Host too long")
+ .refine((val) => HOSTNAME_REGEX.test(val), {
+ message: "Host can only contain alphanumeric characters, dots, and hyphens"
+ })
+ .refine((val) => !val.startsWith("-") && !val.startsWith("."), {
+ message: "Host cannot start with a hyphen or period"
+ }),
port: z.coerce.number().int().min(1).max(65535, "Port must be between 1 and 65535"),
- domain: z.string().trim().optional(),
- username: z.string().trim().min(1, "Username required"),
- password: z.string().trim().min(1, "Password required")
+ domain: z
+ .string()
+ .trim()
+ .max(255, "Domain too long")
+ .refine((val) => val === "" || DOMAIN_REGEX.test(val), {
+ message: "Domain can only contain alphanumeric characters, dots, hyphens, and underscores"
+ })
+ .refine((val) => val === "" || (!val.startsWith("-") && !val.startsWith(".")), {
+ message: "Domain cannot start with a hyphen or period"
+ })
+ .optional(),
+ username: z
+ .string()
+ .trim()
+ .min(1, "Username required")
+ .max(104, "Username too long")
+ .refine((val) => USERNAME_REGEX.test(val), {
+ message: "Username can only contain alphanumeric characters, underscores, hyphens, and periods"
+ })
+ .refine((val) => !val.startsWith("-") && !val.startsWith(".") && !val.endsWith("."), {
+ message: "Username cannot start with a hyphen or period, and cannot end with a period"
+ }),
+ password: z
+ .string()
+ .min(1, "Password required")
+ .refine((val) => validatePassword(val), {
+ message: "Password cannot contain the following characters: ; | & ` $ ( ) or newlines"
+ })
})
});