|
| 1 | +import path from "path" |
| 2 | + |
| 3 | +import { ZodSchema, z } from "zod" |
| 4 | +import { fromError } from "zod-validation-error" |
| 5 | + |
| 6 | +class OptionsError extends Error { |
| 7 | + constructor(message: string) { |
| 8 | + super(message) |
| 9 | + this.name = "OptionsError" |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * The options to use for path validation. |
| 15 | + * |
| 16 | + * @public |
| 17 | + */ |
| 18 | +interface PathValidatorOptions { |
| 19 | + /** |
| 20 | + * The base path to use for validation. This must be an absolute path. |
| 21 | + * |
| 22 | + * All provided paths, resolved relative to the working directory of the Node process, |
| 23 | + * must be within this directory (or its subdirectories), or they will be considered unsafe. |
| 24 | + * You should provide a safe base path that does not contain sensitive files or directories. |
| 25 | + * |
| 26 | + * @example `'/var/www'` |
| 27 | + */ |
| 28 | + basePath: string |
| 29 | +} |
| 30 | + |
| 31 | +const optionsSchema = z.object({ |
| 32 | + basePath: z |
| 33 | + .string() |
| 34 | + .refine( |
| 35 | + (basePath) => |
| 36 | + basePath === path.resolve(basePath) && path.isAbsolute(basePath), |
| 37 | + "The base path must be an absolute path" |
| 38 | + ), |
| 39 | +}) |
| 40 | + |
| 41 | +type ParsedPathValidatorOptions = z.infer<typeof optionsSchema> |
| 42 | + |
| 43 | +export const isSafePath = (absPath: string, basePath: string): boolean => { |
| 44 | + // check for poison null bytes |
| 45 | + if (absPath.indexOf("\0") !== -1) { |
| 46 | + return false |
| 47 | + } |
| 48 | + // check for backslashes |
| 49 | + if (absPath.indexOf("\\") !== -1) { |
| 50 | + return false |
| 51 | + } |
| 52 | + |
| 53 | + // check for dot segments, even if they don't normalize to anything |
| 54 | + if (absPath.includes("..")) { |
| 55 | + return false |
| 56 | + } |
| 57 | + |
| 58 | + // check if the normalized path is within the provided 'safe' base path |
| 59 | + if (path.resolve(basePath, path.relative(basePath, absPath)) !== absPath) { |
| 60 | + return false |
| 61 | + } |
| 62 | + if (absPath.indexOf(basePath) !== 0) { |
| 63 | + return false |
| 64 | + } |
| 65 | + return true |
| 66 | +} |
| 67 | + |
| 68 | +const createValidationSchema = (options: ParsedPathValidatorOptions) => |
| 69 | + z |
| 70 | + .string() |
| 71 | + // resolve the path relative to the Node process's current working directory |
| 72 | + // since that's what fs operations will be relative to |
| 73 | + .transform((untrustedPath) => path.resolve(untrustedPath)) |
| 74 | + // resolvedPath is now an absolute path |
| 75 | + .refine((resolvedPath) => isSafePath(resolvedPath, options.basePath), { |
| 76 | + message: "The provided path is unsafe.", |
| 77 | + }) |
| 78 | + |
| 79 | +const toSchema = (options: ParsedPathValidatorOptions) => |
| 80 | + z.string().trim().pipe(createValidationSchema(options)) |
| 81 | + |
| 82 | +/** |
| 83 | + * Create a schema that validates user-supplied pathnames for filesystem operations. |
| 84 | + * |
| 85 | + * @param options - The options to use for validation |
| 86 | + * @throws {@link OptionsError} If the options are invalid |
| 87 | + * @returns A Zod schema that validates paths. |
| 88 | + * |
| 89 | + * @public |
| 90 | + */ |
| 91 | +export const createPathSchema = ( |
| 92 | + options: PathValidatorOptions |
| 93 | +): ZodSchema<string> => { |
| 94 | + const result = optionsSchema.safeParse(options) |
| 95 | + if (result.success) { |
| 96 | + return toSchema(result.data) |
| 97 | + } |
| 98 | + throw new OptionsError(fromError(result.error).toString()) |
| 99 | +} |
0 commit comments