Skip to content

Commit

Permalink
fix: path validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Jul 31, 2024
1 parent e9d51cb commit 116cc52
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 16 deletions.
10 changes: 5 additions & 5 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.8",
"version": "0.1.9",
"description": "Filen Sync",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
83 changes: 73 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,85 @@ export function isNameOverMaxLength(name: string): boolean {
return name.length + 1 > 255
}

export const isValidPath = memoize((path: string): boolean => {
const illegalCharsWindows = /[<>:"/\\|?*]|^(?:aux|con|clock\$|nul|prn|com[1-9]|lpt[1-9])$/i
const illegalCharsMacOS = /[:]/i
const illegalCharsLinux = /[\0/]/i
export const isValidPath = memoize((inputPath: string): boolean => {
// eslint-disable-next-line no-control-regex
const illegalCharsWindows = /[<>:"/\\|?*\x00-\x1F]/
const reservedNamesWindows = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i
// eslint-disable-next-line no-control-regex, no-useless-escape
const illegalCharsMacOS = /[\/:\x00]/
// eslint-disable-next-line no-control-regex
const illegalCharsLinux = /[\x00]/

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

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

switch (process.platform) {
case "win32": {
return illegalCharsWindows.test(path)
for (const part of parts) {
if (part.trim() === "") {
continue
}

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

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

const nameParts = part.split(".")

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

if (part.endsWith(".") || part.endsWith(" ")) {
return false
}
}

return true
}

case "darwin": {
return illegalCharsMacOS.test(path)
for (const part of parts) {
if (part.trim() === "") {
continue
}

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

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

return true
}

case "linux": {
return illegalCharsLinux.test(path)
for (const part of parts) {
if (part.trim() === "") {
continue
}

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

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

return true
}

default: {
Expand All @@ -124,15 +187,15 @@ export const isValidPath = memoize((path: string): boolean => {

export const isNameIgnoredByDefault = memoize((name: string): boolean => {
const nameLowercase = name.toLowerCase().trim()
const extension = pathModule.extname(name)
const extension = pathModule.extname(nameLowercase)
const extensionLowercase = extension.toLowerCase()

if (
name.length === 0 ||
nameLowercase.length === 0 ||
nameLowercase.startsWith(".~lock.") ||
nameLowercase.startsWith(".~lock") ||
nameLowercase.startsWith("~$") ||
DEFAULT_IGNORED.extensions.includes(extensionLowercase) ||
(extensionLowercase && extensionLowercase.length > 0 && DEFAULT_IGNORED.extensions.includes(extensionLowercase)) ||
DEFAULT_IGNORED.names.includes(nameLowercase)
) {
return true
Expand Down

0 comments on commit 116cc52

Please sign in to comment.