Skip to content

[F] sanitizePathSegment

function sanitizePathSegment(value): string;

Sanitize a value so it is safe to use as a single segment of a path.

Path separators, drive letter separators and NUL are replaced with _ and values only made of .(e.g. . or ..) are replaced with _. This prevents values coming from a TypeSpec spec(e.g. a version or a service name) from escaping the directory the file was meant to be written to.

ParameterTypeDescription
valuestringValue to sanitize.

string

sanitizePathSegment("2021-10-01-preview"); // "2021-10-01-preview"
sanitizePathSegment("../../etc/passwd"); // ".._.._etc_passwd"