Skip to content

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

feat: enhance Windows local account rotation with username validation and update schemas

Victor Santos1c9ae22d192b
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 49875b710d..91317cb54c 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
@@ -8,7 +8,6 @@ import {
} from "@app/ee/services/secret-rotation-v2/secret-rotation-v2-schemas";
import { PasswordRequirementsSchema } from "@app/ee/services/secret-rotation-v2/shared/general";
import { SecretRotations } from "@app/lib/api-docs";
-import { CharacterType, characterValidator } from "@app/lib/validator/validate-string";
import { SecretNameSchema } from "@app/server/lib/schemas";
import { AppConnection } from "@app/services/app-connection/app-connection-enums";
@@ -26,14 +25,17 @@ export const WindowsLocalAccountRotationGeneratedCredentialsSchema = z
.min(1)
.max(2);
+const WINDOWS_USERNAME_REGEX = /^[a-zA-Z0-9_.-]+$/;
+
const WindowsLocalAccountRotationParametersSchema = z.object({
username: z
.string()
.trim()
.min(1, "Username required")
+ .max(256, "Username too long")
.refine(
- (val) => characterValidator([CharacterType.AlphaNumeric, CharacterType.Hyphen, CharacterType.Underscore])(val),
- "Name can only contain alphanumeric characters, dashes, underscores, and spaces"
+ (val) => WINDOWS_USERNAME_REGEX.test(val),
+ "Username can only contain alphanumeric characters, underscores, hyphens, and periods"
)
.describe(SecretRotations.PARAMETERS.WINDOWS_LOCAL_ACCOUNT.username),
passwordRequirements: PasswordRequirementsSchema.optional(),
diff --git a/backend/src/lib/smb-rpc/index.ts b/backend/src/lib/smb-rpc/index.ts
index 213ad81f71..a603eba5ce 100644
--- a/backend/src/lib/smb-rpc/index.ts
+++ b/backend/src/lib/smb-rpc/index.ts
@@ -1 +1,6 @@
-export { changeWindowsPassword, SmbRpcConfig, verifyWindowsCredentials } from "./smb-rpc-client";
+export {
+ changeWindowsPassword,
+ isValidWindowsUsername,
+ SmbRpcConfig,
+ verifyWindowsCredentials
+} from "./smb-rpc-client";
diff --git a/backend/src/lib/smb-rpc/smb-rpc-client.ts b/backend/src/lib/smb-rpc/smb-rpc-client.ts
index f2f41f50a3..656d2a0bc7 100644
--- a/backend/src/lib/smb-rpc/smb-rpc-client.ts
+++ b/backend/src/lib/smb-rpc/smb-rpc-client.ts
@@ -19,21 +19,22 @@ const buildTarget = (config: SmbRpcConfig): string => {
/**
* Helper to build common auth args for SMB commands
+ * Uses --stdin to read password from stdin (avoids exposing password in process listings)
*/
const buildAuthArgs = (config: SmbRpcConfig): string[] => {
- const userCredential = config.domain
- ? `${config.domain}\\${config.adminUser}%${config.adminPassword}`
- : `${config.adminUser}%${config.adminPassword}`;
+ const userCredential = config.domain ? `${config.domain}\\${config.adminUser}` : config.adminUser;
- return ["-U", userCredential, ...SMB3_SECURITY_OPTIONS];
+ return ["-U", userCredential, "--stdin", ...SMB3_SECURITY_OPTIONS];
};
/**
* Execute a command and return stdout/stderr
+ * Optionally writes data to stdin (used for password input)
*/
const executeCommand = (
command: string,
- args: string[]
+ args: string[],
+ stdinData?: string
): Promise<{ stdout: string; stderr: string; exitCode: number }> => {
return new Promise((resolve) => {
const proc = spawn(command, args, {
@@ -52,13 +53,19 @@ const executeCommand = (
});
proc.on("close", (code) => {
- resolve({ stdout, stderr, exitCode: code ?? 0 });
+ resolve({ stdout, stderr, exitCode: code ?? 1 });
});
proc.on("error", (err) => {
stderr += err.message;
resolve({ stdout, stderr, exitCode: 1 });
});
+
+ // Write password to stdin if provided
+ if (stdinData !== undefined) {
+ proc.stdin.write(`${stdinData}\n`);
+ proc.stdin.end();
+ }
});
};
@@ -101,7 +108,7 @@ const getNtStatusMessage = (ntStatus: string): string => {
const testSmbConnection = async (config: SmbRpcConfig): Promise<void> => {
const args = ["-L", buildTarget(config), ...buildAuthArgs(config)];
- const { stdout, stderr, exitCode } = await executeCommand("smbclient", args);
+ const { stdout, stderr, exitCode } = await executeCommand("smbclient", args, config.adminPassword);
const combinedOutput = stdout + stderr;
const ntStatus = parseNtStatusError(combinedOutput);
@@ -122,6 +129,21 @@ const escapePasswordForRpc = (password: string): string => {
return password.replace(/'/g, "'\\''");
};
+/**
+ * Windows username validation regex
+ * Allows alphanumeric, underscore, hyphen, and period
+ * Windows usernames cannot contain: " / \ [ ] : ; | = , + * ? < >
+ */
+const WINDOWS_USERNAME_REGEX = /^[a-zA-Z0-9_.-]+$/;
+const MAX_USERNAME_LENGTH = 256;
+
+/**
+ * Validate that a username contains only safe characters to prevent command injection
+ */
+export const isValidWindowsUsername = (username: string): boolean => {
+ return WINDOWS_USERNAME_REGEX.test(username) && username.length > 0 && username.length <= MAX_USERNAME_LENGTH;
+};
+
/**
* Change a Windows local account password using rpcclient
* Uses setuserinfo2 command with level 23 (password reset)
@@ -131,12 +153,18 @@ export const changeWindowsPassword = async (
targetUser: string,
newPassword: string
): Promise<void> => {
+ if (!isValidWindowsUsername(targetUser)) {
+ throw new Error(
+ "Invalid username format - only alphanumeric characters, underscores, hyphens, and periods allowed"
+ );
+ }
+
const escapedPassword = escapePasswordForRpc(newPassword);
const rpcCommand = `setuserinfo2 ${targetUser} 23 '${escapedPassword}'`;
const args = [buildTarget(config), ...buildAuthArgs(config), "-c", rpcCommand];
- const { stdout, stderr, exitCode } = await executeCommand("rpcclient", args);
+ const { stdout, stderr, exitCode } = await executeCommand("rpcclient", args, config.adminPassword);
const combinedOutput = stdout + stderr;
const ntStatus = parseNtStatusError(combinedOutput);
diff --git a/frontend/src/components/secret-rotations-v2/ReconcileLocalAccountRotationModal.tsx b/frontend/src/components/secret-rotations-v2/ReconcileLocalAccountRotationModal.tsx
index aa03fdbe82..c651eed6b7 100644
--- a/frontend/src/components/secret-rotations-v2/ReconcileLocalAccountRotationModal.tsx
+++ b/frontend/src/components/secret-rotations-v2/ReconcileLocalAccountRotationModal.tsx
@@ -50,7 +50,7 @@ const Content = ({ secretRotation, onComplete }: ContentProps) => {
<div>
<p className="mb-4 text-sm text-mineshaft-200">
Reconciliation ensures the password stored in Infisical matches the actual password on the
- SSH server.
+ server.
</p>
<p className="mb-8 text-sm text-mineshaft-200">
Use this if you suspect the credentials are out of sync, for example after a failed rotation
diff --git a/frontend/src/components/secret-rotations-v2/forms/schemas/windows-local-account-rotation-schema.ts b/frontend/src/components/secret-rotations-v2/forms/schemas/windows-local-account-rotation-schema.ts
index fdafe5060a..0f4e864b71 100644
--- a/frontend/src/components/secret-rotations-v2/forms/schemas/windows-local-account-rotation-schema.ts
+++ b/frontend/src/components/secret-rotations-v2/forms/schemas/windows-local-account-rotation-schema.ts
@@ -5,11 +5,21 @@ import { PasswordRequirementsSchema } from "@app/components/secret-rotations-v2/
import { SecretRotation } from "@app/hooks/api/secretRotationsV2";
import { WindowsLocalAccountRotationMethod } from "@app/hooks/api/secretRotationsV2/types/windows-local-account-rotation";
+const WINDOWS_USERNAME_REGEX = /^[a-zA-Z0-9_.-]+$/;
+
export const WindowsLocalAccountRotationSchema = z
.object({
type: z.literal(SecretRotation.WindowsLocalAccount),
parameters: z.object({
- username: z.string().trim().min(1, "Username required"),
+ username: z
+ .string()
+ .trim()
+ .min(1, "Username required")
+ .max(256, "Username too long")
+ .refine(
+ (val) => WINDOWS_USERNAME_REGEX.test(val),
+ "Username can only contain alphanumeric characters, underscores, hyphens, and periods"
+ ),
passwordRequirements: PasswordRequirementsSchema.optional(),
rotationMethod: z.nativeEnum(WindowsLocalAccountRotationMethod).optional()
}),