{"record":{"id":"2a15e1a6ba08242b","repo":"apache/hadoop","slug":"invalid-dfs-directory-name-result","errorCode":null,"errorMessage":"Invalid DFS directory name ${result}","messagePattern":"Invalid DFS directory name (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"warning","filePath":"hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java","lineNumber":472,"sourceCode":"        LOG.error(\"Unable to get HomeDirectory from original File System\", e);\n        cachedHomeDirectory = new Path(\"/user/\" + ugi.getShortUserName())\n            .makeQualified(this.getUri(), null);\n      }\n    }\n    return cachedHomeDirectory;\n  }\n\n  @Override\n  public synchronized Path getWorkingDirectory() {\n    return workingDir;\n  }\n\n  @Override\n  public synchronized void setWorkingDirectory(final Path dir) {\n    Path absolutePath = makeAbsolute(dir);\n    String result = absolutePath.toUri().getPath();\n    if (!DFSUtilClient.isValidName(result)) {\n      throw new IllegalArgumentException(\"Invalid DFS directory name \" +\n          result);\n    }\n    workingDir = absolutePath;\n  }\n\n  private Path makeAbsolute(Path f) {\n    return f.isAbsolute()? f: new Path(workingDir, f);\n  }\n\n  @VisibleForTesting\n  public static Map<?, ?> jsonParse(final HttpURLConnection c,\n      final boolean useErrorStream) throws IOException {\n    if (c.getContentLength() == 0) {\n      return null;\n    }\n    final InputStream in = useErrorStream ?\n        c.getErrorStream() : c.getInputStream();\n    if (in == null) {","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java#L454-L490","documentation":"WebHdfsFileSystem.setWorkingDirectory makes the supplied Path absolute and then validates its URI path with DFSUtilClient.isValidName. HDFS rejects non-canonical namespace names, including components containing ':', '.', or '..' and interior empty components. An invalid name is rejected immediately with IllegalArgumentException before workingDir is changed.","triggerScenarios":"Calling setWorkingDirectory with an absolute path such as /jobs/a:b, or a relative path that resolves against the current working directory to a name containing ':' or an unresolvable '..' component. The resulting path is passed to Path.toUri().getPath(), so scheme/authority are removed and the remaining namespace string must satisfy HDFS naming rules.","commonSituations":"Using a URL fragment or host:port string as a directory name; passing untrusted configuration paths into setWorkingDirectory; assuming HDFS accepts '.' or '..' components after URI normalization; porting code from a local file system where colons are allowed.","solutions":["Set a clean absolute directory such as new Path(\"/user/alice/jobs\") with no ':' or dot components in any segment.","Validate the resolved absolute URI path with DFSUtilClient.isValidName before assigning it as the working directory.","Sanitize externally supplied directory names instead of embedding raw host:port, URL, or user input values.","Remember relative directories are resolved against the current working directory, so validate the final resolved path rather than the input string alone."],"exampleFix":"// before\nfs.setWorkingDirectory(new Path(\"/jobs/dn1:9866\"));\n\n// after\nString raw = \"/jobs/dn1_9866\";\nPath dir = new Path(raw);\nif (!DFSUtilClient.isValidName(dir.toUri().getPath())) {\n  throw new IllegalArgumentException(\"Invalid HDFS working directory: \" + raw);\n}\nfs.setWorkingDirectory(dir);","handlingStrategy":"validation","validationCode":"Path absolute = dir.isAbsolute() ? dir : new Path(fs.getWorkingDirectory(), dir);\nString name = absolute.toUri().getPath();\nif (!DFSUtilClient.isValidName(name)) {\n  throw new IllegalArgumentException(\"Invalid HDFS working directory name: \" + name);\n}\nfs.setWorkingDirectory(absolute);","typeGuard":null,"tryCatchPattern":"try {\n  fs.setWorkingDirectory(dir);\n} catch (IllegalArgumentException e) {\n  throw new IllegalArgumentException(\"Working directory must be an absolute, canonical HDFS path without ':' or dot components: \" + dir, e);\n}","preventionTips":["Always use qualified absolute HDFS paths for the working directory.","Reject raw host:port or URL-like strings before converting them to Path objects.","Validate externally supplied path configuration with DFSUtilClient.isValidName before assignment."],"tags":["java","hadoop","webhdfs","path","validation"],"backgroundTag":"path-validation-failed","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}