Skip to content

Commit c427509

Browse files
Restore the check to avoid following symlinks that point inside the repo
1 parent 21661c2 commit c427509

File tree

1 file changed

+21
-2
lines changed
  • dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/index

1 file changed

+21
-2
lines changed

dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/source/index/RepoIndexBuilder.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,31 @@ private RepoIndexingFileVisitor(
126126

127127
@Override
128128
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
129-
if (Files.isSymbolicLink(dir) && !followSymlinks) {
130-
return FileVisitResult.SKIP_SUBTREE;
129+
if (Files.isSymbolicLink(dir)) {
130+
if (!followSymlinks) {
131+
// Configured to skip symlinks
132+
return FileVisitResult.SKIP_SUBTREE;
133+
}
134+
if (readSymbolicLink(dir).startsWith(repoRoot)) {
135+
// The path is a symlink that points inside the repo.
136+
// We'll visit the folder that it points to anyway,
137+
// moreover, we don't want two different results for one file
138+
// (one containing the symlink, the other - the actual folder).
139+
return FileVisitResult.SKIP_SUBTREE;
140+
}
131141
}
132142
return FileVisitResult.CONTINUE;
133143
}
134144

145+
private static Path readSymbolicLink(Path path) {
146+
try {
147+
return Files.readSymbolicLink(path);
148+
} catch (Exception e) {
149+
log.debug("Could not read symbolic link {}", path, e);
150+
return path;
151+
}
152+
}
153+
135154
@Override
136155
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
137156
indexingStats.filesVisited++;

0 commit comments

Comments
 (0)