Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ Amplify ] CWE-22 Fix routes/dataErasure.ts:69 #159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion routes/dataErasure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ router.post('/', async (req: Request<{}, {}, DataErasureRequestParams>, res: Res

res.clearCookie('token')
if (req.body.layout !== undefined) {
const filePath: string = path.resolve(req.body.layout).toLowerCase()
const sanitizedLayout: string = path.basename(req.body.layout)
const filePath: string = path.resolve('allowed_directory', sanitizedLayout).toLowerCase()
const isForbiddenFile: boolean = (filePath.includes('ftp') || filePath.includes('ctf.key') || filePath.includes('encryptionkeys'))
if (!isForbiddenFile) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Vulnerability: CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')

🚨 Impact: MEDIUM

📰 The Gist: Detected possible user input going into a path.join or path.resolve function. This could possibly lead to a path traversal vulnerability, where the attacker can access arbitrary files stored in the file system. Instead, be sure to sanitize or validate user input first.

Suggested code
--- routes/dataErasure.ts
+++ routes/dataErasure.ts
@@ -69,7 +69,8 @@
       const sanitizedLayout: string = path.basename(req.body.layout)
       const filePath: string = path.resolve('allowed_directory', sanitizedLayout).toLowerCase()
       const isForbiddenFile: boolean = (filePath.includes('ftp') || filePath.includes('ctf.key') || filePath.includes('encryptionkeys'))
-      if (!isForbiddenFile) {
+      const isValidPath: boolean = filePath.startsWith(path.resolve('allowed_directory') + path.sep)
+      if (!isForbiddenFile && isValidPath) {
         res.render('dataErasureResult', {
           ...req.body
         }, (error, html) => {

review-fix-button

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Vulnerability: CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')

🚨 Impact: MEDIUM

📰 The Gist: Possible writing outside of the destination, make sure that the target path is nested in the intended destination

Suggested code
--- routes/dataErasure.ts
+++ routes/dataErasure.ts
@@ -69,7 +69,8 @@
       const sanitizedLayout: string = path.basename(req.body.layout)
       const filePath: string = path.resolve('allowed_directory', sanitizedLayout).toLowerCase()
       const isForbiddenFile: boolean = (filePath.includes('ftp') || filePath.includes('ctf.key') || filePath.includes('encryptionkeys'))
-      if (!isForbiddenFile) {
+      const isOutsideAllowedDirectory: boolean = !filePath.startsWith(path.resolve('allowed_directory').toLowerCase())
+      if (!isForbiddenFile && !isOutsideAllowedDirectory) {
         res.render('dataErasureResult', {
           ...req.body
         }, (error, html) => {

review-fix-button

res.render('dataErasureResult', {
Expand Down
Loading