From 802f2f1c660db8b08b77f008312d1af64fbb9f50 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Fri, 28 Apr 2023 15:33:05 -0400 Subject: [PATCH] Blacklist some more special characters Also improved readability of the valid character section by using escapeRegExp --- src/Paths/Directory.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Paths/Directory.ts b/src/Paths/Directory.ts index c87f72310..17ceee140 100644 --- a/src/Paths/Directory.ts +++ b/src/Paths/Directory.ts @@ -1,6 +1,7 @@ import { allContentFiles } from "./ContentFile"; import type { BaseServer } from "../Server/BaseServer"; import type { FilePath } from "./FilePath"; +import { escapeRegExp } from "lodash"; /** The directory part of a BasicFilePath. Everything up to and including the last / * e.g. "file.js" => "", or "dir/file.js" => "dir/", or "../test.js" => "../" */ @@ -14,12 +15,20 @@ export type AbsolutePath = string & { __absolutePath: true }; export type Directory = BasicDirectory & AbsolutePath; export const root = "" as Directory; -/** Regex string for a single valid path character. Invalid characters: +/** Invalid characters in actual filepaths and directory names: * /: Invalid because it is the directory separator. It's allowed in the directory part, but only as the separator. * *, ?, [, and ]: Invalid in actual paths because they are used for globbing. + * !: Invalid because it conflicts with terminal history fetching * \: Invalid to avoid confusion with an escape character + * ~: Invalid because it might have a use in the terminal in the future. + * |: Invalid because it might have a use in the terminal in the future. + * #: Invalid because it might have a use in the terminal in the future. + * (quote marks): Invalid to avoid conflict with quote marks used in the terminal. * (space): Invalid to avoid confusion with terminal command separator */ -export const oneValidCharacter = "[^\\\\\\/* ?\\[\\]]"; +const invalidCharacters = ["/", "*", "?", "[", "]", "!", "\\", "~", "|", "#", '"', "'", " "]; + +/** A valid character is any character that is not one of the invalid characters */ +export const oneValidCharacter = `[^${escapeRegExp(invalidCharacters.join(""))}]`; /** Regex string for matching the directory part of a valid filepath */ export const directoryRegexString = `^(?(?:${oneValidCharacter}+\\\/)*)`;