Skip to content

[JENKINS-48625] Restore binding of doCheckUrl methods and add some initial checks #841

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

Merged
merged 20 commits into from
Mar 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
2510bd3
=[JENKINS-56063] added expansion of env variables in refspec in case …
rishabhBudhouliya Jan 30, 2020
7d7f670
=removed the check flag and changed the implementation
rishabhBudhouliya Jan 31, 2020
58320c4
Changed minor indentation issues
rishabhBudhouliya Feb 1, 2020
503e0a3
[JENKINS-48625] Restore binding of doCheckUrl methods and add some in…
rishabhBudhouliya Feb 19, 2020
f4b0710
Merge branch 'master' into JENKINS-48625-Fix
MarkEWaite Feb 20, 2020
f99b832
Proposed AssemblaWebTest changes
MarkEWaite Feb 20, 2020
a062e0b
Clarify comments for my benefit
MarkEWaite Feb 20, 2020
c04b4a7
Extend null project test name to refer to null project
MarkEWaite Feb 20, 2020
3a8d549
Test with a random URL that meets criteria
MarkEWaite Feb 20, 2020
f298249
Fix compile error on use of Random
MarkEWaite Feb 20, 2020
7293a3f
initial checks and utility function is shifted to GitRepositoryBrowse…
rishabhBudhouliya Feb 20, 2020
141c79f
Changed access modifier for utilities in GitRepositoryBrowser
rishabhBudhouliya Feb 20, 2020
454397e
change in checkURIFormat
rishabhBudhouliya Feb 21, 2020
6532b34
Revert "change in checkURIFormat"
rishabhBudhouliya Feb 21, 2020
4a0915a
Change in checkURIFormat and addition of checkURIFormatAndHostname in…
rishabhBudhouliya Feb 21, 2020
5f48308
Removal of fixes of whitespace and temporary variable response
rishabhBudhouliya Feb 21, 2020
ac1beeb
Merge branch 'master' into JENKINS-48625-Fix
MarkEWaite Feb 28, 2020
8b1433c
Revert CloneOptions change from a different branch
MarkEWaite Feb 29, 2020
acebb46
Reduce whitespace differences from master branch
MarkEWaite Feb 29, 2020
d9fe71b
Better variable name in Assembla checker
MarkEWaite Mar 1, 2020
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
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,17 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
<exclusions>
<exclusion>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
35 changes: 26 additions & 9 deletions src/main/java/hudson/plugins/git/browser/AssemblaWeb.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package hudson.plugins.git.browser;

import hudson.Extension;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;
import hudson.plugins.git.Messages;
import hudson.scm.EditType;
import hudson.scm.RepositoryBrowser;
import hudson.util.FormValidation;
import hudson.util.FormValidation.URLCheck;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.validator.routines.UrlValidator;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.kohsuke.stapler.QueryParameter;
Expand All @@ -18,6 +22,8 @@
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
Expand Down Expand Up @@ -94,18 +100,21 @@ public AssemblaWeb newInstance(StaplerRequest req, @Nonnull JSONObject jsonObjec
}

@RequirePOST
public FormValidation doCheckUrl(@QueryParameter(fixEmpty = true) final String url)
throws IOException, ServletException {
if (url == null) // nothing entered yet
public FormValidation doCheckRepoUrl(@AncestorInPath Item project, @QueryParameter(fixEmpty = true) final String repoUrl)
throws IOException, ServletException, URISyntaxException {

String cleanUrl = Util.fixEmptyAndTrim(repoUrl);
if (initialChecksAndReturnOk(project, cleanUrl))
{
return FormValidation.ok();
}
// Connect to URL and check content only if we have admin permission
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER))
return FormValidation.ok();
// Connect to URL and check content only if we have permission
if (!checkURIFormatAndHostName(cleanUrl, "assembla")) {
return FormValidation.error(Messages.invalidUrl());
}
return new URLCheck() {
protected FormValidation check() throws IOException, ServletException {
String v = url;
String v = cleanUrl;
if (!v.endsWith("/")) {
v += '/';
}
Expand All @@ -114,13 +123,21 @@ protected FormValidation check() throws IOException, ServletException {
if (findText(open(new URL(v)), "Assembla")) {
return FormValidation.ok();
} else {
return FormValidation.error("This is a valid URL but it doesn't look like Assembla");
return FormValidation.error("This is a valid URL but it does not look like Assembla");
}
} catch (IOException e) {
return handleIOException(v, e);
}
}
}.check();
}

private boolean checkURIFormatAndHostName(String url, String hostNameFragment) throws URISyntaxException {
URI uri = new URI(url);
String[] schemes = {"http", "https"};
UrlValidator urlValidator = new UrlValidator(schemes);
hostNameFragment = hostNameFragment + ".";
return urlValidator.isValid(uri.toString()) && uri.getHost().contains(hostNameFragment);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package hudson.plugins.git.browser;

import hudson.Extension;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;
import hudson.plugins.git.Messages;
import hudson.scm.EditType;
import hudson.scm.RepositoryBrowser;
import hudson.util.FormValidation;
import hudson.util.FormValidation.URLCheck;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.kohsuke.stapler.QueryParameter;
Expand All @@ -19,6 +22,7 @@
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;

Expand Down Expand Up @@ -66,6 +70,7 @@ public String getProjectName() {
private String encodeString(final String s) throws UnsupportedEncodingException {
return URLEncoder.encode(s, "UTF-8").replaceAll("\\+", "%20");
}

@Extension
public static class ViewGitWebDescriptor extends Descriptor<RepositoryBrowser<?>> {
@Nonnull
Expand All @@ -80,18 +85,21 @@ public GitBlitRepositoryBrowser newInstance(StaplerRequest req, @Nonnull JSONObj
}

@RequirePOST
public FormValidation doCheckUrl(@QueryParameter(fixEmpty = true) final String url)
throws IOException, ServletException {
if (url == null) // nothing entered yet
public FormValidation doCheckRepoUrl(@AncestorInPath Item project, @QueryParameter(fixEmpty = true) final String repoUrl)
throws IOException, ServletException, URISyntaxException {

String cleanUrl = Util.fixEmptyAndTrim(repoUrl);
if (initialChecksAndReturnOk(project, cleanUrl))
{
return FormValidation.ok();
}
// Connect to URL and check content only if we have admin permission
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER))
return FormValidation.ok();
if (!checkURIFormat(cleanUrl))
{
return FormValidation.error(Messages.invalidUrl());
}
return new URLCheck() {
protected FormValidation check() throws IOException, ServletException {
String v = url;
String v = cleanUrl;
if (!v.endsWith("/")) {
v += '/';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package hudson.plugins.git.browser;

import hudson.EnvVars;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.TaskListener;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;
import hudson.scm.RepositoryBrowser;

import org.apache.commons.validator.routines.UrlValidator;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;

Expand Down Expand Up @@ -117,5 +119,25 @@ public static URL encodeURL(URL url) throws IOException {
}
}

protected static boolean initialChecksAndReturnOk(Item project, String cleanUrl){
if (cleanUrl == null) {
return true;
}
if (project == null || !project.hasPermission(Item.CONFIGURE)) {
return true;
}
if (cleanUrl.contains("$")) {
// set by variable, can't validate
return true;
}
return false;
}

protected static boolean checkURIFormat(String url) throws URISyntaxException {
String[] schemes = {"http", "https"};
UrlValidator urlValidator = new UrlValidator(schemes);
return urlValidator.isValid(url);
}

private static final long serialVersionUID = 1L;
}
23 changes: 15 additions & 8 deletions src/main/java/hudson/plugins/git/browser/Gitiles.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package hudson.plugins.git.browser;

import hudson.Extension;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;
import hudson.plugins.git.Messages;
import hudson.scm.RepositoryBrowser;
import hudson.util.FormValidation;
import hudson.util.FormValidation.URLCheck;

import jenkins.model.Jenkins;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import javax.annotation.Nonnull;
import javax.servlet.ServletException;

import net.sf.json.JSONObject;

import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -70,15 +73,19 @@ public Gitiles newInstance(StaplerRequest req, @Nonnull JSONObject jsonObject) t
}

@RequirePOST
public FormValidation doCheckUrl(@QueryParameter(fixEmpty = true) final String url) throws IOException, ServletException {
if (url == null) // nothing entered yet
return FormValidation.ok();
// Connect to URL and check content only if we have admin permission
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER))
public FormValidation doCheckRepoUrl(@AncestorInPath Item project, @QueryParameter(fixEmpty = true) final String repoUrl)
throws IOException, ServletException, URISyntaxException {

String cleanUrl = Util.fixEmptyAndTrim(repoUrl);
if(initialChecksAndReturnOk(project, cleanUrl)){
return FormValidation.ok();
}
if (!checkURIFormat(cleanUrl)) {
return FormValidation.error(Messages.invalidUrl());
}
return new URLCheck() {
protected FormValidation check() throws IOException, ServletException {
String v = url;
String v = cleanUrl;
if (!v.endsWith("/"))
v += '/';

Expand Down
20 changes: 14 additions & 6 deletions src/main/java/hudson/plugins/git/browser/ViewGitWeb.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package hudson.plugins.git.browser;

import hudson.Extension;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.plugins.git.GitChangeSet;
import hudson.plugins.git.GitChangeSet.Path;
import hudson.plugins.git.Messages;
import hudson.scm.EditType;
import hudson.scm.RepositoryBrowser;
import hudson.scm.browsers.QueryBuilder;
import hudson.util.FormValidation;
import hudson.util.FormValidation.URLCheck;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.kohsuke.stapler.QueryParameter;
Expand All @@ -20,6 +23,7 @@
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;

Expand Down Expand Up @@ -89,15 +93,19 @@ public ViewGitWeb newInstance(StaplerRequest req, @Nonnull JSONObject jsonObject
}

@RequirePOST
public FormValidation doCheckUrl(@QueryParameter(fixEmpty = true) final String url) throws IOException, ServletException {
if (url == null) // nothing entered yet
return FormValidation.ok();
public FormValidation doCheckRepoUrl(@AncestorInPath Item project, @QueryParameter(fixEmpty = true) final String repoUrl)
throws IOException, ServletException, URISyntaxException {

String cleanUrl = Util.fixEmptyAndTrim(repoUrl);
// Connect to URL and check content only if we have admin permission
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER))
if (initialChecksAndReturnOk(project, cleanUrl))
return FormValidation.ok();
if (!checkURIFormat(cleanUrl)) {
return FormValidation.error(Messages.invalidUrl());
}
return new URLCheck() {
protected FormValidation check() throws IOException, ServletException {
String v = url;
String v = cleanUrl;
if (!v.endsWith("/"))
v += '/';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ BuildChooser_BuildingLastRevision=No new revisions were found; the most-recently
UserRemoteConfig.FailedToConnect=Failed to connect to repository : {0}
UserRemoteConfig.CheckUrl.UrlIsNull=Please enter Git repository.
UserRemoteConfig.CheckRefSpec.InvalidRefSpec=Specification is invalid.
invalidUrl=Invalid URL

GitPublisher.Check.TagName=Tag Name
GitPublisher.Check.BranchName=Branch Name
Expand Down
Loading