CVE-2022-0436 (published 2022-04-12T21:15Z)

Path Traversal in GitHub repository gruntjs/grunt prior to 1.5.2.

Severity: LOW
CVSS v2 base score: 2.1 (AV:L/AC:L/Au:N/C:P/I:N/A:N)
CVSS v3 base score: 5.5, MEDIUM (CVSS:3.1/AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/gruntjs/grunt
commit_details: repo_url: https://github.com/gruntjs/grunt
author: Vlad Filippov
author_date: 2022-04-10 23:16:06-04:00
msg: Update dependencies, tests...
repository: repo_url: https://github.com/gruntjs/grunt
repo_name: gruntjs/grunt
description: Grunt: The JavaScript Task Runner
repo_language: JavaScript
owner: gruntjs
file_changes: filename: file.js
change_type: ModificationType.MODIFY
diff: @@ -292,8 +292,11 @@ file.write = function(filepath, contents, options) {
 // Read a file, optionally processing its content, then write the output.
 // Or read a directory, recursively creating directories, reading files,
 // processing content, writing output.
+// Handles symlinks by coping them as files or directories.
 file.copy = function copy(srcpath, destpath, options) {
-  if (file.isDir(srcpath)) {
+  if (file._isSymbolicLink(srcpath)) {
+    file._copySymbolicLink(srcpath, destpath);
+  } else if (file.isDir(srcpath)) {
     // Copy a directory, recursively.
     // Explicitly create new dest directory.
     file.mkdir(destpath);
@@ -449,6 +452,24 @@ file.isPathCwd = function() {
   }
 };
 
+file._isSymbolicLink = function() {
+  var filepath = path.join.apply(path, arguments);
+  return fs.lstatSync(filepath).isSymbolicLink();
+};
+
+file._copySymbolicLink = function(srcpath, destpath) {
+  var destdir = path.join(destpath, '..');
+  var fileBase = path.basename(srcpath);
+  // Use the correct relative path for the symlink
+  if (!grunt.file.isPathAbsolute(srcpath)) {
+    srcpath = path.relative(destdir, srcpath) || '.';
+  }
+  file.mkdir(destdir);
+  var mode = grunt.file.isDir(srcpath) ? 'dir' : 'file';
+  var destpath = path.join(destpath, fileBase);
+  return fs.symlinkSync(srcpath, destpath, mode);
+};
+
 // Test to see if a filepath is contained within the CWD.
 file.isPathInCwd = function() {
   var filepath = path.join.apply(path, arguments);

programming_language: JavaScript
filename: package.json
change_type: ModificationType.MODIFY
diff: @@ -42,7 +42,7 @@
     "exit": "~0.1.2",
     "findup-sync": "~0.3.0",
     "glob": "~7.1.6",
-    "grunt-cli": "~1.4.2",
+    "grunt-cli": "~1.4.3",
     "grunt-known-options": "~2.0.0",
     "grunt-legacy-log": "~3.0.0",
     "grunt-legacy-util": "~2.0.1",
@@ -56,7 +56,7 @@
   "devDependencies": {
     "difflet": "~1.0.1",
     "eslint-config-grunt": "~1.0.1",
-    "grunt-contrib-nodeunit": "~3.0.0",
+    "grunt-contrib-nodeunit": "~4.0.0",
     "grunt-contrib-watch": "~1.1.0",
     "grunt-eslint": "~18.1.0",
     "temporary": "~0.0.4",

programming_language: JavaScript
filename: file_test.js
change_type: ModificationType.MODIFY
diff: @@ -893,5 +893,28 @@ exports.file = {
       test.ok(grunt.file.isPathInCwd(path.resolve('deep')), 'subdirectory is in cwd');
       test.done();
     },
+    'symbolicLinkCopy': function(test) {
+      test.expect(4);
+      var srcfile = new Tempdir();
+      fs.symlinkSync(path.resolve('test/fixtures/octocat.png'), path.join(srcfile.path, 'octocat.png'), 'file');
+      // test symlink copy for files
+      var destdir = new Tempdir();
+      grunt.file.copy(path.join(srcfile.path, 'octocat.png'), destdir.path);
+      test.ok(fs.lstatSync(path.join(srcfile.path, 'octocat.png')).isSymbolicLink());
+      test.ok(fs.lstatSync(path.join(destdir.path, 'octocat.png')).isSymbolicLink());
+
+      // test symlink copy for directories
+      var srcdir = new Tempdir();
+      var destdir = new Tempdir();
+      var fixtures = path.resolve('test/fixtures');
+      var symlinkSource = path.join(srcdir.path, path.basename(fixtures));
+      console.log('symlinkSource', symlinkSource);
+      fs.symlinkSync(fixtures, symlinkSource, 'dir');
+
+      grunt.file.copy(symlinkSource, destdir.path);
+      test.ok(fs.lstatSync(symlinkSource).isSymbolicLink());
+      test.ok(fs.lstatSync(path.join(destdir.path, path.basename(fixtures))).isSymbolicLink());
+      test.done();
+    },
   }
 };

programming_language: JavaScript