Computer scienceBackendNode.jsSecurity

Path traversal

Utility function

Report a typo

You need to create a utility function that normalizes a file path using Node.js's path module. Ensure that the normalized path is safe and doesn't contain any directory traversal sequences.

Here, in this task, the normalized path will always be safe (prints true) as it is sanitized using the path module.

Sample Input 1:

/uploads/../../../..//etc/passwd

Sample Output 1:

Normalized Path: /etc/passwd
Is Safe Path: true
Write a program in JavaScript
// Function to normalize and check for safe file path
function normalizeAndCheckPath(filePath) {

// Normalize the file path using Node.js's path module

// Check if the normalized path is safe (doesn't contain directory traversal)

return {
normalizedPath,
isSafePath
};
}

Create a free account to access the full topic