Blacklist some more special characters

Also improved readability of the valid character section by using escapeRegExp
This commit is contained in:
Snarling 2023-04-28 15:33:05 -04:00
parent 8f684fa74c
commit 802f2f1c66

@ -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 = `^(?<directory>(?:${oneValidCharacter}+\\\/)*)`;