Skip to content

Commit

Permalink
fix: win32 path validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Jul 31, 2024
1 parent ea4908a commit 280725a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@filen/sync",
"version": "0.1.10",
"version": "0.1.11",
"description": "Filen Sync",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
30 changes: 12 additions & 18 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,23 @@ export const isValidPath = memoize((inputPath: string): boolean => {
// eslint-disable-next-line no-control-regex
const illegalCharsLinux = /[\x00]/

if (process.platform === "win32") {
inputPath = inputPath.replace(/\\/g, "/")
}

if (inputPath.includes("..")) {
return false
}

const normalizedPath = pathModule.normalize(inputPath)
const parts = normalizedPath.split(pathModule.sep)
const parts = inputPath.split("/")

switch (process.platform) {
case "win32": {
for (const part of parts) {
if (part.trim() === "") {
const trimmed = part.trim()

// Skip drive letter
if (trimmed.length === 0 || (trimmed.length === 2 && part.endsWith(":") && inputPath.startsWith(part))) {
continue
}

Expand All @@ -131,11 +137,7 @@ export const isValidPath = memoize((inputPath: string): boolean => {

const nameParts = part.split(".")

if (nameParts[0] && reservedNamesWindows.test(nameParts[0]) && nameParts.length > 1) {
return false
}

if (part.endsWith(".") || part.endsWith(" ")) {
if (nameParts[0] && nameParts[0].length > 0 && reservedNamesWindows.test(nameParts[0])) {
return false
}
}
Expand All @@ -145,35 +147,27 @@ export const isValidPath = memoize((inputPath: string): boolean => {

case "darwin": {
for (const part of parts) {
if (part.trim() === "") {
if (part.trim().length === 0) {
continue
}

if (illegalCharsMacOS.test(part)) {
return false
}

if (part.startsWith(".")) {
continue
}
}

return true
}

case "linux": {
for (const part of parts) {
if (part.trim() === "") {
if (part.trim().length === 0) {
continue
}

if (illegalCharsLinux.test(part)) {
return false
}

if (part === ".") {
continue
}
}

return true
Expand Down

0 comments on commit 280725a

Please sign in to comment.