Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-72628] Follow symlinks when checking the workspace prefix #176

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/io/jenkins/plugins/prism/FilePermissionEnforcer.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.jenkins.plugins.prism;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import edu.hm.hafner.util.PathUtil;
import edu.hm.hafner.util.VisibleForTesting;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import hudson.FilePath;
Expand Down Expand Up @@ -59,9 +61,24 @@
.map(PATH_UTIL::getAbsolutePath)
.collect(Collectors.toSet());
permittedAbsolutePaths.add(workspace.getRemote());
permittedAbsolutePaths.add(resolveWorkspace(workspace));

return permittedAbsolutePaths.stream()
.map(Paths::get)
.anyMatch(prefix -> Paths.get(sourceFile).startsWith(prefix));
}

@VisibleForTesting
String resolveWorkspace(final FilePath workspace) {
try {
var resolved = workspace.readLink();
if (resolved != null) {
return resolved;
}
}
catch (IOException | InterruptedException ignore) {

Check warning on line 79 in src/main/java/io/jenkins/plugins/prism/FilePermissionEnforcer.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 79 is not covered by tests
// ignore
}
return workspace.getRemote();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.jenkins.plugins.prism;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Set;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.Issue;
Expand Down Expand Up @@ -35,6 +38,23 @@ void shouldComparePathsOnUnix() {
assertThat(validator.isInWorkspace("/b/a/b.c", WORKSPACE_UNIX, "/a")).isFalse();
}

@Test @Issue("JENKINS-72628")
void shouldFollowSymbolicLinks() throws IOException {
var workspace = Files.createTempDirectory("workspace");
workspace.toFile().deleteOnExit();
var link = Files.createSymbolicLink(workspace.resolve("link"), workspace);

FilePermissionEnforcer validator = new FilePermissionEnforcer();
assertThat(validator.resolveWorkspace(new FilePath(link.toFile())))
.isEqualTo(workspace.toString());
assertThat(validator.isInWorkspace(workspace + "/something.txt",
new FilePath(workspace.toFile()), Set.of()))
.isTrue();
assertThat(validator.isInWorkspace(workspace + "/something.txt",
new FilePath(link.toFile()), Set.of()))
.isTrue();
}

@Test
void shouldAllowWorkspaceByDefaultOnUnix() {
assumeThat(isWindows()).isFalse();
Expand Down
Loading