Skip to content

Commit ac1c621

Browse files
committed
remove dependency
1 parent 187b013 commit ac1c621

File tree

5 files changed

+33
-113
lines changed

5 files changed

+33
-113
lines changed

package-lock.json

Lines changed: 1 addition & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@
112112
"validator": "^13.12.0",
113113
"winston": "^3.13.0",
114114
"winston-cloudwatch": "^6.3.0",
115-
"yaml": "^2.4.2",
116-
"zod": "^3.25.76",
117-
"zod-validation-error": "^3.5.3"
115+
"yaml": "^2.4.2"
118116
},
119117
"devDependencies": {
120118
"@octokit/types": "^6.35.0",

src/services/db/GitFileSystemService.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import { NotFoundError } from "@errors/NotFoundError"
2020

2121
import tracer from "@utils/tracer"
2222

23-
import { createPathSchema } from "@validators/path"
24-
2523
import {
2624
EFS_VOL_PATH_STAGING,
2725
EFS_VOL_PATH_STAGING_LITE,
@@ -44,6 +42,7 @@ import type {
4442
import type { IsomerCommitMessage } from "@root/types/github"
4543
import { ALLOWED_FILE_EXTENSIONS } from "@root/utils/file-upload-utils"
4644
import { getPaginatedDirectoryContents } from "@root/utils/files"
45+
import { isSafePath } from "@root/validators/validators"
4746

4847
// methods that do not need to be wrapped for instrumentation
4948
const METHOD_INSTRUMENTATION_BLACKLIST = [
@@ -372,10 +371,9 @@ export default class GitFileSystemService {
372371
// traversal attacks
373372
const repoBaseDirectory = `${efsVolPath}/${repoName}`
374373
const fullFilePath = path.resolve(repoBaseDirectory, filePath)
375-
const pathSchema = createPathSchema({ basePath: repoBaseDirectory })
376-
const parsedPathResult = pathSchema.safeParse(fullFilePath)
374+
const isSafe = isSafePath(fullFilePath, repoBaseDirectory)
377375

378-
if (!parsedPathResult.success) {
376+
if (!isSafe) {
379377
logger.error(`Invalid file path: ${filePath} for repo: ${repoName}`)
380378
return errAsync(new BadRequestError("Invalid file path"))
381379
}

src/validators/path.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/validators/validators.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const path = require("path")
2+
13
const specialCharactersRegexTest = /[~%^*_+\-./\\`;~{}[\]"<>]/
24
const jekyllFirstCharacterRegexTest = /^[._#~]/
35
const dateRegexTest = /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/
@@ -33,9 +35,35 @@ const isMediaPathValid = ({ path, isFile = false }) => {
3335

3436
const isPasswordValid = (password) => passwordRegexTest.test(password)
3537

38+
const isSafePath = (absPath, basePath) => {
39+
// check for poison null bytes
40+
if (absPath.indexOf("\0") !== -1) {
41+
return false
42+
}
43+
// check for backslashes
44+
if (absPath.indexOf("\\") !== -1) {
45+
return false
46+
}
47+
48+
// check for dot segments, even if they don't normalize to anything
49+
if (absPath.includes("..")) {
50+
return false
51+
}
52+
53+
// check if the normalized path is within the provided 'safe' base path
54+
if (path.resolve(basePath, path.relative(basePath, absPath)) !== absPath) {
55+
return false
56+
}
57+
if (absPath.indexOf(basePath) !== 0) {
58+
return false
59+
}
60+
return true
61+
}
62+
3663
module.exports = {
3764
hasSpecialCharInTitle,
3865
isDateValid,
3966
isMediaPathValid,
4067
isPasswordValid,
68+
isSafePath,
4169
}

0 commit comments

Comments
 (0)