soumyakanti3578 commented on code in PR #6851:
URL: https://github.com/apache/hive/pull/6851#discussion_r4531662984


##########
ql/src/java/org/apache/hadoop/hive/ql/io/SymbolicInputFormat.java:
##########
@@ -98,4 +102,58 @@ public void rework(HiveConf job, MapredWork work) throws 
IOException {
       work.getMapWork().removePathToPartitionInfo(toRemove);
     }
   }
+
+  /**
+   * Expands a symlink file line, accepting only matches under the symlink 
file's directory or an allowed root.
+   */
+  static List<Path> resolveTargets(Configuration conf, Path symlinkFile, 
List<Path> allowedRoots, String line)
+      throws IOException {
+    // Qualified against the default filesystem, so no filesystem is created 
for an arbitrary scheme.
+    FileSystem defaultFs = FileSystem.get(conf);
+    List<Path> roots = new ArrayList<>(allowedRoots);
+    roots.add(qualify(defaultFs, symlinkFile.getParent()));
+    Path pattern = qualify(defaultFs, new Path(line));
+    // Matches are checked too, as a glob can climb out of the root.
+    checkAllowed(pattern, roots);
+    FileStatus[] statuses = pattern.getFileSystem(conf).globStatus(pattern);
+    if (statuses == null) {
+      return Collections.emptyList();
+    }
+    List<Path> matches = new ArrayList<>();
+    for (FileStatus status : statuses) {
+      Path match = qualify(defaultFs, status.getPath());
+      checkAllowed(match, roots);
+      // FileInputFormat globs the match again, so it must not contain glob 
metacharacters.
+      if (GLOB_METACHARS.matcher(match.toUri().getPath()).find()) {
+        throw new IOException("Symlink target " + match + " contains glob 
metacharacters");
+      }
+      matches.add(match);
+    }
+    return matches;
+  }
+
+  private static void checkAllowed(Path target, List<Path> roots) throws 
IOException {
+    for (Path root : roots) {
+      if (FileUtils.isPathWithinSubtree(target, root)) {
+        return;
+      }
+    }
+    throw new IOException("Symlink target " + target + " is outside of the 
allowed locations. Additional locations"
+        + " can be configured with " + 
ConfVars.HIVE_SYMLINK_ALLOWED_TARGET_PATHS.varname);
+  }
+
+  static List<Path> getAllowedRoots(Configuration conf) throws IOException {
+    List<Path> allowedRoots = new ArrayList<>();
+    for (String root : HiveConf.getTrimmedStringsVar(conf, 
ConfVars.HIVE_SYMLINK_ALLOWED_TARGET_PATHS)) {
+      if (!root.isEmpty()) {
+        Path rootPath = new Path(root);
+        allowedRoots.add(qualify(rootPath.getFileSystem(conf), rootPath));
+      }
+    }
+    return allowedRoots;
+  }
+
+  private static Path qualify(FileSystem fs, Path path) {
+    return new Path(path.makeQualified(fs.getUri(), 
fs.getWorkingDirectory()).toUri().normalize());
+  }

Review Comment:
   Yes I will remove this in the next commit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to