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-73506] Reject fetch requests that use credentials with HTTP in FIPS mode #1615

Merged
merged 6 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import jenkins.scm.api.trait.SCMTrait;
import jenkins.scm.impl.trait.WildcardSCMHeadFilterTrait;
import jenkins.scm.impl.trait.WildcardSCMSourceFilterTrait;
import jenkins.security.FIPS140;
import jenkins.util.SystemProperties;
import net.jcip.annotations.GuardedBy;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -351,6 +352,17 @@
return doRetrieve(retriever, context, listener, prune, getOwner(), delayFetch);
}

/**
* Returns false if a non-TLS protocol is used when FIPS mode is enabled.
* @param credentialsId any credentials (can be {@code null})
* @param remoteUrl the git remote url
* @return {@code false} if using any credentials with a non TLS protocol with FIPS mode activated
* @see FIPS140#useCompliantAlgorithms()
*/
public static boolean isFIPSCompliantTLS(String credentialsId, String remoteUrl) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Restricted(NoExternalUse.class)?

return !FIPS140.useCompliantAlgorithms() || !StringUtils.isNotEmpty(credentialsId) || (!StringUtils.startsWith(remoteUrl, "http:") && !StringUtils.startsWith(remoteUrl, "git:"));
PereBueno marked this conversation as resolved.
Show resolved Hide resolved
}

@NonNull
private <T, C extends GitSCMSourceContext<C, R>, R extends GitSCMSourceRequest> T doRetrieve(Retriever<T> retriever,
@NonNull C context,
Expand All @@ -359,6 +371,12 @@
@CheckForNull Item retrieveContext,
boolean delayFetch)
throws IOException, InterruptedException {
if (!isFIPSCompliantTLS(this.getCredentialsId(), this.getRemote())) {

Check warning on line 374 in src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 374 is only partially covered, one branch is missing
olamy marked this conversation as resolved.
Show resolved Hide resolved
listener.fatalError(Messages.git_fips_url_notsecured());
LOGGER.log(Level.SEVERE, Messages.git_fips_url_notsecured());
throw new IllegalArgumentException(Messages.git_fips_url_notsecured());

Check warning on line 377 in src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 375-377 are not covered by tests
}

String cacheEntry = getCacheEntry();
Lock cacheLock = getCacheLock(cacheEntry);
cacheLock.lock();
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/jenkins/plugins/git/GitSCMSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@

@DataBoundSetter
public void setCredentialsId(@CheckForNull String credentialsId) {
if (!isFIPSCompliantTLS(credentialsId, this.remote)) {

Check warning on line 167 in src/main/java/jenkins/plugins/git/GitSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 167 is only partially covered, one branch is missing
LOGGER.log(Level.SEVERE, Messages.git_fips_url_notsecured());

Check warning on line 168 in src/main/java/jenkins/plugins/git/GitSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 168 is not covered by tests
olamy marked this conversation as resolved.
Show resolved Hide resolved
}
this.credentialsId = credentialsId;
}

Expand Down Expand Up @@ -409,17 +412,6 @@
return traits;
}

/**
* Returns false if a non-TLS protocol is used when FIPS mode is enabled.
* @param credentialsId any credentials (can be {@code null})
* @param remoteUrl the git remote url
* @return {@code false} if using any credentials with a non TLS protocol with FIPS mode activated
* @see FIPS140#useCompliantAlgorithms()
*/
public static boolean isFIPSCompliantTLS(String credentialsId, String remoteUrl) {
return !FIPS140.useCompliantAlgorithms() || !StringUtils.isNotEmpty(credentialsId) || (!StringUtils.startsWith(remoteUrl, "http:") && !StringUtils.startsWith(remoteUrl, "git:"));
}

@Symbol("git")
@Extension
public static class DescriptorImpl extends SCMSourceDescriptor {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/jenkins/plugins/git/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ GitStep.git=Git
within.Repository=Within Repository
additional=Additional
GitUsernamePasswordBinding.DisplayName=Git Username and Password

git.fips.url.notsecured=Invalid configuration will not fetch any remote. FIPS requires to use git URL with TLS.
PereBueno marked this conversation as resolved.
Show resolved Hide resolved
Loading