1 /*******************************************************************************
2 * Copyright (c) 2011, 2017 The Eclipse Foundation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
11 * The Eclipse Foundation - initial API and implementation
12 * Ian Pun - factored out of RepositorySelectionPage
13 *******************************************************************************/
14 package org
.eclipse
.egit
.ui
.internal
.clone
;
16 import java
.net
.URISyntaxException
;
18 import org
.eclipse
.egit
.ui
.internal
.KnownHosts
;
19 import org
.eclipse
.egit
.ui
.internal
.components
.RepositorySelectionPage
.Protocol
;
20 import org
.eclipse
.jgit
.lib
.Constants
;
21 import org
.eclipse
.jgit
.transport
.Transport
;
22 import org
.eclipse
.jgit
.transport
.TransportProtocol
;
23 import org
.eclipse
.jgit
.transport
.URIish
;
26 * Utility class for checking strings for being valid git URLs, and for
27 * sanitizing arbitrary string input.
29 public abstract class GitUrlChecker
{
31 private static final String GIT_CLONE_COMMAND_PREFIX
= "git clone "; //$NON-NLS-1$
34 * Checks if the incoming string is a valid git URL. It is recommended to
35 * {@link #sanitizeAsGitUrl(String) sanitize} the String first if coming
36 * from an untrustworthy source.
40 * @return {@code true} if the {@code url} is a valid git URL, {@code false}
43 public static boolean isValidGitUrl(String url
) {
46 URIish u
= new URIish(url
);
47 if (canHandleProtocol(u
)) {
48 if (Protocol
.GIT
.handles(u
) || Protocol
.SSH
.handles(u
)
49 || (Protocol
.HTTP
.handles(u
)
50 || Protocol
.HTTPS
.handles(u
))
51 && KnownHosts
.isKnownHost(u
.getHost())
52 || url
.endsWith(Constants
.DOT_GIT_EXT
)) {
57 } catch (URISyntaxException e
) {
58 // Ignore. This is used to check arbitrary input for being a
59 // possibly valid git URL; we don't want to flood the log here.
65 * Sanitize a string for use as a git URL. Strips the Git Clone command if
66 * needed and reduces remaining the input to anything before the first
70 * String to be sanitized
71 * @return sanitized string; if the input came from an untrustworthy source,
72 * is should still be checked using {@link #isValidGitUrl(String)}
73 * before being used for real as a git URL
75 public static String
sanitizeAsGitUrl(String input
) {
76 String sanitized
= input
.trim();
77 if (sanitized
.startsWith(GIT_CLONE_COMMAND_PREFIX
)) {
78 sanitized
= sanitized
.substring(GIT_CLONE_COMMAND_PREFIX
.length())
81 // For file URLs, take everything up to the first vertical space
83 URIish uri
= new URIish(sanitized
);
84 if (Protocol
.FILE
.handles(uri
)) {
85 return sanitized
.split("\\v", 2)[0]; //$NON-NLS-1$
87 } catch (URISyntaxException e
) {
88 // Ignore here; error will be reported later where this method is
91 // Take only the part up to the first whitespace character
92 return sanitized
.split("[\\h|\\v]", 2)[0]; //$NON-NLS-1$
95 private static boolean canHandleProtocol(URIish u
) {
96 for (TransportProtocol proto
: Transport
.getTransportProtocols()) {
97 if (proto
.canHandle(u
)) {