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

Conversation

amplify-lab[bot]
Copy link

@amplify-lab amplify-lab bot commented Dec 12, 2024

This Pull Request fixes CWE-22, Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal'), in routes/dataErasure.ts:69.

💡 This is an automated Pull Request created by Amplify to fix vulnerability 8f6f91ee.
➡️ For more information, visit Amplify Security.

Automated code fix by Amplify Security accepted by [email protected]
Copy link
Author

amplify-lab bot commented Dec 12, 2024

🔍 Amplify code check status:   status looks good

⚠️ 2 issues detected in   📄 1 file and   ❇️ 3 lines of code   🛠️ using Semgrep

Vulnerabilities detected
Click on a CWE to view vulnerability in Amplify
Vulnerability Path Fingerprint Tool
CWE-22 routes/dataErasure.ts:70 [592ba575...] Semgrep
CWE-22 routes/dataErasure.ts:70 [f98f94ec...] Semgrep

💡 To ignore a finding, append @amplify-ignore in a comment to the end of the vulnerable link like // @amplify-ignore or # @amplify-ignore. For more details, visit the Amplify Security Documentation.

Last updated by commit 0d0ccdc at 2024-12-12 15:02:26 UTC.

@@ -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

@@ -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: 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants